Skip to content

Commit ed7b25c

Browse files
committed
feat(import-target): Add resolution error reason
1 parent 3791d11 commit ed7b25c

File tree

6 files changed

+61
-5
lines changed

6 files changed

+61
-5
lines changed

lib/util/check-existence.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function markMissing(context, target) {
2222
loc: /** @type {import('eslint').AST.SourceLocation} */ (
2323
target.node.loc
2424
),
25-
messageId: "notFound",
25+
messageId: target.resolveError ? "notFoundBecause" : "notFound",
2626
data: /** @type {Record<string, *>} */ (target),
2727
})
2828
}
@@ -86,4 +86,5 @@ exports.checkExistence = function checkExistence(context, targets) {
8686

8787
exports.messages = {
8888
notFound: '"{{name}}" is not found.',
89+
notFoundBecause: '"{{name}}" is not found.\n{{resolveError}}',
8990
}

lib/util/import-target.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ module.exports = class ImportTarget {
131131
*/
132132
this.moduleName = this.getModuleName()
133133

134+
/**
135+
* This is the full resolution failure reasons
136+
* @type {string | null}
137+
*/
138+
this.resolveError = null
139+
134140
/**
135141
* The full path of this import target.
136142
* If the target is a module and it does not exist then this is `null`.
@@ -286,12 +292,25 @@ module.exports = class ImportTarget {
286292

287293
const cwd = this.context.settings?.cwd ?? process.cwd()
288294
for (const directory of this.getPaths()) {
295+
const baseDir = resolve(cwd, directory)
296+
289297
try {
290-
const baseDir = resolve(cwd, directory)
291298
const resolved = requireResolve(baseDir, this.name)
292299
if (typeof resolved === "string") return resolved
293-
} catch {
294-
continue
300+
} catch (error) {
301+
if (error instanceof Error === false) {
302+
throw error
303+
}
304+
305+
// This is the usual error
306+
if (
307+
error.message ===
308+
`Can't resolve '${this.name}' in '${baseDir}'`
309+
) {
310+
continue
311+
}
312+
313+
this.resolveError = error.message
295314
}
296315
}
297316

tests/fixtures/no-missing/node_modules/misconfigured-default/build/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/no-missing/node_modules/misconfigured-default/package.json

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/no-missing/node_modules/misconfigured-default/src/index.ts

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/lib/rules/no-missing-import.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function fixture(name) {
3939
return path.resolve(__dirname, "../../fixtures/no-missing", name)
4040
}
4141

42+
/** @type {import('eslint').RuleTester} */
4243
const ruleTester = new RuleTester({
4344
languageOptions: {
4445
sourceType: "module",
@@ -313,7 +314,17 @@ ruleTester.run("no-missing-import", rule, {
313314
{
314315
filename: fixture("test.js"),
315316
code: "import abcdef from 'esm-module/sub.mjs';",
316-
errors: ['"esm-module/sub.mjs" is not found.'],
317+
// errors: ['"esm-module/sub.mjs" is not found.'],
318+
errors: [
319+
{
320+
messageId: "notFoundBecause",
321+
data: {
322+
name: "esm-module/sub.mjs",
323+
resolveError:
324+
"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)",
325+
},
326+
},
327+
],
317328
},
318329
{
319330
filename: fixture("test.js"),
@@ -393,6 +404,20 @@ ruleTester.run("no-missing-import", rule, {
393404
errors: ['"./A.js" is not found.'],
394405
},
395406

407+
{
408+
filename: fixture("test.js"),
409+
code: "import 'misconfigured-default';",
410+
errors: [
411+
{
412+
messageId: "notFoundBecause",
413+
data: {
414+
name: "misconfigured-default",
415+
resolveError: "Default condition should be last one",
416+
},
417+
},
418+
],
419+
},
420+
396421
// import()
397422
...(DynamicImportSupported
398423
? [

0 commit comments

Comments
 (0)