Skip to content

Commit

Permalink
feat(import-target): Add resolution error reason
Browse files Browse the repository at this point in the history
  • Loading branch information
scagood committed Apr 8, 2024
1 parent 3791d11 commit ed7b25c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/util/check-existence.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function markMissing(context, target) {
loc: /** @type {import('eslint').AST.SourceLocation} */ (
target.node.loc
),
messageId: "notFound",
messageId: target.resolveError ? "notFoundBecause" : "notFound",
data: /** @type {Record<string, *>} */ (target),
})
}
Expand Down Expand Up @@ -86,4 +86,5 @@ exports.checkExistence = function checkExistence(context, targets) {

exports.messages = {
notFound: '"{{name}}" is not found.',
notFoundBecause: '"{{name}}" is not found.\n{{resolveError}}',
}
25 changes: 22 additions & 3 deletions lib/util/import-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ module.exports = class ImportTarget {
*/
this.moduleName = this.getModuleName()

/**
* This is the full resolution failure reasons
* @type {string | null}
*/
this.resolveError = null

/**
* The full path of this import target.
* If the target is a module and it does not exist then this is `null`.
Expand Down Expand Up @@ -286,12 +292,25 @@ module.exports = class ImportTarget {

const cwd = this.context.settings?.cwd ?? process.cwd()
for (const directory of this.getPaths()) {
const baseDir = resolve(cwd, directory)

try {
const baseDir = resolve(cwd, directory)
const resolved = requireResolve(baseDir, this.name)
if (typeof resolved === "string") return resolved
} catch {
continue
} catch (error) {
if (error instanceof Error === false) {
throw error
}

// This is the usual error
if (
error.message ===
`Can't resolve '${this.name}' in '${baseDir}'`
) {
continue
}

this.resolveError = error.message
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion tests/lib/rules/no-missing-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function fixture(name) {
return path.resolve(__dirname, "../../fixtures/no-missing", name)
}

/** @type {import('eslint').RuleTester} */
const ruleTester = new RuleTester({
languageOptions: {
sourceType: "module",
Expand Down Expand Up @@ -313,7 +314,17 @@ ruleTester.run("no-missing-import", rule, {
{
filename: fixture("test.js"),
code: "import abcdef from 'esm-module/sub.mjs';",
errors: ['"esm-module/sub.mjs" is not found.'],
// errors: ['"esm-module/sub.mjs" is not found.'],
errors: [
{
messageId: "notFoundBecause",
data: {
name: "esm-module/sub.mjs",
resolveError:
"Package path ./sub.mjs is not exported from package /home/scagood/github/open-source/eslint-plugin-n/tests/fixtures/no-missing/node_modules/esm-module (see exports field in /home/scagood/github/open-source/eslint-plugin-n/tests/fixtures/no-missing/node_modules/esm-module/package.json)",
},
},
],
},
{
filename: fixture("test.js"),
Expand Down Expand Up @@ -393,6 +404,20 @@ ruleTester.run("no-missing-import", rule, {
errors: ['"./A.js" is not found.'],
},

{
filename: fixture("test.js"),
code: "import 'misconfigured-default';",
errors: [
{
messageId: "notFoundBecause",
data: {
name: "misconfigured-default",
resolveError: "Default condition should be last one",
},
},
],
},

// import()
...(DynamicImportSupported
? [
Expand Down

0 comments on commit ed7b25c

Please sign in to comment.