Skip to content

Commit aaab108

Browse files
cjihrigrvagg
authored andcommitted
repl: attach location info to syntax errors
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: #2762 Refs: #3411 Refs: #3784 PR-URL: #4013 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent e68ea16 commit aaab108

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/repl.js

+1
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ function REPLServer(prompt,
274274
self._domain.on('error', function(e) {
275275
debug('domain error');
276276
const top = replMap.get(self);
277+
util.decorateErrorStack(e);
277278
top.outputStream.write((e.stack || e) + '\n');
278279
top.lineParser.reset();
279280
top.bufferedCommand = '';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const path = require('path');
6+
const repl = require('repl');
7+
const util = require('util');
8+
let found = false;
9+
10+
process.on('exit', () => {
11+
assert.strictEqual(found, true);
12+
});
13+
14+
// A stream to push an array into a REPL
15+
function ArrayStream() {
16+
this.run = function(data) {
17+
data.forEach(line => {
18+
this.emit('data', line + '\n');
19+
});
20+
};
21+
}
22+
util.inherits(ArrayStream, require('stream').Stream);
23+
ArrayStream.prototype.readable = true;
24+
ArrayStream.prototype.writable = true;
25+
ArrayStream.prototype.resume = function() {};
26+
ArrayStream.prototype.write = function(output) {
27+
if (/var foo bar;/.test(output))
28+
found = true;
29+
};
30+
31+
const putIn = new ArrayStream();
32+
const testMe = repl.start('', putIn);
33+
let file = path.resolve(__dirname, '../fixtures/syntax/bad_syntax');
34+
35+
if (common.isWindows)
36+
file = file.replace(/\\/g, '\\\\');
37+
38+
putIn.run(['.clear']);
39+
putIn.run([`require('${file}');`]);

0 commit comments

Comments
 (0)