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 'var a; (async () => { void (a = await 1) })()' ], 33 [ 'let a = await 1', 34 'let a; (async () => { void (a = await 1) })()' ], 35 [ 'const a = await 1', 36 'let a; (async () => { void (a = await 1) })()' ], 37 [ 'for (var i = 0; i < 1; ++i) { await i }', 38 'var i; (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 'var a, b, d; (async () => { void ( ({a} = {a:1}), ([b] = [1]), ' + 43 '({c:{d}} = {c:{d: await 1}})) })()' ], 44 [ 'let [a, b, c] = await ([1, 2, 3])', 45 'let a, b, c; (async () => { void ([a, b, c] = await ([1, 2, 3])) })()'], 46 [ 'let {a,b,c} = await ({a: 1, b: 2, c: 3})', 47 'let a, b, c; (async () => { void ({a,b,c} = ' + 48 'await ({a: 1, b: 2, c: 3})) })()'], 49 [ 'let {a: [b]} = {a: [await 1]}, [{d}] = [{d: 3}]', 50 'let b, d; (async () => { void ( ({a: [b]} = {a: [await 1]}),' + 51 ' ([{d}] = [{d: 3}])) })()'], 52 /* eslint-disable no-template-curly-in-string */ 53 [ 'console.log(`${(await { a: 1 }).a}`)', 54 '(async () => { return (console.log(`${(await { a: 1 }).a}`)) })()' ], 55 /* eslint-enable no-template-curly-in-string */ 56 [ 'await 0; function foo() {}', 57 'var foo; (async () => { await 0; this.foo = foo; function foo() {} })()' ], 58 [ 'await 0; class Foo {}', 59 'let Foo; (async () => { await 0; Foo=class Foo {} })()' ], 60 [ 'if (await true) { function foo() {} }', 61 'var foo; (async () => { ' + 62 'if (await true) { this.foo = foo; function foo() {} } })()' ], 63 [ 'if (await true) { class Foo{} }', 64 '(async () => { if (await true) { class Foo{} } })()' ], 65 [ 'if (await true) { var a = 1; }', 66 'var a; (async () => { if (await true) { void (a = 1); } })()' ], 67 [ 'if (await true) { let a = 1; }', 68 '(async () => { if (await true) { let a = 1; } })()' ], 69 [ 'var a = await 1; let b = 2; const c = 3;', 70 'var a; let b; let c; (async () => { void (a = await 1); void (b = 2);' + 71 ' void (c = 3); })()' ], 72 [ 'let o = await 1, p', 73 'let o, p; (async () => { void ( (o = await 1), (p=undefined)) })()' ], 74 [ 'await (async () => { let p = await 1; return p; })()', 75 '(async () => { return (await (async () => ' + 76 '{ let p = await 1; return p; })()) })()' ], 77 [ '{ let p = await 1; }', 78 '(async () => { { let p = await 1; } })()' ], 79 [ 'var p = await 1', 80 'var p; (async () => { void (p = await 1) })()' ], 81 [ 'await (async () => { var p = await 1; return p; })()', 82 '(async () => { return (await (async () => ' + 83 '{ var p = await 1; return p; })()) })()' ], 84 [ '{ var p = await 1; }', 85 'var p; (async () => { { void (p = await 1); } })()' ], 86 [ 'for await (var i of asyncIterable) { i; }', 87 'var i; (async () => { for await (i of asyncIterable) { i; } })()'], 88 [ 'for await (var [i] of asyncIterable) { i; }', 89 'var i; (async () => { for await ([i] of asyncIterable) { i; } })()'], 90 [ 'for await (var {i} of asyncIterable) { i; }', 91 'var i; (async () => { for await ({i} of asyncIterable) { i; } })()'], 92 [ 'for await (var [{i}, [j]] of asyncIterable) { i; }', 93 'var i, j; (async () => { for await ([{i}, [j]] of asyncIterable)' + 94 ' { i; } })()'], 95 [ 'for await (let i of asyncIterable) { i; }', 96 '(async () => { for await (let i of asyncIterable) { i; } })()'], 97 [ 'for await (const i of asyncIterable) { i; }', 98 '(async () => { for await (const i of asyncIterable) { i; } })()'], 99 [ 'for (var i of [1,2,3]) { await 1; }', 100 'var i; (async () => { for (i of [1,2,3]) { await 1; } })()'], 101 [ 'for (var [i] of [[1], [2]]) { await 1; }', 102 'var i; (async () => { for ([i] of [[1], [2]]) { await 1; } })()'], 103 [ 'for (var {i} of [{i: 1}, {i: 2}]) { await 1; }', 104 'var i; (async () => { for ({i} of [{i: 1}, {i: 2}]) { await 1; } })()'], 105 [ 'for (var [{i}, [j]] of [[{i: 1}, [2]]]) { await 1; }', 106 'var i, j; (async () => { for ([{i}, [j]] of [[{i: 1}, [2]]])' + 107 ' { await 1; } })()'], 108 [ 'for (let i of [1,2,3]) { await 1; }', 109 '(async () => { for (let i of [1,2,3]) { await 1; } })()'], 110 [ 'for (const i of [1,2,3]) { await 1; }', 111 '(async () => { for (const i of [1,2,3]) { await 1; } })()'], 112 [ 'for (var i in {x:1}) { await 1 }', 113 'var i; (async () => { for (i in {x:1}) { await 1 } })()'], 114 [ 'for (var [a,b] in {xy:1}) { await 1 }', 115 'var a, b; (async () => { for ([a,b] in {xy:1}) { await 1 } })()'], 116 [ 'for (let i in {x:1}) { await 1 }', 117 '(async () => { for (let i in {x:1}) { await 1 } })()'], 118 [ 'for (const i in {x:1}) { await 1 }', 119 '(async () => { for (const i in {x:1}) { await 1 } })()'], 120 [ 'var x = await foo(); async function foo() { return Promise.resolve(1);}', 121 'var x; var foo; (async () => { void (x = await foo()); this.foo = foo; ' + 122 'async function foo() { return Promise.resolve(1);} })()'], 123]; 124 125for (const [input, expected] of testCases) { 126 assert.strictEqual(processTopLevelAwait(input), expected); 127} 128