Skip to content

Commit

Permalink
fix(no-missing-require): handle multiple resolvePaths (#383)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Good <[email protected]>
  • Loading branch information
GertSallaerts and scagood authored Nov 15, 2024
1 parent c4d1551 commit df6ad2a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/util/import-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/rules/no-missing-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/no-missing-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
{
Expand Down

0 comments on commit df6ad2a

Please sign in to comment.