Skip to content

Commit 566179c

Browse files
Aviv Kellertargos
Aviv Keller
authored andcommitted
lib, tools: remove duplicate requires
PR-URL: #54987 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 538b1eb commit 566179c

File tree

12 files changed

+20
-43
lines changed

12 files changed

+20
-43
lines changed

lib/events.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,13 @@ const {
5151
} = primordials;
5252
const kRejection = SymbolFor('nodejs.rejection');
5353

54-
const { SymbolDispose, kEmptyObject } = require('internal/util');
54+
const { SymbolDispose, kEmptyObject, spliceOne } = require('internal/util');
5555

5656
const {
5757
inspect,
5858
identicalSequenceRange,
5959
} = require('internal/util/inspect');
6060

61-
let spliceOne;
6261
let FixedQueue;
6362
let kFirstEventParam;
6463
let kResistStopPropagation;
@@ -717,8 +716,6 @@ EventEmitter.prototype.removeListener =
717716
if (position === 0)
718717
list.shift();
719718
else {
720-
if (spliceOne === undefined)
721-
spliceOne = require('internal/util').spliceOne;
722719
spliceOne(list, position);
723720
}
724721

lib/internal/abort_controller.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ const {
6868
kDeserialize,
6969
kTransfer,
7070
kTransferList,
71+
markTransferMode,
7172
} = require('internal/worker/js_transferable');
7273

7374
let _MessageChannel;
74-
let markTransferMode;
7575

7676
const kDontThrowSymbol = Symbol('kDontThrowSymbol');
7777

@@ -84,12 +84,6 @@ function lazyMessageChannel() {
8484
return new _MessageChannel();
8585
}
8686

87-
function lazyMarkTransferMode(obj, cloneable, transferable) {
88-
markTransferMode ??=
89-
require('internal/worker/js_transferable').markTransferMode;
90-
markTransferMode(obj, cloneable, transferable);
91-
}
92-
9387
const clearTimeoutRegistry = new SafeFinalizationRegistry(clearTimeout);
9488
const gcPersistentSignals = new SafeSet();
9589

@@ -170,7 +164,7 @@ class AbortSignal extends EventTarget {
170164
this[kReason] = reason;
171165
this[kComposite] = composite;
172166
if (transferable) {
173-
lazyMarkTransferMode(this, false, true);
167+
markTransferMode(this, false, true);
174168
}
175169
}
176170

