Skip to content

Commit 982a723

Browse files
authoredMay 7, 2024··
feat(import-target): Add resolution error reason (#264)
1 parent 60bf29f commit 982a723

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+214
-156
lines changed
 

‎lib/util/check-existence.js

+9-42
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
*/
55
"use strict"
66

7-
const path = require("path")
8-
const exists = require("./exists")
97
const getAllowModules = require("./get-allow-modules")
10-
const isTypescript = require("./is-typescript")
11-
const { convertJsExtensionToTs } = require("../util/map-typescript-extension")
128

139
/**
1410
* Reports a missing file from ImportTarget
@@ -17,13 +13,16 @@ const { convertJsExtensionToTs } = require("../util/map-typescript-extension")
1713
* @returns {void}
1814
*/
1915
function markMissing(context, target) {
16+
// This should never happen... this is just a fallback for typescript
17+
target.resolveError ??= `"${target.name}" is not found`
18+
2019
context.report({
2120
node: target.node,
2221
loc: /** @type {import('eslint').AST.SourceLocation} */ (
2322
target.node.loc
2423
),
2524
messageId: "notFound",
26-
data: /** @type {Record<string, *>} */ (target),
25+
data: { resolveError: target.resolveError },
2726
})
2827
}
2928

@@ -38,52 +37,20 @@ function markMissing(context, target) {
3837
* @returns {void}
3938
*/
4039
exports.checkExistence = function checkExistence(context, targets) {
40+
/** @type {Set<string | undefined>} */
4141
const allowed = new Set(getAllowModules(context))
4242

43-
target: for (const target of targets) {
44-
if (
45-
target.moduleName != null &&
46-
!allowed.has(target.moduleName) &&
47-
target.filePath == null
48-
) {
49-
markMissing(context, target)
50-
continue
51-
}
52-
53-
if (
54-
target.moduleName != null ||
55-
target.filePath == null ||
56-
exists(target.filePath)
57-
) {
43+
for (const target of targets) {
44+
if (allowed.has(target.moduleName)) {
5845
continue
5946
}
6047

61-
if (isTypescript(context) === false) {
48+
if (target.resolveError != null) {
6249
markMissing(context, target)
63-
continue
6450
}
65-
66-
const parsed = path.parse(target.filePath)
67-
const pathWithoutExt = path.resolve(parsed.dir, parsed.name)
68-
69-
const reversedExtensions = convertJsExtensionToTs(
70-
context,
71-
target.filePath,
72-
parsed.ext
73-
)
74-
75-
for (const reversedExtension of reversedExtensions) {
76-
const reversedPath = pathWithoutExt + reversedExtension
77-
78-
if (exists(reversedPath)) {
79-
continue target
80-
}
81-
}
82-
83-
markMissing(context, target)
8451
}
8552
}
8653

8754
exports.messages = {
88-
notFound: '"{{name}}" is not found.',
55+
notFound: "{{resolveError}}",
8956
}

‎lib/util/import-target.js

+28-5
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`.
@@ -239,6 +245,19 @@ module.exports = class ImportTarget {
239245
return [this.options.basedir]
240246
}
241247

248+
/**
249+
* @param {string} baseDir
250+
* @param {unknown} error
251+
* @returns {void}
252+
*/
253+
handleResolutionError(baseDir, error) {
254+
if (error instanceof Error === false) {
255+
throw error
256+
}
257+
258+
this.resolveError = error.message
259+
}
260+
242261
/**
243262
* Resolve the given id to file paths.
244263
* @returns {string | null} The resolved path.
@@ -274,24 +293,28 @@ module.exports = class ImportTarget {
274293
extensionAlias = getTypescriptExtensionMap(this.context).backward
275294
}
276295

277-
const requireResolve = resolver.create.sync({
296+
/** @type {import('enhanced-resolve').ResolveOptionsOptionalFS} */
297+
this.resolverConfig = {
278298
conditionNames,
279299
extensions,
280300
mainFields,
281301
mainFiles,
282302

283303
extensionAlias,
284304
alias,
285-
})
305+
}
306+
307+
const requireResolve = resolver.create.sync(this.resolverConfig)
286308

287309
const cwd = this.context.settings?.cwd ?? process.cwd()
288310
for (const directory of this.getPaths()) {
311+
const baseDir = resolve(cwd, directory)
312+
289313
try {
290-
const baseDir = resolve(cwd, directory)
291314
const resolved = requireResolve(baseDir, this.name)
292315
if (typeof resolved === "string") return resolved
293-
} catch {
294-
continue
316+
} catch (error) {
317+
this.handleResolutionError(baseDir, error)
295318
}
296319
}
297320

0 commit comments

Comments
 (0)
Please sign in to comment.