Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for importing with .js extension as tsx importee #374

Merged
merged 3 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/loud-avocados-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-import-resolver-typescript": patch
---

fix: add support for importing with .js extension as tsx importee
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"path": "./lib/index.js",
"limit": "3.1kB"
"limit": "3.2kB"
}
]
23 changes: 15 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,25 @@ function getMappedPaths(
const isJs = JS_EXT_PATTERN.test(source)
if (isJs) {
const jsExt = path.extname(source)
// cjs -> cts, js -> ts, jsx -> tsx, mjs -> mts
const tsExt = jsExt.replace('js', 'ts')

const basename = source.replace(JS_EXT_PATTERN, '')

const mappedPaths = getMappedPaths(basename + tsExt, file)
let resolved = getMappedPaths(basename + tsExt, file)

const resolved =
mappedPaths.length > 0
? mappedPaths
: getMappedPaths(
basename + '.d' + (tsExt === '.tsx' ? '.ts' : tsExt),
file,
)
if (resolved.length === 0 && jsExt === '.js') {
// js -> tsx
const tsxExt = jsExt.replace('js', 'tsx')
resolved = getMappedPaths(basename + tsxExt, file)
}

if (resolved.length === 0) {
resolved = getMappedPaths(
basename + '.d' + (tsExt === '.tsx' ? '.ts' : tsExt),
file,
)
}

if (resolved.length > 0) {
return resolved
Expand Down
4 changes: 4 additions & 0 deletions tests/withPaths/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import './subfolder/tsxImportee'

// import using tsconfig.json path mapping
import 'folder/tsImportee'
import 'folder/tsImportee.js'
import 'folder/tsxImportee'
import 'folder/tsxImportee.js'
import 'folder/subfolder/tsImportee'
import 'folder/subfolder/tsImportee.js'
import 'folder/subfolder/tsxImportee'
import 'folder/subfolder/tsxImportee.js'

// import module with typings set in package.json
import 'folder/module'
Expand Down