lib, tools: remove duplicate requires

PR-URL: https://github.com/nodejs/node/pull/54987
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Aviv Keller 2024-09-25 04:51:28 -04:00 committed by GitHub
parent c6d20a034d
commit 5c22d19f44
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 20 additions and 43 deletions

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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,

View File

@ -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);
});

View File

@ -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);

View File

@ -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) {

View File

@ -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;

View File

@ -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);

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);',

View File

@ -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);
}
}