Skip to content

Commit

Permalink
repl: fix tla function hoisting
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Aug 13, 2021
1 parent 602fe4e commit a1f77ee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
14 changes: 9 additions & 5 deletions lib/internal/repl/await.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const visitorsWithoutAncestors = {
if (isTopLevelDeclaration(state)) {
state.prepend(node, `${node.id.name}=`);
ArrayPrototypePush(
state.hoistedDeclarationStatements,
`let ${node.id.name}; `
state.hoistedDeclaration,
`this.${node.id.name} = ${node.id.name}; `
);
}

Expand All @@ -49,8 +49,8 @@ const visitorsWithoutAncestors = {
FunctionDeclaration(node, state, c) {
state.prepend(node, `${node.id.name}=`);
ArrayPrototypePush(
state.hoistedDeclarationStatements,
`var ${node.id.name}; `
state.hoistedDeclaration,
`this.${node.id.name} = ${node.id.name}; `
);
},
FunctionExpression: noop,
Expand Down Expand Up @@ -195,6 +195,7 @@ function processTopLevelAwait(src) {
const state = {
body,
ancestors: [],
hoistedDeclaration: [],
hoistedDeclarationStatements: [],
replace(from, to, str) {
for (let i = from; i < to; i++) {
Expand Down Expand Up @@ -239,7 +240,10 @@ function processTopLevelAwait(src) {
state.prepend(last, 'return (');
state.append(last.expression, ')');
}

const hoisted = ArrayPrototypeJoin(state.hoistedDeclaration, '');
if (hoisted.length > 0) {
wrappedArray[wrapPrefix.length - 1] = hoisted;
}
return (
ArrayPrototypeJoin(state.hoistedDeclarationStatements, '') +
ArrayPrototypeJoin(wrappedArray, '')
Expand Down
10 changes: 7 additions & 3 deletions test/parallel/test-repl-preprocess-top-level-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ const testCases = [
'(async () => { return (console.log(`${(await { a: 1 }).a}`)) })()' ],
/* eslint-enable no-template-curly-in-string */
[ 'await 0; function foo() {}',
'var foo; (async () => { await 0; foo=function foo() {} })()' ],
'var foo; (async () => {this.foo = foo; await 0; function foo() {} })()' ],
[ 'await 0; class Foo {}',
'let Foo; (async () => { await 0; Foo=class Foo {} })()' ],
'let Foo; (async () => {this.Foo = Foo; await 0; class Foo {} })()' ],
[ 'if (await true) { function foo() {} }',
'var foo; (async () => { if (await true) { foo=function foo() {} } })()' ],
'var foo; (async () => {this.foo = foo; ' +
'if (await true) { function foo() {} } })()' ],
[ 'if (await true) { class Foo{} }',
'(async () => { if (await true) { class Foo{} } })()' ],
[ 'if (await true) { var a = 1; }',
Expand Down Expand Up @@ -116,6 +117,9 @@ const testCases = [
'(async () => { for (let i in {x:1}) { await 1 } })()'],
[ 'for (const i in {x:1}) { await 1 }',
'(async () => { for (const i in {x:1}) { await 1 } })()'],
[ 'var x = await foo(); async function foo() { return Promise.resolve(1);}',
'var x; var foo; (async () => {this.foo = foo; void (x = await foo()); ' +
'async function foo() { return Promise.resolve(1);} })()'],
];

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

0 comments on commit a1f77ee

Please sign in to comment.