From 6a1b2c5606f0c6a37b38b60d780df8698db22a87 Mon Sep 17 00:00:00 2001 From: Sebastian Good <2230835+scagood@users.noreply.github.com> Date: Wed, 18 Sep 2024 03:56:04 +0200 Subject: [PATCH] fix: Use our data set to work out if a module is a node module (#338) * test: Add failing test for #337 * fix: Use our data set to work out if a module is a node module --- lib/rules/prefer-node-protocol.js | 12 +++++++++++- tests/lib/rules/prefer-node-protocol.js | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/rules/prefer-node-protocol.js b/lib/rules/prefer-node-protocol.js index cb012511..8e5ed23f 100644 --- a/lib/rules/prefer-node-protocol.js +++ b/lib/rules/prefer-node-protocol.js @@ -4,12 +4,22 @@ */ "use strict" -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") const visitRequire = require("../util/visit-require") const mergeVisitorsInPlace = require("../util/merge-visitors-in-place") +const { + NodeBuiltinModules, +} = require("../unsupported-features/node-builtins.js") + +/** + * @param {string} name The name of the node module + * @returns {boolean} + */ +function isBuiltin(name) { + return Object.hasOwn(NodeBuiltinModules, name) +} const messageId = "preferNodeProtocol" diff --git a/tests/lib/rules/prefer-node-protocol.js b/tests/lib/rules/prefer-node-protocol.js index e2796177..91c13f27 100644 --- a/tests/lib/rules/prefer-node-protocol.js +++ b/tests/lib/rules/prefer-node-protocol.js @@ -33,6 +33,8 @@ new RuleTester({ const fs = await import(\`fs\`); } `, + // punycode has no `node:` equivelent + 'import "punycode";', 'import "punycode/";', // https://bun.sh/docs/runtime/bun-apis 'import "bun";',