Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});