Skip to content

Commit 7aa581f

Browse files
BridgeARcodebytere
authored andcommitted
repl: deprecate repl._builtinLibs
This is a manually edited and outdated list of builtin modules. Instead, it is better to rely upon the officially documented way to get a list of builtin modules. As a side by fix this makes sure all exports are in one place. Thus, it is easier to see what parts are actually exported and which are not. Signed-off-by: Ruben Bridgewater <[email protected]> PR-URL: #33294 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ed83202 commit 7aa581f

File tree

3 files changed

+58
-27
lines changed

3 files changed

+58
-27
lines changed

doc/api/deprecations.md

+15
Original file line numberDiff line numberDiff line change
@@ -2680,6 +2680,21 @@ Type: Documentation-only (supports [`--pending-deprecation`][])
26802680
The `repl` module exported the input and output stream twice. Use `.input`
26812681
instead of `.inputStream` and `.output` instead of `.outputStream`.
26822682
2683+
<a id="DEP0XX1"></a>
2684+
### DEP0XX1: `repl._builtinLibs`
2685+
<!-- YAML
2686+
changes:
2687+
- version: REPLACEME
2688+
pr-url: https://github.com/nodejs/node/pull/33294
2689+
description: Documentation-only (supports [`--pending-deprecation`][]).
2690+
-->
2691+
2692+
Type: Documentation-only
2693+
2694+
The `repl` module exports a `_builtinLibs` property that contains an array with
2695+
native modules. It was incomplete so far and instead it's better to rely upon
2696+
`require('module').builtinModules`.
2697+
26832698
[`--pending-deprecation`]: cli.html#cli_pending_deprecation
26842699
[`--throw-deprecation`]: cli.html#cli_throw_deprecation
26852700
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size

lib/repl.js

+40-27
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const {
8585
} = require('internal/readline/utils');
8686
const { Console } = require('console');
8787
const CJSModule = require('internal/modules/cjs/loader').Module;
88-
const builtinModules = [...CJSModule.builtinModules]
88+
let _builtinLibs = [...CJSModule.builtinModules]
8989
.filter((e) => !e.startsWith('_'));
9090
const domain = require('domain');
9191
const debug = require('internal/util/debuglog').debuglog('repl');
@@ -158,11 +158,9 @@ module.paths = CJSModule._nodeModulePaths(module.filename);
158158
// This is the default "writer" value, if none is passed in the REPL options,
159159
// and it can be overridden by custom print functions, such as `probe` or
160160
// `eyes.js`.
161-
const writer = exports.writer = (obj) => inspect(obj, writer.options);
161+
const writer = (obj) => inspect(obj, writer.options);
162162
writer.options = { ...inspect.defaultOptions, showProxy: true };
163163

