babel-preset-dlight
Advanced tools
Comparing version 1.0.0-beta.11 to 1.0.0-beta.12
@@ -1,2 +0,2 @@ | ||
var V=Object.defineProperty;var j=(f,e,t)=>e in f?V(f,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):f[e]=t;var M=(f,e,t)=>(j(f,typeof e!="symbol"?e+"":e,t),t);import T from"babel-plugin-syntax-typescript-new";import B from"@babel/plugin-syntax-decorators";import{minimatch as w}from"minimatch";import{parseView as I}from"@dlightjs/view-parser";import{parseReactivity as E}from"@dlightjs/reactivity-parser";import{generateSubView as O,generateView as F}from"@dlightjs/view-generator";var C=process.env.NODE_ENV==="development",b=["push","pop","shift","unshift","splice","sort","reverse","add","delete","clear","set","delete","clear"],N=["a","abbr","address","area","article","aside","audio","b","base","bdi","bdo","blockquote","body","br","button","canvas","caption","cite","code","col","colgroup","data","datalist","dd","del","details","dfn","dialog","div","dl","dt","em","embed","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","i","iframe","img","input","ins","kbd","label","legend","li","link","main","map","mark","menu","meta","meter","nav","noscript","object","ol","optgroup","option","output","p","picture","pre","progress","q","rp","rt","ruby","s","samp","script","section","select","slot","small","source","span","strong","style","sub","summary","sup","table","tbody","td","template","textarea","tfoot","th","thead","time","title","tr","track","u","ul","var","video","wbr","acronym","applet","basefont","bgsound","big","blink","center","dir","font","frame","frameset","isindex","keygen","listing","marquee","menuitem","multicol","nextid","nobr","noembed","noframes","param","plaintext","rb","rtc","spacer","strike","tt","xmp","animate","animateMotion","animateTransform","circle","clipPath","defs","desc","ellipse","feBlend","feColorMatrix","feComponentTransfer","feComposite","feConvolveMatrix","feDiffuseLighting","feDisplacementMap","feDistantLight","feDropShadow","feFlood","feFuncA","feFuncB","feFuncG","feFuncR","feGaussianBlur","feImage","feMerge","feMergeNode","feMorphology","feOffset","fePointLight","feSpecularLighting","feSpotLight","feTile","feTurbulence","filter","foreignObject","g","image","line","linearGradient","marker","mask","metadata","mpath","path","pattern","polygon","polyline","radialGradient","rect","set","stop","svg","switch","symbol","text","textPath","tspan","use","view"],D=["Static","Prop","Env","Content","Children"],g="@dlightjs/dlight",x=Object.fromEntries(["createElement","setStyle","setDataset","setEvent","delegateEvent","setHTMLProp","setHTMLAttr","setHTMLProps","setHTMLAttrs","createTextNode","updateText","insertNode","ForNode","CondNode","ExpNode","EnvNode","SubViewNode","PropView","render"].map(f=>[f,`$$${f}`])),k={textContent:["*"],innerHTML:["*"],accept:["form","input"],acceptCharset:["form"],accesskey:["*"],action:["form"],align:["caption","col","colgroup","hr","iframe","img","table","tbody","td","tfoot","th","thead","tr"],allow:["iframe"],alt:["area","img","input"],async:["script"],autocapitalize:["*"],autocomplete:["form","input","select","textarea"],autofocus:["button","input","select","textarea"],autoplay:["audio","video"],background:["body","table","td","th"],bgColor:["body","col","colgroup","marquee","table","tbody","tfoot","td","th","tr"],border:["img","object","table"],buffered:["audio","video"],capture:["input"],charset:["meta"],checked:["input"],cite:["blockquote","del","ins","q"],class:["*"],color:["font","hr"],cols:["textarea"],colSpan:["td","th"],content:["meta"],contentEditable:["*"],contextmenu:["*"],controls:["audio","video"],coords:["area"],crossOrigin:["audio","img","link","script","video"],csp:["iframe"],data:["object"],dateTime:["del","ins","time"],decoding:["img"],default:["track"],defer:["script"],dir:["*"],dirname:["input","textarea"],disabled:["button","fieldset","input","optgroup","option","select","textarea"],download:["a","area"],draggable:["*"],enctype:["form"],enterKeyHint:["textarea","contenteditable"],for:["label","output"],form:["button","fieldset","input","label","meter","object","output","progress","select","textarea"],formAction:["input","button"],formEnctype:["button","input"],formMethod:["button","input"],formNoValidate:["button","input"],formTarget:["button","input"],headers:["td","th"],height:["canvas","embed","iframe","img","input","object","video"],hidden:["*"],high:["meter"],href:["a","area","base","link"],hreflang:["a","link"],httpEquiv:["meta"],id:["*"],integrity:["link","script"],intrinsicSize:["img"],inputMode:["textarea","contenteditable"],ismap:["img"],itemProp:["*"],kind:["track"],label:["optgroup","option","track"],lang:["*"],language:["script"],loading:["img","iframe"],list:["input"],loop:["audio","marquee","video"],low:["meter"],manifest:["html"],max:["input","meter","progress"],maxLength:["input","textarea"],minLength:["input","textarea"],media:["a","area","link","source","style"],method:["form"],min:["input","meter"],multiple:["input","select"],muted:["audio","video"],name:["button","form","fieldset","iframe","input","object","output","select","textarea","map","meta","param"],noValidate:["form"],open:["details","dialog"],optimum:["meter"],pattern:["input"],ping:["a","area"],placeholder:["input","textarea"],playsInline:["video"],poster:["video"],preload:["audio","video"],readonly:["input","textarea"],referrerPolicy:["a","area","iframe","img","link","script"],rel:["a","area","link"],required:["input","select","textarea"],reversed:["ol"],role:["*"],rows:["textarea"],rowSpan:["td","th"],sandbox:["iframe"],scope:["th"],scoped:["style"],selected:["option"],shape:["a","area"],size:["input","select"],sizes:["link","img","source"],slot:["*"],span:["col","colgroup"],spellcheck:["*"],src:["audio","embed","iframe","img","input","script","source","track","video"],srcdoc:["iframe"],srclang:["track"],srcset:["img","source"],start:["ol"],step:["input"],style:["*"],summary:["table"],tabIndex:["*"],target:["a","area","base","form"],title:["*"],translate:["*"],type:["button","input","embed","object","ol","script","source","style","menu","link"],usemap:["img","input","object"],value:["button","data","input","li","meter","option","progress","param","text"],width:["canvas","embed","iframe","img","input","object","video"],wrap:["textarea"],ariaAutocomplete:["*"],ariaChecked:["*"],ariaDisabled:["*"],ariaErrorMessage:["*"],ariaExpanded:["*"],ariaHasPopup:["*"],ariaHidden:["*"],ariaInvalid:["*"],ariaLabel:["*"],ariaLevel:["*"],ariaModal:["*"],ariaMultiline:["*"],ariaMultiSelectable:["*"],ariaOrientation:["*"],ariaPlaceholder:["*"],ariaPressed:["*"],ariaReadonly:["*"],ariaRequired:["*"],ariaSelected:["*"],ariaSort:["*"],ariaValuemax:["*"],ariaValuemin:["*"],ariaValueNow:["*"],ariaValueText:["*"],ariaBusy:["*"],ariaLive:["*"],ariaRelevant:["*"],ariaAtomic:["*"],ariaDropEffect:["*"],ariaGrabbed:["*"],ariaActiveDescendant:["*"],ariaColCount:["*"],ariaColIndex:["*"],ariaColSpan:["*"],ariaControls:["*"],ariaDescribedBy:["*"],ariaDescription:["*"],ariaDetails:["*"],ariaFlowTo:["*"],ariaLabelledBy:["*"],ariaOwns:["*"],ariaPosInset:["*"],ariaRowCount:["*"],ariaRowIndex:["*"],ariaRowSpan:["*"],ariaSetSize:["*"]};var v=class{dlightPackageName=g;babelApi;t;traverse;enableDevTools;includes;excludes;htmlTags;attributeMap;constructor(e,t,i,s,r,n,d){this.babelApi=e,this.t=t,this.traverse=e.traverse,this.includes=i,this.excludes=s,this.enableDevTools=C&&r,this.htmlTags=typeof n=="function"?n(N):n.includes("*")?[...new Set([...N,...n])].filter(o=>o!=="*"):n,this.attributeMap=d}classDeclarationNode;classBodyNode;propertiesContainer={};dependencyMap={};enter=!0;dLightModel=!1;enterClassNode=!1;className;programNode;allImports=[];didAlterImports=!1;clearNode(){this.classDeclarationNode=void 0,this.classBodyNode=void 0,this.propertiesContainer={},this.dependencyMap={},this.enter=!0,this.enterClassNode=!1,this.dLightModel=!1,this.className=void 0}get availableProperties(){return Object.entries(this.propertiesContainer).filter(([e,{isWatcher:t,isStatic:i,isChildren:s}])=>e!=="_$compName"&&!t&&!i&&!s).map(([e])=>e)}initNode(e){let t=e.node;if(this.classDeclarationNode=t,this.classBodyNode=t.body,this.propertiesContainer={},t.id?.name||(t.id=this.t.identifier(`Anonymous_${v.uid()}`)),this.className=t.id?.name,this.handleClassCustomDecorators(),this.enableDevTools&&this.classBodyNode.body.unshift(this.t.classProperty(this.t.identifier("_$compName"),this.t.stringLiteral(this.className))),!this.didAlterImports){let i=this.allImports.filter(s=>s.source.value===g);this.dlightPackageName!==g&&i.forEach(s=>{s.source.value=this.dlightPackageName}),this.programNode.body.unshift(this.t.importDeclaration(Object.entries(x).map(([s,r])=>this.t.importSpecifier(this.t.identifier(r),this.t.identifier(s))),this.t.stringLiteral(this.dlightPackageName))),this.didAlterImports=!0}}programEnterVisitor(e,t){if(this.enter=this.fileAllowed(t),!this.enter)return;if(this.allImports=e.node.body.filter(s=>this.t.isImportDeclaration(s)),this.allImports.filter(s=>s.source.value===g).length===0){this.enter=!1;return}this.programNode=e.node}programExitVisitor(){this.enter&&(this.didAlterImports=!1,this.allImports=[],this.programNode=void 0)}classEnter(e){this.enter&&(this.enterClassNode=this.isDLightClass(e),this.enterClassNode&&(this.initNode(e),this.resolveMounting(e)))}classExit(){this.enter&&this.enterClassNode&&(this.transformDLightClass(),this.clearNode(),this.enterClassNode=!1)}classMethodVisitor(e){if(!this.enterClassNode||!this.t.isIdentifier(e.node.key))return;let t=e.node.key.name;if(t==="View"||this.findDecoratorByName(e.node.decorators,"View"))return;let s=e.node,r=this.findDecoratorByName(s.decorators,"Watch");if(this.t.isIdentifier(s.key,{name:"constructor"})||(this.autoBindMethods(s),!r))return;let n=[],d;if(this.t.isIdentifier(r))[n,d]=this.getDependencies(s);else{let o=r.arguments.filter(l=>this.t.isStringLiteral(l)).map(l=>l.value),a=this.t.classMethod("method",s.key,[],this.t.blockStatement([this.t.expressionStatement(this.t.arrayExpression(o.map(l=>this.t.memberExpression(this.t.thisExpression(),this.t.identifier(l)))))]));[n,d]=this.getDependencies(a)}this.propertiesContainer[t]={node:s,deps:n,depsNode:d,isWatcher:!0},s.decorators=this.removeDecorators(s.decorators,["Watch"])}classPropertyVisitor(e){if(!this.enterClassNode)return;let t=e.node;if(!this.t.isIdentifier(t.key))return;let i=t.key.name;if(i==="View")return;let s=t.decorators;if(this.findDecoratorByName(s,"View"))return;let n=this.parseModel(e),d=!!this.findDecoratorByName(s,"Prop"),o=!!this.findDecoratorByName(s,"Env"),a=!!this.findDecoratorByName(t.decorators,"Children"),[l,c]=a?[[]]:this.getDependencies(t);this.propertiesContainer[i]={node:t,deps:l,depsNode:c,isStatic:!!this.findDecoratorByName(s,"Static"),isContent:!!this.findDecoratorByName(s,"Content"),isChildren:a,isPropOrEnv:d?"Prop":o?"Env":void 0,isModel:n},t.decorators=this.removeDecorators(s,D)}resolveWatcherDecorator(e,t){if(!this.t.isIdentifier(e.key))return;let i=e.key.name,s=this.classBodyNode.body.indexOf(e),r=this.t.classProperty(this.t.identifier(`$w$${i}`));this.classBodyNode.body.splice(s,0,r),e.body.body.unshift(this.t.ifStatement(this.t.callExpression(this.t.memberExpression(this.t.thisExpression(),this.t.identifier("_$cache")),[this.t.stringLiteral(i),t]),this.t.blockStatement([this.t.returnStatement()])))}resolveChildrenDecorator(e){if(!this.classBodyNode||!this.t.isIdentifier(e.key))return;let t=e.key.name,i=this.classBodyNode.body.indexOf(e),s=this.t.memberExpression(this.t.thisExpression(),this.t.identifier("_$children")),r=this.t.classMethod("get",this.t.identifier(t),[],this.t.blockStatement([this.t.returnStatement(s)]));this.classBodyNode.body.splice(i,1,r)}resolveContentDecorator(e){if(!this.classBodyNode||!this.t.isIdentifier(e.key)||this.classBodyNode.body.some(r=>this.t.isClassProperty(r)&&r.key.name==="_$contentKey"))return;let t=e.key.name,i=this.classBodyNode.body.indexOf(e),s=this.t.classProperty(this.t.identifier("_$contentKey"),this.t.stringLiteral(t));this.classBodyNode.body.splice(i,0,s)}resolvePropDecorator(e,t){if(!this.classBodyNode||!this.t.isIdentifier(e.key))return;let i=e.key.name,s=this.classBodyNode.body.indexOf(e),r=t.toLowerCase()==="prop"?"p":"e",n=this.t.classProperty(this.t.identifier(`$${r}$${i}`));this.classBodyNode.body.splice(s,0,n)}resolveStateDecorator(e,t,i){if(!this.classBodyNode||!this.t.isIdentifier(e.key))return;let s=e.key.name,r=this.classBodyNode.body.indexOf(e),n=this.dLightModel?[]:[this.t.classProperty(this.t.identifier(`$$${s}`),this.t.numericLiteral(1<<t))],d=i?[this.t.classProperty(this.t.identifier(`$s$${s}`),this.t.arrayExpression([...i].map(o=>this.t.stringLiteral(o))))]:[];this.classBodyNode.body.splice(r+1,0,...n,...d)}handleClassCustomDecorators(){if(!this.classBodyNode)return;let e=this.classDeclarationNode?.decorators;if(!e)return;this.findDecoratorByName(e,"ForwardProps")&&(this.classBodyNode.body.unshift(this.t.classProperty(this.t.identifier("_$forwardProps")),this.t.classProperty(this.t.identifier("_$forwardPropsSet"),this.t.newExpression(this.t.identifier("Set"),[])),this.t.classProperty(this.t.identifier("_$forwardPropsId"),this.t.arrayExpression([]))),this.classDeclarationNode.decorators=this.removeDecorators(e,["ForwardProps"]))}transformDLightClass(){let e=this.handleView();this.dLightModel&&(e=this.availableProperties);let t=Object.entries(this.propertiesContainer).reverse(),i=this.dependencyMapReversed();this.addAutoUpdate(e);for(let[s,{node:r,deps:n,isStatic:d,isChildren:o,isPropOrEnv:a,isWatcher:l,isContent:c,isModel:u,depsNode:h}]of t){if(o){this.resolveChildrenDecorator(r);continue}n.length>0&&(e.push(...n),l?this.resolveWatcherDecorator(r,h):u||this.handleDerivedProperty(r,h)),a&&this.resolvePropDecorator(r,a),c&&(this.resolvePropDecorator(r,"Prop"),this.resolveContentDecorator(r)),!d&&e.includes(s)&&this.resolveStateDecorator(r,this.availableProperties.indexOf(s),i[s])}}addAutoUpdate(e){if(!this.classBodyNode)return;this.classBodyNode.body.filter(i=>!((this.t.isClassProperty(i)||this.t.isClassMethod(i))&&["View","constructor","_$compName"].includes(i.key.name)||this.t.isClassMethod(i,{kind:"method"})&&this.findDecoratorByName(i.decorators,"View"))).forEach(i=>{let s=this.t.isClassProperty(i)?i.value:this.t.isClassMethod(i)?i.body:null;s&&this.addAutoUpdateToNode(s,e)})}addAutoUpdateToNode(e,t){this.traverse(this.valueWrapper(e),{MemberExpression:i=>{if(!this.t.isThisExpression(i.node.object)||!this.t.isIdentifier(i.node.property))return;let s=i.node.property.name;if(!t.includes(s))return;let r=this.isAssignmentExpressionLeft(i);r&&this.addUpdate(r,s)},CallExpression:i=>{if(!this.t.isMemberExpression(i.node.callee))return;let s=i.node.callee.property;if(!this.t.isIdentifier(s)||!b.includes(s.name))return;let r=i.get("callee").get("object");for(;this.t.isMemberExpression(r.node);)r=r.get("object");if(!this.t.isThisExpression(r?.node))return;let n=r.parentPath.node.property.name;this.addUpdate(i,n)}})}addUpdate(e,t){let i=this.t.callExpression(this.t.memberExpression(this.t.thisExpression(),this.t.identifier("_$updateView")),[this.t.stringLiteral(t)]),s=this.t.callExpression(this.t.memberExpression(this.t.thisExpression(),this.t.identifier("_$updateDerived")),[this.t.stringLiteral(t)]),r=this.t.sequenceExpression([e.node,s]),n=e.parentPath;for(;n&&!(this.t.isBlockStatement(n.node)&&!this.t.isIfStatement(n.parentPath?.node)&&!this.t.isLoop(n.parentPath?.node));){if(this.t.isFunction(n.node)){n=n.get("body");break}n=n.parentPath}if(this.t.isBlockStatement(n?.node)){let d=n.node;this.getUpdatePropExp(d).has(t)||(this.getAllTopLevelReturnBlock(d).forEach(a=>{a.body.splice(-1,0,this.t.expressionStatement(i))}),this.t.isReturnStatement(d.body[d.body.length-1])||d.body.push(this.t.expressionStatement(i)))}else r=this.t.sequenceExpression([r,i]);e.replaceWith(r),e.skip()}getUpdatePropExp(e){let t=new Set;return e.body.forEach(i=>{if(!this.t.isExpressionStatement(i))return;let s=i.expression;if(!this.t.isCallExpression(s))return;let r=s.callee;if(!this.t.isMemberExpression(r))return;let n=r.object,d=r.property;if(!this.t.isThisExpression(n)||!this.t.isIdentifier(d,{name:"_$updateView"}))return;let o=s.arguments[0];this.t.isStringLiteral(o)&&t.add(o.value)}),t}addViewAutoUpdate(e){typeof e=="object"&&Object.entries(e).forEach(([,t])=>{t&&(t.type?.[0]&&t.type[0]!==t.type[0].toLowerCase()?this.t.isExpression(t)&&this.addAutoUpdateToNode(t,this.availableProperties):this.addViewAutoUpdate(t))})}handleView(){if(!this.classBodyNode)return[];let e=new Set,t,i=[];for(let o of this.classBodyNode.body){if(!this.t.isClassProperty(o)&&!this.t.isClassMethod(o)||!this.t.isIdentifier(o.key))continue;let a=this.findDecoratorByName(o.decorators,"View"),l=o.key.name==="View";if(!(!a&&!l)){if(this.t.isClassProperty(o)){let c=o.value;for(;this.t.isTSAsExpression(c);)c=c.expression;if(!this.t.isArrowFunctionExpression(c))continue;o.value=c;let u=this.arrowFunctionPropertyToMethod(o);if(!u)continue;o=u}a?(o.decorators=null,i.push(o)):t=o}}let s=i.map(o=>o.key.name),r=Object.fromEntries(i.map(o=>{let a=o.params[0];if(!a||!this.t.isObjectPattern(a))return["-",null];let l=Object.fromEntries(a.properties.map(c=>{if(!this.t.isObjectProperty(c))return["-",null];let u=c.key.name,h=this.getIdentifiers(this.t.assignmentExpression("=",this.t.objectPattern([this.t.objectProperty(this.t.numericLiteral(0),c.value)]),this.t.numericLiteral(0))).filter(m=>m!==u);return[u,h]}).filter(([c,u])=>u));return[o.key.name,l]}).filter(([o,a])=>a)),n=-1;if(t){let o;[o,n]=this.alterMainView(t,s,r),o.forEach(e.add.bind(e))}i.forEach(o=>{let a;[a,n]=this.alterSubView(o,s,r,n),a.forEach(e.add.bind(e))});let d=[];return this.availableProperties.forEach(o=>{e.has(o)&&d.push(o)}),d}alterMainView(e,t,i){let s=I(e.body,{babelApi:this.babelApi,subviewNames:t,htmlTags:this.htmlTags});s.map(l=>this.addViewAutoUpdate(l));let[r,n]=E(s,{babelApi:this.babelApi,availableProperties:this.availableProperties,dependencyMap:this.dependencyMap,reactivityFuncNames:b}),[d,o,a]=F(r,{babelApi:this.babelApi,className:this.className,importMap:x,subViewPropMap:Object.fromEntries(Object.entries(i).map(([l,c])=>[l,Object.keys(c)])),templateIdx:-1,attributeMap:this.attributeMap});return e.body=d,this.classBodyNode?.body.push(...o),[n,a]}alterSubView(e,t,i,s){e.params.length===0?e.params.push(this.t.identifier("_$"),this.t.identifier("$subviewNode")):e.params.length===1?e.params.push(this.t.identifier("$subviewNode")):(e.params[1]=this.t.identifier("$subviewNode"),e.params.length=2);let r=I(e.body,{babelApi:this.babelApi,subviewNames:t,htmlTags:this.htmlTags});r.map(p=>this.addViewAutoUpdate(p));let[n,d]=E(r,{babelApi:this.babelApi,availableProperties:this.availableProperties,dependencyMap:this.dependencyMap,reactivityFuncNames:b}),o=i[e.key.name]??[],a={};Object.entries(o).forEach(([p,y])=>{y.forEach(L=>{a[L]=[p]})});let[l]=E(r,{babelApi:this.babelApi,availableProperties:Object.keys(o),dependencyMap:this.dependencyMap,dependencyParseType:"identifier",identifierDepMap:a,reactivityFuncNames:b}),c=Object.fromEntries(Object.entries(i).map(([p,y])=>[p,Object.keys(y)])),[u,h,m]=O(n,l,e.params[0],{babelApi:this.babelApi,className:this.className,importMap:x,subViewPropMap:c,templateIdx:s,attributeMap:this.attributeMap});return e.body=u,this.classBodyNode?.body.push(...h),[d,m]}fileAllowed(e){return this.includes.includes("*")?!0:!(!e||this.excludes.some(t=>w(e,t))||!this.includes.some(t=>w(e,t)))}isDLightView(e){let t=e.node;return(t.decorators??[]).find(r=>this.t.isIdentifier(r.expression,{name:"View"}))&&(t.superClass=this.t.identifier("View"),t.decorators=t.decorators?.filter(r=>!this.t.isIdentifier(r.expression,{name:"View"}))),this.t.isIdentifier(t.superClass,{name:"View"})}isDLightModel(e){let t=e.node;return(t.decorators??[]).find(r=>this.t.isIdentifier(r.expression,{name:"Model"}))&&(t.superClass=this.t.identifier("Model"),t.decorators=t.decorators?.filter(r=>!this.t.isIdentifier(r.expression,{name:"Model"}))),t.body.body.unshift(this.t.classProperty(this.t.identifier("_$model"))),t.body.body=t.body.body.filter(r=>!((this.t.isClassProperty(r)||this.t.isClassMethod(r,{kind:"method"}))&&(this.findDecoratorByName(r.decorators,"View")||this.t.isIdentifier(r.key)&&r.key.name==="View"))),this.dLightModel=!0,this.t.isIdentifier(t.superClass,{name:"Model"})}isDLightClass(e){return this.isDLightView(e)||this.isDLightModel(e)}parseModel(e){if(!this.allImports.some(h=>h.source.value===this.dlightPackageName&&h.specifiers.some(m=>{if(this.t.isImportSpecifier(m)&&this.t.isIdentifier(m.imported,{name:"use"}))return!0})))return;let i=e.node,s=i.key;if(!this.t.isIdentifier(s))return;let r=i.value;if(!this.t.isCallExpression(r)||!this.t.isIdentifier(r.callee,{name:"use"}))return;let n=r.arguments,d=n[1],o=n[2],a=this.t.nullLiteral();if(d){let h=[],m=[];if(this.t.isObjectExpression(d))d.properties.forEach(p=>{if(this.t.isSpreadElement(p)){let[,y]=this.getDependenciesFromNode(p.argument);h.push([p.argument,y??this.t.nullLiteral()])}else if(this.t.isObjectProperty(p)){let[,y]=this.getDependenciesFromNode(p.value);m.push([!p.computed&&this.t.isIdentifier(p.key)?this.t.stringLiteral(p.key.name):p.key,p.value,y??this.t.nullLiteral()])}else m.push([!p.computed&&this.t.isIdentifier(p.key)?this.t.stringLiteral(p.key.name):p.key,this.t.arrowFunctionExpression([],p.body),this.t.nullLiteral()])});else{let[,p]=this.getDependenciesFromNode(d);h.push([d,p??this.t.nullLiteral()])}a=this.t.objectExpression([this.t.objectProperty(this.t.identifier("m"),this.t.arrayExpression(h.map(p=>this.t.arrayExpression(p)))),this.t.objectProperty(this.t.identifier("s"),this.t.arrayExpression(m.map(p=>this.t.arrayExpression(p))))])}let l=this.t.nullLiteral();if(o){let[,h]=this.getDependenciesFromNode(o);l=this.t.arrayExpression([o,h??this.t.nullLiteral()])}n[1]=this.t.arrowFunctionExpression([],a),n[2]=this.t.arrowFunctionExpression([],l),n[3]=this.t.stringLiteral(s.name),r.callee=this.t.memberExpression(this.t.thisExpression(),this.t.identifier("_$injectModel"));let c=this.classBodyNode.body.indexOf(i),u=this.t.classProperty(this.t.identifier(`$md$${s.name}`));return this.classBodyNode.body.splice(c,0,u),!0}removeDecorators(e,t){return e?e.filter(i=>!(this.t.isIdentifier(i.expression)&&t.includes(i.expression.name)||this.t.isCallExpression(i.expression)&&this.t.isIdentifier(i.expression.callee)&&t.includes(i.expression.callee.name))):[]}findDecoratorByName(e,t){if(e)return e.find(i=>this.t.isIdentifier(i.expression,{name:t})||this.t.isCallExpression(i.expression)&&this.t.isIdentifier(i.expression.callee,{name:t}))?.expression}geneDependencyNode(e){let t=e;for(;t?.parentPath;){let i=t.parentPath;if(!(this.t.isBinaryExpression(i.node)||this.t.isMemberExpression(i.node)))return t.node;t=i}return e.node}addConstructor(){let e=this.classBodyNode.body.find(t=>this.t.isClassMethod(t,{kind:"constructor"}));return e||(e=this.t.classMethod("constructor",this.t.identifier("constructor"),[],this.t.blockStatement([this.t.expressionStatement(this.t.callExpression(this.t.super(),[]))])),this.classBodyNode.body.unshift(e),e)}autoBindMethods(e){this.addConstructor().body.body.push(this.t.expressionStatement(this.t.assignmentExpression("=",this.t.memberExpression(this.t.thisExpression(),e.key),this.t.callExpression(this.t.memberExpression(this.t.memberExpression(this.t.thisExpression(),e.key),this.t.identifier("bind")),[this.t.thisExpression()]))))}handleDerivedProperty(e,t){if(!this.t.isIdentifier(e.key))return;let i=e.key.name,s=e.value,r=this.classBodyNode.body.indexOf(e),n=this.t.classMethod("get",this.t.identifier(`$f$${i}`),[],this.t.blockStatement([this.t.ifStatement(this.t.callExpression(this.t.memberExpression(this.t.thisExpression(),this.t.identifier("_$cache")),[this.t.stringLiteral(i),t]),this.t.blockStatement([this.t.returnStatement(this.t.memberExpression(this.t.thisExpression(),this.t.identifier(i)))])),this.t.returnStatement(s)]));this.classBodyNode.body.splice(r+1,0,n),e.value=null}getDependenciesFromNode(e,t=!1){let i=new Set,s=new Set,r={};this.traverse(this.valueWrapper(e),{MemberExpression:o=>{if(!this.t.isIdentifier(o.node.property)||!this.t.isThisExpression(o.node.object))return;let a=o.node.property.name;this.isAssignmentExpressionLeft(o)||this.isAssignmentFunction(o)?s.add(a):this.availableProperties.includes(a)&&!this.isMemberInEscapeFunction(o,this.classDeclarationNode)&&!this.isMemberInManualFunction(o,this.classDeclarationNode)&&(i.add(a),t&&this.dependencyMap[a]?.forEach(i.add.bind(i)),r[a]||(r[a]=[]),r[a].push(this.geneDependencyNode(o)))}}),s.forEach(o=>{i.delete(o),delete r[o]});let n=Object.values(r).flat();n=n.filter((o,a)=>n.findIndex(c=>this.t.isNodesEquivalent(c,o))===a);let d=[...i];if(t&&i.size>0){let o=e.body.body[0].key.name;this.dependencyMap[o]=d}return[d,this.t.arrayExpression(n)]}getDependencies(e){if(!this.t.isIdentifier(e.key))return[[],void 0];let t=this.t.classDeclaration(null,null,this.t.classBody([e]));return this.getDependenciesFromNode(t,!0)}dependencyMapReversed(){let e={};return Object.entries(this.dependencyMap).forEach(([t,i])=>{i.forEach(s=>{e[s]||(e[s]=new Set),e[s].add(t)})}),e}resolveMounting(e){let t=e.node;if(!this.t.isIdentifier(t.id))return;let i=t.decorators??[],s=a=>{let l=i.find(c=>this.t.isIdentifier(c.expression,{name:a}));return l&&i.splice(i.findIndex(c=>c===l),1),l},r=s("Main")?"main":s("App")?"app":null,n;if(r)n=this.t.stringLiteral(r);else{let a=i.find(l=>this.t.isCallExpression(l.expression)&&this.t.isIdentifier(l.expression.callee,{name:"Mount"})&&l.expression.arguments.length===1);if(!a)return;i.splice(i.findIndex(l=>l===a),1),n=a.expression.arguments[0]}let d=e.parentPath.node;if(!this.t.isBlockStatement(d)&&!this.t.isProgram(d))return;let o=d.body.indexOf(t);d.body.splice(o+1,0,this.t.expressionStatement(this.t.callExpression(this.t.identifier(x.render),[n,t.id])))}arrowFunctionPropertyToMethod(e){if(!this.t.isArrowFunctionExpression(e.value))return;let t=e.value;if(!this.t.isBlockStatement(t.body))return;let i=this.classBodyNode.body.indexOf(e),s=this.t.classMethod("method",e.key,t.params,t.body);return this.classBodyNode.body.splice(i,1,s),s}isMemberExpressionProperty(e,t){return this.t.isMemberExpression(e)&&!e.computed&&e.property===t}isObjectKey(e,t){return this.t.isObjectProperty(e)&&e.key===t}valueWithArrowFunc(e){e.value||(e.value=this.t.identifier("undefined")),e.value=this.t.arrowFunctionExpression([],e.value)}getAllTopLevelReturnBlock(e){let t=[],i=!1;return this.traverse(this.valueWrapper(e),{Function:s=>{i||(i=!0,s.skip())},ReturnStatement:s=>{if(i)return;let r=s.parentPath.node;if(this.t.isBlockStatement(r))t.push(r);else{let n=this.t.blockStatement([s.node]);s.replaceWith(n),t.push(n)}s.skip()},exit:s=>{this.t.isFunction(s.node)&&(i=!1)}}),t}valueWrapper(e){return this.t.file(this.t.program([this.t.isStatement(e)?e:this.t.expressionStatement(e)]))}isAttrFromFunction(e,t){let i=e.parentPath,s=r=>this.t.isIdentifier(r)?r.name===t:this.t.isAssignmentPattern(r)?s(r.left):this.t.isArrayPattern(r)?r.elements.filter(Boolean).map(n=>s(n)).includes(!0):this.t.isObjectPattern(r)?r.properties.filter(n=>this.t.isObjectProperty(n)&&this.t.isIdentifier(n.key)).map(n=>n.key.name).includes(t):this.t.isRestElement(r)?s(r.argument):!1;for(;i;){let r=i.node;if(this.t.isArrowFunctionExpression(r)||this.t.isFunctionDeclaration(r)){for(let n of r.params)if(s(n))return!0}i=i.parentPath}return!1}isStandAloneIdentifier(e){let t=e.node,i=e.parentPath?.node;if(this.t.isMemberExpression(i)&&i.property===t||this.isAttrFromFunction(e,t.name))return!1;for(;e.parentPath;){if(this.t.isVariableDeclarator(e.parentPath.node)||this.t.isObjectProperty(e.parentPath.node)&&e.parentPath.node.key===e.node&&!e.parentPath.node.computed)return!1;e=e.parentPath}return!0}getIdentifiers(e){if(this.t.isIdentifier(e))return[e.name];let t=new Set;return this.traverse(this.valueWrapper(e),{Identifier:i=>{this.isStandAloneIdentifier(i)&&t.add(i.node.name)}}),[...t]}isAssignmentExpressionLeft(e){let t=e.parentPath;for(;t&&!this.t.isStatement(t.node);){if(this.t.isAssignmentExpression(t.node)){if(t.node.left===e.node)return t;let i=t.get("left");if(e.isDescendant(i))return t}else if(this.t.isUpdateExpression(t.node))return t;t=t.parentPath}return null}isAssignmentFunction(e){let t=e.parentPath;for(;t&&this.t.isMemberExpression(t.node);)t=t.parentPath;return t?this.t.isCallExpression(t.node)&&this.t.isMemberExpression(t.node.callee)&&this.t.isIdentifier(t.node.callee.property)&&b.includes(t.node.callee.property.name):!1}isMemberInEscapeFunction(e,t){let i=!1,s=e.parentPath;for(;s&&s.node!==t;){let r=s.node;if(this.t.isCallExpression(r)&&this.t.isIdentifier(r.callee)&&v.escapeNamings.includes(r.callee.name)){i=!0;break}s=s.parentPath}return i}isMemberInManualFunction(e,t){let i=!1,s=e.parentPath;for(;s&&s.node!==t;){let r=s.node,n=s.parentPath?.node,d=this.t.isFunctionExpression(r)||this.t.isArrowFunctionExpression(r),o=this.t.isCallExpression(n)&&this.t.isIdentifier(n.callee)&&n.callee.name==="manual";if(d&&o){i=!0;break}s=s.parentPath}return i}static uid(e=4){return Math.random().toString(32).slice(2,e+2)}},P=v;M(P,"escapeNamings",["escape","$"]);var S=P;function A(f,e){let{types:t}=f,{files:i="**/*.{js,ts}",excludeFiles:s="**/{dist,node_modules,lib}/*",enableDevTools:r=!1,htmlTags:n=a=>a,attributeMap:d=k}=e,o=new S(f,t,Array.isArray(i)?i:[i],Array.isArray(s)?s:[s],r,n,d);return{visitor:{Program:{enter(a,{filename:l}){return o.programEnterVisitor(a,l)},exit:o.programExitVisitor.bind(o)},ClassDeclaration:{enter:o.classEnter.bind(o),exit:o.classExit.bind(o)},ClassMethod:o.classMethodVisitor.bind(o),ClassProperty:o.classPropertyVisitor.bind(o)}}}function ee(f,e){return{plugins:[T,[B.default??B,{legacy:!0}],[A,e]]}}export{ee as default}; | ||
var F=Object.defineProperty;var O=(f,e,t)=>e in f?F(f,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):f[e]=t;var C=(f,e,t)=>(O(f,typeof e!="symbol"?e+"":e,t),t);import $ from"babel-plugin-syntax-typescript-new";import V from"@babel/plugin-syntax-decorators";import{minimatch as I}from"minimatch";import{parseView as S}from"@dlightjs/view-parser";import{parseReactivity as M}from"@dlightjs/reactivity-parser";import{generateSubView as j,generateView as T}from"@dlightjs/view-generator";var D=process.env.NODE_ENV==="development",E={class:"className",for:"htmlFor"},b=["push","pop","shift","unshift","splice","sort","reverse","add","delete","clear","set","delete","clear"],N=["a","abbr","address","area","article","aside","audio","b","base","bdi","bdo","blockquote","body","br","button","canvas","caption","cite","code","col","colgroup","data","datalist","dd","del","details","dfn","dialog","div","dl","dt","em","embed","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","i","iframe","img","input","ins","kbd","label","legend","li","link","main","map","mark","menu","meta","meter","nav","noscript","object","ol","optgroup","option","output","p","picture","pre","progress","q","rp","rt","ruby","s","samp","script","section","select","slot","small","source","span","strong","style","sub","summary","sup","table","tbody","td","template","textarea","tfoot","th","thead","time","title","tr","track","u","ul","var","video","wbr","acronym","applet","basefont","bgsound","big","blink","center","dir","font","frame","frameset","isindex","keygen","listing","marquee","menuitem","multicol","nextid","nobr","noembed","noframes","param","plaintext","rb","rtc","spacer","strike","tt","xmp","animate","animateMotion","animateTransform","circle","clipPath","defs","desc","ellipse","feBlend","feColorMatrix","feComponentTransfer","feComposite","feConvolveMatrix","feDiffuseLighting","feDisplacementMap","feDistantLight","feDropShadow","feFlood","feFuncA","feFuncB","feFuncG","feFuncR","feGaussianBlur","feImage","feMerge","feMergeNode","feMorphology","feOffset","fePointLight","feSpecularLighting","feSpotLight","feTile","feTurbulence","filter","foreignObject","g","image","line","linearGradient","marker","mask","metadata","mpath","path","pattern","polygon","polyline","radialGradient","rect","set","stop","svg","switch","symbol","text","textPath","tspan","use","view"],k=["Static","Prop","Env","Content","Children"],g="@dlightjs/dlight",x=Object.fromEntries(["createElement","setStyle","setDataset","setEvent","delegateEvent","setHTMLProp","setHTMLAttr","setHTMLProps","setHTMLAttrs","createTextNode","updateText","insertNode","ForNode","CondNode","ExpNode","EnvNode","SubViewNode","PropView","render"].map(f=>[f,`$$${f}`])),w={textContent:["*"],innerHTML:["*"],accept:["form","input"],acceptCharset:["form"],accesskey:["*"],action:["form"],align:["caption","col","colgroup","hr","iframe","img","table","tbody","td","tfoot","th","thead","tr"],allow:["iframe"],alt:["area","img","input"],async:["script"],autocapitalize:["*"],autocomplete:["form","input","select","textarea"],autofocus:["button","input","select","textarea"],autoplay:["audio","video"],background:["body","table","td","th"],bgColor:["body","col","colgroup","marquee","table","tbody","tfoot","td","th","tr"],border:["img","object","table"],buffered:["audio","video"],capture:["input"],charset:["meta"],checked:["input"],cite:["blockquote","del","ins","q"],className:["*"],color:["font","hr"],cols:["textarea"],colSpan:["td","th"],content:["meta"],contentEditable:["*"],contextmenu:["*"],controls:["audio","video"],coords:["area"],crossOrigin:["audio","img","link","script","video"],csp:["iframe"],data:["object"],dateTime:["del","ins","time"],decoding:["img"],default:["track"],defer:["script"],dir:["*"],dirname:["input","textarea"],disabled:["button","fieldset","input","optgroup","option","select","textarea"],download:["a","area"],draggable:["*"],enctype:["form"],enterKeyHint:["textarea","contenteditable"],htmlFor:["label","output"],form:["button","fieldset","input","label","meter","object","output","progress","select","textarea"],formAction:["input","button"],formEnctype:["button","input"],formMethod:["button","input"],formNoValidate:["button","input"],formTarget:["button","input"],headers:["td","th"],height:["canvas","embed","iframe","img","input","object","video"],hidden:["*"],high:["meter"],href:["a","area","base","link"],hreflang:["a","link"],httpEquiv:["meta"],id:["*"],integrity:["link","script"],intrinsicSize:["img"],inputMode:["textarea","contenteditable"],ismap:["img"],itemProp:["*"],kind:["track"],label:["optgroup","option","track"],lang:["*"],language:["script"],loading:["img","iframe"],list:["input"],loop:["audio","marquee","video"],low:["meter"],manifest:["html"],max:["input","meter","progress"],maxLength:["input","textarea"],minLength:["input","textarea"],media:["a","area","link","source","style"],method:["form"],min:["input","meter"],multiple:["input","select"],muted:["audio","video"],name:["button","form","fieldset","iframe","input","object","output","select","textarea","map","meta","param"],noValidate:["form"],open:["details","dialog"],optimum:["meter"],pattern:["input"],ping:["a","area"],placeholder:["input","textarea"],playsInline:["video"],poster:["video"],preload:["audio","video"],readonly:["input","textarea"],referrerPolicy:["a","area","iframe","img","link","script"],rel:["a","area","link"],required:["input","select","textarea"],reversed:["ol"],role:["*"],rows:["textarea"],rowSpan:["td","th"],sandbox:["iframe"],scope:["th"],scoped:["style"],selected:["option"],shape:["a","area"],size:["input","select"],sizes:["link","img","source"],slot:["*"],span:["col","colgroup"],spellcheck:["*"],src:["audio","embed","iframe","img","input","script","source","track","video"],srcdoc:["iframe"],srclang:["track"],srcset:["img","source"],start:["ol"],step:["input"],style:["*"],summary:["table"],tabIndex:["*"],target:["a","area","base","form"],title:["*"],translate:["*"],type:["button","input","embed","object","ol","script","source","style","menu","link"],usemap:["img","input","object"],value:["button","data","input","li","meter","option","progress","param","text"],width:["canvas","embed","iframe","img","input","object","video"],wrap:["textarea"],ariaAutocomplete:["*"],ariaChecked:["*"],ariaDisabled:["*"],ariaErrorMessage:["*"],ariaExpanded:["*"],ariaHasPopup:["*"],ariaHidden:["*"],ariaInvalid:["*"],ariaLabel:["*"],ariaLevel:["*"],ariaModal:["*"],ariaMultiline:["*"],ariaMultiSelectable:["*"],ariaOrientation:["*"],ariaPlaceholder:["*"],ariaPressed:["*"],ariaReadonly:["*"],ariaRequired:["*"],ariaSelected:["*"],ariaSort:["*"],ariaValuemax:["*"],ariaValuemin:["*"],ariaValueNow:["*"],ariaValueText:["*"],ariaBusy:["*"],ariaLive:["*"],ariaRelevant:["*"],ariaAtomic:["*"],ariaDropEffect:["*"],ariaGrabbed:["*"],ariaActiveDescendant:["*"],ariaColCount:["*"],ariaColIndex:["*"],ariaColSpan:["*"],ariaControls:["*"],ariaDescribedBy:["*"],ariaDescription:["*"],ariaDetails:["*"],ariaFlowTo:["*"],ariaLabelledBy:["*"],ariaOwns:["*"],ariaPosInset:["*"],ariaRowCount:["*"],ariaRowIndex:["*"],ariaRowSpan:["*"],ariaSetSize:["*"]};var v=class{dlightPackageName=g;babelApi;t;traverse;enableDevTools;includes;excludes;htmlTags;attributeMap;constructor(e,t,s,i,r,n,d){this.babelApi=e,this.t=t,this.traverse=e.traverse,this.includes=s,this.excludes=i,this.enableDevTools=D&&r,this.htmlTags=typeof n=="function"?n(N):n.includes("*")?[...new Set([...N,...n])].filter(o=>o!=="*"):n,this.attributeMap=d}classDeclarationNode;classBodyNode;propertiesContainer={};dependencyMap={};enter=!0;dLightModel=!1;enterClassNode=!1;className;programNode;allImports=[];didAlterImports=!1;clearNode(){this.classDeclarationNode=void 0,this.classBodyNode=void 0,this.propertiesContainer={},this.dependencyMap={},this.enter=!0,this.enterClassNode=!1,this.dLightModel=!1,this.className=void 0}get availableProperties(){return Object.entries(this.propertiesContainer).filter(([e,{isWatcher:t,isStatic:s,isChildren:i}])=>e!=="_$compName"&&!t&&!s&&!i).map(([e])=>e)}initNode(e){let t=e.node;if(this.classDeclarationNode=t,this.classBodyNode=t.body,this.propertiesContainer={},t.id?.name||(t.id=this.t.identifier(`Anonymous_${v.uid()}`)),this.className=t.id?.name,this.handleClassCustomDecorators(),this.enableDevTools&&this.classBodyNode.body.unshift(this.t.classProperty(this.t.identifier("_$compName"),this.t.stringLiteral(this.className))),!this.didAlterImports){let s=this.allImports.filter(i=>i.source.value===g);this.dlightPackageName!==g&&s.forEach(i=>{i.source.value=this.dlightPackageName}),this.programNode.body.unshift(this.t.importDeclaration(Object.entries(x).map(([i,r])=>this.t.importSpecifier(this.t.identifier(r),this.t.identifier(i))),this.t.stringLiteral(this.dlightPackageName))),this.didAlterImports=!0}}programEnterVisitor(e,t){if(this.enter=this.fileAllowed(t),!this.enter)return;if(this.allImports=e.node.body.filter(i=>this.t.isImportDeclaration(i)),this.allImports.filter(i=>i.source.value===g).length===0){this.enter=!1;return}this.programNode=e.node}programExitVisitor(){this.enter&&(this.didAlterImports=!1,this.allImports=[],this.programNode=void 0)}classEnter(e){this.enter&&(this.enterClassNode=this.isDLightClass(e),this.enterClassNode&&(this.initNode(e),this.resolveMounting(e)))}classExit(){this.enter&&this.enterClassNode&&(this.transformDLightClass(),this.clearNode(),this.enterClassNode=!1)}classMethodVisitor(e){if(!this.enterClassNode||!this.t.isIdentifier(e.node.key))return;let t=e.node.key.name;if(t==="View"||this.findDecoratorByName(e.node.decorators,"View"))return;let i=e.node,r=this.findDecoratorByName(i.decorators,"Watch");if(this.t.isIdentifier(i.key,{name:"constructor"})||(this.autoBindMethods(i),!r))return;let n=[],d;if(this.t.isIdentifier(r))[n,d]=this.getDependencies(i);else{let o=r.arguments.filter(l=>this.t.isStringLiteral(l)).map(l=>l.value),a=this.t.classMethod("method",i.key,[],this.t.blockStatement([this.t.expressionStatement(this.t.arrayExpression(o.map(l=>this.t.memberExpression(this.t.thisExpression(),this.t.identifier(l)))))]));[n,d]=this.getDependencies(a)}this.propertiesContainer[t]={node:i,deps:n,depsNode:d,isWatcher:!0},i.decorators=this.removeDecorators(i.decorators,["Watch"])}classPropertyVisitor(e){if(!this.enterClassNode)return;let t=e.node;if(!this.t.isIdentifier(t.key))return;let s=t.key.name;if(s==="View")return;let i=t.decorators;if(this.findDecoratorByName(i,"View"))return;let n=this.parseModel(e),d=!!this.findDecoratorByName(i,"Prop"),o=!!this.findDecoratorByName(i,"Env"),a=!!this.findDecoratorByName(t.decorators,"Children"),[l,c]=a?[[]]:this.getDependencies(t);this.propertiesContainer[s]={node:t,deps:l,depsNode:c,isStatic:!!this.findDecoratorByName(i,"Static"),isContent:!!this.findDecoratorByName(i,"Content"),isChildren:a,isPropOrEnv:d?"Prop":o?"Env":void 0,isModel:n},t.decorators=this.removeDecorators(i,k)}resolveWatcherDecorator(e,t){if(!this.t.isIdentifier(e.key))return;let s=e.key.name,i=this.classBodyNode.body.indexOf(e),r=this.t.classProperty(this.t.identifier(`$w$${s}`));this.classBodyNode.body.splice(i,0,r),e.body.body.unshift(this.t.ifStatement(this.t.callExpression(this.t.memberExpression(this.t.thisExpression(),this.t.identifier("_$cache")),[this.t.stringLiteral(s),t]),this.t.blockStatement([this.t.returnStatement()])))}resolveChildrenDecorator(e){if(!this.classBodyNode||!this.t.isIdentifier(e.key))return;let t=e.key.name,s=this.classBodyNode.body.indexOf(e),i=this.t.memberExpression(this.t.thisExpression(),this.t.identifier("_$children")),r=this.t.classMethod("get",this.t.identifier(t),[],this.t.blockStatement([this.t.returnStatement(i)]));this.classBodyNode.body.splice(s,1,r)}resolveContentDecorator(e){if(!this.classBodyNode||!this.t.isIdentifier(e.key)||this.classBodyNode.body.some(r=>this.t.isClassProperty(r)&&r.key.name==="_$contentKey"))return;let t=e.key.name,s=this.classBodyNode.body.indexOf(e),i=this.t.classProperty(this.t.identifier("_$contentKey"),this.t.stringLiteral(t));this.classBodyNode.body.splice(s,0,i)}resolvePropDecorator(e,t){if(!this.classBodyNode||!this.t.isIdentifier(e.key))return;let s=e.key.name,i=this.classBodyNode.body.indexOf(e),r=t.toLowerCase()==="prop"?"p":"e",n=this.t.classProperty(this.t.identifier(`$${r}$${s}`));this.classBodyNode.body.splice(i,0,n)}resolveStateDecorator(e,t,s){if(!this.classBodyNode||!this.t.isIdentifier(e.key))return;let i=e.key.name,r=this.classBodyNode.body.indexOf(e),n=this.dLightModel?[]:[this.t.classProperty(this.t.identifier(`$$${i}`),this.t.numericLiteral(1<<t))],d=s?[this.t.classProperty(this.t.identifier(`$s$${i}`),this.t.arrayExpression([...s].map(o=>this.t.stringLiteral(o))))]:[];this.classBodyNode.body.splice(r+1,0,...n,...d)}handleClassCustomDecorators(){if(!this.classBodyNode)return;let e=this.classDeclarationNode?.decorators;if(!e)return;this.findDecoratorByName(e,"ForwardProps")&&(this.classBodyNode.body.unshift(this.t.classProperty(this.t.identifier("_$forwardProps")),this.t.classProperty(this.t.identifier("_$forwardPropsSet"),this.t.newExpression(this.t.identifier("Set"),[])),this.t.classProperty(this.t.identifier("_$forwardPropsId"),this.t.arrayExpression([]))),this.classDeclarationNode.decorators=this.removeDecorators(e,["ForwardProps"]))}transformDLightClass(){let e=this.handleView();this.dLightModel&&(e=this.availableProperties);let t=Object.entries(this.propertiesContainer).reverse(),s=this.dependencyMapReversed();this.addAutoUpdate(e);for(let[i,{node:r,deps:n,isStatic:d,isChildren:o,isPropOrEnv:a,isWatcher:l,isContent:c,isModel:u,depsNode:h}]of t){if(o){this.resolveChildrenDecorator(r);continue}n.length>0&&(e.push(...n),l?this.resolveWatcherDecorator(r,h):u||this.handleDerivedProperty(r,h)),a&&this.resolvePropDecorator(r,a),c&&(this.resolvePropDecorator(r,"Prop"),this.resolveContentDecorator(r)),!d&&e.includes(i)&&this.resolveStateDecorator(r,this.availableProperties.indexOf(i),s[i])}}addAutoUpdate(e){if(!this.classBodyNode)return;this.classBodyNode.body.filter(s=>!((this.t.isClassProperty(s)||this.t.isClassMethod(s))&&["View","constructor","_$compName"].includes(s.key.name)||this.t.isClassMethod(s,{kind:"method"})&&this.findDecoratorByName(s.decorators,"View"))).forEach(s=>{let i=this.t.isClassProperty(s)?s.value:this.t.isClassMethod(s)?s.body:null;i&&(this.addUpdateDerived(i,e),this.addUpdateView(i))})}addUpdateDerived(e,t){let s=i=>this.t.callExpression(this.t.memberExpression(this.t.thisExpression(),this.t.identifier("_$updateDerived")),[this.t.stringLiteral(i)]);this.traverse(this.valueWrapper(e),{MemberExpression:i=>{if(!this.t.isThisExpression(i.node.object)||!this.t.isIdentifier(i.node.property))return;let r=i.node.property.name;if(!t.includes(r))return;let n=this.isAssignmentExpressionLeft(i);n&&(n.replaceWith(this.t.sequenceExpression([n.node,s(r)])),n.skip())},CallExpression:i=>{if(!this.t.isMemberExpression(i.node.callee))return;let r=i.node.callee.property;if(!this.t.isIdentifier(r)||!b.includes(r.name))return;let n=i.get("callee").get("object");for(;this.t.isMemberExpression(n.node);)n=n.get("object");if(!this.t.isThisExpression(n?.node))return;let d=n.parentPath.node.property.name;i.replaceWith(this.t.sequenceExpression([i.node,s(d)])),i.skip()}})}addUpdateView(e){let t=()=>this.t.expressionStatement(this.t.callExpression(this.t.memberExpression(this.t.thisExpression(),this.t.identifier("_$updateView")),[])),s=r=>this.t.isIfStatement(r)||this.t.isLoop(r)||this.t.isTryStatement(r)||this.t.isSwitchStatement(r),i=r=>{this.t.isBlockStatement(r.node.body)||(r.node.body=this.t.sequenceExpression([r.node.body,t().expression]))};this.traverse(this.valueWrapper(e),{BlockStatement:r=>{let n=r.node.body,d=n.findIndex(a=>this.t.isReturnStatement(a)),o=r.parentPath;if(d===-1){if(s(o?.node))return;n.push(t());return}n.splice(d,0,t())},FunctionExpression:i,ArrowFunctionExpression:i})}addViewAutoUpdate(e){typeof e=="object"&&Object.entries(e).forEach(([,t])=>{t&&(t.type?.[0]&&t.type[0]!==t.type[0].toLowerCase()?this.t.isExpression(t)&&(this.addUpdateDerived(t,this.availableProperties),this.addUpdateView(t)):this.addViewAutoUpdate(t))})}handleView(){if(!this.classBodyNode)return[];let e=new Set,t,s=[];for(let o of this.classBodyNode.body){if(!this.t.isClassProperty(o)&&!this.t.isClassMethod(o)||!this.t.isIdentifier(o.key))continue;let a=this.findDecoratorByName(o.decorators,"View"),l=o.key.name==="View";if(!(!a&&!l)){if(this.t.isClassProperty(o)){let c=o.value;for(;this.t.isTSAsExpression(c);)c=c.expression;if(!this.t.isArrowFunctionExpression(c))continue;o.value=c;let u=this.arrowFunctionPropertyToMethod(o);if(!u)continue;o=u}a?(o.decorators=null,s.push(o)):t=o}}let i=s.map(o=>o.key.name),r=Object.fromEntries(s.map(o=>{let a=o.params[0];if(!a||!this.t.isObjectPattern(a))return["-",null];let l=Object.fromEntries(a.properties.map(c=>{if(!this.t.isObjectProperty(c))return["-",null];let u=c.key.name,h=this.getIdentifiers(this.t.assignmentExpression("=",this.t.objectPattern([this.t.objectProperty(this.t.numericLiteral(0),c.value)]),this.t.numericLiteral(0))).filter(m=>m!==u);return[u,h]}).filter(([c,u])=>u));return[o.key.name,l]}).filter(([o,a])=>a)),n=-1;if(t){let o;[o,n]=this.alterMainView(t,i,r),o.forEach(e.add.bind(e))}s.forEach(o=>{let a;[a,n]=this.alterSubView(o,i,r,n),a.forEach(e.add.bind(e))});let d=[];return this.availableProperties.forEach(o=>{e.has(o)&&d.push(o)}),d}alterMainView(e,t,s){let i=S(e.body,{babelApi:this.babelApi,subviewNames:t,htmlTags:this.htmlTags});i.map(l=>this.addViewAutoUpdate(l));let[r,n]=M(i,{babelApi:this.babelApi,availableProperties:this.availableProperties,dependencyMap:this.dependencyMap,reactivityFuncNames:b}),[d,o,a]=T(r,{babelApi:this.babelApi,className:this.className,importMap:x,subViewPropMap:Object.fromEntries(Object.entries(s).map(([l,c])=>[l,Object.keys(c)])),templateIdx:-1,attributeMap:this.attributeMap,alterAttributeMap:E});return e.body=d,this.classBodyNode?.body.push(...o),[n,a]}alterSubView(e,t,s,i){e.params.length===0?e.params.push(this.t.identifier("_$"),this.t.identifier("$subviewNode")):e.params.length===1?e.params.push(this.t.identifier("$subviewNode")):(e.params[1]=this.t.identifier("$subviewNode"),e.params.length=2);let r=S(e.body,{babelApi:this.babelApi,subviewNames:t,htmlTags:this.htmlTags});r.map(p=>this.addViewAutoUpdate(p));let n=s[e.key.name]??[],d={};Object.entries(n).forEach(([p,y])=>{y.forEach(L=>{d[L]=[p]})});let[o,a]=M(r,{babelApi:this.babelApi,availableProperties:this.availableProperties,availableIdentifiers:Object.keys(n),dependencyMap:this.dependencyMap,dependencyParseType:"property",reactivityFuncNames:b}),[l]=M(r,{babelApi:this.babelApi,availableProperties:Object.keys(n),dependencyMap:this.dependencyMap,dependencyParseType:"identifier",identifierDepMap:d,reactivityFuncNames:b}),c=Object.fromEntries(Object.entries(s).map(([p,y])=>[p,Object.keys(y)])),[u,h,m]=j(o,l,e.params[0],{babelApi:this.babelApi,className:this.className,importMap:x,subViewPropMap:c,templateIdx:i,attributeMap:this.attributeMap,alterAttributeMap:E});return e.body=u,this.classBodyNode?.body.push(...h),[a,m]}fileAllowed(e){return this.includes.includes("*")?!0:!(!e||this.excludes.some(t=>I(e,t))||!this.includes.some(t=>I(e,t)))}isDLightView(e){let t=e.node;return(t.decorators??[]).find(r=>this.t.isIdentifier(r.expression,{name:"View"}))&&(t.superClass=this.t.identifier("View"),t.decorators=t.decorators?.filter(r=>!this.t.isIdentifier(r.expression,{name:"View"}))),this.t.isIdentifier(t.superClass,{name:"View"})}isDLightModel(e){let t=e.node;return(t.decorators??[]).find(r=>this.t.isIdentifier(r.expression,{name:"Model"}))&&(t.superClass=this.t.identifier("Model"),t.decorators=t.decorators?.filter(r=>!this.t.isIdentifier(r.expression,{name:"Model"}))),t.body.body.unshift(this.t.classProperty(this.t.identifier("_$model"))),t.body.body=t.body.body.filter(r=>!((this.t.isClassProperty(r)||this.t.isClassMethod(r,{kind:"method"}))&&(this.findDecoratorByName(r.decorators,"View")||this.t.isIdentifier(r.key)&&r.key.name==="View"))),this.dLightModel=!0,this.t.isIdentifier(t.superClass,{name:"Model"})}isDLightClass(e){return this.isDLightView(e)||this.isDLightModel(e)}parseModel(e){if(!this.allImports.some(h=>h.source.value===this.dlightPackageName&&h.specifiers.some(m=>{if(this.t.isImportSpecifier(m)&&this.t.isIdentifier(m.imported,{name:"use"}))return!0})))return;let s=e.node,i=s.key;if(!this.t.isIdentifier(i))return;let r=s.value;if(!this.t.isCallExpression(r)||!this.t.isIdentifier(r.callee,{name:"use"}))return;let n=r.arguments,d=n[1],o=n[2],a=this.t.nullLiteral();if(d){let h=[],m=[];if(this.t.isObjectExpression(d))d.properties.forEach(p=>{if(this.t.isSpreadElement(p)){let[,y]=this.getDependenciesFromNode(p.argument);h.push([p.argument,y??this.t.nullLiteral()])}else if(this.t.isObjectProperty(p)){let[,y]=this.getDependenciesFromNode(p.value);m.push([!p.computed&&this.t.isIdentifier(p.key)?this.t.stringLiteral(p.key.name):p.key,p.value,y??this.t.nullLiteral()])}else m.push([!p.computed&&this.t.isIdentifier(p.key)?this.t.stringLiteral(p.key.name):p.key,this.t.arrowFunctionExpression([],p.body),this.t.nullLiteral()])});else{let[,p]=this.getDependenciesFromNode(d);h.push([d,p??this.t.nullLiteral()])}a=this.t.objectExpression([this.t.objectProperty(this.t.identifier("m"),this.t.arrayExpression(h.map(p=>this.t.arrayExpression(p)))),this.t.objectProperty(this.t.identifier("s"),this.t.arrayExpression(m.map(p=>this.t.arrayExpression(p))))])}let l=this.t.nullLiteral();if(o){let[,h]=this.getDependenciesFromNode(o);l=this.t.arrayExpression([o,h??this.t.nullLiteral()])}n[1]=this.t.arrowFunctionExpression([],a),n[2]=this.t.arrowFunctionExpression([],l),n[3]=this.t.stringLiteral(i.name),r.callee=this.t.memberExpression(this.t.thisExpression(),this.t.identifier("_$injectModel"));let c=this.classBodyNode.body.indexOf(s),u=this.t.classProperty(this.t.identifier(`$md$${i.name}`));return this.classBodyNode.body.splice(c,0,u),!0}removeDecorators(e,t){return e?e.filter(s=>!(this.t.isIdentifier(s.expression)&&t.includes(s.expression.name)||this.t.isCallExpression(s.expression)&&this.t.isIdentifier(s.expression.callee)&&t.includes(s.expression.callee.name))):[]}findDecoratorByName(e,t){if(e)return e.find(s=>this.t.isIdentifier(s.expression,{name:t})||this.t.isCallExpression(s.expression)&&this.t.isIdentifier(s.expression.callee,{name:t}))?.expression}geneDependencyNode(e){let t=e;for(;t?.parentPath;){let s=t.parentPath;if(!(this.t.isBinaryExpression(s.node)||this.t.isMemberExpression(s.node)))return t.node;t=s}return e.node}addConstructor(){let e=this.classBodyNode.body.find(t=>this.t.isClassMethod(t,{kind:"constructor"}));return e||(e=this.t.classMethod("constructor",this.t.identifier("constructor"),[],this.t.blockStatement([this.t.expressionStatement(this.t.callExpression(this.t.super(),[]))])),this.classBodyNode.body.unshift(e),e)}autoBindMethods(e){this.addConstructor().body.body.push(this.t.expressionStatement(this.t.assignmentExpression("=",this.t.memberExpression(this.t.thisExpression(),e.key),this.t.callExpression(this.t.memberExpression(this.t.memberExpression(this.t.thisExpression(),e.key),this.t.identifier("bind")),[this.t.thisExpression()]))))}handleDerivedProperty(e,t){if(!this.t.isIdentifier(e.key))return;let s=e.key.name,i=e.value,r=this.classBodyNode.body.indexOf(e),n=this.t.classMethod("get",this.t.identifier(`$f$${s}`),[],this.t.blockStatement([this.t.ifStatement(this.t.callExpression(this.t.memberExpression(this.t.thisExpression(),this.t.identifier("_$cache")),[this.t.stringLiteral(s),t]),this.t.blockStatement([this.t.returnStatement(this.t.memberExpression(this.t.thisExpression(),this.t.identifier(s)))])),this.t.returnStatement(i)]));this.classBodyNode.body.splice(r+1,0,n),e.value=null}getDependenciesFromNode(e,t=!1){let s=new Set,i=new Set,r={};this.traverse(this.valueWrapper(e),{MemberExpression:o=>{if(!this.t.isIdentifier(o.node.property)||!this.t.isThisExpression(o.node.object))return;let a=o.node.property.name;this.isAssignmentExpressionLeft(o)||this.isAssignmentFunction(o)?i.add(a):this.availableProperties.includes(a)&&!this.isMemberInEscapeFunction(o,this.classDeclarationNode)&&!this.isMemberInManualFunction(o,this.classDeclarationNode)&&(s.add(a),t&&this.dependencyMap[a]?.forEach(s.add.bind(s)),r[a]||(r[a]=[]),r[a].push(this.geneDependencyNode(o)))}}),i.forEach(o=>{s.delete(o),delete r[o]});let n=Object.values(r).flat();n=n.filter((o,a)=>n.findIndex(c=>this.t.isNodesEquivalent(c,o))===a);let d=[...s];if(t&&s.size>0){let o=e.body.body[0].key.name;this.dependencyMap[o]=d}return[d,this.t.arrayExpression(n)]}getDependencies(e){if(!this.t.isIdentifier(e.key))return[[],void 0];let t=this.t.classDeclaration(null,null,this.t.classBody([e]));return this.getDependenciesFromNode(t,!0)}dependencyMapReversed(){let e={};return Object.entries(this.dependencyMap).forEach(([t,s])=>{s.forEach(i=>{e[i]||(e[i]=new Set),e[i].add(t)})}),e}resolveMounting(e){let t=e.node;if(!this.t.isIdentifier(t.id))return;let s=t.decorators??[],i=a=>{let l=s.find(c=>this.t.isIdentifier(c.expression,{name:a}));return l&&s.splice(s.findIndex(c=>c===l),1),l},r=i("Main")?"main":i("App")?"app":null,n;if(r)n=this.t.stringLiteral(r);else{let a=s.find(l=>this.t.isCallExpression(l.expression)&&this.t.isIdentifier(l.expression.callee,{name:"Mount"})&&l.expression.arguments.length===1);if(!a)return;s.splice(s.findIndex(l=>l===a),1),n=a.expression.arguments[0]}let d=e.parentPath.node;if(!this.t.isBlockStatement(d)&&!this.t.isProgram(d))return;let o=d.body.indexOf(t);d.body.splice(o+1,0,this.t.expressionStatement(this.t.callExpression(this.t.identifier(x.render),[n,t.id])))}arrowFunctionPropertyToMethod(e){if(!this.t.isArrowFunctionExpression(e.value))return;let t=e.value;if(!this.t.isBlockStatement(t.body))return;let s=this.classBodyNode.body.indexOf(e),i=this.t.classMethod("method",e.key,t.params,t.body);return this.classBodyNode.body.splice(s,1,i),i}isMemberExpressionProperty(e,t){return this.t.isMemberExpression(e)&&!e.computed&&e.property===t}isObjectKey(e,t){return this.t.isObjectProperty(e)&&e.key===t}valueWithArrowFunc(e){e.value||(e.value=this.t.identifier("undefined")),e.value=this.t.arrowFunctionExpression([],e.value)}getAllTopLevelReturnBlock(e){let t=[],s=!1;return this.traverse(this.valueWrapper(e),{Function:i=>{s||(s=!0,i.skip())},ReturnStatement:i=>{if(s)return;let r=i.parentPath.node;if(this.t.isBlockStatement(r))t.push(r);else{let n=this.t.blockStatement([i.node]);i.replaceWith(n),t.push(n)}i.skip()},exit:i=>{this.t.isFunction(i.node)&&(s=!1)}}),t}valueWrapper(e){return this.t.file(this.t.program([this.t.isStatement(e)?e:this.t.expressionStatement(e)]))}isAttrFromFunction(e,t){let s=e.parentPath,i=r=>this.t.isIdentifier(r)?r.name===t:this.t.isAssignmentPattern(r)?i(r.left):this.t.isArrayPattern(r)?r.elements.filter(Boolean).map(n=>i(n)).includes(!0):this.t.isObjectPattern(r)?r.properties.filter(n=>this.t.isObjectProperty(n)&&this.t.isIdentifier(n.key)).map(n=>n.key.name).includes(t):this.t.isRestElement(r)?i(r.argument):!1;for(;s;){let r=s.node;if(this.t.isArrowFunctionExpression(r)||this.t.isFunctionDeclaration(r)){for(let n of r.params)if(i(n))return!0}s=s.parentPath}return!1}isStandAloneIdentifier(e){let t=e.node,s=e.parentPath?.node;if(this.t.isMemberExpression(s)&&s.property===t||this.isAttrFromFunction(e,t.name))return!1;for(;e.parentPath;){if(this.t.isVariableDeclarator(e.parentPath.node)||this.t.isObjectProperty(e.parentPath.node)&&e.parentPath.node.key===e.node&&!e.parentPath.node.computed)return!1;e=e.parentPath}return!0}getIdentifiers(e){if(this.t.isIdentifier(e))return[e.name];let t=new Set;return this.traverse(this.valueWrapper(e),{Identifier:s=>{this.isStandAloneIdentifier(s)&&t.add(s.node.name)}}),[...t]}isAssignmentExpressionLeft(e){let t=e.parentPath;for(;t&&!this.t.isStatement(t.node);){if(this.t.isAssignmentExpression(t.node)){if(t.node.left===e.node)return t;let s=t.get("left");if(e.isDescendant(s))return t}else if(this.t.isUpdateExpression(t.node))return t;t=t.parentPath}return null}isAssignmentFunction(e){let t=e.parentPath;for(;t&&this.t.isMemberExpression(t.node);)t=t.parentPath;return t?this.t.isCallExpression(t.node)&&this.t.isMemberExpression(t.node.callee)&&this.t.isIdentifier(t.node.callee.property)&&b.includes(t.node.callee.property.name):!1}isMemberInEscapeFunction(e,t){let s=!1,i=e.parentPath;for(;i&&i.node!==t;){let r=i.node;if(this.t.isCallExpression(r)&&this.t.isIdentifier(r.callee)&&v.escapeNamings.includes(r.callee.name)){s=!0;break}i=i.parentPath}return s}isMemberInManualFunction(e,t){let s=!1,i=e.parentPath;for(;i&&i.node!==t;){let r=i.node,n=i.parentPath?.node,d=this.t.isFunctionExpression(r)||this.t.isArrowFunctionExpression(r),o=this.t.isCallExpression(n)&&this.t.isIdentifier(n.callee)&&n.callee.name==="manual";if(d&&o){s=!0;break}i=i.parentPath}return s}static uid(e=4){return Math.random().toString(32).slice(2,e+2)}},P=v;C(P,"escapeNamings",["escape","$"]);var A=P;function B(f,e){let{types:t}=f,{files:s="**/*.{js,ts}",excludeFiles:i="**/{dist,node_modules,lib}/*",enableDevTools:r=!1,htmlTags:n=a=>a,attributeMap:d=w}=e,o=new A(f,t,Array.isArray(s)?s:[s],Array.isArray(i)?i:[i],r,n,d);return{visitor:{Program:{enter(a,{filename:l}){return o.programEnterVisitor(a,l)},exit:o.programExitVisitor.bind(o)},ClassDeclaration:{enter:o.classEnter.bind(o),exit:o.classExit.bind(o)},ClassMethod:o.classMethodVisitor.bind(o),ClassProperty:o.classPropertyVisitor.bind(o)}}}function te(f,e){return{plugins:[$,[V.default??V,{legacy:!0}],[B,e]]}}export{te as default}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "babel-preset-dlight", | ||
"version": "1.0.0-beta.11", | ||
"version": "1.0.0-beta.12", | ||
"description": "DLight transpiler babel preset", | ||
@@ -31,5 +31,5 @@ "author": { | ||
"@dlightjs/reactivity-parser": "1.0.0-beta.2", | ||
"babel-plugin-syntax-typescript-new": "1.0.0", | ||
"@dlightjs/view-parser": "1.0.0-alpha.4", | ||
"@dlightjs/view-generator": "1.0.0-beta.3" | ||
"@dlightjs/view-generator": "1.0.0-beta.3", | ||
"babel-plugin-syntax-typescript-new": "1.0.0" | ||
}, | ||
@@ -36,0 +36,0 @@ "tsup": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
272646
268