• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3require('../common');
4const assert = require('assert');
5const { processTopLevelAwait } = require('internal/repl/await');
6
7// Flags: --expose-internals
8
9// This test was created based on
10// https://cs.chromium.org/chromium/src/third_party/WebKit/LayoutTests/http/tests/inspector-unit/preprocess-top-level-awaits.js?rcl=358caaba5e763e71c4abb9ada2d9cd8b1188cac9
11
12const surrogate = (
13  '"\u{1F601}\u{1f468}\u200d\u{1f469}\u200d\u{1f467}\u200d\u{1f466}"'
14);
15
16const testCases = [
17  [ '0',
18    null ],
19  [ 'await 0',
20    '(async () => { return { value: (await 0) } })()' ],
21  [ `await ${surrogate}`,
22    `(async () => { return { value: (await ${surrogate}) } })()` ],
23  [ 'await 0;',
24    '(async () => { return { value: (await 0) }; })()' ],
25  [ 'await 0;;;',
26    '(async () => { return { value: (await 0) };;; })()' ],
27  [ `await ${surrogate};`,
28    `(async () => { return { value: (await ${surrogate}) }; })()` ],
29  [ `await ${surrogate};`,
30    `(async () => { return { value: (await ${surrogate}) }; })()` ],
31  [ '(await 0)',
32    '(async () => { return ({ value: (await 0) }) })()' ],
33  [ `(await ${surrogate})`,
34    `(async () => { return ({ value: (await ${surrogate}) }) })()` ],
35  [ '(await 0);',
36    '(async () => { return ({ value: (await 0) }); })()' ],
37  [ `(await ${surrogate});`,
38    `(async () => { return ({ value: (await ${surrogate}) }); })()` ],
39  [ 'async function foo() { await 0; }',
40    null ],
41  [ 'async () => await 0',
42    null ],
43  [ 'class A { async method() { await 0 } }',
44    null ],
45  [ 'await 0; return 0;',
46    null ],
47  [ `await ${surrogate}; await ${surrogate};`,
48    `(async () => { await ${surrogate}; return { value: (await ${surrogate}) }; })()` ],
49  [ 'var a = await 1',
50    'var a; (async () => { void (a = await 1) })()' ],
51  [ `var a = await ${surrogate}`,
52    `var a; (async () => { void (a = await ${surrogate}) })()` ],
53  [ 'let a = await 1',
54    'let a; (async () => { void (a = await 1) })()' ],
55  [ 'const a = await 1',
56    'let a; (async () => { void (a = await 1) })()' ],
57  [ 'for (var i = 0; i < 1; ++i) { await i }',
58    'var i; (async () => { for (void (i = 0); i < 1; ++i) { await i } })()' ],
59  [ 'for (let i = 0; i < 1; ++i) { await i }',
60    '(async () => { for (let i = 0; i < 1; ++i) { await i } })()' ],
61  [ 'var {a} = {a:1}, [b] = [1], {c:{d}} = {c:{d: await 1}}',
62    'var a, b, d; (async () => { void ( ({a} = {a:1}), ([b] = [1]), ' +
63        '({c:{d}} = {c:{d: await 1}})) })()' ],
64  [ 'let [a, b, c] = await ([1, 2, 3])',
65    'let a, b, c; (async () => { void ([a, b, c] = await ([1, 2, 3])) })()'],
66  [ 'let {a,b,c} = await ({a: 1, b: 2, c: 3})',
67    'let a, b, c; (async () => { void ({a,b,c} = ' +
68        'await ({a: 1, b: 2, c: 3})) })()'],
69  [ 'let {a: [b]} = {a: [await 1]}, [{d}] = [{d: 3}]',
70    'let b, d; (async () => { void ( ({a: [b]} = {a: [await 1]}),' +
71        ' ([{d}] = [{d: 3}])) })()'],
72  /* eslint-disable no-template-curly-in-string */
73  [ 'console.log(`${(await { a: 1 }).a}`)',
74    '(async () => { return { value: (console.log(`${(await { a: 1 }).a}`)) } })()' ],
75  /* eslint-enable no-template-curly-in-string */
76  [ 'await 0; function foo() {}',
77    'var foo; (async () => { await 0; this.foo = foo; function foo() {} })()' ],
78  [ 'await 0; class Foo {}',
79    'let Foo; (async () => { await 0; Foo=class Foo {} })()' ],
80  [ 'if (await true) { function foo() {} }',
81    'var foo; (async () => { ' +
82      'if (await true) { this.foo = foo; function foo() {} } })()' ],
83  [ 'if (await true) { class Foo{} }',
84    '(async () => { if (await true) { class Foo{} } })()' ],
85  [ 'if (await true) { var a = 1; }',
86    'var a; (async () => { if (await true) { void (a = 1); } })()' ],
87  [ 'if (await true) { let a = 1; }',
88    '(async () => { if (await true) { let a = 1; } })()' ],
89  [ 'var a = await 1; let b = 2; const c = 3;',
90    'var a; let b; let c; (async () => { void (a = await 1); void (b = 2);' +
91        ' void (c = 3); })()' ],
92  [ 'let o = await 1, p',
93    'let o, p; (async () => { void ( (o = await 1), (p=undefined)) })()' ],
94  [ 'await (async () => { let p = await 1; return p; })()',
95    '(async () => { return { value: (await (async () => ' +
96      '{ let p = await 1; return p; })()) } })()' ],
97  [ '{ let p = await 1; }',
98    '(async () => { { let p = await 1; } })()' ],
99  [ 'var p = await 1',
100    'var p; (async () => { void (p = await 1) })()' ],
101  [ 'await (async () => { var p = await 1; return p; })()',
102    '(async () => { return { value: (await (async () => ' +
103      '{ var p = await 1; return p; })()) } })()' ],
104  [ '{ var p = await 1; }',
105    'var p; (async () => { { void (p = await 1); } })()' ],
106  [ 'for await (var i of asyncIterable) { i; }',
107    'var i; (async () => { for await (i of asyncIterable) { i; } })()'],
108  [ 'for await (var [i] of asyncIterable) { i; }',
109    'var i; (async () => { for await ([i] of asyncIterable) { i; } })()'],
110  [ 'for await (var {i} of asyncIterable) { i; }',
111    'var i; (async () => { for await ({i} of asyncIterable) { i; } })()'],
112  [ 'for await (var [{i}, [j]] of asyncIterable) { i; }',
113    'var i, j; (async () => { for await ([{i}, [j]] of asyncIterable)' +
114      ' { i; } })()'],
115  [ 'for await (let i of asyncIterable) { i; }',
116    '(async () => { for await (let i of asyncIterable) { i; } })()'],
117  [ 'for await (const i of asyncIterable) { i; }',
118    '(async () => { for await (const i of asyncIterable) { i; } })()'],
119  [ 'for (var i of [1,2,3]) { await 1; }',
120    'var i; (async () => { for (i of [1,2,3]) { await 1; } })()'],
121  [ 'for (var [i] of [[1], [2]]) { await 1; }',
122    'var i; (async () => { for ([i] of [[1], [2]]) { await 1; } })()'],
123  [ 'for (var {i} of [{i: 1}, {i: 2}]) { await 1; }',
124    'var i; (async () => { for ({i} of [{i: 1}, {i: 2}]) { await 1; } })()'],
125  [ 'for (var [{i}, [j]] of [[{i: 1}, [2]]]) { await 1; }',
126    'var i, j; (async () => { for ([{i}, [j]] of [[{i: 1}, [2]]])' +
127      ' { await 1; } })()'],
128  [ 'for (let i of [1,2,3]) { await 1; }',
129    '(async () => { for (let i of [1,2,3]) { await 1; } })()'],
130  [ 'for (const i of [1,2,3]) { await 1; }',
131    '(async () => { for (const i of [1,2,3]) { await 1; } })()'],
132  [ 'for (var i in {x:1}) { await 1 }',
133    'var i; (async () => { for (i in {x:1}) { await 1 } })()'],
134  [ 'for (var [a,b] in {xy:1}) { await 1 }',
135    'var a, b; (async () => { for ([a,b] in {xy:1}) { await 1 } })()'],
136  [ 'for (let i in {x:1}) { await 1 }',
137    '(async () => { for (let i in {x:1}) { await 1 } })()'],
138  [ 'for (const i in {x:1}) { await 1 }',
139    '(async () => { for (const i in {x:1}) { await 1 } })()'],
140  [ 'var x = await foo(); async function foo() { return Promise.resolve(1);}',
141    'var x; var foo; (async () => { void (x = await foo()); this.foo = foo; ' +
142      'async function foo() { return Promise.resolve(1);} })()'],
143  [ '(await x).y',
144    '(async () => { return { value: ((await x).y) } })()'],
145  [ 'await (await x).y',
146    '(async () => { return { value: (await (await x).y) } })()'],
147];
148
149for (const [input, expected] of testCases) {
150  assert.strictEqual(processTopLevelAwait(input), expected);
151}
152