From c6d20a034d5574706c43e4e75399e4b62bef6069 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Wed, 25 Sep 2024 04:51:11 -0400 Subject: [PATCH] repl: catch `\v` and `\r` in new-line detection PR-URL: https://github.com/nodejs/node/pull/54512 Reviewed-By: Ruben Bridgewater Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: LiviaMedeiros Reviewed-By: Antoine du Hamel --- lib/internal/repl/utils.js | 6 ++--- test/parallel/test-repl-preview-newlines.js | 29 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-repl-preview-newlines.js diff --git a/lib/internal/repl/utils.js b/lib/internal/repl/utils.js index ce0caf6d2a3..27e1011ec9d 100644 --- a/lib/internal/repl/utils.js +++ b/lib/internal/repl/utils.js @@ -436,9 +436,9 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) { // Line breaks are very rare and probably only occur in case of error // messages with line breaks. - const lineBreakPos = StringPrototypeIndexOf(inspected, '\n'); - if (lineBreakPos !== -1) { - inspected = `${StringPrototypeSlice(inspected, 0, lineBreakPos)}`; + const lineBreakMatch = RegExpPrototypeExec(/[\r\n\v]/, inspected); + if (lineBreakMatch !== null) { + inspected = `${StringPrototypeSlice(inspected, 0, lineBreakMatch.index)}`; } const result = repl.useColors ? diff --git a/test/parallel/test-repl-preview-newlines.js b/test/parallel/test-repl-preview-newlines.js new file mode 100644 index 00000000000..02d294032ac --- /dev/null +++ b/test/parallel/test-repl-preview-newlines.js @@ -0,0 +1,29 @@ +'use strict'; + +const common = require('../common'); +const ArrayStream = require('../common/arraystream'); +const assert = require('assert'); +const repl = require('repl'); + +common.skipIfInspectorDisabled(); + +const inputStream = new ArrayStream(); +const outputStream = new ArrayStream(); +repl.start({ + input: inputStream, + output: outputStream, + useGlobal: false, + terminal: true, + useColors: true +}); + +let output = ''; +outputStream.write = (chunk) => output += chunk; + +for (const char of ['\\n', '\\v', '\\r']) { + inputStream.emit('data', `"${char}"()`); + // Make sure the output is on a single line + assert.strictEqual(output, `"${char}"()\n\x1B[90mTypeError: "\x1B[39m\x1B[9G\x1B[1A`); + inputStream.run(['']); + output = ''; +}