From aaab108dfefcd29c222c1623ffa0645ff4212751 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Tue, 24 Nov 2015 19:55:51 -0500 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/2762 Refs: https://github.com/nodejs/node/issues/3411 Refs: https://github.com/nodejs/node/issues/3784 PR-URL: https://github.com/nodejs/node/pull/4013 Reviewed-By: Ben Noordhuis --- lib/repl.js | 1 + test/parallel/test-repl-syntax-error-stack.js | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 test/parallel/test-repl-syntax-error-stack.js diff --git a/lib/repl.js b/lib/repl.js index 2da336eae8cf93..bd3a75d88e5826 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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 = ''; diff --git a/test/parallel/test-repl-syntax-error-stack.js b/test/parallel/test-repl-syntax-error-stack.js new file mode 100644 index 00000000000000..573059e8d9f34f --- /dev/null +++ b/test/parallel/test-repl-syntax-error-stack.js @@ -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}');`]);