Skip to content

Commit 02b4684

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 Backport-PR-URL: #15775 PR-URL: #14861 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent d8f5637 commit 02b4684

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

lib/repl.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1301,15 +1301,16 @@ function defineDefaultCommands(repl) {
13011301
try {
13021302
var stats = fs.statSync(file);
13031303
if (stats && stats.isFile()) {
1304-
var self = this;
1304+
this.editorMode = true;
1305+
REPLServer.super_.prototype.setPrompt.call(this, '');
13051306
var data = fs.readFileSync(file, 'utf8');
13061307
var lines = data.split('\n');
1307-
this.displayPrompt();
1308-
lines.forEach(function(line) {
1309-
if (line) {
1310-
self.write(line + '\n');
1311-
}
1312-
});
1308+
for (var n = 0; n < lines.length; n++) {
1309+
if (lines[n])
1310+
this.write(`${lines[n]}\n`);
1311+
}
1312+
this.turnOffEditorMode();
1313+
this.write('\n');
13131314
} else {
13141315
this.outputStream.write('Failed to load:' + file +
13151316
' 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>';
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
const common = require('../common');
3+
const path = require('path');
4+
const fixtures = common.fixturesDir;
5+
const assert = require('assert');
6+
const repl = require('repl');
7+
8+
const command = `.load ${path.join(fixtures, 'repl-load-multiline.js')}`;
9+
const terminalCode = '\u001b[1G\u001b[0J \u001b[1G';
10+
const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g');
11+
12+
const expected = `${command}
13+
const getLunch = () =>
14+
placeOrder('tacos')
15+
.then(eat);
16+
const placeOrder = (order) => Promise.resolve(order);
17+
const eat = (food) => '<nom nom nom>';
18+
19+
undefined
20+
`;
21+
22+
let accum = '';
23+
24+
const inputStream = new common.ArrayStream();
25+
const outputStream = new common.ArrayStream();
26+
27+
outputStream.write = (data) => accum += data.replace('\r', '');
28+
29+
const r = repl.start({
30+
prompt: '',
31+
input: inputStream,
32+
output: outputStream,
33+
terminal: true,
34+
useColors: false
35+
});
36+
37+
r.write(`${command}\n`);
38+
assert.strictEqual(accum.replace(terminalCodeRegex, ''), expected);
39+
r.close();

0 commit comments

Comments
 (0)