Skip to content

Commit a37fd6c

Browse files
authored
Merge pull request #50 from 6utt3rfly/feat/compound
add jsep Compound expression support
2 parents 47c7a83 + 6393994 commit a37fd6c

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ _Forked from [expression-eval](https://github.com/donmccurdy/expression-eval]) v
3131

3232
## Usage
3333
Evaluates an [estree](https://github.com/estree/estree) expression from [jsep](https://github.com/EricSmekens/jsep)
34-
(as well as [@babel/parser][], [esprima][], [acorn][], or any other library that parses and returns a valid `estree` expression).
34+
(as well as [@babel/parser](https://babeljs.io/docs/en/babel-parser), [esprima](https://esprima.org/),
35+
[acorn](https://github.com/acornjs/acorn), or any other library that parses and returns a valid `estree` expression).
3536

3637

3738
### Install
@@ -140,12 +141,15 @@ This project will try to stay current with all JSEP's node types::
140141
- `LogicalExpression`/`BinaryExpression`
141142
- `CallExpression`
142143
- `ConditionalExpression`
144+
- `Compound` *
143145
- `Identifier`
144146
- `Literal`
145147
- `MemberExpression`
146148
- `ThisExpression`
147149
- `UnaryExpression`
148150

151+
**Compound support will evaluate each expression and return the result of the final one
152+
149153
As well as the optional plugin node types:
150154
- `ArrowFunctionExpression`
151155
- `AssignmentExpression`/`UpdateExpression`

index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type AnyExpression = jsep.ArrayExpression
2323
| jsep.BinaryExpression
2424
| jsep.MemberExpression
2525
| jsep.CallExpression
26+
| jsep.Compound
2627
| jsep.ConditionalExpression
2728
| jsep.Identifier
2829
| jsep.Literal
@@ -51,6 +52,7 @@ export default class ExpressionEval {
5152
'LogicalExpression': ExpressionEval.prototype.evalBinaryExpression,
5253
'BinaryExpression': ExpressionEval.prototype.evalBinaryExpression,
5354
'CallExpression': ExpressionEval.prototype.evalCallExpression,
55+
'Compound': ExpressionEval.prototype.evalCompoundExpression,
5456
'ConditionalExpression': ExpressionEval.prototype.evalConditionalExpression,
5557
'Identifier': ExpressionEval.prototype.evalIdentifier,
5658
'Literal': ExpressionEval.evalLiteral,
@@ -270,6 +272,12 @@ export default class ExpressionEval {
270272
: op(leftRight as [operand, operand]);
271273
}
272274

275+
private evalCompoundExpression(node: jsep.Compound) {
276+
return this.isAsync
277+
? node.body.reduce((p: Promise<any>, node) => p.then(() => this.eval(node)), Promise.resolve())
278+
: node.body.map(v => this.eval(v))[node.body.length - 1]
279+
}
280+
273281
private evalCallExpression(node: jsep.CallExpression) {
274282
return this.evalSyncAsync(this.evalCall(node.callee), ([fn, caller]) => this
275283
.evalSyncAsync(this.evalArray(node.arguments), args => fn

test.js

+3
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ const fixtures = [
134134
{expr: '--a', expected: 0, context: {a: 1}, expObj: {a: 0}},
135135
{expr: 'a[0] = 3', expected: 3, context: {a: [0, 0]}, expObj: {a: [3, 0]}},
136136

137+
// compound
138+
{expr: 'a=1; b=a; c=a+b;', expected: 2, context: {}, expObj: {a: 1, b: 1, c: 2}},
139+
137140
// new
138141
{expr: '(new Date(2021, 8)).getFullYear()', expected: 2021 },
139142
{expr: '(new sub.sub2["Date"](2021, 8)).getFullYear()', expected: 2021 },

0 commit comments

Comments
 (0)