summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDon Jayamanne <don.jayamanne@yahoo.com>2021-08-14 17:01:28 -0700
committerGuy Bedford <guybedford@gmail.com>2021-08-19 09:14:31 -0700
commit3fe92a2554e2b1edc8304a4063fe92138d008336 (patch)
tree432a6580081a2e45ad1f8fc5f8b69366165f28ac
parent7ca38f05a023666274569343f128c5aed81599f3 (diff)
downloadnode-new-3fe92a2554e2b1edc8304a4063fe92138d008336.tar.gz
repl: fix tla function hoisting
PR-URL: https://github.com/nodejs/node/pull/39745 Fixes: https://github.com/nodejs/node/issues/39744 Reviewed-By: Guy Bedford <guybedford@gmail.com>
-rw-r--r--lib/internal/repl/await.js2
-rw-r--r--test/parallel/test-repl-preprocess-top-level-await.js8
2 files changed, 7 insertions, 3 deletions
diff --git a/lib/internal/repl/await.js b/lib/internal/repl/await.js
index e36fa5bfd7..f28a7ea412 100644
--- a/lib/internal/repl/await.js
+++ b/lib/internal/repl/await.js
@@ -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}; `
diff --git a/test/parallel/test-repl-preprocess-top-level-await.js b/test/parallel/test-repl-preprocess-top-level-await.js
index 3ec4da7e8f..93d3d79a87 100644
--- a/test/parallel/test-repl-preprocess-top-level-await.js
+++ b/test/parallel/test-repl-preprocess-top-level-await.js
@@ -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; }',
@@ -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) {