diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index 2fba1e3fdbe81b..f028f0adf02be5 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -66,10 +66,10 @@ const { kDeserialize, kTransfer, kTransferList, + markTransferMode, } = require('internal/worker/js_transferable'); let _MessageChannel; -let markTransferMode; const kDontThrowSymbol = Symbol('kDontThrowSymbol'); @@ -82,12 +82,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(); @@ -168,7 +162,7 @@ class AbortSignal extends EventTarget { this[kReason] = reason; this[kComposite] = composite; if (transferable) { - lazyMarkTransferMode(this, false, true); + markTransferMode(this, false, true); } } @@ -425,7 +419,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/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 451b7c2195e7ad..ffdcaa9d06edf5 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'); @@ -1362,7 +1363,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(); @@ -1576,7 +1576,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'); } @@ -1592,7 +1591,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/test/parallel/test-eslint-duplicate-requires.js b/test/parallel/test-eslint-duplicate-requires.js index f2a11b37ca5d87..36c43d9d161b3f 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 fb4f36eed8ab7e..c330119790c502 100644 --- a/tools/eslint-rules/no-duplicate-requires.js +++ b/tools/eslint-rules/no-duplicate-requires.js @@ -1,6 +1,7 @@ /** * @fileoverview Ensure modules are not required twice at top level of a module * @author devsnek + * @author RedYetiDev */ 'use strict'; @@ -10,20 +11,18 @@ const { isRequireCall, isString } = require('./rules-utils.js'); // Rule Definition //------------------------------------------------------------------------------ -const secondLevelTypes = [ +const topLevelTypes = new Set([ 'FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression', 'ClassBody', 'MethodDefinition', -]; +]); -function isTopLevel(node) { - while (!secondLevelTypes.includes(node.type)) { +const isTopLevel = (node) => { + while (node) { + if (topLevelTypes.has(node.type)) return false; node = node.parent; - if (!node) { - return true; - } } - return false; -} + return true; +}; module.exports = { create(context) { @@ -31,37 +30,25 @@ module.exports = { return {}; } - function getRequiredModuleNameFromCall(node) { - // Node has arguments and first argument is string - if (node.arguments.length && isString(node.arguments[0])) { - return node.arguments[0].value.trim(); - } - - return undefined; - } - - const required = new Set(); - - const rules = { - CallExpression: (node) => { - if (isRequireCall(node) && isTopLevel(node)) { - const moduleName = getRequiredModuleNameFromCall(node); - if (moduleName === undefined) { - return; - } - if (required.has(moduleName)) { - context.report( - node, - '\'{{moduleName}}\' require is duplicated.', - { moduleName }, - ); - } else { - required.add(moduleName); + const requiredModules = new Set(); + + return { + CallExpression(node) { + if (isRequireCall(node)) { + const [firstArg] = node.arguments; + if (isString(firstArg)) { + const moduleName = firstArg.value.trim(); + if (requiredModules.has(moduleName)) { + context.report({ + node, + message: `'${moduleName}' require is duplicated.`, + }); + } else if (isTopLevel(node)) { + requiredModules.add(moduleName); + } } } }, }; - - return rules; }, };