@@ -455,7 +449,7 @@ class AbortController {
455449
function transferableAbortSignal(signal) {
456450
if (signal?.[kAborted] === undefined)
457451
throw new ERR_INVALID_ARG_TYPE('signal', 'AbortSignal', signal);
458-
lazyMarkTransferMode(signal, false, true);
452+
markTransferMode(signal, false, true);
459453
return signal;
460454
}
461455

lib/internal/bootstrap/switches/does_own_process_state.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const {
3333
parseFileMode,
3434
validateArray,
3535
validateString,
36+
validateUint32,
3637
} = require('internal/validators');
3738

3839
function wrapPosixCredentialSetters(credentials) {
@@ -42,9 +43,6 @@ function wrapPosixCredentialSetters(credentials) {
4243
ERR_UNKNOWN_CREDENTIAL,
4344
},
4445
} = require('internal/errors');
45-
const {
46-
validateUint32,
47-
} = require('internal/validators');
4846

4947
const {
5048
initgroups: _initgroups,

lib/internal/main/worker_thread.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const { setupMainThreadPort } = require('internal/worker/messaging');
4848

4949
const {
5050
onGlobalUncaughtException,
51+
evalScript,
52+
evalModuleEntryPoint,
5153
} = require('internal/process/execution');
5254

5355
let debug = require('internal/util/debuglog').debuglog('worker', (fn) => {
@@ -154,7 +156,6 @@ port.on('message', (message) => {
154156
}
155157

156158
case 'classic': if (getOptionValue('--input-type') !== 'module') {
157-
const { evalScript } = require('internal/process/execution');
158159
const name = '[worker eval]';
159160
// This is necessary for CJS module compilation.
160161
// TODO: pass this with something really internal.
@@ -171,7 +172,6 @@ port.on('message', (message) => {
171172

172173
// eslint-disable-next-line no-fallthrough
173174
case 'module': {
174-
const { evalModuleEntryPoint } = require('internal/process/execution');
175175
PromisePrototypeThen(evalModuleEntryPoint(filename), undefined, (e) => {
176176
workerOnGlobalUncaughtException(e, true);
177177
});

lib/internal/modules/cjs/loader.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ const {
152152
setHasStartedUserCJSExecution,
153153
stripBOM,
154154
toRealPath,
155+
stripTypeScriptTypes,
155156
} = require('internal/modules/helpers');
156157
const packageJsonReader = require('internal/modules/package_json_reader');
157158
const { getOptionValue, getEmbedderOptions } = require('internal/options');
@@ -1362,7 +1363,6 @@ function loadESMFromCJS(mod, filename) {
13621363
if (isUnderNodeModules(filename)) {
13631364
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
13641365
}
1365-
const { stripTypeScriptTypes } = require('internal/modules/helpers');
13661366
source = stripTypeScriptTypes(source, filename);
13671367
}
13681368
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
@@ -1576,7 +1576,6 @@ function loadCTS(module, filename) {
15761576
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
15771577
}
15781578
const source = getMaybeCachedSource(module, filename);
1579-
const { stripTypeScriptTypes } = require('internal/modules/helpers');
15801579
const code = stripTypeScriptTypes(source, filename);
15811580
module._compile(code, filename, 'commonjs');
15821581
}
@@ -1592,7 +1591,6 @@ function loadTS(module, filename) {
15921591
}
15931592
// If already analyzed the source, then it will be cached.
15941593
const source = getMaybeCachedSource(module, filename);
1595-
const { stripTypeScriptTypes } = require('internal/modules/helpers');
15961594
const content = stripTypeScriptTypes(source, filename);
15971595
let format;
15981596
const pkg = packageJsonReader.getNearestParentPackageJSON(filename);

lib/internal/modules/esm/loader.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const { kEmptyObject } = require('internal/util');
3434
const {
3535
compileSourceTextModule,
3636
getDefaultConditions,
37+
forceDefaultLoader,
3738
} = require('internal/modules/esm/utils');
3839
const { kImplicitTypeAttribute } = require('internal/modules/esm/assert');
3940
const { ModuleWrap, kEvaluating, kEvaluated } = internalBinding('module_wrap');
@@ -798,7 +799,7 @@ function createModuleLoader() {
798799
// Don't spawn a new worker if custom loaders are disabled. For instance, if
799800
// we're already in a worker thread created by instantiating
800801
// CustomizedModuleLoader; doing so would cause an infinite loop.
801-
if (!require('internal/modules/esm/utils').forceDefaultLoader()) {
802+
if (!forceDefaultLoader()) {
802803
const userLoaderPaths = getOptionValue('--experimental-loader');
803804
if (userLoaderPaths.length > 0) {
804805
if (!emittedLoaderFlagWarning) {

lib/internal/modules/esm/resolve.js

-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
237237
try {
238238
path = fileURLToPath(resolved);
239239
} catch (err) {
240-
const { setOwnProperty } = require('internal/util');
241240
setOwnProperty(err, 'input', `${resolved}`);
242241
setOwnProperty(err, 'module', `${base}`);
243242
throw err;

lib/internal/process/pre_execution.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const {
3333
emitExperimentalWarning,
3434
SymbolAsyncDispose,
3535
SymbolDispose,
36+
deprecate,
3637
} = require('internal/util');
3738

3839
const {
@@ -47,6 +48,7 @@ const {
4748
addSerializeCallback,
4849
isBuildingSnapshot,
4950
},
51+
runDeserializeCallbacks,
5052
} = require('internal/v8/startup_snapshot');
5153

5254
function prepareMainThreadExecution(expandArgv1 = false, initializeModules = true) {
@@ -147,7 +149,7 @@ function prepareExecution(options) {
147149
initializeClusterIPC();
148150

149151
// TODO(joyeecheung): do this for worker threads as well.
150-
require('internal/v8/startup_snapshot').runDeserializeCallbacks();
152+
runDeserializeCallbacks();
151153
} else {
152154
assert(!internalBinding('worker').isMainThread);
153155
// The setup should be called in LOAD_SCRIPT message handler.
@@ -524,7 +526,6 @@ function setupNetworkInspection() {
524526
// this is used to deprecate APIs implemented in C++ where the deprecation
525527
// utilities are not easily accessible.
526528
function initializeDeprecations() {
527-
const { deprecate } = require('internal/util');
528529
const pendingDeprecation = getOptionValue('--pending-deprecation');
529530

530531
// DEP0103: access to `process.binding('util').isX` type checkers
@@ -586,8 +587,6 @@ function initializeDeprecations() {
586587

587588
function setupChildProcessIpcChannel() {
588589
if (process.env.NODE_CHANNEL_FD) {
589-
const assert = require('internal/assert');
590-
591590
const fd = NumberParseInt(process.env.NODE_CHANNEL_FD, 10);
592591
assert(fd >= 0);
593592

lib/internal/process/warning.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const { validateString } = require('internal/validators');
2626
let fs;
2727
let fd;
2828
let warningFile;
29-
let options;
3029
let traceWarningHelperShown = false;
3130

3231
function resetForSerialization() {
@@ -42,15 +41,9 @@ function lazyOption() {
4241
// This will load `warningFile` only once. If the flag is not set,
4342
// `warningFile` will be set to an empty string.
4443
if (warningFile === undefined) {
45-
options = require('internal/options');
46-
if (options.getOptionValue('--diagnostic-dir') !== '') {
47-
warningFile = options.getOptionValue('--diagnostic-dir');
48-
}
49-
if (options.getOptionValue('--redirect-warnings') !== '') {
50-
warningFile = options.getOptionValue('--redirect-warnings');
51-
} else {
52-
warningFile = '';
53-
}
44+
const diagnosticDir = getOptionValue('--diagnostic-dir');
45+
const redirectWarnings = getOptionValue('--redirect-warnings');
46+
warningFile = diagnosticDir || redirectWarnings || '';
5447
}
5548
return warningFile;
5649
}

lib/internal/source_map/source_map_cache.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {
2020
setSourceMapsEnabled: setSourceMapsNative,
2121
} = internalBinding('errors');
2222
const {
23+
defaultPrepareStackTrace,
2324
setInternalPrepareStackTrace,
2425
} = require('internal/errors');
2526
const { getLazy } = require('internal/util');
@@ -64,9 +65,6 @@ function setSourceMapsEnabled(val) {
6465
setInternalPrepareStackTrace(prepareStackTraceWithSourceMaps);
6566
} else if (sourceMapsEnabled !== undefined) {
6667
// Reset prepare stack trace callback only when disabling source maps.
67-
const {
68-
defaultPrepareStackTrace,
69-
} = require('internal/errors');
7068
setInternalPrepareStackTrace(defaultPrepareStackTrace);
7169
}
7270

test/parallel/test-eslint-duplicate-requires.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ new RuleTester({
1717
}).run('no-duplicate-requires', rule, {
1818
valid: [
1919
{
20-
code: 'require("a"); require("b"); (function() { require("a"); });',
20+
code: '(function() { require("a"); }); (function() { require("a"); });',
2121
},
2222
{
2323
code: 'require(a); require(a);',

tools/eslint-rules/no-duplicate-requires.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = {
3434

3535
return {
3636
CallExpression(node) {
37-
if (isRequireCall(node) && isTopLevel(node)) {
37+
if (isRequireCall(node)) {
3838
const [firstArg] = node.arguments;
3939
if (isString(firstArg)) {
4040
const moduleName = firstArg.value.trim();
@@ -43,7 +43,7 @@ module.exports = {
4343
node,
4444
message: `'${moduleName}' require is duplicated.`,
4545
});
46-
} else {
46+
} else if (isTopLevel(node)) {
4747
requiredModules.add(moduleName);
4848
}
4949
}

0 commit comments

Comments
 (0)