GeezerVison is showing github.com — repl: fix repl crashing on variable declarations without init · nodejs/node@467dbd3
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/repl.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1735,6 +1735,10 @@ function findExpressionCompleteTarget(code) { |
1735 | 1735 | // what we can potentially complete on, so let's re-run the function's logic on that |
1736 | 1736 | if (lastBodyStatement.type === 'VariableDeclaration') { |
1737 | 1737 | const lastDeclarationInit = lastBodyStatement.declarations.at(-1).init; |
| 1738 | + if (!lastDeclarationInit) { |
| 1739 | + // If there is no initialization we can simply return |
| 1740 | + return null; |
| 1741 | + } |
1738 | 1742 | const lastDeclarationInitCode = code.slice(lastDeclarationInit.start, lastDeclarationInit.end); |
1739 | 1743 | return findExpressionCompleteTarget(lastDeclarationInitCode); |
1740 | 1744 | } |
|
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-repl-tab-complete.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -50,6 +50,12 @@ function prepareREPL() { |
50 | 50 | } |
51 | 51 |
|
52 | 52 | 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 | + |
53 | 59 | it('does not break in an object literal', () => { |
54 | 60 | const { replServer, input } = prepareREPL(); |
55 | 61 |
|
|