repl: fix repl crashing on variable declarations without init

PR-URL: https://github.com/nodejs/node/pull/59032
Fixes: https://github.com/nodejs/node/issues/59029
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Dario Piotrowicz authored and Michaël Zasso committed 2025-07-17 16:57:13 +02:00
1 parent 9d02960149
commit 467dbd31e6
2 files changed
+10

No files matched your search

+4
View File
@@ -1735,6 +1735,10 @@ function findExpressionCompleteTarget(code) {
// what we can potentially complete on, so let's re-run the function's logic on that
if (lastBodyStatement.type === 'VariableDeclaration') {
const lastDeclarationInit = lastBodyStatement.declarations.at(-1).init;
if (!lastDeclarationInit) {
// If there is no initialization we can simply return
return null;
}
const lastDeclarationInitCode = code.slice(lastDeclarationInit.start, lastDeclarationInit.end);
return findExpressionCompleteTarget(lastDeclarationInitCode);
}
+6
View File
@@ -50,6 +50,12 @@ function prepareREPL() {
}
describe('REPL tab completion (core functionality)', () => {
it('does not break with variable declarations without an initialization', () => {
const { replServer } = prepareREPL();
replServer.complete('let a', getNoResultsFunction());
replServer.close();
});
it('does not break in an object literal', () => {
const { replServer, input } = prepareREPL();