From f6e2ba4b3c7d0789d79fabd81c24342c5dbcba85 Mon Sep 17 00:00:00 2001 From: killagu Date: Tue, 20 Feb 2018 20:12:58 +0800 Subject: [PATCH] repl: fix tab-complete warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When create a nest repl, will register `Runtime.executionContextCreated` listener to the inspector session.This patch will fix listener repeatedly register. PR-URL: https://github.com/nodejs/node/pull/18881 Fixes: https://github.com/nodejs/node/issues/18284 Reviewed-By: Michaƫl Zasso Reviewed-By: Anna Henningsen Reviewed-By: Benjamin Gruenbaum Reviewed-By: Colin Ihrig Reviewed-By: Ruben Bridgewater --- lib/repl.js | 3 +-- .../test-repl-tab-complete-no-warn.js | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-repl-tab-complete-no-warn.js diff --git a/lib/repl.js b/lib/repl.js index ef62753134db6d..b7af18ed492a9f 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -650,7 +650,7 @@ REPLServer.prototype.createContext = function() { } else { sendInspectorCommand((session) => { session.post('Runtime.enable'); - session.on('Runtime.executionContextCreated', ({ params }) => { + session.once('Runtime.executionContextCreated', ({ params }) => { this[kContextId] = params.context.id; }); context = vm.createContext(); @@ -834,7 +834,6 @@ function complete(line, callback) { var flat = new ArrayStream(); // make a new "input" stream var magic = new REPLServer('', flat); // make a nested REPL replMap.set(magic, replMap.get(this)); - magic.resetContext(); flat.run(tmp); // eval the flattened code // all this is only profitable if the nested REPL // does not have a bufferedCommand diff --git a/test/parallel/test-repl-tab-complete-no-warn.js b/test/parallel/test-repl-tab-complete-no-warn.js new file mode 100644 index 00000000000000..3379cec8453cba --- /dev/null +++ b/test/parallel/test-repl-tab-complete-no-warn.js @@ -0,0 +1,22 @@ +'use strict'; + +const common = require('../common'); +const repl = require('repl'); +const DEFAULT_MAX_LISTENERS = require('events').defaultMaxListeners; + +common.ArrayStream.prototype.write = () => { +}; + +const putIn = new common.ArrayStream(); +const testMe = repl.start('', putIn); + +// https://github.com/nodejs/node/issues/18284 +// Tab-completion should not repeatedly add the +// `Runtime.executionContextCreated` listener +process.on('warning', common.mustNotCall()); + +putIn.run(['.clear']); +putIn.run(['async function test() {']); +for (let i = 0; i < DEFAULT_MAX_LISTENERS; i++) { + testMe.complete('await Promise.resolve()', () => {}); +}