Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: Don't accumulate excess indentation in .load #49461

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
  • Loading branch information
STRd6 committed Sep 7, 2023
commit 3385323b7b898446263b1dd2a3dc19fe0b4dca28
5 changes: 4 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ const domainSet = new SafeWeakSet();

const kBufferedCommandSymbol = Symbol('bufferedCommand');
const kContextId = Symbol('contextId');
const kLoadingSymbol = Symbol('loading');

let addedNewListener = false;

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-repl-save-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down