◐ Shell
clean mode source ↗

repl: fix repl crashing on variable declarations without init · nodejs/node@467dbd3

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

}

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