From 3385323b7b898446263b1dd2a3dc19fe0b4dca28 Mon Sep 17 00:00:00 2001 From: Daniel Moore Date: Sat, 2 Sep 2023 10:54:59 -0700 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/47673 --- lib/repl.js | 5 ++++- test/parallel/test-repl-save-load.js | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 2a63050923b198..18a1a6e85a9b12 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -204,6 +204,7 @@ const domainSet = new SafeWeakSet(); const kBufferedCommandSymbol = Symbol('bufferedCommand'); const kContextId = Symbol('contextId'); +const kLoadingSymbol = Symbol('loading'); let addedNewListener = false; @@ -882,7 +883,7 @@ function REPLServer(prompt, self[kBufferedCommandSymbol] += cmd + '\n'; // code alignment - const matches = self._sawKeyPress ? + const matches = self._sawKeyPress && !self[kLoadingSymbol] ? RegExpPrototypeExec(/^\s+/, cmd) : null; if (matches) { const prefix = matches[0]; @@ -1801,8 +1802,10 @@ function defineDefaultCommands(repl) { const stats = fs.statSync(file); if (stats && stats.isFile()) { _turnOnEditorMode(this); + this[kLoadingSymbol] = true; const data = fs.readFileSync(file, 'utf8'); this.write(data); + this[kLoadingSymbol] = false; _turnOffEditorMode(this); this.write('\n'); } else { diff --git a/test/parallel/test-repl-save-load.js b/test/parallel/test-repl-save-load.js index 19b4cf8f755407..365bad8260bfc6 100644 --- a/test/parallel/test-repl-save-load.js +++ b/test/parallel/test-repl-save-load.js @@ -67,9 +67,14 @@ testMe.complete('inner.o', common.mustSucceed((data) => { // Clear the REPL. putIn.run(['.clear']); +testMe._sawKeyPress = true; // Load the file back in. putIn.run([`.load ${saveFileName}`]); +// Make sure loading doesn't insert extra indentation +// https://github.com/nodejs/node/issues/47673 +assert.strictEqual(testMe.line, ''); + // Make sure that the REPL data is "correct". testMe.complete('inner.o', common.mustSucceed((data) => { assert.deepStrictEqual(data, works);