@dlightjs/view-parser
Advanced tools
Comparing version 1.0.0-beta.5 to 1.0.0-beta.6
@@ -43,2 +43,8 @@ import Babel, { types } from '@babel/core'; | ||
} | ||
interface TryUnit { | ||
type: "try"; | ||
children: ViewUnit[]; | ||
exception: types.Identifier | types.ArrayPattern | types.ObjectPattern | null; | ||
catchChildren: ViewUnit[]; | ||
} | ||
interface SwitchUnit { | ||
@@ -65,3 +71,3 @@ type: "switch"; | ||
} | ||
type ViewUnit = TextUnit | HTMLUnit | CompUnit | IfUnit | ForUnit | EnvUnit | ExpUnit | SubviewUnit | SwitchUnit; | ||
type ViewUnit = TextUnit | HTMLUnit | CompUnit | IfUnit | ForUnit | EnvUnit | ExpUnit | SubviewUnit | SwitchUnit | TryUnit; | ||
interface ViewParserConfig { | ||
@@ -82,2 +88,2 @@ babelApi: typeof Babel; | ||
export { CompUnit, EnvUnit, ExpUnit, ForUnit, HTMLUnit, IfBranch, IfUnit, SubviewUnit, SwitchBranch, SwitchUnit, TextUnit, ViewParserConfig, ViewProp, ViewUnit, parseView }; | ||
export { CompUnit, EnvUnit, ExpUnit, ForUnit, HTMLUnit, IfBranch, IfUnit, SubviewUnit, SwitchBranch, SwitchUnit, TextUnit, TryUnit, ViewParserConfig, ViewProp, ViewUnit, parseView }; |
@@ -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 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}; | ||
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 l=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.isTryStatement(t)){this.parseTry(t);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 h=this.parseView(n),f={case:s.test??this.t.booleanLiteral(!0),children:h,break:a};e.push(f)}),this.viewUnits.push({type:"switch",discriminant:t.discriminant,branches:e})}parseTry(t){let e=this.t.blockStatement(t.block.body),i=t.handler?.body??this.t.blockStatement([]);this.viewUnits.push({type:"try",children:this.parseView(e),exception:t.handler?.param??null,catchChildren:this.parseView(i)})}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 h=a.filter(p=>this.t.isDirective(p)),f=a.filter(p=>!this.t.isDirective(p)),v=this.t.blockStatement(f,h);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 h=this.t.stringLiteral(a);s===t&&(t=h),i.replaceWith(h),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 l(this.config).parse(t)}uid(){return Math.random().toString(36).slice(2)}};function x(u,t){return new l(t).parse(u)}export{x as parseView}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@dlightjs/view-parser", | ||
"version": "1.0.0-beta.5", | ||
"version": "1.0.0-beta.6", | ||
"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
77273
143