Skip to content

Commit

Permalink
repl: add builtinModules
Browse files Browse the repository at this point in the history
This adds an alias to `_builtinLibs` that is documented and should
as such also be accessed publicly. It does not contain any
underscored modules.

Signed-off-by: Ruben Bridgewater <[email protected]>

PR-URL: #33295
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
  • Loading branch information
BridgeAR authored and codebytere committed Jun 18, 2020
1 parent 500bad1 commit 011fe1d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
9 changes: 9 additions & 0 deletions doc/api/repl.md
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,15 @@ by default. However, this is not the case when creating a REPL
programmatically. Use this method to initialize a history log file when working
with REPL instances programmatically.

## `repl.builtinModules`
<!-- YAML
added: REPLACEME
-->

* {string[]}

A list of the names of all Node.js modules, e.g., `'http'`.

## `repl.start([options])`
<!-- YAML
added: v0.1.91
Expand Down
9 changes: 8 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const {
const { Console } = require('console');
const CJSModule = require('internal/modules/cjs/loader').Module;
let _builtinLibs = [...CJSModule.builtinModules]
.filter((e) => !e.startsWith('_'));
.filter((e) => !e.startsWith('_') && !e.includes('/'));
const domain = require('domain');
const debug = require('internal/util/debuglog').debuglog('repl');
const {
Expand Down Expand Up @@ -1593,6 +1593,13 @@ module.exports = {
Recoverable
};

ObjectDefineProperty(module.exports, 'builtinModules', {
get: () => _builtinLibs,
set: (val) => _builtinLibs = val,
enumerable: true,
configurable: true
});

ObjectDefineProperty(module.exports, '_builtinLibs', {
get: pendingDeprecation ? deprecate(
() => _builtinLibs,
Expand Down
13 changes: 11 additions & 2 deletions test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,18 @@ putIn.run(['.clear']);
testMe.complete('require(\'', common.mustCall(function(error, data) {
assert.strictEqual(error, null);
builtinModules.forEach((lib) => {
if (!lib.startsWith('_'))
assert(data[0].includes(lib), `${lib} not found`);
assert(
data[0].includes(lib) || lib.startsWith('_') || lib.includes('/'),
`${lib} not found`
);
});
const newModule = 'foobar';
assert(!builtinModules.includes(newModule));
repl.builtinModules.push(newModule);
testMe.complete('require(\'', common.mustCall((_, [modules]) => {
assert.strictEqual(data[0].length + 1, modules.length);
assert(modules.includes(newModule));
}));
}));

testMe.complete("require\t( 'n", common.mustCall(function(error, data) {
Expand Down

0 comments on commit 011fe1d

Please sign in to comment.