Skip to content

Commit a7a217b

Browse files
DonJayamannetargos
authored andcommitted
repl: fix tla function hoisting
PR-URL: #39745 Fixes: #39744 Reviewed-By: Guy Bedford <[email protected]>
1 parent 2e90b10 commit a7a217b

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/internal/repl/await.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const visitorsWithoutAncestors = {
4747
walk.base.ForOfStatement(node, state, c);
4848
},
4949
FunctionDeclaration(node, state, c) {
50-
state.prepend(node, `${node.id.name}=`);
50+
state.prepend(node, `this.${node.id.name} = ${node.id.name}; `);
5151
ArrayPrototypePush(
5252
state.hoistedDeclarationStatements,
5353
`var ${node.id.name}; `

test/parallel/test-repl-preprocess-top-level-await.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ const testCases = [
5454
'(async () => { return (console.log(`${(await { a: 1 }).a}`)) })()' ],
5555
/* eslint-enable no-template-curly-in-string */
5656
[ 'await 0; function foo() {}',
57-
'var foo; (async () => { await 0; foo=function foo() {} })()' ],
57+
'var foo; (async () => { await 0; this.foo = foo; function foo() {} })()' ],
5858
[ 'await 0; class Foo {}',
5959
'let Foo; (async () => { await 0; Foo=class Foo {} })()' ],
6060
[ 'if (await true) { function foo() {} }',
61-
'var foo; (async () => { if (await true) { foo=function foo() {} } })()' ],
61+
'var foo; (async () => { ' +
62+
'if (await true) { this.foo = foo; function foo() {} } })()' ],
6263
[ 'if (await true) { class Foo{} }',
6364
'(async () => { if (await true) { class Foo{} } })()' ],
6465
[ 'if (await true) { var a = 1; }',
@@ -116,6 +117,9 @@ const testCases = [
116117
'(async () => { for (let i in {x:1}) { await 1 } })()'],
117118
[ 'for (const i in {x:1}) { await 1 }',
118119
'(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);} })()'],
119123
];
120124

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

0 commit comments

Comments
 (0)