• 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 testCases = [
13  [ '0',
14    null ],
15  [ 'await 0',
16    '(async () => { return (await 0) })()' ],
17  [ 'await 0;',
18    '(async () => { return (await 0); })()' ],
19  [ '(await 0)',
20    '(async () => { return ((await 0)) })()' ],
21  [ '(await 0);',
22    '(async () => { return ((await 0)); })()' ],
23  [ 'async function foo() { await 0; }',
24    null ],
25  [ 'async () => await 0',
26    null ],
27  [ 'class A { async method() { await 0 } }',
28    null ],
29  [ 'await 0; return 0;',
30    null ],
31  [ 'var a = await 1',
32    '(async () => { void (a = await 1) })()' ],
33  [ 'let a = await 1',
34    '(async () => { void (a = await 1) })()' ],
35  [ 'const a = await 1',
36    '(async () => { void (a = await 1) })()' ],
37  [ 'for (var i = 0; i < 1; ++i) { await i }',
38    '(async () => { for (void (i = 0); i < 1; ++i) { await i } })()' ],
39  [ 'for (let i = 0; i < 1; ++i) { await i }',
40    '(async () => { for (let i = 0; i < 1; ++i) { await i } })()' ],
41  [ 'var {a} = {a:1}, [b] = [1], {c:{d}} = {c:{d: await 1}}',
42    '(async () => { void ( ({a} = {a:1}), ([b] = [1]), ' +
43        '({c:{d}} = {c:{d: await 1}})) })()' ],
44  /* eslint-disable no-template-curly-in-string */
45  [ 'console.log(`${(await { a: 1 }).a}`)',
46    '(async () => { return (console.log(`${(await { a: 1 }).a}`)) })()' ],
47  /* eslint-enable no-template-curly-in-string */
48  [ 'await 0; function foo() {}',
49    '(async () => { await 0; foo=function foo() {} })()' ],
50  [ 'await 0; class Foo {}',
51    '(async () => { await 0; Foo=class Foo {} })()' ],
52  [ 'if (await true) { function foo() {} }',
53    '(async () => { if (await true) { foo=function foo() {} } })()' ],
54  [ 'if (await true) { class Foo{} }',
55    '(async () => { if (await true) { class Foo{} } })()' ],
56  [ 'if (await true) { var a = 1; }',
57    '(async () => { if (await true) { void (a = 1); } })()' ],
58  [ 'if (await true) { let a = 1; }',
59    '(async () => { if (await true) { let a = 1; } })()' ],
60  [ 'var a = await 1; let b = 2; const c = 3;',
61    '(async () => { void (a = await 1); void (b = 2); void (c = 3); })()' ],
62  [ 'let o = await 1, p',
63    '(async () => { void ( (o = await 1), (p=undefined)) })()' ]
64];
65
66for (const [input, expected] of testCases) {
67  assert.strictEqual(processTopLevelAwait(input), expected);
68}
69