Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions packages/eslint-plugin/src/rules/consistent-type-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,25 @@ export default util.createRule<Options, MessageIds>({
report.unusedSpecifiers.length === 0 &&
report.node.importKind !== 'type'
) {
context.report({
node: report.node,
messageId: 'typeOverValue',
*fix(fixer) {
yield* fixToTypeImportDeclaration(
fixer,
report,
sourceImports,
);
},
});
/** checks if import has type assertions
* ```
* import * as type from 'mod' assert { type: 'json' };
* ```
* https://github.com/typescript-eslint/typescript-eslint/issues/7527
*/
if (report.node.assertions.length === 0) {
context.report({
node: report.node,
messageId: 'typeOverValue',
*fix(fixer) {
yield* fixToTypeImportDeclaration(
fixer,
report,
sourceImports,
);
},
});
}
} else {
const isTypeImport = report.node.importKind === 'type';

Expand Down Expand Up @@ -612,7 +620,11 @@ export default util.createRule<Options, MessageIds>({

if (namespaceSpecifier && !defaultSpecifier) {
// import * as types from 'foo'
yield* fixInsertTypeSpecifierForImportDeclaration(fixer, node, false);

// checks for presence of import assertions
if (node.assertions.length === 0) {
yield* fixInsertTypeSpecifierForImportDeclaration(fixer, node, false);
}
return;
} else if (defaultSpecifier) {
if (
Expand Down
10 changes: 10 additions & 0 deletions packages/eslint-plugin/tests/rules/consistent-type-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ ruleTester.run('consistent-type-imports', rule, {
`,
options: [{ prefer: 'no-type-imports' }],
},
{
code: `
import * as Type from 'foo' assert { type: 'json' };
const a: typeof Type = Type;
`,
options: [{ prefer: 'no-type-imports' }],
dependencyConstraints: {
typescript: '4.5',
},
},
`
import { type A } from 'foo';
type T = A;
Expand Down