From 5c22d19f4430fcc2db0e4fb6547165e9973a4b56 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Wed, 25 Sep 2024 04:51:28 -0400 Subject: [PATCH] lib, tools: remove duplicate requires PR-URL: https://github.com/nodejs/node/pull/54987 Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel --- lib/events.js | 5 +---- lib/internal/abort_controller.js | 12 +++--------- .../bootstrap/switches/does_own_process_state.js | 4 +--- lib/internal/main/worker_thread.js | 4 ++-- lib/internal/modules/cjs/loader.js | 4 +--- lib/internal/modules/esm/loader.js | 3 ++- lib/internal/modules/esm/resolve.js | 1 - lib/internal/process/pre_execution.js | 7 +++---- lib/internal/process/warning.js | 13 +++---------- lib/internal/source_map/source_map_cache.js | 4 +--- test/parallel/test-eslint-duplicate-requires.js | 2 +- tools/eslint-rules/no-duplicate-requires.js | 4 ++-- 12 files changed, 20 insertions(+), 43 deletions(-) diff --git a/lib/events.js b/lib/events.js index cb20ddd6b85..256dd580e15 100644 --- a/lib/events.js +++ b/lib/events.js @@ -51,14 +51,13 @@ const { } = primordials; const kRejection = SymbolFor('nodejs.rejection'); -const { SymbolDispose, kEmptyObject } = require('internal/util'); +const { SymbolDispose, kEmptyObject, spliceOne } = require('internal/util'); const { inspect, identicalSequenceRange, } = require('internal/util/inspect'); -let spliceOne; let FixedQueue; let kFirstEventParam; let kResistStopPropagation; @@ -705,8 +704,6 @@ EventEmitter.prototype.removeListener = if (position === 0) list.shift(); else { - if (spliceOne === undefined) - spliceOne = require('internal/util').spliceOne; spliceOne(list, position); } diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index d8607c499b3..3b34ed34afe 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -68,10 +68,10 @@ const { kDeserialize, kTransfer, kTransferList, + markTransferMode, } = require('internal/worker/js_transferable'); let _MessageChannel; -let markTransferMode; const kDontThrowSymbol = Symbol('kDontThrowSymbol'); @@ -84,12 +84,6 @@ function lazyMessageChannel() { return new _MessageChannel(); } -function lazyMarkTransferMode(obj, cloneable, transferable) { - markTransferMode ??= - require('internal/worker/js_transferable').markTransferMode; - markTransferMode(obj, cloneable, transferable); -} - const clearTimeoutRegistry = new SafeFinalizationRegistry(clearTimeout); const gcPersistentSignals = new SafeSet(); @@ -170,7 +164,7 @@ class AbortSignal extends EventTarget { this[kReason] = reason; this[kComposite] = composite; if (transferable) { - lazyMarkTransferMode(this, false, true); + markTransferMode(this, false, true); } } @@ -455,7 +449,7 @@ class AbortController { function transferableAbortSignal(signal) { if (signal?.[kAborted] === undefined) throw new ERR_INVALID_ARG_TYPE('signal', 'AbortSignal', signal); - lazyMarkTransferMode(signal, false, true); + markTransferMode(signal, false, true); return signal; } diff --git a/lib/internal/bootstrap/switches/does_own_process_state.js b/lib/internal/bootstrap/switches/does_own_process_state.js index 21c5a060910..370da66a825 100644 --- a/lib/internal/bootstrap/switches/does_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_own_process_state.js @@ -33,6 +33,7 @@ const { parseFileMode, validateArray, validateString, + validateUint32, } = require('internal/validators'); function wrapPosixCredentialSetters(credentials) { @@ -42,9 +43,6 @@ function wrapPosixCredentialSetters(credentials) { ERR_UNKNOWN_CREDENTIAL, }, } = require('internal/errors'); - const { - validateUint32, - } = require('internal/validators'); const { initgroups: _initgroups, diff --git a/lib/internal/main/worker_thread.js b/lib/internal/main/worker_thread.js index ab244293667..f8c410b5b25 100644 --- a/lib/internal/main/worker_thread.js +++ b/lib/internal/main/worker_thread.js @@ -48,6 +48,8 @@ const { setupMainThreadPort } = require('internal/worker/messaging'); const { onGlobalUncaughtException, + evalScript, + evalModuleEntryPoint, } = require('internal/process/execution'); let debug = require('internal/util/debuglog').debuglog('worker', (fn) => { @@ -154,7 +156,6 @@ port.on('message', (message) => { } case 'classic': if (getOptionValue('--input-type') !== 'module') { - const { evalScript } = require('internal/process/execution'); const name = '[worker eval]'; // This is necessary for CJS module compilation. // TODO: pass this with something really internal. @@ -171,7 +172,6 @@ port.on('message', (message) => { // eslint-disable-next-line no-fallthrough case 'module': { - const { evalModuleEntryPoint } = require('internal/process/execution'); PromisePrototypeThen(evalModuleEntryPoint(filename), undefined, (e) => { workerOnGlobalUncaughtException(e, true); }); diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index a8d245b1081..674aaca5b4a 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -152,6 +152,7 @@ const { setHasStartedUserCJSExecution, stripBOM, toRealPath, + stripTypeScriptTypes, } = require('internal/modules/helpers'); const packageJsonReader = require('internal/modules/package_json_reader'); const { getOptionValue, getEmbedderOptions } = require('internal/options'); @@ -1373,7 +1374,6 @@ function loadESMFromCJS(mod, filename) { if (isUnderNodeModules(filename)) { throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename); } - const { stripTypeScriptTypes } = require('internal/modules/helpers'); source = stripTypeScriptTypes(source, filename); } const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader(); @@ -1587,7 +1587,6 @@ function loadCTS(module, filename) { throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename); } const source = getMaybeCachedSource(module, filename); - const { stripTypeScriptTypes } = require('internal/modules/helpers'); const code = stripTypeScriptTypes(source, filename); module._compile(code, filename, 'commonjs'); } @@ -1603,7 +1602,6 @@ function loadTS(module, filename) { } // If already analyzed the source, then it will be cached. const source = getMaybeCachedSource(module, filename); - const { stripTypeScriptTypes } = require('internal/modules/helpers'); const content = stripTypeScriptTypes(source, filename); let format; const pkg = packageJsonReader.getNearestParentPackageJSON(filename); diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index bcd0844ea4b..26aec37d6e0 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -34,6 +34,7 @@ const { kEmptyObject } = require('internal/util'); const { compileSourceTextModule, getDefaultConditions, + forceDefaultLoader, } = require('internal/modules/esm/utils'); const { kImplicitTypeAttribute } = require('internal/modules/esm/assert'); const { ModuleWrap, kEvaluating, kEvaluated } = internalBinding('module_wrap'); @@ -798,7 +799,7 @@ function createModuleLoader() { // Don't spawn a new worker if custom loaders are disabled. For instance, if // we're already in a worker thread created by instantiating // CustomizedModuleLoader; doing so would cause an infinite loop. - if (!require('internal/modules/esm/utils').forceDefaultLoader()) { + if (!forceDefaultLoader()) { const userLoaderPaths = getOptionValue('--experimental-loader'); if (userLoaderPaths.length > 0) { if (!emittedLoaderFlagWarning) { diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 375a14ef301..b7d95479ced 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -237,7 +237,6 @@ function finalizeResolution(resolved, base, preserveSymlinks) { try { path = fileURLToPath(resolved); } catch (err) { - const { setOwnProperty } = require('internal/util'); setOwnProperty(err, 'input', `${resolved}`); setOwnProperty(err, 'module', `${base}`); throw err; diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index 287ebd3e8ed..0da8c50a6f8 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -31,6 +31,7 @@ const { emitExperimentalWarning, SymbolAsyncDispose, SymbolDispose, + deprecate, } = require('internal/util'); const { @@ -43,6 +44,7 @@ const { addSerializeCallback, isBuildingSnapshot, }, + runDeserializeCallbacks, } = require('internal/v8/startup_snapshot'); function prepareMainThreadExecution(expandArgv1 = false, initializeModules = true) { @@ -141,7 +143,7 @@ function prepareExecution(options) { initializeClusterIPC(); // TODO(joyeecheung): do this for worker threads as well. - require('internal/v8/startup_snapshot').runDeserializeCallbacks(); + runDeserializeCallbacks(); } else { assert(!internalBinding('worker').isMainThread); // The setup should be called in LOAD_SCRIPT message handler. @@ -464,7 +466,6 @@ function setupNetworkInspection() { // this is used to deprecate APIs implemented in C++ where the deprecation // utilities are not easily accessible. function initializeDeprecations() { - const { deprecate } = require('internal/util'); const pendingDeprecation = getOptionValue('--pending-deprecation'); // DEP0103: access to `process.binding('util').isX` type checkers @@ -526,8 +527,6 @@ function initializeDeprecations() { function setupChildProcessIpcChannel() { if (process.env.NODE_CHANNEL_FD) { - const assert = require('internal/assert'); - const fd = NumberParseInt(process.env.NODE_CHANNEL_FD, 10); assert(fd >= 0); diff --git a/lib/internal/process/warning.js b/lib/internal/process/warning.js index 162d0ca8153..364e88d2776 100644 --- a/lib/internal/process/warning.js +++ b/lib/internal/process/warning.js @@ -26,7 +26,6 @@ const { validateString } = require('internal/validators'); let fs; let fd; let warningFile; -let options; let traceWarningHelperShown = false; function resetForSerialization() { @@ -42,15 +41,9 @@ function lazyOption() { // This will load `warningFile` only once. If the flag is not set, // `warningFile` will be set to an empty string. if (warningFile === undefined) { - options = require('internal/options'); - if (options.getOptionValue('--diagnostic-dir') !== '') { - warningFile = options.getOptionValue('--diagnostic-dir'); - } - if (options.getOptionValue('--redirect-warnings') !== '') { - warningFile = options.getOptionValue('--redirect-warnings'); - } else { - warningFile = ''; - } + const diagnosticDir = getOptionValue('--diagnostic-dir'); + const redirectWarnings = getOptionValue('--redirect-warnings'); + warningFile = diagnosticDir || redirectWarnings || ''; } return warningFile; } diff --git a/lib/internal/source_map/source_map_cache.js b/lib/internal/source_map/source_map_cache.js index 9defc32da8e..b24ad244384 100644 --- a/lib/internal/source_map/source_map_cache.js +++ b/lib/internal/source_map/source_map_cache.js @@ -20,6 +20,7 @@ const { setSourceMapsEnabled: setSourceMapsNative, } = internalBinding('errors'); const { + defaultPrepareStackTrace, setInternalPrepareStackTrace, } = require('internal/errors'); const { getLazy } = require('internal/util'); @@ -64,9 +65,6 @@ function setSourceMapsEnabled(val) { setInternalPrepareStackTrace(prepareStackTraceWithSourceMaps); } else if (sourceMapsEnabled !== undefined) { // Reset prepare stack trace callback only when disabling source maps. - const { - defaultPrepareStackTrace, - } = require('internal/errors'); setInternalPrepareStackTrace(defaultPrepareStackTrace); } diff --git a/test/parallel/test-eslint-duplicate-requires.js b/test/parallel/test-eslint-duplicate-requires.js index f2a11b37ca5..36c43d9d161 100644 --- a/test/parallel/test-eslint-duplicate-requires.js +++ b/test/parallel/test-eslint-duplicate-requires.js @@ -17,7 +17,7 @@ new RuleTester({ }).run('no-duplicate-requires', rule, { valid: [ { - code: 'require("a"); require("b"); (function() { require("a"); });', + code: '(function() { require("a"); }); (function() { require("a"); });', }, { code: 'require(a); require(a);', diff --git a/tools/eslint-rules/no-duplicate-requires.js b/tools/eslint-rules/no-duplicate-requires.js index 413c0294b9b..c330119790c 100644 --- a/tools/eslint-rules/no-duplicate-requires.js +++ b/tools/eslint-rules/no-duplicate-requires.js @@ -34,7 +34,7 @@ module.exports = { return { CallExpression(node) { - if (isRequireCall(node) && isTopLevel(node)) { + if (isRequireCall(node)) { const [firstArg] = node.arguments; if (isString(firstArg)) { const moduleName = firstArg.value.trim(); @@ -43,7 +43,7 @@ module.exports = { node, message: `'${moduleName}' require is duplicated.`, }); - } else { + } else if (isTopLevel(node)) { requiredModules.add(moduleName); } }