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: https://github.com/nodejs/node/issues/14022 PR-URL: https://github.com/nodejs/node/pull/14861 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Refael Ackermann <refack@gmail.com>
This commit is contained in:
parent
2ef0f007a4
commit
4bd44c11a5
@ -1254,13 +1254,16 @@ function defineDefaultCommands(repl) {
|
|||||||
try {
|
try {
|
||||||
var stats = fs.statSync(file);
|
var stats = fs.statSync(file);
|
||||||
if (stats && stats.isFile()) {
|
if (stats && stats.isFile()) {
|
||||||
|
this.editorMode = true;
|
||||||
|
REPLServer.super_.prototype.setPrompt.call(this, '');
|
||||||
var data = fs.readFileSync(file, 'utf8');
|
var data = fs.readFileSync(file, 'utf8');
|
||||||
var lines = data.split('\n');
|
var lines = data.split('\n');
|
||||||
this.displayPrompt();
|
|
||||||
for (var n = 0; n < lines.length; n++) {
|
for (var n = 0; n < lines.length; n++) {
|
||||||
if (lines[n])
|
if (lines[n])
|
||||||
this.write(`${lines[n]}\n`);
|
this.write(`${lines[n]}\n`);
|
||||||
}
|
}
|
||||||
|
this.turnOffEditorMode();
|
||||||
|
this.write('\n');
|
||||||
} else {
|
} else {
|
||||||
this.outputStream.write('Failed to load:' + file +
|
this.outputStream.write('Failed to load:' + file +
|
||||||
' is not a valid file\n');
|
' is not a valid file\n');
|
||||||
|
6
test/fixtures/repl-load-multiline.js
vendored
Normal file
6
test/fixtures/repl-load-multiline.js
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
const getLunch = () =>
|
||||||
|
placeOrder('tacos')
|
||||||
|
.then(eat);
|
||||||
|
|
||||||
|
const placeOrder = (order) => Promise.resolve(order);
|
||||||
|
const eat = (food) => '<nom nom nom>';
|
38
test/parallel/test-repl-load-multiline.js
Normal file
38
test/parallel/test-repl-load-multiline.js
Normal file
@ -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();
|
Loading…
x
Reference in New Issue
Block a user