summaryrefslogtreecommitdiff
path: root/test/parallel/test-repl-preprocess-top-level-await.js
blob: 2a173b7339b8c719e788a87f1c7feb11c32b5853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';

require('../common');
const assert = require('assert');
const { processTopLevelAwait } = require('internal/repl/await');

// Flags: --expose-internals

// This test was created based on
// https://cs.chromium.org/chromium/src/third_party/WebKit/LayoutTests/http/tests/inspector-unit/preprocess-top-level-awaits.js?rcl=358caaba5e763e71c4abb9ada2d9cd8b1188cac9

const testCases = [
  [ '0',
    null ],
  [ 'await 0',
    '(async () => { return (await 0) })()' ],
  [ 'await 0;',
    '(async () => { return (await 0); })()' ],
  [ '(await 0)',
    '(async () => { return ((await 0)) })()' ],
  [ '(await 0);',
    '(async () => { return ((await 0)); })()' ],
  [ 'async function foo() { await 0; }',
    null ],
  [ 'async () => await 0',
    null ],
  [ 'class A { async method() { await 0 } }',
    null ],
  [ 'await 0; return 0;',
    null ],
  [ 'var a = await 1',
    '(async () => { void (a = await 1) })()' ],
  [ 'let a = await 1',
    '(async () => { void (a = await 1) })()' ],
  [ 'const a = await 1',
    '(async () => { void (a = await 1) })()' ],
  [ 'for (var i = 0; i < 1; ++i) { await i }',
    '(async () => { for (void (i = 0); i < 1; ++i) { await i } })()' ],
  [ 'for (let i = 0; i < 1; ++i) { await i }',
    '(async () => { for (let i = 0; i < 1; ++i) { await i } })()' ],
  [ 'var {a} = {a:1}, [b] = [1], {c:{d}} = {c:{d: await 1}}',
    '(async () => { void ( ({a} = {a:1}), ([b] = [1]), ' +
        '({c:{d}} = {c:{d: await 1}})) })()' ],
  /* eslint-disable no-template-curly-in-string */
  [ 'console.log(`${(await { a: 1 }).a}`)',
    '(async () => { return (console.log(`${(await { a: 1 }).a}`)) })()' ],
  /* eslint-enable no-template-curly-in-string */
  [ 'await 0; function foo() {}',
    '(async () => { await 0; foo=function foo() {} })()' ],
  [ 'await 0; class Foo {}',
    '(async () => { await 0; Foo=class Foo {} })()' ],
  [ 'if (await true) { function foo() {} }',
    '(async () => { if (await true) { foo=function foo() {} } })()' ],
  [ 'if (await true) { class Foo{} }',
    '(async () => { if (await true) { class Foo{} } })()' ],
  [ 'if (await true) { var a = 1; }',
    '(async () => { if (await true) { void (a = 1); } })()' ],
  [ 'if (await true) { let a = 1; }',
    '(async () => { if (await true) { let a = 1; } })()' ],
  [ 'var a = await 1; let b = 2; const c = 3;',
    '(async () => { void (a = await 1); void (b = 2); void (c = 3); })()' ],
  [ 'let o = await 1, p',
    '(async () => { void ( (o = await 1), (p=undefined)) })()' ]
];

for (const [input, expected] of testCases) {
  assert.strictEqual(processTopLevelAwait(input), expected);
}