Skip to content

Commit 23c9993

Browse files
committed
fix: support multiple matching ts paths
1 parent 129052d commit 23c9993

File tree

3 files changed

+58
-45
lines changed

3 files changed

+58
-45
lines changed

.nvmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18.20.6
1+
18.20.7

src/index.ts

+38-29
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,30 @@ export function resolve(
181181

182182
initMappers(cachedOptions)
183183

184-
const mappedPath = getMappedPath(source, file, cachedOptions.extensions, true)
185-
if (mappedPath) {
186-
log('matched ts path:', mappedPath)
184+
let mappedPaths = getMappedPaths(source, file, cachedOptions.extensions, true)
185+
186+
if (mappedPaths.length > 0) {
187+
log('matched ts path:', ...mappedPaths)
188+
} else {
189+
mappedPaths = [source]
187190
}
188191

189192
// note that even if we map the path, we still need to do a final resolve
190-
let foundNodePath: string | null
191-
try {
192-
foundNodePath =
193-
resolver.resolveSync(
193+
let foundNodePath: string | undefined
194+
for (const mappedPath of mappedPaths) {
195+
try {
196+
const resolved = resolver.resolveSync(
194197
{},
195198
path.dirname(path.resolve(file)),
196-
mappedPath ?? source,
197-
) || null
198-
} catch {
199-
foundNodePath = null
199+
mappedPath,
200+
)
201+
if (resolved) {
202+
foundNodePath = resolved
203+
break
204+
}
205+
} catch {
206+
log('failed to resolve with', mappedPath)
207+
}
200208
}
201209

202210
// naive attempt at `@types/*` resolution,
@@ -286,16 +294,16 @@ const isModule = (modulePath?: string | undefined): modulePath is string => {
286294
* @returns The mapped path of the module or undefined
287295
*/
288296
// eslint-disable-next-line sonarjs/cognitive-complexity
289-
function getMappedPath(
297+
function getMappedPaths(
290298
source: string,
291299
file: string,
292300
extensions: string[] = defaultExtensions,
293301
retry?: boolean,
294-
): string | undefined {
302+
): string[] {
295303
const originalExtensions = extensions
296304
extensions = ['', ...extensions]
297305

298-
let paths: Array<string | undefined> | undefined = []
306+
let paths: string[] = []
299307

300308
if (RELATIVE_PATH_PATTERN.test(source)) {
301309
const resolved = path.resolve(path.dirname(file), source)
@@ -341,34 +349,35 @@ function getMappedPath(
341349
const tsExt = jsExt.replace('js', 'ts')
342350
const basename = source.replace(JS_EXT_PATTERN, '')
343351

344-
const resolved =
345-
getMappedPath(basename + tsExt, file) ||
346-
getMappedPath(
347-
basename + '.d' + (tsExt === '.tsx' ? '.ts' : tsExt),
348-
file,
349-
)
352+
const mappedPaths = getMappedPaths(basename + tsExt, file)
350353

351-
if (resolved) {
354+
const resolved =
355+
mappedPaths.length > 0
356+
? mappedPaths
357+
: getMappedPaths(
358+
basename + '.d' + (tsExt === '.tsx' ? '.ts' : tsExt),
359+
file,
360+
)
361+
362+
if (resolved.length > 0) {
352363
return resolved
353364
}
354365
}
355366

356367
for (const ext of extensions) {
368+
const mappedPaths = isJs ? [] : getMappedPaths(source + ext, file)
357369
const resolved =
358-
(isJs ? null : getMappedPath(source + ext, file)) ||
359-
getMappedPath(source + `/index${ext}`, file)
370+
mappedPaths.length > 0
371+
? mappedPaths
372+
: getMappedPaths(source + `/index${ext}`, file)
360373

361-
if (resolved) {
374+
if (resolved.length > 0) {
362375
return resolved
363376
}
364377
}
365378
}
366379

367-
if (paths.length > 1) {
368-
log('found multiple matching ts paths:', paths)
369-
}
370-
371-
return paths[0]
380+
return paths
372381
}
373382

374383
// eslint-disable-next-line sonarjs/cognitive-complexity

tests/importXResolverV3/eslint.config.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ const absoluteGlobPath = path.join(__dirname, globPattern)
1010

1111
const base = require('../baseEslintConfig.cjs')()
1212

13-
module.exports = {
14-
files: ['**/*.ts', '**/*.tsx'],
15-
plugins: {
16-
import: importX,
17-
},
18-
settings: {
19-
...importX.flatConfigs.typescript.settings,
20-
'import-x/resolver-next': [
21-
createTypeScriptImportResolver({
22-
project: absoluteGlobPath,
23-
}),
24-
],
25-
},
26-
rules: base.rules,
27-
}
13+
module.exports =
14+
// don't run on node 16 because lacking of `structuredClone`
15+
+process.versions.node.split('.')[0] <= 16
16+
? {}
17+
: {
18+
files: ['**/*.ts', '**/*.tsx'],
19+
plugins: {
20+
import: importX,
21+
},
22+
settings: {
23+
...importX.flatConfigs.typescript.settings,
24+
'import-x/resolver-next': [
25+
createTypeScriptImportResolver({
26+
project: absoluteGlobPath,
27+
}),
28+
],
29+
},
30+
rules: base.rules,
31+
}

0 commit comments

Comments
 (0)