Skip to content

Commit 8a7c101

Browse files
STRd6ruyadorno
authored andcommitted
repl: don't accumulate excess indentation in .load
When using .load the REPL would accumulate indentation with each line including the indentation from all previous lines. Now it keeps the indentation at the correct level. Fixes: #47673 PR-URL: #49461 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com>
1 parent 4175ea3 commit 8a7c101

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/repl.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ const domainSet = new SafeWeakSet();
204204

205205
const kBufferedCommandSymbol = Symbol('bufferedCommand');
206206
const kContextId = Symbol('contextId');
207+
const kLoadingSymbol = Symbol('loading');
207208

208209
let addedNewListener = false;
209210

@@ -882,7 +883,7 @@ function REPLServer(prompt,
882883
self[kBufferedCommandSymbol] += cmd + '\n';
883884

884885
// code alignment
885-
const matches = self._sawKeyPress ?
886+
const matches = self._sawKeyPress && !self[kLoadingSymbol] ?
886887
RegExpPrototypeExec(/^\s+/, cmd) : null;
887888
if (matches) {
888889
const prefix = matches[0];
@@ -1801,8 +1802,10 @@ function defineDefaultCommands(repl) {
18011802
const stats = fs.statSync(file);
18021803
if (stats && stats.isFile()) {
18031804
_turnOnEditorMode(this);
1805+
this[kLoadingSymbol] = true;
18041806
const data = fs.readFileSync(file, 'utf8');
18051807
this.write(data);
1808+
this[kLoadingSymbol] = false;
18061809
_turnOffEditorMode(this);
18071810
this.write('\n');
18081811
} else {

test/parallel/test-repl-save-load.js

+5
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,14 @@ testMe.complete('inner.o', common.mustSucceed((data) => {
6767
// Clear the REPL.
6868
putIn.run(['.clear']);
6969

70+
testMe._sawKeyPress = true;
7071
// Load the file back in.
7172
putIn.run([`.load ${saveFileName}`]);
7273

74+
// Make sure loading doesn't insert extra indentation
75+
// https://github.com/nodejs/node/issues/47673
76+
assert.strictEqual(testMe.line, '');
77+
7378
// Make sure that the REPL data is "correct".
7479
testMe.complete('inner.o', common.mustSucceed((data) => {
7580
assert.deepStrictEqual(data, works);

0 commit comments

Comments
 (0)