Skip to content

Commit 2b88523

Browse files
princejwesleyFishrock123
authored andcommitted
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: #5416 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io>
1 parent ea8331e commit 2b88523

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/repl.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ function REPLServer(prompt,
230230
(self.replMode === exports.REPL_MODE_STRICT || retry)) {
231231
// "void 0" keeps the repl from returning "use strict" as the
232232
// result value for let/const statements.
233-
code = `'use strict'; void 0; ${code}`;
233+
code = `'use strict'; void 0;\n${code}`;
234234
}
235235
var script = vm.createScript(code, {
236236
filename: file,
@@ -289,6 +289,10 @@ function REPLServer(prompt,
289289
debug('domain error');
290290
const top = replMap.get(self);
291291
internalUtil.decorateErrorStack(e);
292+
if (e.stack && self.replMode === exports.REPL_MODE_STRICT) {
293+
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
294+
(_, pre, line) => pre + (line - 1));
295+
}
292296
top.outputStream.write((e.stack || e) + '\n');
293297
top.lineParser.reset();
294298
top.bufferedCommand = '';

test/parallel/test-repl.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const prompt_npm = 'npm should be run outside of the ' +
1515
'node repl, in your normal shell.\n' +
1616
'(Press Control-D to exit.)\n';
1717
const expect_npm = prompt_npm + prompt_unix;
18-
var server_tcp, server_unix, client_tcp, client_unix, timer;
18+
var server_tcp, server_unix, client_tcp, client_unix, timer, replServer;
1919

2020

2121
// absolute path to test/fixtures/a.js
@@ -48,9 +48,17 @@ function clean_up() {
4848
clearTimeout(timer);
4949
}
5050

51+
function strict_mode_error_test() {
52+
send_expect([
53+
{ client: client_unix, send: 'ref = 1',
54+
expect: /^ReferenceError:\sref\sis\snot\sdefined\n\s+at\srepl:1:5/ },
55+
]);
56+
}
57+
5158
function error_test() {
5259
// The other stuff is done so reuse unix socket
5360
var read_buffer = '';
61+
var run_strict_test = true;
5462
client_unix.removeAllListeners('data');
5563

5664
client_unix.on('data', function(data) {
@@ -72,6 +80,10 @@ function error_test() {
7280
read_buffer = '';
7381
if (client_unix.list && client_unix.list.length > 0) {
7482
send_expect(client_unix.list);
83+
} else if (run_strict_test) {
84+
replServer.replMode = repl.REPL_MODE_STRICT;
85+
run_strict_test = false;
86+
strict_mode_error_test();
7587
} else {
7688
console.error('End of Error test, running TCP test.');
7789
tcp_test();
@@ -83,6 +95,10 @@ function error_test() {
8395
read_buffer = '';
8496
if (client_unix.list && client_unix.list.length > 0) {
8597
send_expect(client_unix.list);
98+
} else if (run_strict_test) {
99+
replServer.replMode = repl.REPL_MODE_STRICT;
100+
run_strict_test = false;
101+
strict_mode_error_test();
86102
} else {
87103
console.error('End of Error test, running TCP test.\n');
88104
tcp_test();
@@ -381,12 +397,13 @@ function unix_test() {
381397
socket.end();
382398
});
383399

384-
repl.start({
400+
replServer = repl.start({
385401
prompt: prompt_unix,
386402
input: socket,
387403
output: socket,
388404
useGlobal: true
389-
}).context.message = message;
405+
});
406+
replServer.context.message = message;
390407
});
391408

392409
server_unix.on('listening', function() {

0 commit comments

Comments
 (0)