Skip to content

Commit 344c03b

Browse files
Mesteerytargos
authored andcommitted
repl: skip EmptyStatements and return result with TLA
Fixes: #39932 PR-URL: #40194 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 9fa6dfb commit 344c03b

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

lib/internal/repl/await.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -221,22 +221,26 @@ function processTopLevelAwait(src) {
221221
return null;
222222
}
223223

224-
const last = body.body[body.body.length - 1];
225-
if (last.type === 'ExpressionStatement') {
226-
// For an expression statement of the form
227-
// ( expr ) ;
228-
// ^^^^^^^^^^ // last
229-
// ^^^^ // last.expression
230-
//
231-
// We do not want the left parenthesis before the `return` keyword;
232-
// therefore we prepend the `return (` to `last`.
233-
//
234-
// On the other hand, we do not want the right parenthesis after the
235-
// semicolon. Since there can only be more right parentheses between
236-
// last.expression.end and the semicolon, appending one more to
237-
// last.expression should be fine.
238-
state.prepend(last, 'return (');
239-
state.append(last.expression, ')');
224+
for (let i = body.body.length - 1; i >= 0; i--) {
225+
const node = body.body[i];
226+
if (node.type === 'EmptyStatement') continue;
227+
if (node.type === 'ExpressionStatement') {
228+
// For an expression statement of the form
229+
// ( expr ) ;
230+
// ^^^^^^^^^^ // node
231+
// ^^^^ // node.expression
232+
//
233+
// We do not want the left parenthesis before the `return` keyword;
234+
// therefore we prepend the `return (` to `node`.
235+
//
236+
// On the other hand, we do not want the right parenthesis after the
237+
// semicolon. Since there can only be more right parentheses between
238+
// node.expression.end and the semicolon, appending one more to
239+
// node.expression should be fine.
240+
state.prepend(node, 'return (');
241+
state.append(node.expression, ')');
242+
}
243+
break;
240244
}
241245

242246
return (

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

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const testCases = [
2222
`(async () => { return (await ${surrogate}) })()` ],
2323
[ 'await 0;',
2424
'(async () => { return (await 0); })()' ],
25+
[ 'await 0;;;',
26+
'(async () => { return (await 0);;; })()' ],
2527
[ `await ${surrogate};`,
2628
`(async () => { return (await ${surrogate}); })()` ],
2729
[ `await ${surrogate};`,

0 commit comments

Comments
 (0)