Skip to content

Commit 467dbd3

Browse files
dario-piotrowicztargos
authored andcommitted
repl: fix repl crashing on variable declarations without init
PR-URL: #59032Fixes: #59029 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matthew Aitken <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9d02960 commit 467dbd3

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
constlastDeclarationInit=lastBodyStatement.declarations.at(-1).init;
1738+
if(!lastDeclarationInit){
1739+
// If there is no initialization we can simply return
1740+
returnnull;
1741+
}
17381742
constlastDeclarationInitCode=code.slice(lastDeclarationInit.start,lastDeclarationInit.end);
17391743
returnfindExpressionCompleteTarget(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)