Skip to content

Commit beb892a

Browse files
committed
update assert to be always srict, add top-level await examples
1 parent f55b4bf commit beb892a

20 files changed

+127
-31
lines changed

test/async/builtins.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate: e } = require('../support');
55

6-
const opts = { functions: true };
6+
const opts = { functions: true, allowAwaitOutsideFunction: true };
77

88
describe('built in objects', () => {
9+
it('top level await', async () => {
10+
assert.equal(await e('await 1', {}, opts), 1);
11+
assert.equal(await e('Promise.resolve(1)', { Promise }, opts), 1);
12+
assert.equal(await e('await Promise.resolve(1)', { Promise }, opts), 1);
13+
});
14+
915
it('should evaluate Array', async () => {
1016
const ctx = { Array };
1117
assert.deepEqual(await e('new Array("1", ...arr)', { Array, arr: ['a', 'b'] }, opts), ['1', 'a', 'b']);

test/async/expressions.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
3+
const assert = require('node:assert/strict');
4+
const { evaluate: e } = require('../support');
5+
const { generate } = require('escodegen');
6+
7+
const opts = { functions: true, generate, strict: false };
8+
9+
describe('expressions', () => {
10+
describe('ObjectExpression', () => {
11+
it('should evaluate object expressions', async () => {
12+
assert.deepEqual(await e('{ a }', { a: 2 }, opts), { a: 2 });
13+
assert.deepEqual(await e('({ a })', { a: 2 }, opts), { a: 2 });
14+
assert.deepEqual(await e('{ a, b: b() }', { a: 2, b: () => 6 }, opts), { a: 2, b: 6 });
15+
});
16+
17+
it('should return undefined when object expressions are invalid', async () => {
18+
assert.deepEqual(await e('{ a: b }', { a: 2 }, opts), undefined);
19+
});
20+
21+
it('should support computed properties', async () => {
22+
assert.deepEqual(await e('({ [a]: a })', { a: 'x' }, opts), { x: 'x' });
23+
assert.deepEqual(await e('({ [a]: b })', { a: 'x', b: 'y' }, opts), { x: 'y' });
24+
assert.deepEqual(await e('({ [a]: b })', { a: 'x', b: 'y' }, opts), { x: 'y' });
25+
});
26+
});
27+
28+
describe('ArrayExpression', () => {
29+
it('should evaluate array expressions', async () => {
30+
assert.deepEqual(await e('[a, b()]', { a: 2, b: () => 6 }, opts), [2, 6]);
31+
});
32+
33+
it('should return undefined when array expressions are invalid', async () => {
34+
assert.deepEqual(await e('[a, b]', { a: 2 }, opts), undefined);
35+
});
36+
});
37+
38+
describe('ArrowFunctionExpression', () => {
39+
it('array methods with fat arrow function', async () => {
40+
assert.deepEqual(await e('[1, 2, 3].map((n) => { return n * x })', { x: 2 }, opts), [2, 4, 6]);
41+
assert.deepEqual(await e('[1, 2, 3].map(n => n * x)', { x: 2 }, opts), [2, 4, 6]);
42+
});
43+
44+
it('immediately invoked fat arrow', async () => {
45+
assert.deepEqual(await e('((a, b, c) => a + b + c)(x, y, z) ', { x: 1, y: 2, z: 3 }, opts), 6);
46+
});
47+
48+
it('immediately invoked fat arrow with function args', async () => {
49+
assert.deepEqual(await e('((a, b, c) => a + b + c)(x, y, z(zz)) ', { x: 1, y: 2, z: v => v, zz: 3 }, opts), 6);
50+
});
51+
});
52+
53+
describe('OptionalCallExpression', () => {
54+
it('should evaluate optional call expressions', async () => {
55+
assert.deepEqual(await e('a?.()', { a: () => 2 }, opts), 2);
56+
assert.deepEqual(await e('a?.()', {}, opts), undefined);
57+
assert.deepEqual(await e('a?.b?.()', { a: { b: () => 2 } }, opts), 2);
58+
assert.deepEqual(await e('a?.b?.()', { a: {} }, opts), undefined);
59+
});
60+
});
61+
62+
describe('AwaitExpression', () => {
63+
it('async await', async () => {
64+
const ctx = { x: Promise.resolve(1), y: Promise.resolve(2), z: Promise.resolve(3) };
65+
assert.deepEqual(await await e('(async (a, b, c) => await a + await b + await c)(x, y, z) ', ctx, opts), 6);
66+
});
67+
});
68+
});

test/async/function-calls.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const assert = require('node:assert/strict');
4+
const { evaluate: e } = require('../support');
5+
6+
describe('function calls', () => {
7+
it('should support regex.test()', async () => {
8+
assert.deepEqual(await e('/./.test("a")', {}, { functions: true }), true);
9+
});
10+
11+
it('should support regex.test()', async () => {
12+
assert.deepEqual(await e('a.upper("b")', { a: { upper: v => v.toUpperCase() } }, { functions: true }), 'B');
13+
});
14+
15+
it('should support regex.exec()', async () => {
16+
assert.deepEqual((await e('/./.exec("abc")', {}, { functions: true })).slice(), ['a']);
17+
});
18+
19+
it('should support [].slice()', async () => {
20+
assert.deepEqual((await e('[1, 2, 3, 4, 5].slice(1, 3)', {}, { functions: true })).slice(), [2, 3]);
21+
});
22+
});

test/async/identifiers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate: e } = require('../support');
55

66
describe('identifiers', () => {

test/async/literals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate: e } = require('../support');
55

66
describe('literals', () => {

test/async/operators.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate: e } = require('../support');
55

66
describe('operators', () => {

test/expr-eval/expression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate: e, variables } = require('../support');
55

66
/**

test/expr-eval/functions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
/* eslint-disable no-loss-of-precision */
12
'use strict';
23

3-
const assert = require('assert').strict;
4+
const assert = require('node:assert/strict');
45
const { expression, evaluate: e } = require('../support');
56
const opts = { functions: true };
67
const ctx = { Math };

test/expr-eval/operators.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate: e } = require('../support');
55
const opts = { functions: true };
66

test/inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const babel = require('@babel/parser');
4-
const assert = require('assert').strict;
4+
const assert = require('node:assert/strict');
55
const inspect = require('./support/inspect');
66

77
describe('inspect', () => {

test/static-eval/eval.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const escodegen = require('escodegen');
55
const { evaluate } = require('../..');
66
const { parse } = require('esprima');

test/static-eval/prop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { parse } = require('esprima');
55
const { evaluate } = require('../..');
66

test/static-eval/template-strings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate } = require('../..');
55
const { parse } = require('esprima');
66

test/support/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,36 @@ exports.parse = (source, options = {}) => {
1414

1515
const input = source;
1616
const ast = babel.parseExpression(input, opts);
17-
return { input, ast, opts };
17+
return ast;
1818
}
1919

20-
return { input: '', ast: source, opts: options };
20+
return source;
2121
};
2222

2323
exports.expression = (input, options = {}) => {
24-
const { ast, opts } = exports.parse(input, options);
25-
return context => evaluate(ast, context, opts);
24+
const ast = exports.parse(input, options);
25+
return context => evaluate(ast, context, options);
2626
};
2727

2828
exports.expression.sync = (input, options = {}) => {
29-
const { ast, opts } = exports.parse(input, options);
29+
const ast = exports.parse(input, options);
3030
return context => {
31-
const expression = new ExpressionSync(ast, opts);
31+
const expression = new ExpressionSync(ast, options);
3232
return expression.evaluate(context);
3333
};
3434
};
3535

3636
exports.evaluate = (input, context = {}, options = {}) => {
37-
const { ast, opts } = exports.parse(input, options);
38-
return evaluate(ast, context, opts);
37+
const ast = exports.parse(input, options);
38+
return evaluate(ast, context, options);
3939
};
4040

4141
exports.variables = (input, options = {}) => {
42-
const { ast, opts } = exports.parse(input, options);
43-
return variables(ast, opts);
42+
const ast = exports.parse(input, options);
43+
return variables(ast, options);
4444
};
4545

4646
exports.evaluate.sync = (input, context = {}, options = {}) => {
47-
const { ast, opts } = exports.parse(input, options);
48-
return evaluate.sync(ast, context, opts);
47+
const ast = exports.parse(input, options);
48+
return evaluate.sync(ast, context, options);
4949
};

test/sync/builtins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate: e } = require('../support');
55

66
const opts = { functions: true, strict: true };

test/sync/expressions.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate } = require('../support');
55
const { generate } = require('escodegen');
66

@@ -52,7 +52,6 @@ describe('expressions', () => {
5252

5353
describe('OptionalCallExpression', () => {
5454
it('should evaluate optional call expressions', () => {
55-
// evaluate.sync('a?.()', { a: () => 2 }, opts);
5655
assert.deepEqual(evaluate.sync('a?.()', { a: () => 2 }, opts), 2);
5756
assert.deepEqual(evaluate.sync('a?.()', {}, opts), undefined);
5857
assert.deepEqual(evaluate.sync('a?.b?.()', { a: { b: () => 2 } }, opts), 2);

test/sync/function-calls.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate: e } = require('../support');
55

66
describe('function calls', () => {

test/sync/identifiers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate: e } = require('../support');
55

66
describe('identifiers', () => {

test/sync/literals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate: e } = require('../support');
55

66
describe('literals', () => {

test/sync/operators.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const assert = require('assert').strict;
3+
const assert = require('node:assert/strict');
44
const { evaluate: e } = require('../support');
55

66
describe('operators', () => {

0 commit comments

Comments
 (0)