Skip to content

Commit 46c4709

Browse files
jablkoljharb
authored andcommittedOct 23, 2021
[Fix] extensions: ignore type-only imports
1 parent c3633c6 commit 46c4709

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed
 

‎CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
66

77
## [Unreleased]
88

9+
### Fixed
10+
- [`extensions`]: ignore type-only imports ([#2270], [@jablko])
11+
912
## [2.25.2] - 2021-10-12
1013

1114
### Fixed
@@ -929,6 +932,7 @@ for info on changes for earlier releases.
929932

930933
[`memo-parser`]: ./memo-parser/README.md
931934

935+
[#2270]: https://github.com/import-js/eslint-plugin-import/pull/2270
932936
[#2240]: https://github.com/import-js/eslint-plugin-import/pull/2240
933937
[#2233]: https://github.com/import-js/eslint-plugin-import/pull/2233
934938
[#2226]: https://github.com/import-js/eslint-plugin-import/pull/2226

‎src/rules/extensions.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@ module.exports = {
135135
return false;
136136
}
137137

138-
function checkFileExtension(source) {
138+
function checkFileExtension(source, node) {
139+
// ignore type-only imports
140+
if (node.importKind === 'type') {
141+
return;
142+
}
143+
139144
// bail if the declaration doesn't have a source, e.g. "export { foo };", or if it's only partially typed like in an editor
140145
if (!source || !source.value) return;
141146

‎tests/src/rules/extensions.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RuleTester } from 'eslint';
22
import rule from 'rules/extensions';
3-
import { test, testFilePath } from '../utils';
3+
import { getTSParsers, test, testFilePath } from '../utils';
44

55
const ruleTester = new RuleTester();
66

@@ -597,3 +597,34 @@ ruleTester.run('extensions', rule, {
597597
}),
598598
],
599599
});
600+
601+
describe('TypeScript', () => {
602+
getTSParsers()
603+
// Type-only imports were added in TypeScript ESTree 2.23.0
604+
.filter((parser) => parser !== require.resolve('typescript-eslint-parser'))
605+
.forEach((parser) => {
606+
ruleTester.run(`${parser}: extensions ignore type-only`, rule, {
607+
valid: [
608+
test({
609+
code: 'import type { T } from "./typescript-declare";',
610+
options: [
611+
'always',
612+
{ ts: 'never', tsx: 'never', js: 'never', jsx: 'never' },
613+
],
614+
parser,
615+
}),
616+
],
617+
invalid: [
618+
test({
619+
code: 'import { T } from "./typescript-declare";',
620+
errors: ['Missing file extension for "./typescript-declare"'],
621+
options: [
622+
'always',
623+
{ ts: 'never', tsx: 'never', js: 'never', jsx: 'never' },
624+
],
625+
parser,
626+
}),
627+
],
628+
});
629+
});
630+
});

0 commit comments

Comments
 (0)
Please sign in to comment.