164-
exports._builtinLibs = builtinModules;
165-
166164
function REPLServer(prompt,
167165
stream,
168166
eval_,
@@ -259,7 +257,7 @@ function REPLServer(prompt,
259257
this._domain = options.domain || domain.create();
260258
this.useGlobal = !!useGlobal;
261259
this.ignoreUndefined = !!ignoreUndefined;
262-
this.replMode = replMode || exports.REPL_MODE_SLOPPY;
260+
this.replMode = replMode || module.exports.REPL_MODE_SLOPPY;
263261
this.underscoreAssigned = false;
264262
this.last = undefined;
265263
this.underscoreErrAssigned = false;
@@ -278,7 +276,7 @@ function REPLServer(prompt,
278276
if (options[kStandaloneREPL]) {
279277
// It is possible to introspect the running REPL accessing this variable
280278
// from inside the REPL. This is useful for anyone working on the REPL.
281-
exports.repl = this;
279+
module.exports.repl = this;
282280
} else if (!addedNewListener) {
283281
// Add this listener only once and use a WeakSet that contains the REPLs
284282
// domains. Otherwise we'd have to add a single listener to each REPL
@@ -399,7 +397,8 @@ function REPLServer(prompt,
399397
}
400398
while (true) {
401399
try {
402-
if (self.replMode === exports.REPL_MODE_STRICT && !/^\s*$/.test(code)) {
400+
if (self.replMode === module.exports.REPL_MODE_STRICT &&
401+
!/^\s*$/.test(code)) {
403402
// "void 0" keeps the repl from returning "use strict" as the result
404403
// value for statements and declarations that don't return a value.
405404
code = `'use strict'; void 0;\n${code}`;
@@ -579,7 +578,7 @@ function REPLServer(prompt,
579578
e.stack = e.stack
580579
.replace(/^repl:\d+\r?\n/, '')
581580
.replace(/^\s+at\s.*\n?/gm, '');
582-
} else if (self.replMode === exports.REPL_MODE_STRICT) {
581+
} else if (self.replMode === module.exports.REPL_MODE_STRICT) {
583582
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
584583
(_, pre, line) => pre + (line - 1));
585584
}
@@ -667,7 +666,7 @@ function REPLServer(prompt,
667666
defineDefaultCommands(this);
668667

669668
// Figure out which "writer" function to use
670-
self.writer = options.writer || exports.writer;
669+
self.writer = options.writer || module.exports.writer;
671670

672671
if (self.writer === writer) {
673672
// Conditionally turn on ANSI coloring.
@@ -923,22 +922,12 @@ function REPLServer(prompt,
923922
ObjectSetPrototypeOf(REPLServer.prototype, Interface.prototype);
924923
ObjectSetPrototypeOf(REPLServer, Interface);
925924

926-
exports.REPLServer = REPLServer;
927-
928-
exports.REPL_MODE_SLOPPY = REPL_MODE_SLOPPY;
929-
exports.REPL_MODE_STRICT = REPL_MODE_STRICT;
930-
931925
// Prompt is a string to print on each line for the prompt,
932926
// source is a stream to use for I/O, defaulting to stdin/stdout.
933-
exports.start = function(prompt,
934-
source,
935-
eval_,
936-
useGlobal,
937-
ignoreUndefined,
938-
replMode) {
927+
function start(prompt, source, eval_, useGlobal, ignoreUndefined, replMode) {
939928
return new REPLServer(
940929
prompt, source, eval_, useGlobal, ignoreUndefined, replMode);
941-
};
930+
}
942931

943932
REPLServer.prototype.setupHistory = function setupHistory(historyFile, cb) {
944933
history(this, historyFile, cb);
@@ -993,18 +982,18 @@ REPLServer.prototype.createContext = function() {
993982
});
994983
}
995984

996-
const module = new CJSModule('<repl>');
997-
module.paths = CJSModule._resolveLookupPaths('<repl>', parentModule);
985+
const replModule = new CJSModule('<repl>');
986+
replModule.paths = CJSModule._resolveLookupPaths('<repl>', parentModule);
998987

999988
ObjectDefineProperty(context, 'module', {
1000989
configurable: true,
1001990
writable: true,
1002-
value: module
991+
value: replModule
1003992
});
1004993
ObjectDefineProperty(context, 'require', {
1005994
configurable: true,
1006995
writable: true,
1007-
value: makeRequireFunction(module)
996+
value: makeRequireFunction(replModule)
1008997
});
1009998

1010999
addBuiltinLibsToObject(context);
@@ -1016,6 +1005,7 @@ REPLServer.prototype.resetContext = function() {
10161005
this.context = this.createContext();
10171006
this.underscoreAssigned = false;
10181007
this.underscoreErrAssigned = false;
1008+
// TODO(BridgeAR): Deprecate the lines.
10191009
this.lines = [];
10201010
this.lines.level = [];
10211011

@@ -1217,7 +1207,7 @@ function complete(line, callback) {
12171207
}
12181208

12191209
if (!subdir) {
1220-
completionGroups.push(exports._builtinLibs);
1210+
completionGroups.push(_builtinLibs);
12211211
}
12221212

12231213
completionGroupsLoaded();
@@ -1610,4 +1600,27 @@ function Recoverable(err) {
16101600
}
16111601
ObjectSetPrototypeOf(Recoverable.prototype, SyntaxError.prototype);
16121602
ObjectSetPrototypeOf(Recoverable, SyntaxError);
1613-
exports.Recoverable = Recoverable;
1603+
1604+
module.exports = {
1605+
start,
1606+
writer,
1607+
REPLServer,
1608+
REPL_MODE_SLOPPY,
1609+
REPL_MODE_STRICT,
1610+
Recoverable
1611+
};
1612+
1613+
ObjectDefineProperty(module.exports, '_builtinLibs', {
1614+
get: pendingDeprecation ? deprecate(
1615+
() => _builtinLibs,
1616+
'repl._builtinLibs is deprecated. Check module.builtinModules instead',
1617+
'DEP0XX1'
1618+
) : () => _builtinLibs,
1619+
set: pendingDeprecation ? deprecate(
1620+
(val) => _builtinLibs = val,
1621+
'repl._builtinLibs is deprecated. Check module.builtinModules instead',
1622+
'DEP0XX1'
1623+
) : (val) => _builtinLibs = val,
1624+
enumerable: false,
1625+
configurable: true
1626+
});

test/parallel/test-repl-options.js

+3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ const repl = require('repl');
2929
const cp = require('child_process');
3030

3131
assert.strictEqual(repl.repl, undefined);
32+
repl._builtinLibs;
3233

3334
common.expectWarning({
3435
DeprecationWarning: {
36+
DEP0XX1:
37+
'repl._builtinLibs is deprecated. Check module.builtinModules instead',
3538
DEP0XXX: 'repl.inputStream and repl.outputStream is deprecated. ' +
3639
'Use repl.input and repl.output instead',
3740
DEP0124: 'REPLServer.rli is deprecated',

0 commit comments

Comments
 (0)