Skip to content

Commit abc2bda

Browse files
committed
fixup! repl: show reference errors during preview
This adds support for `--use-strict`. Signed-off-by: Ruben Bridgewater <[email protected]>
1 parent 00cd4d8 commit abc2bda

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

lib/internal/repl/utils.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
271271
});
272272
}
273273

274+
function isInStrictMode(repl) {
275+
return repl.replMode === REPL_MODE_STRICT || process.execArgv
276+
.map((e) => e.toLowerCase().replace(/_/g, '-'))
277+
.includes('--use-strict');
278+
}
279+
274280
// This returns a code preview for arbitrary input code.
275281
function getInputPreview(input, callback) {
276282
// For similar reasons as `defaultEval`, wrap expressions starting with a
@@ -302,7 +308,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
302308
// Report ReferenceError in case the strict mode is active
303309
// for input that has no completions.
304310
(result.className === 'ReferenceError' &&
305-
(hasCompletions || repl.replMode !== REPL_MODE_STRICT)))) {
311+
(hasCompletions || !isInStrictMode(repl))))) {
306312
callback(null, null);
307313
} else if (result.objectId) {
308314
// The writer options might change and have influence on the inspect

lib/repl.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ function REPLServer(prompt,
366366
}
367367
while (true) {
368368
try {
369-
if (!/^\s*$/.test(code) &&
370-
self.replMode === exports.REPL_MODE_STRICT) {
369+
if (self.replMode === exports.REPL_MODE_STRICT && !/^\s*$/.test(code)) {
371370
// "void 0" keeps the repl from returning "use strict" as the result
372371
// value for statements and declarations that don't return a value.
373372
code = `'use strict'; void 0;\n${code}`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Previews in strict mode should indicate ReferenceErrors.
2+
3+
'use strict';
4+
5+
require('../common');
6+
7+
if (process.argv[2] === 'child') {
8+
const stream = require('stream');
9+
const repl = require('repl');
10+
class ActionStream extends stream.Stream {
11+
readable = true;
12+
run(data) {
13+
this.emit('data', `${data}`);
14+
this.emit('keypress', '', { ctrl: true, name: 'd' });
15+
}
16+
resume() {}
17+
pause() {}
18+
}
19+
20+
repl.start({
21+
input: new ActionStream(),
22+
output: new stream.Writable({
23+
write(chunk, _, next) {
24+
console.log(chunk.toString());
25+
next();
26+
}
27+
}),
28+
useColors: false,
29+
terminal: true
30+
}).inputStream.run('xyz');
31+
} else {
32+
const assert = require('assert');
33+
const { spawnSync } = require('child_process');
34+
35+
const result = spawnSync(
36+
process.execPath,
37+
['--use-strict', `${__filename}`, 'child']
38+
);
39+
40+
assert.match(
41+
result.stdout.toString(),
42+
/\/\/ ReferenceError: xyz is not defined/
43+
);
44+
}

0 commit comments

Comments
 (0)