Skip to content

Commit 9d4cd68

Browse files
committed
fix: support multiple matching ts paths
1 parent 129052d commit 9d4cd68

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

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

0 commit comments

Comments
 (0)