From df6ad2a3f2cbc2218fe8bd23222e3867642d1e70 Mon Sep 17 00:00:00 2001 From: Gert Sallaerts <1267900+GertSallaerts@users.noreply.github.com> Date: Fri, 15 Nov 2024 08:31:14 +0100 Subject: [PATCH] fix(no-missing-require): handle multiple resolvePaths (#383) Co-authored-by: Sebastian Good <2230835+scagood@users.noreply.github.com> --- lib/util/import-target.js | 12 +++++++++++- tests/lib/rules/no-missing-import.js | 5 +++++ tests/lib/rules/no-missing-require.js | 10 ++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/util/import-target.js b/lib/util/import-target.js index 75a5e11b..dfb00ea0 100644 --- a/lib/util/import-target.js +++ b/lib/util/import-target.js @@ -270,6 +270,13 @@ module.exports = class ImportTarget { this.resolveError = error.message } + /** + * @returns {void} + */ + resetResolutionError() { + this.resolveError = null + } + /** * Resolve the given id to file paths. * @returns {string | null} The resolved path. @@ -324,7 +331,10 @@ module.exports = class ImportTarget { try { const resolved = requireResolve(baseDir, this.name) - if (typeof resolved === "string") return resolved + if (typeof resolved === "string") { + this.resetResolutionError() + return resolved + } } catch (error) { this.handleResolutionError(error) } diff --git a/tests/lib/rules/no-missing-import.js b/tests/lib/rules/no-missing-import.js index 1a224f65..8f719be5 100644 --- a/tests/lib/rules/no-missing-import.js +++ b/tests/lib/rules/no-missing-import.js @@ -181,6 +181,11 @@ ruleTester.run("no-missing-import", rule, { code: "import a from './fixtures/no-missing/a.js';", options: [{ resolvePaths: ["tests"] }], }, + { + filename: fixture("test.js"), + code: "import a from './fixtures/no-missing/a.js';", + options: [{ resolvePaths: ["scripts", "tests"] }], + }, // typescriptExtensionMap { diff --git a/tests/lib/rules/no-missing-require.js b/tests/lib/rules/no-missing-require.js index c21541d6..75fac031 100644 --- a/tests/lib/rules/no-missing-require.js +++ b/tests/lib/rules/no-missing-require.js @@ -130,6 +130,16 @@ ruleTester.run("no-missing-require", rule, { code: "require('./fixtures/no-missing/a');", options: [{ resolvePaths: ["tests"] }], }, + { + filename: fixture("test.js"), + code: "require('./fixtures/no-missing/a');", + options: [{ resolvePaths: ["scripts", "tests"] }], + }, + { + filename: fixture("test.js"), + code: "require('./a');", + options: [{ resolvePaths: ["tests"] }], + }, // Ignores it if not callee. {