Skip to content

Commit 1ecd407

Browse files
BridgeARMylesBorins
authored andcommitted
repl: align preview with the actual executed code
This adds preview output for input that may not be wrapped. PR-URL: #32154 Reviewed-By: Shelley Vohr <codebytere@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 5080734 commit 1ecd407

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

lib/internal/repl/utils.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
136136
let previewCompletionCounter = 0;
137137
let completionPreview = null;
138138

139+
let wrapped = false;
140+
139141
function getPreviewPos() {
140142
const displayPos = repl._getDisplayPos(`${repl._prompt}${repl.line}`);
141143
const cursorPos = repl.line.length !== repl.cursor ?
@@ -244,8 +246,9 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
244246
function getInputPreview(input, callback) {
245247
// For similar reasons as `defaultEval`, wrap expressions starting with a
246248
// curly brace with parenthesis.
247-
if (input.startsWith('{') && !input.endsWith(';')) {
249+
if (input.startsWith('{') && !input.endsWith(';') && !wrapped) {
248250
input = `(${input})`;
251+
wrapped = true;
249252
}
250253
sendInspectorCommand((session) => {
251254
session.post('Runtime.evaluate', {
@@ -329,13 +332,19 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
329332
return;
330333
}
331334

332-
getInputPreview(line, (error, inspected) => {
335+
const inputPreviewCallback = (error, inspected) => {
336+
if (inspected === null) {
337+
return;
338+
}
339+
340+
wrapped = false;
341+
333342
// Ignore the output if the value is identical to the current line and the
334343
// former preview is not identical to this preview.
335-
if ((line === inspected && lastInputPreview !== inspected) ||
336-
inspected === null) {
344+
if (line === inspected && lastInputPreview !== inspected) {
337345
return;
338346
}
347+
339348
if (error) {
340349
debug('Error while generating preview', error);
341350
return;
@@ -386,7 +395,13 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
386395
repl.output.write(`\n${result}`);
387396
cursorTo(repl.output, cursorPos.cols);
388397
moveCursor(repl.output, 0, -rows - 1);
389-
});
398+
};
399+
400+
getInputPreview(line, inputPreviewCallback);
401+
if (wrapped) {
402+
getInputPreview(line, inputPreviewCallback);
403+
}
404+
wrapped = false;
390405
};
391406

392407
// -------------------------------------------------------------------------//

test/parallel/test-repl-preview.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,13 @@ async function tests(options) {
125125
'\x1B[90m{ a: true }\x1B[39m\x1B[20G\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
126126
'\x1B[90mtrue\x1B[39m\x1B[21G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
127127
'\x1B[33mtrue\x1B[39m',
128-
'\x1B[1G\x1B[0Jrepl > \x1B[8G']
128+
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
129+
['{};1', [2, 4], '\x1B[33m1\x1B[39m',
130+
'{};1',
131+
'\x1B[90m1\x1B[39m\x1B[12G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
132+
'\x1B[33m1\x1B[39m',
133+
'\x1B[1G\x1B[0Jrepl > \x1B[8G'
134+
]
129135
];
130136

131137
const hasPreview = repl.terminal &&

test/parallel/test-repl.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,28 @@ const errorTests = [
457457
/'thefourtheye'/
458458
]
459459
},
460+
// Check for wrapped objects.
461+
{
462+
send: '{ a: 1 }.a', // ({ a: 1 }.a);
463+
expect: '1'
464+
},
465+
{
466+
send: '{ a: 1 }.a;', // { a: 1 }.a;
467+
expect: [
468+
kSource,
469+
kArrow,
470+
'',
471+
/^Uncaught SyntaxError: /
472+
]
473+
},
474+
{
475+
send: '{ a: 1 }["a"] === 1', // ({ a: 1 }['a'] === 1);
476+
expect: 'true'
477+
},
478+
{
479+
send: '{ a: 1 }["a"] === 1;', // { a: 1 }; ['a'] === 1;
480+
expect: 'false'
481+
},
460482
// Empty lines in the REPL should be allowed
461483
{
462484
send: '\n\r\n\r\n',

0 commit comments

Comments
 (0)