Skip to content

Commit cab0f16

Browse files
committed
repl: reverse the order of multiline histories
This will make it consistent when loading the new history format with an old node binary
1 parent 23f7e47 commit cab0f16

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

lib/internal/readline/interface.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,15 @@ class Interface extends InterfaceConstructor {
461461

462462
// Convert newlines to a consistent format for history storage
463463
[kNormalizeHistoryLineEndings](line, from, to) {
464+
// Multiline history entries are saved reversed
465+
if (StringPrototypeIncludes(line, '\r')) {
466+
// First reverse the lines for proper order, then convert separators
467+
return ArrayPrototypeJoin(
468+
ArrayPrototypeReverse(StringPrototypeSplit(line, from)),
469+
to,
470+
);
471+
}
472+
// For normal cases (saving to history or non-multiline entries)
464473
return StringPrototypeReplaceAll(line, from, to);
465474
}
466475

lib/repl.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const {
5353
ArrayPrototypePop,
5454
ArrayPrototypePush,
5555
ArrayPrototypePushApply,
56+
ArrayPrototypeReverse,
5657
ArrayPrototypeShift,
5758
ArrayPrototypeSlice,
5859
ArrayPrototypeSome,
@@ -969,7 +970,7 @@ function REPLServer(prompt,
969970
const lines = StringPrototypeSplit(self[kBufferedCommandSymbol], '\n');
970971
self.history = ArrayPrototypeSlice(self.history, lines.length);
971972
lines[lines.length - 1] = cmd;
972-
const newHistoryLine = ArrayPrototypeJoin(lines, '\r');
973+
const newHistoryLine = ArrayPrototypeJoin(ArrayPrototypeReverse(lines), '\r');
973974
if (self.history[0] !== newHistoryLine) {
974975
ArrayPrototypeUnshift(self.history, newHistoryLine);
975976
}

test/fixtures/.node_repl_history_multiline

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var d = [ { a: 1, b: 2, }, { a: 3, b: 4, c: [{ a: 1, b: 2 }, { a: 3, b: 4, } ] }]
2-
const c = [ { a: 1, b: 2, }]
3-
`const b = [ 1, 2, 3, 4,]`
4-
a = `I am a multiline stringI can be as long as I want`
1+
] } ] } b: 4, a: 3, { c: [{ a: 1, b: 2 }, b: 4, a: 3, { }, b: 2, a: 1, {var d = [
2+
] } b: 2, a: 1, {const c = [
3+
]` 4, 3, 2, 1,`const b = [
4+
I can be as long as I want`I am a multiline stringa = `

0 commit comments

Comments
 (0)