Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions lib/internal/repl/await.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,22 +221,26 @@ function processTopLevelAwait(src) {
return null;
}

const last = body.body[body.body.length - 1];
if (last.type === 'ExpressionStatement') {
// For an expression statement of the form
// ( expr ) ;
// ^^^^^^^^^^ // last
// ^^^^ // last.expression
//
// We do not want the left parenthesis before the `return` keyword;
// therefore we prepend the `return (` to `last`.
//
// On the other hand, we do not want the right parenthesis after the
// semicolon. Since there can only be more right parentheses between
// last.expression.end and the semicolon, appending one more to
// last.expression should be fine.
state.prepend(last, 'return (');
state.append(last.expression, ')');
for (let i = body.body.length - 1; i >= 0; i--) {
const node = body.body[i];
if (node.type === 'EmptyStatement') continue;
if (node.type === 'ExpressionStatement') {
// For an expression statement of the form
// ( expr ) ;
// ^^^^^^^^^^ // node
// ^^^^ // node.expression
//
// We do not want the left parenthesis before the `return` keyword;
// therefore we prepend the `return (` to `node`.
//
// On the other hand, we do not want the right parenthesis after the
// semicolon. Since there can only be more right parentheses between
// node.expression.end and the semicolon, appending one more to
// node.expression should be fine.
state.prepend(node, 'return (');
state.append(node.expression, ')');
}
break;
}

return (
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-repl-preprocess-top-level-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const testCases = [
`(async () => { return (await ${surrogate}) })()` ],
[ 'await 0;',
'(async () => { return (await 0); })()' ],
[ 'await 0;;;',
'(async () => { return (await 0);;; })()' ],
[ `await ${surrogate};`,
`(async () => { return (await ${surrogate}); })()` ],
[ `await ${surrogate};`,
Expand Down