UNPKG

jse-eval

Version:

JavaScript expression parsing and evaluation.

3 lines (2 loc) 10.8 kB
import e from"jsep";export{default as jsep,default as parse}from"jsep";function n(){return n=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},n.apply(this,arguments)}var t=/*#__PURE__*/function(){function t(e,n){this.context=void 0,this.isAsync=void 0,this.context=e,this.isAsync=n}t.addUnaryOp=function(n,r){e.addUnaryOp(n),t.unops[n]=r},t.addBinaryOp=function(n,r,i,o){var a,s,u;"function"==typeof r?u=r:(a=r,"function"==typeof i?u=i:(s=i,u=o)),e.addBinaryOp(n,a||1,s),t.binops[n]=u},t.addEvaluator=function(e,n){t.evaluators[e]=n},t.registerPlugin=function(){[].slice.call(arguments).forEach(function(e){e.init&&t.parse.plugins.register(e),e.initEval&&e.initEval.call(t,t)})},t.eval=function(e,n){return new t(n).eval(e)},t.evalAsync=function(e,n){try{return Promise.resolve(new t(n,!0).eval(e))}catch(e){return Promise.reject(e)}},t.compile=function(e){return t.eval.bind(null,t.jsep(e))},t.compileAsync=function(e){return t.evalAsync.bind(null,t.jsep(e))},t.evalExpr=function(e,n){return t.compile(e)(n)},t.evalExprAsync=function(e,n){return t.compileAsync(e)(n)};var r=t.prototype;return r.eval=function(e,n){void 0===n&&(n=function(e){return e});var r=t.evaluators[e.type]||t.evaluators.default;if(!r)throw new Error("unknown node type: "+JSON.stringify(e,null,2));return this.evalSyncAsync(r.bind(this)(e,this.context),function(t){return e._value=t,n(t)})},r.evalSyncAsync=function(e,n){return this.isAsync?Promise.resolve(e).then(n):n(e)},r.evalArrayExpression=function(e){return this.evalArray(e.elements)},r.evalArray=function(e){var n=this,t=e.map(function(e){return n.eval(e)}),r=function(n){return n.reduce(function(n,t,r){return"SpreadElement"===e[r].type?[].concat(n,t):(n.push(t),n)},[])};return this.isAsync?Promise.all(t).then(r):r(t)},r.evalBinaryExpression=function(e){var n=this;if("||"===e.operator)return this.eval(e.left,function(t){return t||n.eval(e.right)});if("&&"===e.operator)return this.eval(e.left,function(t){return t&&n.eval(e.right)});var r=[this.eval(e.left),this.eval(e.right)],i=function(n){return t.binops[e.operator](n[0],n[1])};return this.isAsync?Promise.all(r).then(i):i(r)},r.evalCompoundExpression=function(e){var n=this;return this.isAsync?e.body.reduce(function(e,t){return e.then(function(){return n.eval(t)})},Promise.resolve()):e.body.map(function(e){return n.eval(e)})[e.body.length-1]},r.evalCallExpression=function(e){var n=this;return this.evalSyncAsync(this.evalCall(e.callee),function(t){var r=t[0],i=t[1];return n.evalSyncAsync(n.evalArray(e.arguments),function(t){return r.apply(i===e.callee?n.context:i,t)})})},r.evalCall=function(e){return"MemberExpression"===e.type?this.evalSyncAsync(this.evaluateMember(e),function(n){return t.validateFnAndCall(n[1],n[0],e)}):this.eval(e,function(n){return t.validateFnAndCall(n,e)})},r.evalConditionalExpression=function(e){var n=this;return this.eval(e.test,function(t){return n.eval(t?e.consequent:e.alternate)})},r.evalIdentifier=function(e){return this.context[e.name]},t.evalLiteral=function(e){return e.value},r.evalMemberExpression=function(e){return this.evalSyncAsync(this.evaluateMember(e),function(e){return e[1]})},r.evaluateMember=function(e){var n=this;return this.eval(e.object,function(t){return n.evalSyncAsync(e.computed?n.eval(e.property):e.property.name,function(n){if(/^__proto__|prototype|constructor$/.test(n))throw Error('Access to member "'+n+'" disallowed.');return[t,(e.optional?t||{}:t)[n],n]})})},r.evalThisExpression=function(){return this.context},r.evalUnaryExpression=function(e){return this.eval(e.argument,function(n){return t.unops[e.operator](n)})},r.evalArrowFunctionExpression=function(e){var n=this;return this.isAsync!==e.async?t[e.async?"evalAsync":"eval"](e,this.context):function(){var r=n.evalArrowContext(e,[].slice.call(arguments));return t[e.async?"evalAsync":"eval"](e.body,r)}},r.evalArrowContext=function(e,t){var r=this,i=n({},this.context);return(e.params||[]).forEach(function(e,o){if("AssignmentExpression"===e.type&&(void 0===t[o]&&(t[o]=r.eval(e.right)),e=e.left),"Identifier"===e.type)i[e.name]=t[o];else if("ArrayExpression"===e.type)e.elements.forEach(function(e,n){var a=t[o][n];if("AssignmentExpression"===e.type&&(void 0===a&&(a=r.eval(e.right)),e=e.left),"Identifier"!==e.type)throw new Error("Unexpected arrow function argument");i[e.name]=a});else if("ObjectExpression"===e.type){var a=[];e.properties.forEach(function(e){var s,u=e;if("AssignmentExpression"===u.type&&(u=u.left),"Property"===u.type)s="Identifier"===u.key.type?u.key.name:r.eval(u.key).toString();else if("Identifier"===u.type)s=u.name;else{if("SpreadElement"!==u.type||"Identifier"!==u.argument.type)throw new Error("Unexpected arrow function argument");s=u.argument.name}var c=t[o][s];"SpreadElement"===u.type?(c=n({},t[o]),a.forEach(function(e){delete c[e]})):void 0===c&&"AssignmentExpression"===e.type&&(c=r.eval(e.right)),i[s]=c,a.push(s)})}else{if("SpreadElement"!==e.type||"Identifier"!==e.argument.type)throw new Error("Unexpected arrow function argument");i[e.argument.name]=t.slice(o)}}),i},r.evalAssignmentExpression=function(e){var n=this;return this.evalSyncAsync(this.getContextAndKey(e.left),function(r){var i=r[0],o=r[1];return n.eval(e.right,function(n){return t.assignOps[e.operator](i,o,n)})})},r.evalUpdateExpression=function(e){return this.evalSyncAsync(this.getContextAndKey(e.argument),function(n){return t.evalUpdateOperation(e,n[0],n[1])})},r.evalAwaitExpression=function(e){return t.evalAsync(e.argument,this.context)},t.evalUpdateOperation=function(e,n,t){return e.prefix?"++"===e.operator?++n[t]:--n[t]:"++"===e.operator?n[t]++:n[t]--},r.getContextAndKey=function(e){var n=this;if("MemberExpression"===e.type)return this.evalSyncAsync(this.evaluateMember(e),function(e){return[e[0],e[2]]});if("Identifier"===e.type)return[this.context,e.name];if("ConditionalExpression"===e.type)return this.eval(e.test,function(t){return n.getContextAndKey(t?e.consequent:e.alternate)});throw new Error("Invalid Member Key")},r.evalNewExpression=function(e){var n=this;return this.evalSyncAsync(this.evalCall(e.callee),function(r){var i=r[0];return n.evalSyncAsync(n.evalArray(e.arguments),function(n){return t.construct(i,n,e)})})},r.evalObjectExpression=function(e){var n=this,r={},i=e.properties.map(function(e){if("SpreadElement"===e.type)Object.assign(r,t.eval(e.argument,n.context));else if("Property"===e.type)return n.evalSyncAsync("Identifier"===e.key.type?e.key.name:n.eval(e.key),function(t){return n.eval(e.shorthand?e.key:e.value,function(e){r[t]=e})})});return this.isAsync?Promise.all(i).then(function(){return r}):r},r.evalSpreadElement=function(e){return this.eval(e.argument)},r.evalTaggedTemplateExpression=function(e){var n=[this.evalCall(e.tag),this.evalSyncAsync(this.evalArray(e.quasi.expressions),function(n){return[e.quasi.quasis.map(function(e){return e.value.cooked})].concat(n)})],t=function(e){var n=e[0];return n[0].apply(n[1],e[1])};return this.isAsync?Promise.all(n).then(t):t(n)},r.evalTemplateLiteral=function(e){return this.evalSyncAsync(this.evalArray(e.expressions),function(n){return e.quasis.reduce(function(e,t,r){return e+=t.value.cooked,t.tail||(e+=n[r]),e},"")})},t.construct=function(e,n,r){try{return new(Function.prototype.bind.apply(e,[null].concat(n)))}catch(e){throw new Error(t.nodeFunctionName(r.callee)+" is not a constructor")}},t.validateFnAndCall=function(e,n,r){if("function"!=typeof e){if(!e&&r&&r.optional)return[function(){},n];var i=t.nodeFunctionName(r||n);throw new Error("'"+i+"' is not a function")}return[e,n]},t.nodeFunctionName=function(e){return e&&(e.name||e.property&&e.property.name)},t}();t.jsep=e,t.parse=e,t.evaluate=t.eval,t.evaluators={ArrayExpression:t.prototype.evalArrayExpression,LogicalExpression:t.prototype.evalBinaryExpression,BinaryExpression:t.prototype.evalBinaryExpression,CallExpression:t.prototype.evalCallExpression,Compound:t.prototype.evalCompoundExpression,ConditionalExpression:t.prototype.evalConditionalExpression,Identifier:t.prototype.evalIdentifier,Literal:t.evalLiteral,OptionalMemberExpression:t.prototype.evalMemberExpression,MemberExpression:t.prototype.evalMemberExpression,ThisExpression:t.prototype.evalThisExpression,UnaryExpression:t.prototype.evalUnaryExpression,ArrowFunctionExpression:t.prototype.evalArrowFunctionExpression,AssignmentExpression:t.prototype.evalAssignmentExpression,UpdateExpression:t.prototype.evalUpdateExpression,AwaitExpression:t.prototype.evalAwaitExpression,NewExpression:t.prototype.evalNewExpression,ObjectExpression:t.prototype.evalObjectExpression,SpreadElement:t.prototype.evalSpreadElement,TaggedTemplateExpression:t.prototype.evalTaggedTemplateExpression,TemplateLiteral:t.prototype.evalTemplateLiteral},t.DEFAULT_PRECEDENCE={"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10},t.binops={"||":function(e,n){return e||n},"&&":function(e,n){return e&&n},"|":function(e,n){return e|n},"^":function(e,n){return e^n},"&":function(e,n){return e&n},"==":function(e,n){return e==n},"!=":function(e,n){return e!=n},"===":function(e,n){return e===n},"!==":function(e,n){return e!==n},"<":function(e,n){return e<n},">":function(e,n){return e>n},"<=":function(e,n){return e<=n},">=":function(e,n){return e>=n},"<<":function(e,n){return e<<n},">>":function(e,n){return e>>n},">>>":function(e,n){return e>>>n},"+":function(e,n){return e+n},"-":function(e,n){return e-n},"*":function(e,n){return e*n},"/":function(e,n){return e/n},"%":function(e,n){return e%n}},t.unops={"-":function(e){return-e},"+":function(e){return+e},"~":function(e){return~e},"!":function(e){return!e}},t.assignOps={"=":function(e,n,t){return e[n]=t},"*=":function(e,n,t){return e[n]*=t},"**=":function(e,n,t){var r;return e[r=n]=Math.pow(e[r],t)},"/=":function(e,n,t){return e[n]/=t},"%=":function(e,n,t){return e[n]%=t},"+=":function(e,n,t){return e[n]+=t},"-=":function(e,n,t){return e[n]-=t},"<<=":function(e,n,t){return e[n]<<=t},">>=":function(e,n,t){return e[n]>>=t},">>>=":function(e,n,t){return e[n]>>>=t},"&=":function(e,n,t){return e[n]&=t},"^=":function(e,n,t){return e[n]^=t},"|=":function(e,n,t){return e[n]|=t}};var r=t.DEFAULT_PRECEDENCE,i=t.evaluators,o=t.binops,a=t.unops,s=t.assignOps,u=t.addUnaryOp,c=t.addBinaryOp,l=t.addEvaluator,p=t.registerPlugin,f=t.eval,v=t.evalAsync,y=t.compile,h=t.compileAsync,d=t.evalExpr,E=t.evalExprAsync;export{r as DEFAULT_PRECEDENCE,c as addBinaryOp,l as addEvaluator,u as addUnaryOp,s as assignOps,o as binops,y as compile,h as compileAsync,t as default,v as evalAsync,d as evalExpr,E as evalExprAsync,f as evaluate,i as evaluators,p as registerPlugin,a as unops}; //# sourceMappingURL=jse-eval.module.js.map