Skip to content

Commit 97d0817

Browse files
Trottrvagg
authored andcommitted
lib: avoid REPL exit on completion error
If a tab completion is attempted on an undefined reference inside of a function, the REPL was exiting without reporting an error or anything else. This change results in the REPL reporting the ReferenceError and continuing. Fixes: #3346 PR-URL: #3358 Reviewed-By: James M Snell <[email protected]>
1 parent ff9ef89 commit 97d0817

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

lib/repl.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const Console = require('console').Console;
3232
const domain = require('domain');
3333
const debug = util.debuglog('repl');
3434

35+
const replMap = new WeakMap();
36+
3537
try {
3638
// hack for require.resolve("./relative") to work properly.
3739
module.filename = path.resolve('repl');
@@ -189,11 +191,12 @@ function REPLServer(prompt,
189191

190192
self._domain.on('error', function(e) {
191193
debug('domain error');
192-
self.outputStream.write((e.stack || e) + '\n');
193-
self._currentStringLiteral = null;
194-
self.bufferedCommand = '';
195-
self.lines.level = [];
196-
self.displayPrompt();
194+
const top = replMap.get(self);
195+
top.outputStream.write((e.stack || e) + '\n');
196+
top._currentStringLiteral = null;
197+
top.bufferedCommand = '';
198+
top.lines.level = [];
199+
top.displayPrompt();
197200
});
198201

199202
if (!input && !output) {
@@ -472,6 +475,7 @@ exports.start = function(prompt,
472475
ignoreUndefined,
473476
replMode);
474477
if (!exports.repl) exports.repl = repl;
478+
replMap.set(repl, repl);
475479
return repl;
476480
};
477481

@@ -601,6 +605,7 @@ REPLServer.prototype.complete = function(line, callback) {
601605
// all this is only profitable if the nested REPL
602606
// does not have a bufferedCommand
603607
if (!magic.bufferedCommand) {
608+
replMap.set(magic, replMap.get(this));
604609
return magic.complete(line, callback);
605610
}
606611
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const util = require('util');
6+
const repl = require('repl');
7+
8+
var referenceErrorCount = 0;
9+
10+
// A stream to push an array into a REPL
11+
function ArrayStream() {
12+
this.run = function(data) {
13+
const self = this;
14+
data.forEach(function(line) {
15+
self.emit('data', line + '\n');
16+
});
17+
};
18+
}
19+
util.inherits(ArrayStream, require('stream').Stream);
20+
ArrayStream.prototype.readable = true;
21+
ArrayStream.prototype.writable = true;
22+
ArrayStream.prototype.resume = function() {};
23+
ArrayStream.prototype.write = function(msg) {
24+
if (msg.startsWith('ReferenceError: ')) {
25+
referenceErrorCount++;
26+
}
27+
};
28+
29+
const putIn = new ArrayStream();
30+
const testMe = repl.start('', putIn);
31+
32+
// https://github.com/nodejs/node/issues/3346
33+
// Tab-completion for an undefined variable inside a function should report a
34+
// ReferenceError.
35+
putIn.run(['.clear']);
36+
putIn.run(['function () {']);
37+
testMe.complete('arguments.');
38+
39+
process.on('exit', function() {
40+
assert.strictEqual(referenceErrorCount, 1);
41+
});

0 commit comments

Comments
 (0)