diff --git a/lib/internal/repl/utils.js b/lib/internal/repl/utils.js index 8405e6758fc7b7..e5bd406544fccd 100644 --- a/lib/internal/repl/utils.js +++ b/lib/internal/repl/utils.js @@ -365,7 +365,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) { }, () => callback(new ERR_INSPECTOR_NOT_AVAILABLE())); } - const showPreview = () => { + const showPreview = (noCompletion = false) => { // Prevent duplicated previews after a refresh. if (inputPreview !== null || !repl.isCompletionEnabled) { return; @@ -382,7 +382,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) { // Add the autocompletion preview. const insertPreview = false; - showCompletionPreview(repl.line, insertPreview); + !noCompletion && showCompletionPreview(repl.line, insertPreview); // Do not preview if the command is buffered. if (repl[bufferSymbol]) { diff --git a/lib/repl.js b/lib/repl.js index 3875858871ebfb..0ef3f7835882bc 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -993,9 +993,7 @@ function REPLServer(prompt, clearPreview(key); if (!reverseSearch(d, key)) { ttyWrite(d, key); - if (key.name !== 'escape') { - showPreview(); - } + showPreview(key.name === 'escape'); } return; } diff --git a/test/parallel/test-repl-history-navigation.js b/test/parallel/test-repl-history-navigation.js index 39ccc3732c3d82..a9b09839625527 100644 --- a/test/parallel/test-repl-history-navigation.js +++ b/test/parallel/test-repl-history-navigation.js @@ -614,6 +614,24 @@ const tests = [ ], clean: false }, + { + // test that preview should not be removed when pressing ESCAPE key + env: { NODE_REPL_HISTORY: defaultHistoryPath }, + skip: !process.features.inspector, + test: [ + `1+1`, + ESCAPE, + ENTER, + ], + expected: [ + prompt, ...'1+1', + '\n// 2', + '\n// 2', + '2\n', + prompt, + ], + clean: false + }, ]; const numtests = tests.length; @@ -633,9 +651,9 @@ function cleanupTmpFile() { function runTest() { const opts = tests.shift(); if (!opts) return; // All done - + const { expected, skip } = opts; - + // Test unsupported on platform. if (skip) { setImmediate(runTestWrap, true);