Skip to content

Commit

Permalink
repl: fix tla function hoisting
Browse files Browse the repository at this point in the history
PR-URL: #39745
Fixes: #39744
Reviewed-By: Guy Bedford <guybedford@gmail.com>
  • Loading branch information
DonJayamanne authored and targos committed Sep 4, 2021
1 parent ed3c332 commit 37b4708
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/internal/repl/await.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const visitorsWithoutAncestors = {
walk.base.ForOfStatement(node, state, c);
},
FunctionDeclaration(node, state, c) {
state.prepend(node, `${node.id.name}=`);
state.prepend(node, `this.${node.id.name} = ${node.id.name}; `);
ArrayPrototypePush(
state.hoistedDeclarationStatements,
`var ${node.id.name}; `
Expand Down
8 changes: 6 additions & 2 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 () => { await 0; this.foo = foo; function foo() {} })()' ],
[ 'await 0; class Foo {}',
'let Foo; (async () => { await 0; Foo=class Foo {} })()' ],
[ 'if (await true) { function foo() {} }',
'var foo; (async () => { if (await true) { foo=function foo() {} } })()' ],
'var foo; (async () => { ' +
'if (await true) { this.foo = foo; 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 () => { void (x = await foo()); this.foo = foo; ' +
'async function foo() { return Promise.resolve(1);} })()'],
];

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

0 comments on commit 37b4708

Please sign in to comment.