From e9574f3009a6d9ea23b4de30ff4e8fceeed33a76 Mon Sep 17 00:00:00 2001 From: Kohei Ueno Date: Tue, 28 Jun 2022 19:10:50 +0900 Subject: [PATCH] readline: fix to not access a property on an undefined value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/43543 Reviewed-By: Antoine du Hamel Reviewed-By: Tobias Nießen Reviewed-By: Yongsheng Zhang Reviewed-By: James M Snell --- lib/internal/readline/utils.js | 3 +++ .../test-repl-tab-complete-on-editor-mode.js | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 test/parallel/test-repl-tab-complete-on-editor-mode.js diff --git a/lib/internal/readline/utils.js b/lib/internal/readline/utils.js index 7d1d93b3a98913..83609b71fb5974 100644 --- a/lib/internal/readline/utils.js +++ b/lib/internal/readline/utils.js @@ -365,6 +365,9 @@ function* emitKeys(stream) { // This runs in O(n log n). function commonPrefix(strings) { + if (strings.length === 0) { + return ''; + } if (strings.length === 1) { return strings[0]; } diff --git a/test/parallel/test-repl-tab-complete-on-editor-mode.js b/test/parallel/test-repl-tab-complete-on-editor-mode.js new file mode 100644 index 00000000000000..610724de2a2844 --- /dev/null +++ b/test/parallel/test-repl-tab-complete-on-editor-mode.js @@ -0,0 +1,21 @@ +'use strict'; + +require('../common'); +const ArrayStream = require('../common/arraystream'); +const repl = require('repl'); + +const stream = new ArrayStream(); +const replServer = repl.start({ + input: stream, + output: stream, + terminal: true, +}); + +// Editor mode +replServer.write('.editor\n'); + +// Regression test for https://github.com/nodejs/node/issues/43528 +replServer.write('a'); +replServer.write(null, { name: 'tab' }); // Should not throw + +replServer.close();