Skip to content

Commit

Permalink
repl: catch \v and \r in new-line detection
Browse files Browse the repository at this point in the history
PR-URL: #54512
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
Reviewed-By: LiviaMedeiros <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
RedYetiDev authored and targos committed Oct 4, 2024
1 parent 111261e commit 538b1eb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/internal/repl/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-repl-preview-newlines.js
Original file line number Diff line number Diff line change
@@ -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 = '';
}

0 comments on commit 538b1eb

Please sign in to comment.