Skip to content

Commit f302f7d

Browse files
benkrejciljharb
authored andcommitted
[Fix] no-duplicates: Prefer combined type and regular imports when using prefer-inline
1 parent e2cf99c commit f302f7d

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1313
- [`no-extraneous-dependencies`]/TypeScript: do not error when importing inline type from dev dependencies ([#1820], thanks [@andyogo])
1414
- [`newline-after-import`]/TypeScript: do not error when re-exporting a namespaced import ([#2832], thanks [@laurens-dg])
1515
* [`order`]: partial fix for [#2687] (thanks [@ljharb])
16+
- [`no-duplicates`]: Detect across type and regular imports ([#2835], thanks [@benkrejci])
1617

1718
### Changed
1819
- [Docs] [`no-duplicates`]: fix example schema ([#2684], thanks [@simmo])
@@ -1073,6 +1074,7 @@ for info on changes for earlier releases.
10731074

10741075
[`memo-parser`]: ./memo-parser/README.md
10751076

1077+
[#2835]: https://github.com/import-js/eslint-plugin-import/pull/2835
10761078
[#2832]: https://github.com/import-js/eslint-plugin-import/pull/2832
10771079
[#2756]: https://github.com/import-js/eslint-plugin-import/pull/2756
10781080
[#2754]: https://github.com/import-js/eslint-plugin-import/pull/2754
@@ -1647,6 +1649,7 @@ for info on changes for earlier releases.
16471649
[@BarryThePenguin]: https://github.com/BarryThePenguin
16481650
[@be5invis]: https://github.com/be5invis
16491651
[@beatrizrezener]: https://github.com/beatrizrezener
1652+
[@benkrejci]: https://github.com/benkrejci
16501653
[@benmosher]: https://github.com/benmosher
16511654
[@benmunro]: https://github.com/benmunro
16521655
[@BenoitZugmeyer]: https://github.com/BenoitZugmeyer

docs/rules/no-duplicates.md

+5
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,17 @@ Config:
8484
```js
8585
import { AValue, type AType } from './mama-mia'
8686
import type { BType } from './mama-mia'
87+
88+
import { CValue } from './papa-mia'
89+
import type { CType } from './papa-mia'
8790
```
8891

8992
✅ Valid with `["error", {"prefer-inline": true}]`
9093

9194
```js
9295
import { AValue, type AType, type BType } from './mama-mia'
96+
97+
import { CValue, type CType } from './papa-mia'
9398
```
9499

95100
<!--tabs-->

src/rules/no-duplicates.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,11 @@ module.exports = {
318318
});
319319
}
320320
const map = moduleMaps.get(n.parent);
321-
if (n.importKind === 'type') {
321+
const preferInline = context.options[0] && context.options[0]['prefer-inline'];
322+
if (!preferInline && n.importKind === 'type') {
322323
return n.specifiers.length > 0 && n.specifiers[0].type === 'ImportDefaultSpecifier' ? map.defaultTypesImported : map.namedTypesImported;
323324
}
324-
if (n.specifiers.some((spec) => spec.importKind === 'type')) {
325+
if (!preferInline && n.specifiers.some((spec) => spec.importKind === 'type')) {
325326
return map.namedTypesImported;
326327
}
327328

tests/src/rules/no-duplicates.js

+19
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,25 @@ context('TypeScript', function () {
711711
},
712712
],
713713
}),
714+
// #2834 Detect duplicates across type and regular imports
715+
test({
716+
code: "import {AValue} from './foo'; import type {AType} from './foo'",
717+
...parserConfig,
718+
options: [{ 'prefer-inline': true }],
719+
output: `import {AValue,type AType} from './foo'; `,
720+
errors: [
721+
{
722+
line: 1,
723+
column: 22,
724+
message: "'./foo' imported multiple times.",
725+
},
726+
{
727+
line: 1,
728+
column: 56,
729+
message: "'./foo' imported multiple times.",
730+
},
731+
],
732+
}),
714733
]);
715734

716735
ruleTester.run('no-duplicates', rule, {

0 commit comments

Comments
 (0)