Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: catch \v and \r in new-line detection #54512

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = '';
}
Loading