From 797f7f8a38b106759e0da3ec0456102cd41357ce Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Sat, 6 Feb 2021 12:20:00 +0100 Subject: [PATCH] =?UTF-8?q?repl:=20add=C2=A0auto=E2=80=91completion=20for?= =?UTF-8?q?=C2=A0`node:`=E2=80=91prefixed=20`require(=E2=80=A6)`=C2=A0call?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs: https://github.com/nodejs/node/pull/37178 PR-URL: https://github.com/nodejs/node/pull/37246 Reviewed-By: Bradley Farias Reviewed-By: Antoine du Hamel Reviewed-By: Benjamin Gruenbaum Reviewed-By: Darshan Sen Reviewed-By: Matteo Collina --- lib/repl.js | 4 ++-- test/parallel/test-repl-tab-complete.js | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index cef5ca177d701d..79917beb3ca3b2 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -1144,7 +1144,7 @@ REPLServer.prototype.turnOffEditorMode = deprecate( 'DEP0078'); const importRE = /\bimport\s*\(\s*['"`](([\w@./:-]+\/)?(?:[\w@./:-]*))(?![^'"`])$/; -const requireRE = /\brequire\s*\(\s*['"`](([\w@./-]+\/)?(?:[\w@./-]*))(?![^'"`])$/; +const requireRE = /\brequire\s*\(\s*['"`](([\w@./:-]+\/)?(?:[\w@./:-]*))(?![^'"`])$/; const fsAutoCompleteRE = /fs(?:\.promises)?\.\s*[a-z][a-zA-Z]+\(\s*["'](.*)/; const simpleExpressionRE = /(?:[a-zA-Z_$](?:\w|\$)*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; @@ -1306,7 +1306,7 @@ function complete(line, callback) { } if (!subdir) { - ArrayPrototypePush(completionGroups, _builtinLibs); + ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs); } } else if (RegExpPrototypeTest(importRE, line) && this.allowBlockingCompletions) { diff --git a/test/parallel/test-repl-tab-complete.js b/test/parallel/test-repl-tab-complete.js index bfcf810ddd6125..01eac9a0eee2a9 100644 --- a/test/parallel/test-repl-tab-complete.js +++ b/test/parallel/test-repl-tab-complete.js @@ -31,6 +31,10 @@ const assert = require('assert'); const path = require('path'); const fixtures = require('../common/fixtures'); const { builtinModules } = require('module'); +const publicModules = builtinModules.filter( + (lib) => !lib.startsWith('_') && !lib.includes('/'), +); + const hasInspector = process.features.inspector; if (!common.isMainThread) @@ -239,9 +243,9 @@ putIn.run(['.clear']); testMe.complete('require(\'', common.mustCall(function(error, data) { assert.strictEqual(error, null); - builtinModules.forEach((lib) => { + publicModules.forEach((lib) => { assert( - data[0].includes(lib) || lib.startsWith('_') || lib.includes('/'), + data[0].includes(lib) && data[0].includes(`node:${lib}`), `${lib} not found` ); }); @@ -258,11 +262,15 @@ testMe.complete("require\t( 'n", common.mustCall(function(error, data) { assert.strictEqual(error, null); assert.strictEqual(data.length, 2); assert.strictEqual(data[1], 'n'); + // require(...) completions include `node:`-prefixed modules: + publicModules.forEach((lib, index) => + assert.strictEqual(data[0][index], `node:${lib}`)); + assert.strictEqual(data[0][publicModules.length], ''); // There is only one Node.js module that starts with n: - assert.strictEqual(data[0][0], 'net'); - assert.strictEqual(data[0][1], ''); + assert.strictEqual(data[0][publicModules.length + 1], 'net'); + assert.strictEqual(data[0][publicModules.length + 2], ''); // It's possible to pick up non-core modules too - data[0].slice(2).forEach((completion) => { + data[0].slice(publicModules.length + 3).forEach((completion) => { assert.match(completion, /^n/); }); }));