Skip to content

Commit

Permalink
repl: align preview with the actual executed code
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
BridgeAR authored and targos committed Apr 28, 2020
1 parent 12b8345 commit 3903aec
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
25 changes: 20 additions & 5 deletions lib/internal/repl/utils.js
Expand Up @@ -136,6 +136,8 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
let previewCompletionCounter = 0;
let completionPreview = null;

let wrapped = false;

function getPreviewPos() {
const displayPos = repl._getDisplayPos(`${repl._prompt}${repl.line}`);
const cursorPos = repl.line.length !== repl.cursor ?
Expand Down Expand Up @@ -244,8 +246,9 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
function getInputPreview(input, callback) {
// For similar reasons as `defaultEval`, wrap expressions starting with a
// curly brace with parenthesis.
if (input.startsWith('{') && !input.endsWith(';')) {
if (input.startsWith('{') && !input.endsWith(';') && !wrapped) {
input = `(${input})`;
wrapped = true;
}
sendInspectorCommand((session) => {
session.post('Runtime.evaluate', {
Expand Down Expand Up @@ -329,13 +332,19 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
return;
}

getInputPreview(line, (error, inspected) => {
const inputPreviewCallback = (error, inspected) => {
if (inspected === null) {
return;
}

wrapped = false;

// Ignore the output if the value is identical to the current line and the
// former preview is not identical to this preview.
if ((line === inspected && lastInputPreview !== inspected) ||
inspected === null) {
if (line === inspected && lastInputPreview !== inspected) {
return;
}

if (error) {
debug('Error while generating preview', error);
return;
Expand Down Expand Up @@ -386,7 +395,13 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
repl.output.write(`\n${result}`);
cursorTo(repl.output, cursorPos.cols);
moveCursor(repl.output, 0, -rows - 1);
});
};

getInputPreview(line, inputPreviewCallback);
if (wrapped) {
getInputPreview(line, inputPreviewCallback);
}
wrapped = false;
};

// -------------------------------------------------------------------------//
Expand Down
8 changes: 7 additions & 1 deletion test/parallel/test-repl-preview.js
Expand Up @@ -125,7 +125,13 @@ async function tests(options) {
'\x1B[90m{ a: true }\x1B[39m\x1B[20G\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
'\x1B[90mtrue\x1B[39m\x1B[21G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[33mtrue\x1B[39m',
'\x1B[1G\x1B[0Jrepl > \x1B[8G']
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
['{};1', [2, 4], '\x1B[33m1\x1B[39m',
'{};1',
'\x1B[90m1\x1B[39m\x1B[12G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[33m1\x1B[39m',
'\x1B[1G\x1B[0Jrepl > \x1B[8G'
]
];

const hasPreview = repl.terminal &&
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-repl.js
Expand Up @@ -457,6 +457,28 @@ const errorTests = [
/'thefourtheye'/
]
},
// Check for wrapped objects.
{
send: '{ a: 1 }.a', // ({ a: 1 }.a);
expect: '1'
},
{
send: '{ a: 1 }.a;', // { a: 1 }.a;
expect: [
kSource,
kArrow,
'',
/^Uncaught SyntaxError: /
]
},
{
send: '{ a: 1 }["a"] === 1', // ({ a: 1 }['a'] === 1);
expect: 'true'
},
{
send: '{ a: 1 }["a"] === 1;', // { a: 1 }; ['a'] === 1;
expect: 'false'
},
// Empty lines in the REPL should be allowed
{
send: '\n\r\n\r\n',
Expand Down

0 comments on commit 3903aec

Please sign in to comment.