From 2b88523836b30ac06b1832827b8b93e33f2196af Mon Sep 17 00:00:00 2001 From: Prince J Wesley Date: Thu, 25 Feb 2016 00:26:41 +0530 Subject: [PATCH] repl: fix stack trace column number in strict mode On strict mode, "'use strict'; void 0; " is added as prefix in order to prevent "use strict" as the result value for let/const statements. It causes wrong column number in stack trace. PR-URL: https://github.com/nodejs/node/pull/5416 Reviewed-By: Jeremiah Senkpiel Reviewed-By: James M Snell Reviewed-By: Roman Reiss --- lib/repl.js | 6 +++++- test/parallel/test-repl.js | 23 ++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 37607ef0c9d8c1..3beb7e4c892118 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -230,7 +230,7 @@ function REPLServer(prompt, (self.replMode === exports.REPL_MODE_STRICT || retry)) { // "void 0" keeps the repl from returning "use strict" as the // result value for let/const statements. - code = `'use strict'; void 0; ${code}`; + code = `'use strict'; void 0;\n${code}`; } var script = vm.createScript(code, { filename: file, @@ -289,6 +289,10 @@ function REPLServer(prompt, debug('domain error'); const top = replMap.get(self); internalUtil.decorateErrorStack(e); + if (e.stack && self.replMode === exports.REPL_MODE_STRICT) { + e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/, + (_, pre, line) => pre + (line - 1)); + } top.outputStream.write((e.stack || e) + '\n'); top.lineParser.reset(); top.bufferedCommand = ''; diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 539878a9475585..7ec9f1c3637349 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -15,7 +15,7 @@ const prompt_npm = 'npm should be run outside of the ' + 'node repl, in your normal shell.\n' + '(Press Control-D to exit.)\n'; const expect_npm = prompt_npm + prompt_unix; -var server_tcp, server_unix, client_tcp, client_unix, timer; +var server_tcp, server_unix, client_tcp, client_unix, timer, replServer; // absolute path to test/fixtures/a.js @@ -48,9 +48,17 @@ function clean_up() { clearTimeout(timer); } +function strict_mode_error_test() { + send_expect([ + { client: client_unix, send: 'ref = 1', + expect: /^ReferenceError:\sref\sis\snot\sdefined\n\s+at\srepl:1:5/ }, + ]); +} + function error_test() { // The other stuff is done so reuse unix socket var read_buffer = ''; + var run_strict_test = true; client_unix.removeAllListeners('data'); client_unix.on('data', function(data) { @@ -72,6 +80,10 @@ function error_test() { read_buffer = ''; if (client_unix.list && client_unix.list.length > 0) { send_expect(client_unix.list); + } else if (run_strict_test) { + replServer.replMode = repl.REPL_MODE_STRICT; + run_strict_test = false; + strict_mode_error_test(); } else { console.error('End of Error test, running TCP test.'); tcp_test(); @@ -83,6 +95,10 @@ function error_test() { read_buffer = ''; if (client_unix.list && client_unix.list.length > 0) { send_expect(client_unix.list); + } else if (run_strict_test) { + replServer.replMode = repl.REPL_MODE_STRICT; + run_strict_test = false; + strict_mode_error_test(); } else { console.error('End of Error test, running TCP test.\n'); tcp_test(); @@ -381,12 +397,13 @@ function unix_test() { socket.end(); }); - repl.start({ + replServer = repl.start({ prompt: prompt_unix, input: socket, output: socket, useGlobal: true - }).context.message = message; + }); + replServer.context.message = message; }); server_unix.on('listening', function() {