New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@dlightjs/view-parser

Package Overview
Dependencies
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dlightjs/view-parser - npm Package Compare versions

Comparing version 1.0.0-alpha.0 to 1.0.0-alpha.1

14

dist/index.d.ts

@@ -40,2 +40,12 @@ import Babel, { types } from '@babel/core';

}
interface SwitchBranch {
case: types.Expression | null | undefined;
children: ViewUnit[];
break: boolean;
}
interface SwitchUnit {
type: "switch";
discriminant: types.Expression;
branches: SwitchBranch[];
}
interface EnvUnit {

@@ -57,3 +67,3 @@ type: "env";

}
type ViewUnit = TextUnit | HTMLUnit | CompUnit | IfUnit | ForUnit | EnvUnit | ExpUnit | SubviewUnit;
type ViewUnit = TextUnit | HTMLUnit | CompUnit | IfUnit | ForUnit | EnvUnit | ExpUnit | SubviewUnit | SwitchUnit;
interface ViewParserConfig {

@@ -80,2 +90,2 @@ babelApi: typeof Babel;

export { CompUnit, EnvUnit, ExpUnit, ForUnit, HTMLUnit, IfBranch, IfUnit, SubviewUnit, TextUnit, ViewParserConfig, ViewParserOption, ViewProp, ViewUnit, parseView };
export { CompUnit, EnvUnit, ExpUnit, ForUnit, HTMLUnit, IfBranch, IfUnit, SubviewUnit, SwitchBranch, SwitchUnit, TextUnit, ViewParserConfig, ViewParserOption, ViewProp, ViewUnit, parseView };

2

dist/index.js

@@ -1,2 +0,2 @@

import{createErrorHandler as g}from"@dlightjs/error-handler";var a=g("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;options;t;traverse;subviewNames;htmlTags;viewUnits=[];constructor(t,e){this.config=t,this.options=e,this.t=t.babelApi.types,this.traverse=t.babelApi.traverse,this.subviewNames=t.subviewNames,this.htmlTags=t.htmlTags,e?.environmentTagName&&(this.environmentTagName=e.environmentTagName),e?.expressionTagName&&(this.expressionTagName=e.expressionTagName),e?.htmlTagWrapper&&(this.htmlTagWrapper=e.htmlTagWrapper),e?.compWrapper&&(this.compWrapper=e.compWrapper)}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?(a.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.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.content,e.children=s):i==="comp"||i==="subview"?e.children=s:i==="env"?s.length>0?e.children=s:(this.viewUnits.pop(),a.error2()):a.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 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)})}parseFor(t){let e=t.left;this.t.isVariableDeclaration(e)||a.throw1();let i=e.declarations[0].id,s=t.right,r,n=t.body,p;if(this.t.isExpressionStatement(n))p=[n];else if(this.t.isBlockStatement(n)){let o=n.body;if(o.length===0)return a.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))a.error4();else{let c=h.body.expression;this.t.isExpression(c)&&!(this.t.isNullLiteral(c)||this.t.isIdentifier(c)&&c.name==="undefined")&&(r=c)}p=o.slice(1)}else p=o}else return;let m=p.filter(o=>this.t.isDirective(o)),v=p.filter(o=>!this.t.isDirective(o)),u=this.t.blockStatement(v,m);this.viewUnits.push({type:"for",item:i,array:s,key:r,children:this.parseView(u)})}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)&&a.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)]),p=this.uid();e[p]=this.parseView(n);let m=this.t.stringLiteral(p);s===t&&(t=m),i.replaceWith(m),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)){a.throw1();continue}let n=r.name,p=this.parseProp(i.arguments[0]);e[n]=p,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){a.warn1();return}this.viewUnits.push({type:"env",props:e,children:[]});return}if(this.htmlTags.includes(r)){this.viewUnits.push({type:"html",tag:this.t.stringLiteral(r),content:s,props:e});return}this.viewUnits.push({type:"comp",tag:i.callee,content:s,props:e});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 a.throw4();this.viewUnits.push({type:"subview",tag:i.callee.property.name,props:e});return}if(this.t.isExpression(i.callee)){let[r,n]=this.alterTagType(i.callee);this.viewUnits.push({type:r,tag:n,content:s,props:e})}}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)||a.throw2(e),[i,s]}}return["comp",t]}isInvalidExpression(t){return this.t.isForStatement(t)&&!this.t.isForOfStatement(t)?(a.error1(),!0):!1}valueWrapper(t){return this.t.file(this.t.program([this.t.expressionStatement(t)]))}parseView(t){return new l(this.config,this.options).parse(t)}uid(){return Math.random().toString(36).slice(2)}};function T(f,t,e){return new l(t,e).parse(f)}export{T 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;options;t;traverse;subviewNames;htmlTags;viewUnits=[];constructor(t,e){this.config=t,this.options=e,this.t=t.babelApi.types,this.traverse=t.babelApi.traverse,this.subviewNames=t.subviewNames,this.htmlTags=t.htmlTags,e?.environmentTagName&&(this.environmentTagName=e.environmentTagName),e?.expressionTagName&&(this.expressionTagName=e.expressionTagName),e?.htmlTagWrapper&&(this.htmlTagWrapper=e.htmlTagWrapper),e?.compWrapper&&(this.compWrapper=e.compWrapper)}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.content,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)})}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,children:h,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,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)),u=this.t.blockStatement(f,h);this.viewUnits.push({type:"for",item:i,array:s,key:r,children:this.parseView(u)})}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 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)){this.viewUnits.push({type:"html",tag:this.t.stringLiteral(r),content:s,props:e});return}this.viewUnits.push({type:"comp",tag:i.callee,content:s,props:e});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});return}if(this.t.isExpression(i.callee)){let[r,n]=this.alterTagType(i.callee);this.viewUnits.push({type:r,tag:n,content:s,props:e})}}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,this.options).parse(t)}uid(){return Math.random().toString(36).slice(2)}};function x(v,t,e){return new l(t,e).parse(v)}export{x as parseView};
//# sourceMappingURL=index.js.map
{
"name": "@dlightjs/view-parser",
"version": "1.0.0-alpha.0",
"version": "1.0.0-alpha.1",
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc