@dlightjs/view-parser
Advanced tools
Comparing version 1.0.0-next.2 to 1.0.0
@@ -86,2 +86,2 @@ import Babel, { types } from '@babel/core'; | ||
export { CompUnit, EnvUnit, ExpUnit, ForUnit, HTMLUnit, IfBranch, IfUnit, SnippetUnit, SwitchBranch, SwitchUnit, TextUnit, TryUnit, ViewParserConfig, ViewProp, ViewUnit, parseView }; | ||
export { type CompUnit, type EnvUnit, type ExpUnit, type ForUnit, type HTMLUnit, type IfBranch, type IfUnit, type SnippetUnit, type SwitchBranch, type SwitchUnit, type TextUnit, type TryUnit, type ViewParserConfig, type ViewProp, type ViewUnit, parseView }; |
@@ -1,2 +0,2 @@ | ||
import{createErrorHandler as y}from"@dlightjs/error-handler";var p=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 Snippet calling, only accepts static snippet calling like `this.Snippet()`"},{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;snippetNames;htmlTags;viewUnits=[];constructor(t){this.config=t,this.t=t.babelApi.types,this.traverse=t.babelApi.traverse,this.snippetNames=t.snippetNames,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?(p.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.push(...s)):i==="comp"||i==="snippet"?e.children.push(...s):i==="env"?s.length>0?e.children.push(...s):(this.viewUnits.pop(),p.error2()):p.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})}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)||p.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 o=n.body;if(o.length===0)return p.error5();let c=o[0];if(this.t.isLabeledStatement(c)&&this.t.isIdentifier(c.label)){if(c.label.name!=="key"||!this.t.isExpressionStatement(c.body))p.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=o.slice(1)}else a=o}else return;let l=a.filter(o=>this.t.isDirective(o)),f=a.filter(o=>!this.t.isDirective(o)),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:{}})}isPropView(t){if(!(this.t.isArrowFunctionExpression(t)&&(this.t.isIdentifier(t.params[0],{name:"View"})||this.t.isIdentifier(t.params[0],{name:"_View"}))))return null;let e=t.body;return this.t.isBlockStatement(e)?e:this.t.blockStatement([this.t.expressionStatement(e)])}parseProp(t){if(t&&!this.t.isExpression(t)&&p.throw3(),t=t,!t)return{value:this.t.booleanLiteral(!0),viewPropMap:{}};let e={};return this.traverse(this.valueWrapper(t),{ArrowFunctionExpression:i=>{let s=this.isPropView(i.node);if(!s)return;let r=this.uid();e[r]=this.parseView(s);let n=this.t.stringLiteral(r);i.node===t&&(t=n),i.replaceWith(n),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)){p.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){p.warn1();return}this.viewUnits.push({type:"env",props:e,children:[]});return}if(this.htmlTags.includes(r)){let n=[];if(s){let a=!1;if(s.viewPropMap&&Object.keys(s.viewPropMap).length===1){let l=Object.keys(s.viewPropMap)[0];this.t.isStringLiteral(s.value,{value:l})&&(a=!0,n=s.viewPropMap[l])}a||(e.textContent=s)}this.viewUnits.push({type:"html",tag:this.t.stringLiteral(r),props:e,children:n});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.snippetNames.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 p.throw4();this.viewUnits.push({type:"snippet",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)||p.throw2(e),[i,s]}}return["comp",t]}isInvalidExpression(t){return this.t.isForStatement(t)&&!this.t.isForOfStatement(t)?(p.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 b(u,t){return new h(t).parse(u)}export{b as parseView}; | ||
import{createErrorHandler as y}from"@dlightjs/error-handler";var p=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 Snippet calling, only accepts static snippet calling like `this.Snippet()`"},{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 f=class u{compWrapper="comp";htmlTagWrapper="tag";environmentTagName="env";expressionTagName="_";config;t;traverse;snippetNames;htmlTags;viewUnits=[];constructor(t){this.config=t,this.t=t.babelApi.types,this.traverse=t.babelApi.traverse,this.snippetNames=t.snippetNames,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?(p.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.push(...s)):i==="comp"||i==="snippet"?e.children.push(...s):i==="env"?s.length>0?e.children.push(...s):(this.viewUnits.pop(),p.error2()):p.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),m={case:s.test??this.t.booleanLiteral(!0),children:l,break:a};e.push(m)}),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)||p.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 o=n.body;if(o.length===0)return p.error5();let h=o[0];if(this.t.isLabeledStatement(h)&&this.t.isIdentifier(h.label)){if(h.label.name!=="key"||!this.t.isExpressionStatement(h.body))p.error4();else{let c=h.body.expression;this.t.isExpression(c)&&!(this.t.isNullLiteral(c)||this.t.isIdentifier(c)&&c.name==="undefined")&&(r=c)}a=o.slice(1)}else a=o}else return;let l=a.filter(o=>this.t.isDirective(o)),m=a.filter(o=>!this.t.isDirective(o)),v=this.t.blockStatement(m,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:{}})}isPropView(t){if(!(this.t.isArrowFunctionExpression(t)&&(this.t.isIdentifier(t.params[0],{name:"View"})||this.t.isIdentifier(t.params[0],{name:"_View"}))))return null;let e=t.body;return this.t.isBlockStatement(e)?e:this.t.blockStatement([this.t.expressionStatement(e)])}parseProp(t){if(t&&!this.t.isExpression(t)&&p.throw3(),t=t,!t)return{value:this.t.booleanLiteral(!0),viewPropMap:{}};let e={};return this.traverse(this.valueWrapper(t),{ArrowFunctionExpression:i=>{let s=this.isPropView(i.node);if(!s)return;let r=this.uid();e[r]=this.parseView(s);let n=this.t.stringLiteral(r);i.node===t&&(t=n),i.replaceWith(n),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)){p.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){p.warn1();return}this.viewUnits.push({type:"env",props:e,children:[]});return}if(this.htmlTags.includes(r)){let n=[];if(s){let a=!1;if(s.viewPropMap&&Object.keys(s.viewPropMap).length===1){let l=Object.keys(s.viewPropMap)[0];this.t.isStringLiteral(s.value,{value:l})&&(a=!0,n=s.viewPropMap[l])}a||(e.textContent=s)}this.viewUnits.push({type:"html",tag:this.t.stringLiteral(r),props:e,children:n});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.snippetNames.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 p.throw4();this.viewUnits.push({type:"snippet",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)||p.throw2(e),[i,s]}}return["comp",t]}isInvalidExpression(t){return this.t.isForStatement(t)&&!this.t.isForOfStatement(t)?(p.error1(),!0):!1}valueWrapper(t){return this.t.file(this.t.program([this.t.expressionStatement(t)]))}parseView(t){return new u(this.config).parse(t)}uid(){return Math.random().toString(36).slice(2)}};function b(u,t){return new f(t).parse(u)}export{b as parseView}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@dlightjs/view-parser", | ||
"version": "1.0.0-next.2", | ||
"version": "1.0.0", | ||
"description": "DLight DSL syntax parser", | ||
@@ -20,2 +20,5 @@ "author": { | ||
"typings": "dist/index.d.ts", | ||
"dependencies": { | ||
"@dlightjs/error-handler": "1.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -25,9 +28,4 @@ "@babel/core": "^7.20.12", | ||
"@vitest/ui": "^0.34.5", | ||
"tsup": "^6.7.0", | ||
"typescript": "^5.3.2", | ||
"vitest": "^0.34.5" | ||
}, | ||
"dependencies": { | ||
"@dlightjs/error-handler": "1.0.0-alpha.0" | ||
}, | ||
"tsup": { | ||
@@ -47,4 +45,5 @@ "entry": [ | ||
"build": "tsup --sourcemap", | ||
"test": "vitest --ui" | ||
"test": "vitest --ui", | ||
"typecheck": "tsc --noEmit" | ||
} | ||
} |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
82728
4
8
2
1
+ Added@dlightjs/error-handler@1.0.0(transitive)
- Removed@dlightjs/error-handler@1.0.0-alpha.0(transitive)