From 03619eed4d24cb8ed79c467fe4a620bd58fea4cd Mon Sep 17 00:00:00 2001 From: Sebastian Good <2230835+scagood@users.noreply.github.com> Date: Tue, 9 Apr 2024 09:28:06 +0100 Subject: [PATCH] feat: remove "is-builtin-module" dependency (fixes #232) (#227) --- lib/rules/prefer-node-protocol.js | 6 +++--- lib/util/extend-trackmap-with-node-prefix.js | 6 ++---- lib/util/import-target.js | 4 ++-- lib/util/visit-import.js | 4 ++-- lib/util/visit-require.js | 4 ++-- package.json | 1 - tests/lib/rules/no-missing-require.js | 4 ++++ 7 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/rules/prefer-node-protocol.js b/lib/rules/prefer-node-protocol.js index 85fba1cf..cb012511 100644 --- a/lib/rules/prefer-node-protocol.js +++ b/lib/rules/prefer-node-protocol.js @@ -4,7 +4,7 @@ */ "use strict" -const isBuiltinModule = require("is-builtin-module") +const { isBuiltin } = require("node:module") const getConfiguredNodeVersion = require("../util/get-configured-node-version") const getSemverRange = require("../util/get-semver-range") const visitImport = require("../util/visit-import") @@ -150,8 +150,8 @@ module.exports = { if ( typeof value !== "string" || value.startsWith("node:") || - !isBuiltinModule(value) || - !isBuiltinModule(`node:${value}`) + !isBuiltin(value) || + !isBuiltin(`node:${value}`) ) { continue } diff --git a/lib/util/extend-trackmap-with-node-prefix.js b/lib/util/extend-trackmap-with-node-prefix.js index db549eba..2d1c7b13 100644 --- a/lib/util/extend-trackmap-with-node-prefix.js +++ b/lib/util/extend-trackmap-with-node-prefix.js @@ -1,6 +1,6 @@ "use strict" -const isBuiltinModule = require("is-builtin-module") +const { isBuiltin } = require("node:module") /** * Extend traceMap.modules with `node:` prefixed modules @@ -14,9 +14,7 @@ module.exports = function extendTraceMapWithNodePrefix(modules) { ...Object.fromEntries( Object.entries(modules) .map(([name, value]) => [`node:${name}`, value]) - .filter(([name]) => - isBuiltinModule(/** @type {string} */ (name)) - ) + .filter(([name]) => isBuiltin(/** @type {string} */ (name))) ), } return ret diff --git a/lib/util/import-target.js b/lib/util/import-target.js index 93f72c74..54741451 100644 --- a/lib/util/import-target.js +++ b/lib/util/import-target.js @@ -5,7 +5,7 @@ "use strict" const { resolve } = require("path") -const isBuiltinModule = require("is-builtin-module") +const { isBuiltin } = require("node:module") const resolver = require("enhanced-resolve") const isTypescript = require("./is-typescript") @@ -152,7 +152,7 @@ module.exports = class ImportTarget { return "absolute" } - if (isBuiltinModule(this.name)) { + if (isBuiltin(this.name)) { return "node" } diff --git a/lib/util/visit-import.js b/lib/util/visit-import.js index 4742dcbe..6cee5f78 100644 --- a/lib/util/visit-import.js +++ b/lib/util/visit-import.js @@ -5,7 +5,7 @@ "use strict" const path = require("path") -const isBuiltinModule = require("is-builtin-module") +const { isBuiltin } = require("node:module") const getResolvePaths = require("./get-resolve-paths") const getTryExtensions = require("./get-try-extensions") const ImportTarget = require("./import-target") @@ -58,7 +58,7 @@ module.exports = function visitImport( } const name = stripImportPathParams(node.source?.value) - if (includeCore === true || isBuiltinModule(name) === false) { + if (includeCore === true || isBuiltin(name) === false) { targets.push( new ImportTarget(context, node.source, name, options, "import") ) diff --git a/lib/util/visit-require.js b/lib/util/visit-require.js index 394f83a6..01ef7c9d 100644 --- a/lib/util/visit-require.js +++ b/lib/util/visit-require.js @@ -10,7 +10,7 @@ const { ReferenceTracker, getStringIfConstant, } = require("@eslint-community/eslint-utils") -const isBuiltinModule = require("is-builtin-module") +const { isBuiltin } = require("node:module") const getResolvePaths = require("./get-resolve-paths") const getTryExtensions = require("./get-try-extensions") const ImportTarget = require("./import-target") @@ -70,7 +70,7 @@ module.exports = function visitRequire( } const name = stripImportPathParams(rawName) - if (includeCore || !isBuiltinModule(name)) { + if (includeCore || !isBuiltin(name)) { targets.push( new ImportTarget( context, diff --git a/package.json b/package.json index 13ea5d70..e606c6cb 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "get-tsconfig": "^4.7.0", "globals": "^14.0.0", "ignore": "^5.2.4", - "is-builtin-module": "^3.2.1", "minimatch": "^9.0.0", "semver": "^7.5.3" }, diff --git a/tests/lib/rules/no-missing-require.js b/tests/lib/rules/no-missing-require.js index 4c291674..97c8506b 100644 --- a/tests/lib/rules/no-missing-require.js +++ b/tests/lib/rules/no-missing-require.js @@ -36,6 +36,10 @@ ruleTester.run("no-missing-require", rule, { filename: fixture("test.js"), code: "require('node:fs');", }, + { + filename: fixture("test.js"), + code: "require('node:test');", + }, { filename: fixture("test.js"), code: "require('eslint');",