Skip to content

Commit

Permalink
repl: attach location info to syntax errors
Browse files Browse the repository at this point in the history
Currently, when a file with a syntax error is imported in the
REPL, no information is provided on the error's location. This
commit adds the error's location to the stack trace.

Refs: nodejs#2762
Refs: nodejs#3411
Refs: nodejs#3784
PR-URL: nodejs#4013
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
cjihrig authored and Michael Scovetta committed Apr 2, 2016
1 parent 3a1e7e9 commit 4cc0ab9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ function REPLServer(prompt,
self._domain.on('error', function(e) {
debug('domain error');
const top = replMap.get(self);
util.decorateErrorStack(e);
top.outputStream.write((e.stack || e) + '\n');
top.lineParser.reset();
top.bufferedCommand = '';
Expand Down
39 changes: 39 additions & 0 deletions test/parallel/test-repl-syntax-error-stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const path = require('path');
const repl = require('repl');
const util = require('util');
let found = false;

process.on('exit', () => {
assert.strictEqual(found, true);
});

// A stream to push an array into a REPL
function ArrayStream() {
this.run = function(data) {
data.forEach(line => {
this.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(output) {
if (/var foo bar;/.test(output))
found = true;
};

const putIn = new ArrayStream();
const testMe = repl.start('', putIn);
let file = path.resolve(__dirname, '../fixtures/syntax/bad_syntax');

if (common.isWindows)
file = file.replace(/\\/g, '\\\\');

putIn.run(['.clear']);
putIn.run([`require('${file}');`]);

0 comments on commit 4cc0ab9

Please sign in to comment.