diff --git a/lib/internal/repl/await.js b/lib/internal/repl/await.js index e36fa5bfd735b1..f28a7ea412bc3f 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 3ec4da7e8fb72f..93d3d79a87bb08 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) {