jse-eval
Version:
JavaScript expression parsing and evaluation.
3 lines (2 loc) • 11.1 kB
JavaScript
function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=/*#__PURE__*/e(require("jsep"));function t(){return t=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},t.apply(this,arguments)}var r=/*#__PURE__*/function(){function e(e,n){this.context=void 0,this.isAsync=void 0,this.context=e,this.isAsync=n}e.addUnaryOp=function(t,r){n.default.addUnaryOp(t),e.unops[t]=r},e.addBinaryOp=function(t,r,o,i){var a,s,u;"function"==typeof r?u=r:(a=r,"function"==typeof o?u=o:(s=o,u=i)),n.default.addBinaryOp(t,a||1,s),e.binops[t]=u},e.addEvaluator=function(n,t){e.evaluators[n]=t},e.registerPlugin=function(){[].slice.call(arguments).forEach(function(n){n.init&&e.parse.plugins.register(n),n.initEval&&n.initEval.call(e,e)})},e.eval=function(n,t){return new e(t).eval(n)},e.evalAsync=function(n,t){try{return Promise.resolve(new e(t,!0).eval(n))}catch(e){return Promise.reject(e)}},e.compile=function(n){return e.eval.bind(null,e.jsep(n))},e.compileAsync=function(n){return e.evalAsync.bind(null,e.jsep(n))},e.evalExpr=function(n,t){return e.compile(n)(t)},e.evalExprAsync=function(n,t){return e.compileAsync(n)(t)};var r=e.prototype;return r.eval=function(n,t){void 0===t&&(t=function(e){return e});var r=e.evaluators[n.type]||e.evaluators.default;if(!r)throw new Error("unknown node type: "+JSON.stringify(n,null,2));return this.evalSyncAsync(r.bind(this)(n,this.context),function(e){return n._value=e,t(e)})},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(n){var t=this;if("||"===n.operator)return this.eval(n.left,function(e){return e||t.eval(n.right)});if("&&"===n.operator)return this.eval(n.left,function(e){return e&&t.eval(n.right)});var r=[this.eval(n.left),this.eval(n.right)],o=function(t){return e.binops[n.operator](t[0],t[1])};return this.isAsync?Promise.all(r).then(o):o(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],o=t[1];return n.evalSyncAsync(n.evalArray(e.arguments),function(t){return r.apply(o===e.callee?n.context:o,t)})})},r.evalCall=function(n){return"MemberExpression"===n.type?this.evalSyncAsync(this.evaluateMember(n),function(t){return e.validateFnAndCall(t[1],t[0],n)}):this.eval(n,function(t){return e.validateFnAndCall(t,n)})},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]},e.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(n){return this.eval(n.argument,function(t){return e.unops[n.operator](t)})},r.evalArrowFunctionExpression=function(n){var t=this;return this.isAsync!==n.async?e[n.async?"evalAsync":"eval"](n,this.context):function(){var r=t.evalArrowContext(n,[].slice.call(arguments));return e[n.async?"evalAsync":"eval"](n.body,r)}},r.evalArrowContext=function(e,n){var r=this,o=t({},this.context);return(e.params||[]).forEach(function(e,i){if("AssignmentExpression"===e.type&&(void 0===n[i]&&(n[i]=r.eval(e.right)),e=e.left),"Identifier"===e.type)o[e.name]=n[i];else if("ArrayExpression"===e.type)e.elements.forEach(function(e,t){var a=n[i][t];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");o[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 l=n[i][s];"SpreadElement"===u.type?(l=t({},n[i]),a.forEach(function(e){delete l[e]})):void 0===l&&"AssignmentExpression"===e.type&&(l=r.eval(e.right)),o[s]=l,a.push(s)})}else{if("SpreadElement"!==e.type||"Identifier"!==e.argument.type)throw new Error("Unexpected arrow function argument");o[e.argument.name]=n.slice(i)}}),o},r.evalAssignmentExpression=function(n){var t=this;return this.evalSyncAsync(this.getContextAndKey(n.left),function(r){var o=r[0],i=r[1];return t.eval(n.right,function(t){return e.assignOps[n.operator](o,i,t)})})},r.evalUpdateExpression=function(n){return this.evalSyncAsync(this.getContextAndKey(n.argument),function(t){return e.evalUpdateOperation(n,t[0],t[1])})},r.evalAwaitExpression=function(n){return e.evalAsync(n.argument,this.context)},e.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(n){var t=this;return this.evalSyncAsync(this.evalCall(n.callee),function(r){var o=r[0];return t.evalSyncAsync(t.evalArray(n.arguments),function(t){return e.construct(o,t,n)})})},r.evalObjectExpression=function(n){var t=this,r={},o=n.properties.map(function(n){if("SpreadElement"===n.type)Object.assign(r,e.eval(n.argument,t.context));else if("Property"===n.type)return t.evalSyncAsync("Identifier"===n.key.type?n.key.name:t.eval(n.key),function(e){return t.eval(n.shorthand?n.key:n.value,function(n){r[e]=n})})});return this.isAsync?Promise.all(o).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},"")})},e.construct=function(n,t,r){try{return new(Function.prototype.bind.apply(n,[null].concat(t)))}catch(n){throw new Error(e.nodeFunctionName(r.callee)+" is not a constructor")}},e.validateFnAndCall=function(n,t,r){if("function"!=typeof n){if(!n&&r&&r.optional)return[function(){},t];var o=e.nodeFunctionName(r||t);throw new Error("'"+o+"' is not a function")}return[n,t]},e.nodeFunctionName=function(e){return e&&(e.name||e.property&&e.property.name)},e}();r.jsep=n.default,r.parse=n.default,r.evaluate=r.eval,r.evaluators={ArrayExpression:r.prototype.evalArrayExpression,LogicalExpression:r.prototype.evalBinaryExpression,BinaryExpression:r.prototype.evalBinaryExpression,CallExpression:r.prototype.evalCallExpression,Compound:r.prototype.evalCompoundExpression,ConditionalExpression:r.prototype.evalConditionalExpression,Identifier:r.prototype.evalIdentifier,Literal:r.evalLiteral,OptionalMemberExpression:r.prototype.evalMemberExpression,MemberExpression:r.prototype.evalMemberExpression,ThisExpression:r.prototype.evalThisExpression,UnaryExpression:r.prototype.evalUnaryExpression,ArrowFunctionExpression:r.prototype.evalArrowFunctionExpression,AssignmentExpression:r.prototype.evalAssignmentExpression,UpdateExpression:r.prototype.evalUpdateExpression,AwaitExpression:r.prototype.evalAwaitExpression,NewExpression:r.prototype.evalNewExpression,ObjectExpression:r.prototype.evalObjectExpression,SpreadElement:r.prototype.evalSpreadElement,TaggedTemplateExpression:r.prototype.evalTaggedTemplateExpression,TemplateLiteral:r.prototype.evalTemplateLiteral},r.DEFAULT_PRECEDENCE={"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10},r.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}},r.unops={"-":function(e){return-e},"+":function(e){return+e},"~":function(e){return~e},"!":function(e){return!e}},r.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 o=r.DEFAULT_PRECEDENCE,i=r.evaluators,a=r.binops,s=r.unops,u=r.assignOps,l=r.addUnaryOp,c=r.addBinaryOp,p=r.addEvaluator,f=r.registerPlugin,v=r.eval,y=r.evalAsync,d=r.compile,x=r.compileAsync,E=r.evalExpr,h=r.evalExprAsync;Object.defineProperty(exports,"jsep",{enumerable:!0,get:function(){return n.default}}),Object.defineProperty(exports,"parse",{enumerable:!0,get:function(){return n.default}}),exports.DEFAULT_PRECEDENCE=o,exports.addBinaryOp=c,exports.addEvaluator=p,exports.addUnaryOp=l,exports.assignOps=u,exports.binops=a,exports.compile=d,exports.compileAsync=x,exports.default=r,exports.evalAsync=y,exports.evalExpr=E,exports.evalExprAsync=h,exports.evaluate=v,exports.evaluators=i,exports.registerPlugin=f,exports.unops=s;
//# sourceMappingURL=jse-eval.cjs.map