Skip to content

Commit eee2aa6

Browse files
lanceMylesBorins
authored andcommitted
repl: force editorMode in .load
The `.load` command would fail with any file that contains multiline `.` operator expressions. This was particularly noticeable when chaining promises or multi-line arrow expressions. This change Forces the REPL to be in `editorMode` while loading a file from disk using the `.load` command. Fixes: #14022 PR-URL: #14861 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 72aae04 commit eee2aa6

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

lib/repl.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1239,13 +1239,16 @@ function defineDefaultCommands(repl) {
12391239
try {
12401240
var stats = fs.statSync(file);
12411241
if (stats && stats.isFile()) {
1242+
this.editorMode = true;
1243+
REPLServer.super_.prototype.setPrompt.call(this, '');
12421244
var data = fs.readFileSync(file, 'utf8');
12431245
var lines = data.split('\n');
1244-
this.displayPrompt();
12451246
for (var n = 0; n < lines.length; n++) {
12461247
if (lines[n])
12471248
this.write(`${lines[n]}\n`);
12481249
}
1250+
this.turnOffEditorMode();
1251+
this.write('\n');
12491252
} else {
12501253
this.outputStream.write('Failed to load:' + file +
12511254
' is not a valid file\n');

test/fixtures/repl-load-multiline.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const getLunch = () =>
2+
placeOrder('tacos')
3+
.then(eat);
4+
5+
const placeOrder = (order) => Promise.resolve(order);
6+
const eat = (food) => '<nom nom nom>';
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
const common = require('../common');
3+
const fixtures = require('../common/fixtures');
4+
const assert = require('assert');
5+
const repl = require('repl');
6+
7+
const command = `.load ${fixtures.path('repl-load-multiline.js')}`;
8+
const terminalCode = '\u001b[1G\u001b[0J \u001b[1G';
9+
const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g');
10+
11+
const expected = `${command}
12+
const getLunch = () =>
13+
placeOrder('tacos')
14+
.then(eat);
15+
const placeOrder = (order) => Promise.resolve(order);
16+
const eat = (food) => '<nom nom nom>';
17+
18+
undefined
19+
`;
20+
21+
let accum = '';
22+
23+
const inputStream = new common.ArrayStream();
24+
const outputStream = new common.ArrayStream();
25+
26+
outputStream.write = (data) => accum += data.replace('\r', '');
27+
28+
const r = repl.start({
29+
prompt: '',
30+
input: inputStream,
31+
output: outputStream,
32+
terminal: true,
33+
useColors: false
34+
});
35+
36+
r.write(`${command}\n`);
37+
assert.strictEqual(accum.replace(terminalCodeRegex, ''), expected);
38+
r.close();

0 commit comments

Comments
 (0)