Skip to content

Commit

Permalink
repl: skip EmptyStatements and return result with TLA
Browse files Browse the repository at this point in the history
Fixes: #39932

PR-URL: #40194
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
Mesteery authored and targos committed Oct 4, 2021
1 parent 9fa6dfb commit 344c03b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
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

0 comments on commit 344c03b

Please sign in to comment.