Skip to content

Commit df02a83

Browse files
committed
repl: fix crash when bare 'import' is typed
1 parent 2e3daf6 commit df02a83

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

lib/repl.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,12 @@ writer.options = { ...inspect.defaultOptions, showProxy: true };
250250
// Converts static import statement to dynamic import statement
251251
const toDynamicImport = (codeLine) => {
252252
let dynamicImportStatement = '';
253-
const ast = acornParse(codeLine, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' });
253+
let ast;
254+
try {
255+
ast = acornParse(codeLine, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' });
256+
} catch {
257+
return codeLine;
258+
}
254259
acornWalk.ancestor(ast, {
255260
ImportDeclaration(node) {
256261
const awaitDynamicImport = `await import(${JSONStringify(node.source.value)});`;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
const common = require('../common');
3+
const child_process = require('child_process');
4+
const assert = require('assert');
5+
6+
// Regression test for https://github.com/nodejs/node/issues/63551:
7+
// Typing a bare `import` keyword in the REPL must not crash the process.
8+
const proc = child_process.spawn(process.execPath, ['-i']);
9+
proc.on('error', common.mustNotCall());
10+
proc.on('exit', common.mustCall((code) => {
11+
assert.strictEqual(code, 0);
12+
}));
13+
proc.stdin.write('import\n.exit\n');

0 commit comments

Comments
 (0)