Skip to content

Commit

Permalink
repl: force editorMode in .load
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
lance authored and MylesBorins committed Sep 12, 2017
1 parent 72aae04 commit eee2aa6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1239,13 +1239,16 @@ function defineDefaultCommands(repl) {
try {
var stats = fs.statSync(file);
if (stats && stats.isFile()) {
this.editorMode = true;
REPLServer.super_.prototype.setPrompt.call(this, '');
var data = fs.readFileSync(file, 'utf8');
var lines = data.split('\n');
this.displayPrompt();
for (var n = 0; n < lines.length; n++) {
if (lines[n])
this.write(`${lines[n]}\n`);
}
this.turnOffEditorMode();
this.write('\n');
} else {
this.outputStream.write('Failed to load:' + file +
' is not a valid file\n');
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/repl-load-multiline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const getLunch = () =>
placeOrder('tacos')
.then(eat);

const placeOrder = (order) => Promise.resolve(order);
const eat = (food) => '<nom nom nom>';
38 changes: 38 additions & 0 deletions test/parallel/test-repl-load-multiline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
const repl = require('repl');

const command = `.load ${fixtures.path('repl-load-multiline.js')}`;
const terminalCode = '\u001b[1G\u001b[0J \u001b[1G';
const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g');

const expected = `${command}
const getLunch = () =>
placeOrder('tacos')
.then(eat);
const placeOrder = (order) => Promise.resolve(order);
const eat = (food) => '<nom nom nom>';
undefined
`;

let accum = '';

const inputStream = new common.ArrayStream();
const outputStream = new common.ArrayStream();

outputStream.write = (data) => accum += data.replace('\r', '');

const r = repl.start({
prompt: '',
input: inputStream,
output: outputStream,
terminal: true,
useColors: false
});

r.write(`${command}\n`);
assert.strictEqual(accum.replace(terminalCodeRegex, ''), expected);
r.close();

0 comments on commit eee2aa6

Please sign in to comment.