Skip to content

Commit

Permalink
Update jsepEval
Browse files Browse the repository at this point in the history
  • Loading branch information
sjchmiela committed Nov 14, 2024
1 parent 56e163f commit ac36fa8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/steps/src/utils/__tests__/jsepEval-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const TEST_CASES = [
{ success: () => true, env: { NODE_ENV: 'production' } },
],
['0 == 1 ? "a" : "b"', 'b'],
['fromJSON("{\\"a\\": 1}").a', 1, { fromJSON: JSON.parse }],
['fromJSON("{\\"a\\": 1}")[fromJSON(\'"a"\')]', 1, { fromJSON: JSON.parse }],
['fromJSON(null).a', undefined, { fromJSON: JSON.parse }],
] as const;

describe(jsepEval, () => {
Expand Down
28 changes: 23 additions & 5 deletions packages/steps/src/utils/jsepEval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ function getParameterPath(node: jsep.MemberExpression, context: Record<string, u
// if computed -> evaluate anew
const propertyPath = evaluateExpressionNode(property, context);
return objectPath + '[' + propertyPath + ']';
} else if (isValid(property, ['Identifier'])) {
} else if (property.type === 'Identifier') {
return (objectPath ? objectPath + '.' : '') + property.name;
} else if (property.type === 'CallExpression') {
const propertyPath = evaluateExpressionNode(property, context);
return (objectPath ? objectPath + '.' : '') + propertyPath;
} else if (property.type === 'Literal') {
return (objectPath ? objectPath + '.' : '') + `${property.value}`;
} else {
assert(isValid(property, ['MemberExpression']), 'Invalid object type');
const propertyPath = getParameterPath(property, context);
Expand Down Expand Up @@ -140,7 +145,7 @@ function evaluateExpressionNode(node: jsep.Expression, context: Record<string, u
throw new Error(
`Invalid function callee type: ${
callNode.callee.type
}. Expected one of ${allowedCalleeTypes.join(', ')}.`
}. Expected one of [${allowedCalleeTypes.join(', ')}].`
);
}
const callee = evaluateExpressionNode(callNode.callee, context);
Expand All @@ -153,14 +158,27 @@ function evaluateExpressionNode(node: jsep.Expression, context: Record<string, u
const identifier = (node as jsep.Identifier).name;
if (!(identifier in context)) {
throw new Error(
`Invalid identifier "${identifier}". Expected one of ${Object.keys(context).join(', ')}`
`Invalid identifier "${identifier}". Expected one of [${Object.keys(context).join(
', '
)}].`
);
}
return get(context, identifier);
}
case 'MemberExpression': {
const path = getParameterPath(node as jsep.MemberExpression, context);
return get(context, path);
const memberNode = node as jsep.MemberExpression;
return get(
evaluateExpressionNode(memberNode.object, context),
getParameterPath(
{
type: 'MemberExpression',
object: { type: 'ThisExpression' },
property: memberNode.property,
computed: false,
} as jsep.MemberExpression,
context
)
);
}
case 'ArrayExpression': {
const elements = (node as jsep.ArrayExpression).elements.map((el) =>
Expand Down

0 comments on commit ac36fa8

Please sign in to comment.