Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: hoist declarations when processing top level await #39265

Closed
74 changes: 58 additions & 16 deletions lib/internal/repl/await.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
ArrayFrom,
ArrayPrototypeForEach,
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypePop,
ArrayPrototypePush,
FunctionPrototype,
Expand All @@ -25,9 +26,9 @@ const { Recoverable } = require('internal/repl');
const noop = FunctionPrototype;
const visitorsWithoutAncestors = {
ClassDeclaration(node, state, c) {
if (state.ancestors[state.ancestors.length - 2] === state.body) {
state.prepend(node, `${node.id.name}=`);
}
state.prepend(node, `${node.id.name}=`);
state.prepend(state.root.body[0], `let ${node.id.name}; `);

walk.base.ClassDeclaration(node, state, c);
},
ForOfStatement(node, state, c) {
Expand All @@ -38,6 +39,7 @@ const visitorsWithoutAncestors = {
},
FunctionDeclaration(node, state, c) {
state.prepend(node, `${node.id.name}=`);
state.prepend(state.root.body[0], `let ${node.id.name}; `);
},
FunctionExpression: noop,
ArrowFunctionExpression: noop,
Expand All @@ -51,24 +53,63 @@ const visitorsWithoutAncestors = {
walk.base.ReturnStatement(node, state, c);
},
VariableDeclaration(node, state, c) {
if (node.kind === 'var' ||
state.ancestors[state.ancestors.length - 2] === state.body) {
if (node.declarations.length === 1) {
state.replace(node.start, node.start + node.kind.length, 'void');
} else {
state.replace(node.start, node.start + node.kind.length, 'void (');
}
if (node.declarations.length === 1) {
state.replace(node.start, node.start + node.kind.length, 'void');
} else {
state.replace(node.start, node.start + node.kind.length, 'void (');
}

ArrayPrototypeForEach(node.declarations, (decl) => {
state.prepend(decl, '(');
state.append(decl, decl.init ? ')' : '=undefined)');
});

ArrayPrototypeForEach(node.declarations, (decl) => {
state.prepend(decl, '(');
state.append(decl, decl.init ? ')' : '=undefined)');
});
if (node.declarations.length !== 1) {
state.append(node.declarations[node.declarations.length - 1], ')');
}

const declarationSeparator = ', ';

function getVariableDeclarationIdentifier(node) {
switch (node.type) {
case 'Identifier':
return node.name;
case 'ObjectPattern':
return ArrayPrototypeJoin(
ArrayPrototypeMap(
node.properties,
(property) => getVariableDeclarationIdentifier(property.value)
),
declarationSeparator
);
case 'ArrayPattern':
return ArrayPrototypeJoin(
ArrayPrototypeMap(
node.elements,
(element) => getVariableDeclarationIdentifier(element)
),
declarationSeparator
);
}
}

if (node.declarations.length !== 1) {
state.append(node.declarations[node.declarations.length - 1], ')');
const variableIdentifiersToHoist = [];
for (const decl of node.declarations) {
const identifier = getVariableDeclarationIdentifier(decl.id);
if (identifier !== undefined) {
variableIdentifiersToHoist.push(identifier);
}
}
ejose19 marked this conversation as resolved.
Show resolved Hide resolved

state.prepend(
state.root.body[0],
'let ' +
ArrayPrototypeJoin(
variableIdentifiersToHoist,
declarationSeparator
) + '; '
);

walk.base.VariableDeclaration(node, state, c);
}
};
Expand Down Expand Up @@ -126,6 +167,7 @@ function processTopLevelAwait(src) {
}
const body = root.body[0].expression.callee.body;
const state = {
root,
body,
ancestors: [],
replace(from, to, str) {
Expand Down
37 changes: 23 additions & 14 deletions test/parallel/test-repl-preprocess-top-level-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,47 @@ const testCases = [
[ 'await 0; return 0;',
null ],
[ 'var a = await 1',
'(async () => { void (a = await 1) })()' ],
'let a; (async () => { void (a = await 1) })()' ],
[ 'let a = await 1',
'(async () => { void (a = await 1) })()' ],
'let a; (async () => { void (a = await 1) })()' ],
[ 'const a = await 1',
'(async () => { void (a = await 1) })()' ],
'let a; (async () => { void (a = await 1) })()' ],
[ 'for (var i = 0; i < 1; ++i) { await i }',
'(async () => { for (void (i = 0); i < 1; ++i) { await i } })()' ],
'let 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 } })()' ],
'let i; (async () => { for (void (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]), ' +
'let a, b, d; (async () => { void ( ({a} = {a:1}), ([b] = [1]), ' +
'({c:{d}} = {c:{d: await 1}})) })()' ],
[ 'let [a, b, c] = await ([1, 2, 3])',
'let a, b, c; (async () => { void ([a, b, c] = await ([1, 2, 3])) })()'],
[ 'let {a,b,c} = await ({a: 1, b: 2, c: 3})',
'let a, b, c; (async () => { void ({a,b,c} = ' +
'await ({a: 1, b: 2, c: 3})) })()'],
[ 'let {a: [b]} = {a: [await 1]}, [{d}] = [{d: 3}]',
'let b, d; (async () => { void ( ({a: [b]} = {a: [await 1]}),' +
' ([{d}] = [{d: 3}])) })()'],
/* 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() {} })()' ],
'let foo; (async () => { await 0; foo=function foo() {} })()' ],
ejose19 marked this conversation as resolved.
Show resolved Hide resolved
[ 'await 0; class Foo {}',
'(async () => { await 0; Foo=class Foo {} })()' ],
'let Foo; (async () => { await 0; Foo=class Foo {} })()' ],
[ 'if (await true) { function foo() {} }',
'(async () => { if (await true) { foo=function foo() {} } })()' ],
'let foo; (async () => { if (await true) { foo=function foo() {} } })()' ],
ejose19 marked this conversation as resolved.
Show resolved Hide resolved
[ 'if (await true) { class Foo{} }',
'(async () => { if (await true) { class Foo{} } })()' ],
'let Foo; (async () => { if (await true) { Foo=class Foo{} } })()' ],
[ 'if (await true) { var a = 1; }',
'(async () => { if (await true) { void (a = 1); } })()' ],
'let a; (async () => { if (await true) { void (a = 1); } })()' ],
[ 'if (await true) { let a = 1; }',
'(async () => { if (await true) { let a = 1; } })()' ],
'let a; (async () => { if (await true) { void (a = 1); } })()' ],
[ 'var a = await 1; let b = 2; const c = 3;',
'(async () => { void (a = await 1); void (b = 2); void (c = 3); })()' ],
'let c; let b; let a; (async () => { void (a = await 1); void (b = 2);' +
' void (c = 3); })()' ],
[ 'let o = await 1, p',
'(async () => { void ( (o = await 1), (p=undefined)) })()' ],
'let o, p; (async () => { void ( (o = await 1), (p=undefined)) })()' ],
ejose19 marked this conversation as resolved.
Show resolved Hide resolved
];

for (const [input, expected] of testCases) {
Expand Down