Skip to content

Commit c9d5ab0

Browse files
fix: add support for importing with .js extension as tsx importee (#374)
Co-authored-by: David Ensinger <[email protected]>
1 parent 7d63ec7 commit c9d5ab0

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

.changeset/loud-avocados-bake.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-import-resolver-typescript": patch
3+
---
4+
5+
fix: add support for importing with .js extension as tsx importee

.size-limit.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
33
"path": "./lib/index.js",
4-
"limit": "3.1kB"
4+
"limit": "3.2kB"
55
}
66
]

src/index.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,25 @@ function getMappedPaths(
362362
const isJs = JS_EXT_PATTERN.test(source)
363363
if (isJs) {
364364
const jsExt = path.extname(source)
365+
// cjs -> cts, js -> ts, jsx -> tsx, mjs -> mts
365366
const tsExt = jsExt.replace('js', 'ts')
367+
366368
const basename = source.replace(JS_EXT_PATTERN, '')
367369

368-
const mappedPaths = getMappedPaths(basename + tsExt, file)
370+
let resolved = getMappedPaths(basename + tsExt, file)
369371

370-
const resolved =
371-
mappedPaths.length > 0
372-
? mappedPaths
373-
: getMappedPaths(
374-
basename + '.d' + (tsExt === '.tsx' ? '.ts' : tsExt),
375-
file,
376-
)
372+
if (resolved.length === 0 && jsExt === '.js') {
373+
// js -> tsx
374+
const tsxExt = jsExt.replace('js', 'tsx')
375+
resolved = getMappedPaths(basename + tsxExt, file)
376+
}
377+
378+
if (resolved.length === 0) {
379+
resolved = getMappedPaths(
380+
basename + '.d' + (tsExt === '.tsx' ? '.ts' : tsExt),
381+
file,
382+
)
383+
}
377384

378385
if (resolved.length > 0) {
379386
return resolved

tests/withPaths/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import './subfolder/tsxImportee'
66

77
// import using tsconfig.json path mapping
88
import 'folder/tsImportee'
9+
import 'folder/tsImportee.js'
910
import 'folder/tsxImportee'
11+
import 'folder/tsxImportee.js'
1012
import 'folder/subfolder/tsImportee'
13+
import 'folder/subfolder/tsImportee.js'
1114
import 'folder/subfolder/tsxImportee'
15+
import 'folder/subfolder/tsxImportee.js'
1216

1317
// import module with typings set in package.json
1418
import 'folder/module'

0 commit comments

Comments
 (0)