Skip to content

Commit 049664b

Browse files
repl: fix repl crashing on variable declarations without init
PR-URL: #59032 Fixes: #59029 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matthew Aitken <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a472745 commit 049664b

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/repl.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,10 @@ function findExpressionCompleteTarget(code) {
17351735
// what we can potentially complete on, so let's re-run the function's logic on that
17361736
if (lastBodyStatement.type === 'VariableDeclaration') {
17371737
const lastDeclarationInit = lastBodyStatement.declarations.at(-1).init;
1738+
if (!lastDeclarationInit) {
1739+
// If there is no initialization we can simply return
1740+
return null;
1741+
}
17381742
const lastDeclarationInitCode = code.slice(lastDeclarationInit.start, lastDeclarationInit.end);
17391743
return findExpressionCompleteTarget(lastDeclarationInitCode);
17401744
}

test/parallel/test-repl-tab-complete.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ function prepareREPL() {
5050
}
5151

5252
describe('REPL tab completion (core functionality)', () => {
53+
it('does not break with variable declarations without an initialization', () => {
54+
const { replServer } = prepareREPL();
55+
replServer.complete('let a', getNoResultsFunction());
56+
replServer.close();
57+
});
58+
5359
it('does not break in an object literal', () => {
5460
const { replServer, input } = prepareREPL();
5561

0 commit comments

Comments
 (0)