Skip to content

Commit

Permalink
fix: backports import-js#3033
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Sep 4, 2024
1 parent ceb8e65 commit b068d54
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-shirts-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

Fix `no-duplicates` for TypeScript, backports https://github.com/import-js/eslint-plugin-import/pull/3033
26 changes: 26 additions & 0 deletions src/rules/no-duplicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,32 @@ function getFix(

const fixes = []

if (shouldAddSpecifiers && preferInline && first.importKind === 'type') {
// `import type {a} from './foo'` → `import {type a} from './foo'`
const typeIdentifierToken = tokens.find(
token => token.type === 'Identifier' && token.value === 'type',
)
if (typeIdentifierToken) {
fixes.push(
fixer.removeRange([
typeIdentifierToken.range[0],
typeIdentifierToken.range[1] + 1,
]),
)
}

for (const identifier of tokens.filter(token =>
firstExistingIdentifiers.has(token.value),
)) {
fixes.push(
fixer.replaceTextRange(
[identifier.range[0], identifier.range[1]],
`type ${identifier.value}`,
),
)
}
}

if (openBrace == null && shouldAddSpecifiers && shouldAddDefault()) {
// `import './foo'` → `import def, {...} from './foo'`
fixes.push(
Expand Down
18 changes: 18 additions & 0 deletions test/rules/no-duplicates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,24 @@ describe('TypeScript', () => {
},
],
}),
test({
code: "import type {x} from 'foo'; import {type y} from 'foo'",
...parserConfig,
options: [{ 'prefer-inline': true }],
output: `import {type x,type y} from 'foo'; `,
errors: [
{
line: 1,
column: 22,
message: "'foo' imported multiple times.",
},
{
line: 1,
column: 50,
message: "'foo' imported multiple times.",
},
],
}),
test({
code: "import {type x} from 'foo'; import type {y} from 'foo'",
...parserConfig,
Expand Down

0 comments on commit b068d54

Please sign in to comment.