Skip to content

Commit 240ed58

Browse files
silverwindclaudeautofix-ci[bot]
authored
fix: make extensions rule handle .d.ts correctly (#468)
Co-authored-by: Claude (Opus 4.6) <noreply@anthropic.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 69ddbba commit 240ed58

File tree

5 files changed

+24
-3
lines changed

5 files changed

+24
-3
lines changed

.changeset/fix-extensions-dts.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-import-x": patch
3+
---
4+
5+
Make `extensions` handle `.d.ts` correctly

src/rules/extensions.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
stringifyPath,
1717
} from '../utils/index.js'
1818

19+
const dtsRe = /\.d\.[cm]?ts$/
20+
1921
const modifierValues = ['always', 'ignorePackages', 'never'] as const
2022

2123
const modifierSchema = {
@@ -328,9 +330,15 @@ export default createRule<Options, MessageId>({
328330

329331
const resolvedPath = resolve(importPath, context)
330332

331-
// get extension from resolved path, if possible.
332-
// for unresolved, use source value.
333-
const extension = path.extname(resolvedPath || importPath).slice(1)
333+
// get extension from resolved path, or source value if unresolved.
334+
// for .d.ts/.d.mts/.d.cts, use the import path extension instead.
335+
const extension = path
336+
.extname(
337+
resolvedPath && dtsRe.test(resolvedPath)
338+
? importPath
339+
: resolvedPath || importPath,
340+
)
341+
.slice(1)
334342

335343
// determine if this is a module
336344
const isPackage =
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare const foo: string

test/fixtures/dist-import/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'bar'

test/rules/extensions.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,12 @@ describe('TypeScript', () => {
12491249
options: ['always', { checkTypeImports: true }],
12501250
}),
12511251

1252+
// .js import that resolves to .d.ts should not demand .ts extension
1253+
tValid({
1254+
code: 'import { foo } from "./dist-import/index.js";',
1255+
options: ['always'],
1256+
}),
1257+
12521258
// pathGroupOverrides: no patterns match good bespoke specifiers
12531259
tValid({
12541260
code: `

0 commit comments

Comments
 (0)