Skip to content

Commit

Permalink
repl: fix stack trace column number in strict mode
Browse files Browse the repository at this point in the history
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: #5416
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
  • Loading branch information
princejwesley authored and Myles Borins committed Mar 30, 2016
1 parent af0aba4 commit 54a6306
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = '';
Expand Down
23 changes: 20 additions & 3 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -369,12 +385,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() {
Expand Down

0 comments on commit 54a6306

Please sign in to comment.