Skip to content

Commit

Permalink
lib: avoid REPL exit on completion error
Browse files Browse the repository at this point in the history
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 <jasnell@gmail.com>
  • Loading branch information
Trott authored and rvagg committed Oct 22, 2015
1 parent ff9ef89 commit 97d0817
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const Console = require('console').Console;
const domain = require('domain');
const debug = util.debuglog('repl');

const replMap = new WeakMap();

try {
// hack for require.resolve("./relative") to work properly.
module.filename = path.resolve('repl');
Expand Down Expand Up @@ -189,11 +191,12 @@ function REPLServer(prompt,

self._domain.on('error', function(e) {
debug('domain error');
self.outputStream.write((e.stack || e) + '\n');
self._currentStringLiteral = null;
self.bufferedCommand = '';
self.lines.level = [];
self.displayPrompt();
const top = replMap.get(self);
top.outputStream.write((e.stack || e) + '\n');
top._currentStringLiteral = null;
top.bufferedCommand = '';
top.lines.level = [];
top.displayPrompt();
});

if (!input && !output) {
Expand Down Expand Up @@ -472,6 +475,7 @@ exports.start = function(prompt,
ignoreUndefined,
replMode);
if (!exports.repl) exports.repl = repl;
replMap.set(repl, repl);
return repl;
};

Expand Down Expand Up @@ -601,6 +605,7 @@ REPLServer.prototype.complete = function(line, callback) {
// all this is only profitable if the nested REPL
// does not have a bufferedCommand
if (!magic.bufferedCommand) {
replMap.set(magic, replMap.get(this));
return magic.complete(line, callback);
}
}
Expand Down
41 changes: 41 additions & 0 deletions test/parallel/test-repl-tab-complete-crash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

require('../common');
const assert = require('assert');
const util = require('util');
const repl = require('repl');

var referenceErrorCount = 0;

// A stream to push an array into a REPL
function ArrayStream() {
this.run = function(data) {
const self = this;
data.forEach(function(line) {
self.emit('data', line + '\n');
});
};
}
util.inherits(ArrayStream, require('stream').Stream);
ArrayStream.prototype.readable = true;
ArrayStream.prototype.writable = true;
ArrayStream.prototype.resume = function() {};
ArrayStream.prototype.write = function(msg) {
if (msg.startsWith('ReferenceError: ')) {
referenceErrorCount++;
}
};

const putIn = new ArrayStream();
const testMe = repl.start('', putIn);

// https://github.com/nodejs/node/issues/3346
// Tab-completion for an undefined variable inside a function should report a
// ReferenceError.
putIn.run(['.clear']);
putIn.run(['function () {']);
testMe.complete('arguments.');

process.on('exit', function() {
assert.strictEqual(referenceErrorCount, 1);
});

0 comments on commit 97d0817

Please sign in to comment.