Skip to content

Commit 68ac0d0

Browse files
committedJul 27, 2016
repl: Use displayErrors for SyntaxError
```js node 🙈 ₹ git:(upstream ⚡ display-error-repl) ./node > var 4; var 4; ^ SyntaxError: Unexpected number > ``` PR-URL: #7589 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rod Vagg <rod@vagg.org>
1 parent 2554549 commit 68ac0d0

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed
 

‎lib/repl.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ function REPLServer(prompt,
254254
}
255255
var script = vm.createScript(code, {
256256
filename: file,
257-
displayErrors: false
257+
displayErrors: true
258258
});
259259
} catch (e) {
260260
debug('parse error %j', code, e);
@@ -298,7 +298,7 @@ function REPLServer(prompt,
298298
try {
299299
try {
300300
const scriptOptions = {
301-
displayErrors: false,
301+
displayErrors: true,
302302
breakOnSigint: self.breakEvalOnSigint
303303
};
304304

@@ -350,7 +350,12 @@ function REPLServer(prompt,
350350
debug('domain error');
351351
const top = replMap.get(self);
352352
internalUtil.decorateErrorStack(e);
353-
if (e.stack && self.replMode === exports.REPL_MODE_STRICT) {
353+
if (e instanceof SyntaxError && e.stack) {
354+
// remove repl:line-number and stack trace
355+
e.stack = e.stack
356+
.replace(/^repl:\d+\r?\n/, '')
357+
.replace(/^\s+at\s.*\n?/gm, '');
358+
} else if (e.stack && self.replMode === exports.REPL_MODE_STRICT) {
354359
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
355360
(_, pre, line) => pre + (line - 1));
356361
}

‎test/parallel/test-repl.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function error_test() {
120120
expect: prompt_unix },
121121
// But passing the same string to eval() should throw
122122
{ client: client_unix, send: 'eval("function test_func() {")',
123-
expect: /^SyntaxError: Unexpected end of input/ },
123+
expect: /\bSyntaxError: Unexpected end of input/ },
124124
// Can handle multiline template literals
125125
{ client: client_unix, send: '`io.js',
126126
expect: prompt_multiline },
@@ -149,35 +149,35 @@ function error_test() {
149149
// invalid input to JSON.parse error is special case of syntax error,
150150
// should throw
151151
{ client: client_unix, send: 'JSON.parse(\'{invalid: \\\'json\\\'}\');',
152-
expect: /^SyntaxError: Unexpected token i/ },
152+
expect: /\bSyntaxError: Unexpected token i/ },
153153
// end of input to JSON.parse error is special case of syntax error,
154154
// should throw
155155
{ client: client_unix, send: 'JSON.parse(\'066\');',
156-
expect: /^SyntaxError: Unexpected number/ },
156+
expect: /\bSyntaxError: Unexpected number/ },
157157
// should throw
158158
{ client: client_unix, send: 'JSON.parse(\'{\');',
159-
expect: /^SyntaxError: Unexpected end of JSON input/ },
159+
expect: /\bSyntaxError: Unexpected end of JSON input/ },
160160
// invalid RegExps are a special case of syntax error,
161161
// should throw
162162
{ client: client_unix, send: '/(/;',
163-
expect: /^SyntaxError: Invalid regular expression\:/ },
163+
expect: /\bSyntaxError: Invalid regular expression\:/ },
164164
// invalid RegExp modifiers are a special case of syntax error,
165165
// should throw (GH-4012)
166166
{ client: client_unix, send: 'new RegExp("foo", "wrong modifier");',
167-
expect: /^SyntaxError: Invalid flags supplied to RegExp constructor/ },
167+
expect: /\bSyntaxError: Invalid flags supplied to RegExp constructor/ },
168168
// strict mode syntax errors should be caught (GH-5178)
169169
{ client: client_unix, send: '(function() { "use strict"; return 0755; })()',
170-
expect: /^SyntaxError: Octal literals are not allowed in strict mode/ },
170+
expect: /\bSyntaxError: Octal literals are not allowed in strict mode/ },
171171
{ client: client_unix, send: '(function(a, a, b) { "use strict"; return a + b + c; })()',
172-
expect: /^SyntaxError: Duplicate parameter name not allowed in this context/ },
172+
expect: /\bSyntaxError: Duplicate parameter name not allowed in this context/ },
173173
{ client: client_unix, send: '(function() { "use strict"; with (this) {} })()',
174-
expect: /^SyntaxError: Strict mode code may not include a with statement/ },
174+
expect: /\bSyntaxError: Strict mode code may not include a with statement/ },
175175
{ client: client_unix, send: '(function() { "use strict"; var x; delete x; })()',
176-
expect: /^SyntaxError: Delete of an unqualified identifier in strict mode/ },
176+
expect: /\bSyntaxError: Delete of an unqualified identifier in strict mode/ },
177177
{ client: client_unix, send: '(function() { "use strict"; eval = 17; })()',
178-
expect: /^SyntaxError: Unexpected eval or arguments in strict mode/ },
178+
expect: /\bSyntaxError: Unexpected eval or arguments in strict mode/ },
179179
{ client: client_unix, send: '(function() { "use strict"; if (true) function f() { } })()',
180-
expect: /^SyntaxError: In strict mode code, functions can only be declared at top level or inside a block./ },
180+
expect: /\bSyntaxError: In strict mode code, functions can only be declared at top level or inside a block./ },
181181
// Named functions can be used:
182182
{ client: client_unix, send: 'function blah() { return 1; }',
183183
expect: prompt_unix },
@@ -228,7 +228,7 @@ function error_test() {
228228
expect: 'Invalid REPL keyword\n' + prompt_unix },
229229
// fail when we are not inside a String and a line continuation is used
230230
{ client: client_unix, send: '[] \\',
231-
expect: /^SyntaxError: Invalid or unexpected token/ },
231+
expect: /\bSyntaxError: Invalid or unexpected token/ },
232232
// do not fail when a String is created with line continuation
233233
{ client: client_unix, send: '\'the\\\nfourth\\\neye\'',
234234
expect: prompt_multiline + prompt_multiline +
@@ -327,12 +327,18 @@ function error_test() {
327327
// Illegal token is not recoverable outside string literal, RegExp literal,
328328
// or block comment. https://github.com/nodejs/node/issues/3611
329329
{ client: client_unix, send: 'a = 3.5e',
330-
expect: /^SyntaxError: Invalid or unexpected token/ },
330+
expect: /\bSyntaxError: Invalid or unexpected token/ },
331331
// Mitigate https://github.com/nodejs/node/issues/548
332332
{ client: client_unix, send: 'function name(){ return "node"; };name()',
333333
expect: "'node'\n" + prompt_unix },
334334
{ client: client_unix, send: 'function name(){ return "nodejs"; };name()',
335335
expect: "'nodejs'\n" + prompt_unix },
336+
// Avoid emitting repl:line-number for SyntaxError
337+
{ client: client_unix, send: 'a = 3.5e',
338+
expect: /^(?!repl)/ },
339+
// Avoid emitting stack trace
340+
{ client: client_unix, send: 'a = 3.5e',
341+
expect: /^(?!\s+at\s)/gm },
336342
]);
337343
}
338344

0 commit comments

Comments
 (0)
Please sign in to comment.