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
  • Loading branch information
avivkeller committed Aug 23, 2024
1 parent e70bd47 commit eef9462
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';

require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert');
const repl = require('repl');

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;

const testChars = ['\\n', '\\v', '\\r'];

for (const test of testChars) {
inputStream.emit('data', `"${test}"()`);
// Make sure the output is on a single line
assert.strictEqual(output, `"${test}"()\n\x1B[90mTypeError: "\x1B[39m\x1B[9G\x1B[1A`);
inputStream.run(['']);
output = '';
}

0 comments on commit eef9462

Please sign in to comment.