From 320ef5e37d54355dec0ee7fa385820ee9b5b9792 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 --- 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 acbfe8a9b709ae..7802d23c62338a 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -1113,7 +1113,7 @@ REPLServer.prototype.setPrompt = function setPrompt(prompt) { }; 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|\$)*\??\.?$/; @@ -1275,7 +1275,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/); }); }));