@dlightjs/view-parser
Advanced tools
Comparing version 1.0.0-alpha.4 to 1.0.0-beta.5
@@ -5,3 +5,3 @@ import Babel, { types } from '@babel/core'; | ||
value: types.Expression; | ||
viewPropMap?: Record<string, ViewUnit[]>; | ||
viewPropMap: Record<string, ViewUnit[]>; | ||
} | ||
@@ -15,5 +15,4 @@ interface TextUnit { | ||
tag: types.Expression; | ||
content?: ViewProp; | ||
props?: Record<string, ViewProp>; | ||
children?: ViewUnit[]; | ||
props: Record<string, ViewProp>; | ||
children: ViewUnit[]; | ||
} | ||
@@ -23,5 +22,4 @@ interface CompUnit { | ||
tag: types.Expression; | ||
content?: ViewProp; | ||
props?: Record<string, ViewProp>; | ||
children?: ViewUnit[]; | ||
props: Record<string, ViewProp>; | ||
children: ViewUnit[]; | ||
} | ||
@@ -32,3 +30,3 @@ interface ForUnit { | ||
array: types.Expression; | ||
key?: types.Expression; | ||
key: types.Expression; | ||
children: ViewUnit[]; | ||
@@ -45,3 +43,3 @@ } | ||
interface SwitchBranch { | ||
case: types.Expression | null | undefined; | ||
case: types.Expression; | ||
children: ViewUnit[]; | ||
@@ -63,3 +61,3 @@ break: boolean; | ||
content: ViewProp; | ||
props?: Record<string, ViewProp>; | ||
props: Record<string, ViewProp>; | ||
} | ||
@@ -69,4 +67,4 @@ interface SubviewUnit { | ||
tag: string; | ||
props?: Record<string, ViewProp>; | ||
children?: ViewUnit[]; | ||
props: Record<string, ViewProp>; | ||
children: ViewUnit[]; | ||
} | ||
@@ -73,0 +71,0 @@ type ViewUnit = TextUnit | HTMLUnit | CompUnit | IfUnit | ForUnit | EnvUnit | ExpUnit | SubviewUnit | SwitchUnit; |
@@ -1,2 +0,2 @@ | ||
import{createErrorHandler as y}from"@dlightjs/error-handler";var o=y("ViewParser",{1:"Invalid syntax in DLight's View, only accepts dot chain call expression",2:"First argument of $0() must be an expression",3:"Invalid syntax in DLight's View, only accepts expression as props",4:"Invalid SubView calling, only accepts static subview calling like `this.SubView()`"},{1:"DLight only accepts ForOfStatement as for loop, skipping this statement",2:"EnvUnit must have at least one child, skipping this statement",3:"Only Env/Comp/HTMLUnit can have a statement block as its children, skipping this statement",4:"If you want to use a key in a for loop, make the first statement as a label statement like `key: item`, skipping this key for now",5:"ForUnit must have at least one child, skipping this statement"},{1:"EnvUnit must have at least one prop, skipping this statement and flattening its children"});var h=class{compWrapper="comp";htmlTagWrapper="tag";environmentTagName="env";expressionTagName="_";config;t;traverse;subviewNames;htmlTags;viewUnits=[];constructor(t){this.config=t,this.t=t.babelApi.types,this.traverse=t.babelApi.traverse,this.subviewNames=t.subviewNames,this.htmlTags=t.htmlTags}parse(t){return[...t.directives,...t.body].forEach(this.parseStatement.bind(this)),this.viewUnits.length===1&&this.viewUnits[0].type==="env"&&this.viewUnits[0].children.length===0?(o.error2(),[]):this.viewUnits}parseStatement(t){if(!this.isInvalidExpression(t)){if(this.t.isExpressionStatement(t)){this.parseExpression(t.expression);return}if(this.t.isForOfStatement(t)){this.parseFor(t);return}if(this.t.isIfStatement(t)){this.parseIf(t);return}if(this.t.isSwitchStatement(t)){this.parseSwitch(t);return}if(this.t.isDirective(t)){this.parseText(t.value);return}if(this.t.isBlockStatement(t)){let i=this.viewUnits[this.viewUnits.length-1],e=i?.type,s=this.parseView(t);e==="html"?(delete i.content,i.children=s):e==="comp"||e==="subview"?i.children=s:e==="env"?s.length>0?i.children=s:(this.viewUnits.pop(),o.error2()):o.error3()}}}parseExpression(t){if(this.t.isCallExpression(t)){this.parseTag(t);return}if(this.t.isStringLiteral(t)||this.t.isTemplateLiteral(t)){this.parseText(t);return}if(this.t.isTaggedTemplateExpression(t)){this.parseTaggedTemplate(t);return}this.viewUnits.push({type:"exp",content:this.parseProp(t)})}parseIfBranches(t){let i=[],e=t.test,s=this.t.isBlockStatement(t.consequent)?t.consequent:this.t.blockStatement([t.consequent]);if(i.push({condition:e,children:this.parseView(s)}),this.t.isIfStatement(t.alternate))i.push(...this.parseIfBranches(t.alternate));else if(t.alternate){let r=this.t.isBlockStatement(t.alternate)?t.alternate:this.t.blockStatement([t.alternate]);i.push({condition:this.t.booleanLiteral(!0),children:this.parseView(r)})}return i}parseIf(t){this.viewUnits.push({type:"if",branches:this.parseIfBranches(t)})}parseSwitch(t){let i=[];t.cases.forEach(s=>{let r=s.consequent,n=r.length===1&&this.t.isBlockStatement(r[0])?r[0]:this.t.blockStatement(r),a=this.t.isBreakStatement(n.body[n.body.length-1]);a&&n.body.pop();let l=this.parseView(n),f={case:s.test,children:l,break:a};i.push(f)}),this.viewUnits.push({type:"switch",discriminant:t.discriminant,branches:i})}parseFor(t){let i=t.left;this.t.isVariableDeclaration(i)||o.throw1();let e=i.declarations[0].id,s=t.right,r,n=t.body,a;if(this.t.isExpressionStatement(n))a=[n];else if(this.t.isBlockStatement(n)){let p=n.body;if(p.length===0)return o.error5();let c=p[0];if(this.t.isLabeledStatement(c)&&this.t.isIdentifier(c.label)){if(c.label.name!=="key"||!this.t.isExpressionStatement(c.body))o.error4();else{let m=c.body.expression;this.t.isExpression(m)&&!(this.t.isNullLiteral(m)||this.t.isIdentifier(m)&&m.name==="undefined")&&(r=m)}a=p.slice(1)}else a=p}else return;let l=a.filter(p=>this.t.isDirective(p)),f=a.filter(p=>!this.t.isDirective(p)),v=this.t.blockStatement(f,l);this.viewUnits.push({type:"for",item:e,array:s,key:r,children:this.parseView(v)})}parseText(t){this.t.isDirectiveLiteral(t)&&(t=this.t.stringLiteral(t.value)),this.viewUnits.push({type:"text",content:t})}parseTaggedTemplate(t){if(this.t.isStringLiteral(t.tag)||this.t.isTemplateLiteral(t.tag)){this.viewUnits.push({type:"text",content:t.tag}),this.viewUnits.push({type:"text",content:t.quasi});return}this.viewUnits.push({type:"exp",content:this.parseProp(t)})}parseProp(t){if(t&&!this.t.isExpression(t)&&o.throw3(),t=t,!t)return{value:this.t.booleanLiteral(!0),viewPropMap:{}};let i={};return this.traverse(this.valueWrapper(t),{ArrowFunctionExpression:e=>{let s=e.node;if(s.params.length===0)return;let r=s.params[0];if(!this.t.isIdentifier(r,{name:"View"})&&!this.t.isIdentifier(r,{name:"_View"}))return;let n=this.t.isBlockStatement(s.body)?s.body:this.t.blockStatement([this.t.expressionStatement(s.body)]),a=this.uid();i[a]=this.parseView(n);let l=this.t.stringLiteral(a);s===t&&(t=l),e.replaceWith(l),e.skip()}}),{value:t,viewPropMap:i}}parseTag(t){let i={},e=t;for(;this.t.isMemberExpression(e?.callee)&&e?.callee?.object&&!this.isPureMemberExpression(e.callee);){let r=e.callee.property;if(!this.t.isIdentifier(r)||!this.t.isCallExpression(e.callee.object)){o.throw1();continue}let n=r.name,a=this.parseProp(e.arguments[0]);i[n]=a,e=e.callee.object}let s;if(e.arguments.length>0&&(s=this.parseProp(e.arguments[0])),this.t.isIdentifier(e.callee)){let r=e.callee.name;if(r===this.expressionTagName&&s){this.viewUnits.push({type:"exp",content:s,props:i});return}if(r===this.environmentTagName){if(Object.keys(i).length===0){o.warn1();return}this.viewUnits.push({type:"env",props:i,children:[]});return}if(this.htmlTags.includes(r)){this.viewUnits.push({type:"html",tag:this.t.stringLiteral(r),content:s,props:i});return}this.viewUnits.push({type:"comp",tag:e.callee,content:s,props:i});return}if(this.t.isMemberExpression(e.callee)&&this.t.isThisExpression(e.callee.object)&&this.t.isIdentifier(e.callee.property)&&this.subviewNames.includes(e.callee.property.name)){if(s&&(i.content=s),!(this.t.isMemberExpression(e.callee)&&this.t.isThisExpression(e.callee.object)&&this.t.isIdentifier(e.callee.property)))return o.throw4();this.viewUnits.push({type:"subview",tag:e.callee.property.name,props:i});return}if(this.t.isExpression(e.callee)){let[r,n]=this.alterTagType(e.callee);this.viewUnits.push({type:r,tag:n,content:s,props:i})}}isPureMemberExpression(t){let i=!0;return this.traverse(this.valueWrapper(t),{CallExpression:()=>{i=!1}}),i}alterTagType(t){if(this.t.isCallExpression(t)&&this.t.isIdentifier(t.callee)){let i=t.callee.name,e=i===this.htmlTagWrapper?"html":i===this.compWrapper?"comp":void 0;if(e){let s=t.arguments[0];return this.t.isExpression(s)||o.throw2(i),[e,s]}}return["comp",t]}isInvalidExpression(t){return this.t.isForStatement(t)&&!this.t.isForOfStatement(t)?(o.error1(),!0):!1}valueWrapper(t){return this.t.file(this.t.program([this.t.expressionStatement(t)]))}parseView(t){return new h(this.config).parse(t)}uid(){return Math.random().toString(36).slice(2)}};function x(u,t){return new h(t).parse(u)}export{x as parseView}; | ||
import{createErrorHandler as y}from"@dlightjs/error-handler";var o=y("ViewParser",{1:"Invalid syntax in DLight's View, only accepts dot chain call expression",2:"First argument of $0() must be an expression",3:"Invalid syntax in DLight's View, only accepts expression as props",4:"Invalid SubView calling, only accepts static subview calling like `this.SubView()`"},{1:"DLight only accepts ForOfStatement as for loop, skipping this statement",2:"EnvUnit must have at least one child, skipping this statement",3:"Only Env/Comp/HTMLUnit can have a statement block as its children, skipping this statement",4:"If you want to use a key in a for loop, make the first statement as a label statement like `key: item`, skipping this key for now",5:"ForUnit must have at least one child, skipping this statement"},{1:"EnvUnit must have at least one prop, skipping this statement and flattening its children"});var h=class{compWrapper="comp";htmlTagWrapper="tag";environmentTagName="env";expressionTagName="_";config;t;traverse;subviewNames;htmlTags;viewUnits=[];constructor(t){this.config=t,this.t=t.babelApi.types,this.traverse=t.babelApi.traverse,this.subviewNames=t.subviewNames,this.htmlTags=t.htmlTags}parse(t){return[...t.directives,...t.body].forEach(this.parseStatement.bind(this)),this.viewUnits.length===1&&this.viewUnits[0].type==="env"&&this.viewUnits[0].children.length===0?(o.error2(),[]):this.viewUnits}parseStatement(t){if(!this.isInvalidExpression(t)){if(this.t.isExpressionStatement(t)){this.parseExpression(t.expression);return}if(this.t.isForOfStatement(t)){this.parseFor(t);return}if(this.t.isIfStatement(t)){this.parseIf(t);return}if(this.t.isSwitchStatement(t)){this.parseSwitch(t);return}if(this.t.isDirective(t)){this.parseText(t.value);return}if(this.t.isBlockStatement(t)){let e=this.viewUnits[this.viewUnits.length-1],i=e?.type,s=this.parseView(t);i==="html"?(delete e.props.textContent,e.children=s):i==="comp"||i==="subview"?e.children=s:i==="env"?s.length>0?e.children=s:(this.viewUnits.pop(),o.error2()):o.error3()}}}parseExpression(t){if(this.t.isCallExpression(t)){this.parseTag(t);return}if(this.t.isStringLiteral(t)||this.t.isTemplateLiteral(t)){this.parseText(t);return}if(this.t.isTaggedTemplateExpression(t)){this.parseTaggedTemplate(t);return}this.viewUnits.push({type:"exp",content:this.parseProp(t),props:{}})}parseIfBranches(t){let e=[],i=t.test,s=this.t.isBlockStatement(t.consequent)?t.consequent:this.t.blockStatement([t.consequent]);if(e.push({condition:i,children:this.parseView(s)}),this.t.isIfStatement(t.alternate))e.push(...this.parseIfBranches(t.alternate));else if(t.alternate){let r=this.t.isBlockStatement(t.alternate)?t.alternate:this.t.blockStatement([t.alternate]);e.push({condition:this.t.booleanLiteral(!0),children:this.parseView(r)})}return e}parseIf(t){this.viewUnits.push({type:"if",branches:this.parseIfBranches(t)})}parseSwitch(t){let e=[];t.cases.forEach(s=>{let r=s.consequent,n=r.length===1&&this.t.isBlockStatement(r[0])?r[0]:this.t.blockStatement(r),a=this.t.isBreakStatement(n.body[n.body.length-1]);a&&n.body.pop();let l=this.parseView(n),f={case:s.test??this.t.booleanLiteral(!0),children:l,break:a};e.push(f)}),this.viewUnits.push({type:"switch",discriminant:t.discriminant,branches:e})}parseFor(t){let e=t.left;this.t.isVariableDeclaration(e)||o.throw1();let i=e.declarations[0].id,s=t.right,r=this.t.nullLiteral(),n=t.body,a;if(this.t.isExpressionStatement(n))a=[n];else if(this.t.isBlockStatement(n)){let p=n.body;if(p.length===0)return o.error5();let c=p[0];if(this.t.isLabeledStatement(c)&&this.t.isIdentifier(c.label)){if(c.label.name!=="key"||!this.t.isExpressionStatement(c.body))o.error4();else{let m=c.body.expression;this.t.isExpression(m)&&!(this.t.isNullLiteral(m)||this.t.isIdentifier(m)&&m.name==="undefined")&&(r=m)}a=p.slice(1)}else a=p}else return;let l=a.filter(p=>this.t.isDirective(p)),f=a.filter(p=>!this.t.isDirective(p)),v=this.t.blockStatement(f,l);this.viewUnits.push({type:"for",item:i,array:s,key:r,children:this.parseView(v)})}parseText(t){this.t.isDirectiveLiteral(t)&&(t=this.t.stringLiteral(t.value)),this.viewUnits.push({type:"text",content:t})}parseTaggedTemplate(t){if(this.t.isStringLiteral(t.tag)||this.t.isTemplateLiteral(t.tag)){this.viewUnits.push({type:"text",content:t.tag}),this.viewUnits.push({type:"text",content:t.quasi});return}this.viewUnits.push({type:"exp",content:this.parseProp(t),props:{}})}parseProp(t){if(t&&!this.t.isExpression(t)&&o.throw3(),t=t,!t)return{value:this.t.booleanLiteral(!0),viewPropMap:{}};let e={};return this.traverse(this.valueWrapper(t),{ArrowFunctionExpression:i=>{let s=i.node;if(s.params.length===0)return;let r=s.params[0];if(!this.t.isIdentifier(r,{name:"View"})&&!this.t.isIdentifier(r,{name:"_View"}))return;let n=this.t.isBlockStatement(s.body)?s.body:this.t.blockStatement([this.t.expressionStatement(s.body)]),a=this.uid();e[a]=this.parseView(n);let l=this.t.stringLiteral(a);s===t&&(t=l),i.replaceWith(l),i.skip()}}),{value:t,viewPropMap:e}}parseTag(t){let e={},i=t;for(;this.t.isMemberExpression(i?.callee)&&i?.callee?.object&&!this.isPureMemberExpression(i.callee);){let r=i.callee.property;if(!this.t.isIdentifier(r)||!this.t.isCallExpression(i.callee.object)){o.throw1();continue}let n=r.name,a=this.parseProp(i.arguments[0]);e[n]=a,i=i.callee.object}let s;if(i.arguments.length>0&&(s=this.parseProp(i.arguments[0])),this.t.isIdentifier(i.callee)){let r=i.callee.name;if(r===this.expressionTagName&&s){this.viewUnits.push({type:"exp",content:s,props:e});return}if(r===this.environmentTagName){if(Object.keys(e).length===0){o.warn1();return}this.viewUnits.push({type:"env",props:e,children:[]});return}if(this.htmlTags.includes(r)){s&&(e.textContent=s),this.viewUnits.push({type:"html",tag:this.t.stringLiteral(r),props:e,children:[]});return}s&&(e._$content=s),this.viewUnits.push({type:"comp",tag:i.callee,props:e,children:[]});return}if(this.t.isMemberExpression(i.callee)&&this.t.isThisExpression(i.callee.object)&&this.t.isIdentifier(i.callee.property)&&this.subviewNames.includes(i.callee.property.name)){if(s&&(e.content=s),!(this.t.isMemberExpression(i.callee)&&this.t.isThisExpression(i.callee.object)&&this.t.isIdentifier(i.callee.property)))return o.throw4();this.viewUnits.push({type:"subview",tag:i.callee.property.name,props:e,children:[]});return}if(this.t.isExpression(i.callee)){let[r,n]=this.alterTagType(i.callee);s&&(e[r==="html"?"textContent":"content"]=s),this.viewUnits.push({type:r,tag:n,props:e,children:[]})}}isPureMemberExpression(t){let e=!0;return this.traverse(this.valueWrapper(t),{CallExpression:()=>{e=!1}}),e}alterTagType(t){if(this.t.isCallExpression(t)&&this.t.isIdentifier(t.callee)){let e=t.callee.name,i=e===this.htmlTagWrapper?"html":e===this.compWrapper?"comp":void 0;if(i){let s=t.arguments[0];return this.t.isExpression(s)||o.throw2(e),[i,s]}}return["comp",t]}isInvalidExpression(t){return this.t.isForStatement(t)&&!this.t.isForOfStatement(t)?(o.error1(),!0):!1}valueWrapper(t){return this.t.file(this.t.program([this.t.expressionStatement(t)]))}parseView(t){return new h(this.config).parse(t)}uid(){return Math.random().toString(36).slice(2)}};function x(u,t){return new h(t).parse(u)}export{x as parseView}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@dlightjs/view-parser", | ||
"version": "1.0.0-alpha.4", | ||
"version": "1.0.0-beta.5", | ||
"description": "DLight DSL syntax parser", | ||
@@ -5,0 +5,0 @@ "author": { |
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
74741