Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/jsep-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// evaluates javascript expression statements parsed with jsep
//

const _ = require('lodash');
const get = require('lodash/get');
const map = require('lodash/map');
const includes = require('lodash/includes');
const jsep = require('jsep');
const assert = require('assert');

Expand Down Expand Up @@ -61,8 +63,8 @@ const undefOperator = () => undefined;
const getParameterPath = (node, context) => {
assert(node, 'Node missing');
const type = node.type;
assert(_.includes(types, type), 'invalid node type');
assert(_.includes([types.MEMBER, types.IDENTIFIER], type), 'Invalid parameter path node type: ', type);
assert(includes(types, type), 'invalid node type');
assert(includes([types.MEMBER, types.IDENTIFIER], type), 'Invalid parameter path node type: ', type);
// the easy case: 'IDENTIFIER's
if (type === types.IDENTIFIER) {
return node.name;
Expand All @@ -74,7 +76,7 @@ const getParameterPath = (node, context) => {
const object = node.object;
const property = node.property;
// object is either 'IDENTIFIER', 'MEMBER', or 'THIS'
assert(_.includes([types.MEMBER, types.IDENTIFIER, types.THIS], object.type), 'Invalid object type');
assert(includes([types.MEMBER, types.IDENTIFIER, types.THIS], object.type), 'Invalid object type');
assert(property, 'Member expression property is missing');

let objectPath = '';
Expand All @@ -89,15 +91,15 @@ const getParameterPath = (node, context) => {
const propertyPath = evaluateExpressionNode(property, context);
return objectPath + '[' + propertyPath + ']';
} else {
assert(_.includes([types.MEMBER, types.IDENTIFIER], property.type), 'Invalid object type');
assert(includes([types.MEMBER, types.IDENTIFIER], property.type), 'Invalid object type');
const propertyPath = property.name || getParameterPath(property, context);
return (objectPath ? objectPath + '.': '') + propertyPath;
}
};

const evaluateExpressionNode = (node, context) => {
assert(node, 'Node missing');
assert(_.includes(types, node.type), 'invalid node type');
assert(includes(types, node.type), 'invalid node type');
switch (node.type) {
case types.LITERAL: {
return node.value;
Expand All @@ -106,23 +108,23 @@ const evaluateExpressionNode = (node, context) => {
return context;
}
case types.COMPOUND: {
const expressions = _.map(node.body, el => evaluateExpressionNode(el, context));
const expressions = map(node.body, el => evaluateExpressionNode(el, context));
return expressions.pop();
}
case types.ARRAY: {
const elements = _.map(node.elements, el => evaluateExpressionNode(el, context));
const elements = map(node.elements, el => evaluateExpressionNode(el, context));
return elements;
}
case types.UNARY: {
const operator = operators.unary[node.operator] || undefOperator;
assert(_.includes(operators.unary, operator), 'Invalid unary operator');
assert(includes(operators.unary, operator), 'Invalid unary operator');
const argument = evaluateExpressionNode(node.argument, context);
return operator(argument);
}
case types.LOGICAL: // !!! fall-through to BINARY !!! //
case types.BINARY: {
const operator = operators.binary[node.operator] || undefOperator;
assert(_.includes(operators.binary, operator), 'Invalid binary operator');
assert(includes(operators.binary, operator), 'Invalid binary operator');
const left = evaluateExpressionNode(node.left, context);
const right = evaluateExpressionNode(node.right, context);
return operator(left, right);
Expand All @@ -134,15 +136,15 @@ const evaluateExpressionNode = (node, context) => {
return test ? consequent : alternate;
}
case types.CALL : {
assert(_.includes([types.MEMBER, types.IDENTIFIER, types.THIS], node.callee.type), 'Invalid function callee type');
assert(includes([types.MEMBER, types.IDENTIFIER, types.THIS], node.callee.type), 'Invalid function callee type');
const callee = evaluateExpressionNode(node.callee, context);
const args = _.map(node.arguments, arg => evaluateExpressionNode(arg, context));
const args = map(node.arguments, arg => evaluateExpressionNode(arg, context));
return callee.apply(null, args);
}
case types.IDENTIFIER: // !!! fall-through to MEMBER !!! //
case types.MEMBER: {
const path = getParameterPath(node, context);
return _.get(context, path);
return get(context, path);
}
default:
return undefined;
Expand Down