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
59 changes: 55 additions & 4 deletions lib/internal/repl/await.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ const parser = require('internal/deps/acorn/acorn/dist/acorn').Parser;
const walk = require('internal/deps/acorn/acorn-walk/dist/walk');
const { Recoverable } = require('internal/repl');

function isTopLevelDeclaration(state) {
return state.ancestors[state.ancestors.length - 2] === state.body;
}

const noop = FunctionPrototype;
const visitorsWithoutAncestors = {
ClassDeclaration(node, state, c) {
if (state.ancestors[state.ancestors.length - 2] === state.body) {
if (isTopLevelDeclaration(state)) {
state.prepend(node, `${node.id.name}=`);
state.prepend(state.wrapper, `let ${node.id.name}; `);
}

walk.base.ClassDeclaration(node, state, c);
},
ForOfStatement(node, state, c) {
Expand All @@ -37,7 +43,10 @@ const visitorsWithoutAncestors = {
walk.base.ForOfStatement(node, state, c);
},
FunctionDeclaration(node, state, c) {
state.prepend(node, `${node.id.name}=`);
if (isTopLevelDeclaration(state)) {
state.prepend(node, `${node.id.name}=`);
state.prepend(state.wrapper, `let ${node.id.name}; `);
}
},
FunctionExpression: noop,
ArrowFunctionExpression: noop,
Expand All @@ -51,8 +60,8 @@ const visitorsWithoutAncestors = {
walk.base.ReturnStatement(node, state, c);
},
VariableDeclaration(node, state, c) {
if (node.kind === 'var' ||
state.ancestors[state.ancestors.length - 2] === state.body) {
const variableKind = node.kind;
if (variableKind === 'var' || isTopLevelDeclaration(state)) {
if (node.declarations.length === 1) {
state.replace(node.start, node.start + node.kind.length, 'void');
} else {
Expand All @@ -67,6 +76,47 @@ const visitorsWithoutAncestors = {
if (node.declarations.length !== 1) {
state.append(node.declarations[node.declarations.length - 1], ')');
}

const variableIdentifiersToHoist = [
['var', []],
['let', []],
];
function registerVariableDeclarationIdentifiers(node) {
switch (node.type) {
case 'Identifier':
ArrayPrototypePush(
variableIdentifiersToHoist[variableKind === 'var' ? 0 : 1][1],
node.name
);
break;
case 'ObjectPattern':
ArrayPrototypeForEach(node.properties, (property) => {
registerVariableDeclarationIdentifiers(property.value);
});
break;
case 'ArrayPattern':
ArrayPrototypeForEach(node.elements, (element) => {
registerVariableDeclarationIdentifiers(element);
});
break;
}
}

ArrayPrototypeForEach(node.declarations, (decl) => {
registerVariableDeclarationIdentifiers(decl.id);
});

ArrayPrototypeForEach(
variableIdentifiersToHoist,
({ 0: kind, 1: identifiers }) => {
if (identifiers.length > 0) {
state.prepend(
state.wrapper,
`${kind} ${ArrayPrototypeJoin(identifiers, ', ')}; `
);
}
}
);
}

walk.base.VariableDeclaration(node, state, c);
Expand Down Expand Up @@ -126,6 +176,7 @@ function processTopLevelAwait(src) {
}
const body = root.body[0].expression.callee.body;
const state = {
wrapper: root.body[0],
body,
ancestors: [],
replace(from, to, str) {
Expand Down
43 changes: 32 additions & 11 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,59 @@ const testCases = [
[ 'await 0; return 0;',
null ],
[ 'var a = await 1',
'(async () => { void (a = await 1) })()' ],
'var 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 } })()' ],
'var 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]), ' +
'var 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() {} } })()' ],
'(async () => { if (await true) { 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); } })()' ],
'var a; (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 c; let b; var 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
[ 'await (async () => { let p = await 1; return p; })()',
'(async () => { return (await (async () => ' +
'{ let p = await 1; return p; })()) })()' ],
[ '{ let p = await 1; }',
'(async () => { { let p = await 1; } })()' ],
[ 'var p = await 1',
'var p; (async () => { void (p = await 1) })()' ],
[ 'await (async () => { var p = await 1; return p; })()',
'(async () => { return (await (async () => ' +
'{ var p = await 1; return p; })()) })()' ],
[ '{ var p = await 1; }',
'var p; (async () => { { void (p = await 1); } })()' ],
];

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