repl: fix multiline history editing string order

PR-URL: https://github.com/nodejs/node/pull/57874
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Giovanni Bucci 2025-04-16 08:01:38 -07:00 committed by GitHub
parent 5077ea4149
commit af85f3f169
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 23 deletions

View file

@ -467,9 +467,15 @@ class Interface extends InterfaceConstructor {
}
// Convert newlines to a consistent format for history storage
[kNormalizeHistoryLineEndings](line, from, to) {
[kNormalizeHistoryLineEndings](line, from, to, reverse = true) {
// Multiline history entries are saved reversed
if (StringPrototypeIncludes(line, '\r')) {
// History is structured with the newest entries at the top
// and the oldest at the bottom. Multiline histories, however, only occupy
// one line in the history file. When loading multiline history with
// an old node binary, the history will be saved in the old format.
// This is why we need to reverse the multilines.
// Reversing the multilines is necessary when adding / editing and displaying them
if (reverse) {
// First reverse the lines for proper order, then convert separators
return ArrayPrototypeJoin(
ArrayPrototypeReverse(StringPrototypeSplit(line, from)),
@ -488,7 +494,7 @@ class Interface extends InterfaceConstructor {
// If the trimmed line is empty then return the line
if (StringPrototypeTrim(this.line).length === 0) return this.line;
const normalizedLine = this[kNormalizeHistoryLineEndings](this.line, '\n', '\r');
const normalizedLine = this[kNormalizeHistoryLineEndings](this.line, '\n', '\r', false);
if (this.history.length === 0 || this.history[0] !== normalizedLine) {
if (this[kLastCommandErrored] && this.historyIndex === 0) {