Skip to content

Commit eb8882d

Browse files
committed
fix: Removing duplicates breaks in TypeScript
fixes #3016
1 parent 09476d7 commit eb8882d

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/rules/no-duplicates.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ function getFix(first, rest, sourceCode, context) {
132132
const shouldAddDefault = getDefaultImportName(first) == null && defaultImportNames.size === 1;
133133
const shouldAddSpecifiers = specifiers.length > 0;
134134
const shouldRemoveUnnecessary = unnecessaryImports.length > 0;
135+
const preferInline = context.options[0] && context.options[0]['prefer-inline'];
135136

136137
if (!(shouldAddDefault || shouldAddSpecifiers || shouldRemoveUnnecessary)) {
137138
return undefined;
@@ -157,7 +158,6 @@ function getFix(first, rest, sourceCode, context) {
157158
([result, needsComma, existingIdentifiers], specifier) => {
158159
const isTypeSpecifier = specifier.importNode.importKind === 'type';
159160

160-
const preferInline = context.options[0] && context.options[0]['prefer-inline'];
161161
// a user might set prefer-inline but not have a supporting TypeScript version. Flow does not support inline types so this should fail in that case as well.
162162
if (preferInline && (!typescriptPkg || !semver.satisfies(typescriptPkg.version, '>= 4.5'))) {
163163
throw new Error('Your version of TypeScript does not support inline type imports.');
@@ -186,6 +186,17 @@ function getFix(first, rest, sourceCode, context) {
186186

187187
const fixes = [];
188188

189+
if (shouldAddSpecifiers && preferInline && first.importKind === 'type') {
190+
// `import type {a} from './foo'` → `import {type a} from './foo'`
191+
const typeIdentifierToken = tokens.find((token) => token.type === 'Identifier' && token.value === 'type');
192+
fixes.push(fixer.removeRange([typeIdentifierToken.range[0], typeIdentifierToken.range[1] + 1]));
193+
194+
const firstIdentifierTokens = tokens.filter((token) => firstExistingIdentifiers.has(token.value));
195+
for (const identifier of firstIdentifierTokens) {
196+
fixes.push(fixer.replaceTextRange([identifier.range[0], identifier.range[1]], `type ${identifier.value}`));
197+
}
198+
}
199+
189200
if (shouldAddDefault && openBrace == null && shouldAddSpecifiers) {
190201
// `import './foo'` → `import def, {...} from './foo'`
191202
fixes.push(

tests/src/rules/no-duplicates.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,24 @@ context('TypeScript', function () {
704704
},
705705
],
706706
}),
707+
test({
708+
code: "import type {x} from 'foo'; import {type y} from 'foo'",
709+
...parserConfig,
710+
options: [{ 'prefer-inline': true }],
711+
output: `import {type x,type y} from 'foo'; `,
712+
errors: [
713+
{
714+
line: 1,
715+
column: 22,
716+
message: "'foo' imported multiple times.",
717+
},
718+
{
719+
line: 1,
720+
column: 50,
721+
message: "'foo' imported multiple times.",
722+
},
723+
],
724+
}),
707725
test({
708726
code: "import {type x} from 'foo'; import type {y} from 'foo'",
709727
...parserConfig,

0 commit comments

Comments
 (0)