From 148e47e7502c3784b1f2b86aae594c7fc58b31a3 Mon Sep 17 00:00:00 2001 From: Sebastian Good <2230835+scagood@users.noreply.github.com> Date: Wed, 9 Oct 2024 08:04:33 +0100 Subject: [PATCH] fix(no-missing-import): Ignore node builtins in package.json `imports` (#346) --- lib/util/import-target.js | 9 +++++++++ tests/fixtures/no-missing/issue-285/package.json | 8 ++++++++ tests/lib/rules/no-missing-import.js | 6 ++++++ 3 files changed, 23 insertions(+) create mode 100644 tests/fixtures/no-missing/issue-285/package.json diff --git a/lib/util/import-target.js b/lib/util/import-target.js index f5f90a9c..0b0edaeb 100644 --- a/lib/util/import-target.js +++ b/lib/util/import-target.js @@ -8,10 +8,17 @@ const { resolve } = require("path") const { isBuiltin } = require("node:module") const resolver = require("enhanced-resolve") +const { + NodeBuiltinModules, +} = require("../unsupported-features/node-builtins.js") const isTypescript = require("./is-typescript") const { getTSConfigForContext } = require("./get-tsconfig.js") const getTypescriptExtensionMap = require("./get-typescript-extension-map") +const nodeBuiltinFallback = Object.keys(NodeBuiltinModules).map( + name => /** @type {const} */ ({ name, alias: false }) +) + /** * @overload * @param {string[]} input @@ -304,6 +311,8 @@ module.exports = class ImportTarget { extensionAlias, alias, + + fallback: nodeBuiltinFallback, } const requireResolve = resolver.create.sync(this.resolverConfig) diff --git a/tests/fixtures/no-missing/issue-285/package.json b/tests/fixtures/no-missing/issue-285/package.json new file mode 100644 index 00000000..e09d2a59 --- /dev/null +++ b/tests/fixtures/no-missing/issue-285/package.json @@ -0,0 +1,8 @@ +{ + "imports": { + "#is-ip": { + "node": "net", + "browser": "is-ip" + } + } +} diff --git a/tests/lib/rules/no-missing-import.js b/tests/lib/rules/no-missing-import.js index f8478f9c..0eb7f785 100644 --- a/tests/lib/rules/no-missing-import.js +++ b/tests/lib/rules/no-missing-import.js @@ -336,6 +336,12 @@ ruleTester.run("no-missing-import", rule, { code: "import plugin from 'eslint-plugin-n';", }, + // imports alias + { + filename: fixture("issue-285/test.js"), + code: "import isIp from '#is-ip';", + }, + // import() ...(DynamicImportSupported ? [