Skip to content

Commit

Permalink
fix: Removing duplicates breaks in TypeScript
Browse files Browse the repository at this point in the history
dquote>
dquote> fixes import-js#3016
  • Loading branch information
yesl-kim committed Aug 11, 2024
1 parent 09476d7 commit 4ddcfe7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function getFix(first, rest, sourceCode, context) {
const shouldAddDefault = getDefaultImportName(first) == null && defaultImportNames.size === 1;
const shouldAddSpecifiers = specifiers.length > 0;
const shouldRemoveUnnecessary = unnecessaryImports.length > 0;
const preferInline = context.options[0] && context.options[0]['prefer-inline'];

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

const preferInline = context.options[0] && context.options[0]['prefer-inline'];
// 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.
if (preferInline && (!typescriptPkg || !semver.satisfies(typescriptPkg.version, '>= 4.5'))) {
throw new Error('Your version of TypeScript does not support inline type imports.');
Expand Down Expand Up @@ -186,6 +186,17 @@ function getFix(first, rest, sourceCode, context) {

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');
fixes.push(fixer.removeRange([typeIdentifierToken.range[0], typeIdentifierToken.range[1] + 1]));

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

if (shouldAddDefault && openBrace == null && shouldAddSpecifiers) {
// `import './foo'` → `import def, {...} from './foo'`
fixes.push(
Expand Down
18 changes: 18 additions & 0 deletions tests/src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,24 @@ context('TypeScript', function () {
},
],
}),
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 4ddcfe7

Please sign in to comment.