Skip to content

Commit afdae37

Browse files
authored
fix(eslint-plugin): [consistent-type-imports] import assertion checks added (#7722)
* Updated consistent-type-imports Added type assertion for json files * Import assertion checks added The linter checks for import assertions before suggesting fixes
1 parent e5ea1d0 commit afdae37

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

‎packages/eslint-plugin/src/rules/consistent-type-imports.ts‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,25 @@ export default createRule<Options, MessageIds>({
271271
report.unusedSpecifiers.length === 0 &&
272272
report.node.importKind !== 'type'
273273
) {
274-
context.report({
275-
node: report.node,
276-
messageId: 'typeOverValue',
277-
*fix(fixer) {
278-
yield* fixToTypeImportDeclaration(
279-
fixer,
280-
report,
281-
sourceImports,
282-
);
283-
},
284-
});
274+
/** checks if import has type assertions
275+
* ```
276+
* import * as type from 'mod' assert { type: 'json' };
277+
* ```
278+
* https://github.com/typescript-eslint/typescript-eslint/issues/7527
279+
*/
280+
if (report.node.assertions.length === 0) {
281+
context.report({
282+
node: report.node,
283+
messageId: 'typeOverValue',
284+
*fix(fixer) {
285+
yield* fixToTypeImportDeclaration(
286+
fixer,
287+
report,
288+
sourceImports,
289+
);
290+
},
291+
});
292+
}
285293
} else {
286294
const isTypeImport = report.node.importKind === 'type';
287295

@@ -622,7 +630,11 @@ export default createRule<Options, MessageIds>({
622630

623631
if (namespaceSpecifier && !defaultSpecifier) {
624632
// import * as types from 'foo'
625-
yield* fixInsertTypeSpecifierForImportDeclaration(fixer, node, false);
633+
634+
// checks for presence of import assertions
635+
if (node.assertions.length === 0) {
636+
yield* fixInsertTypeSpecifierForImportDeclaration(fixer, node, false);
637+
}
626638
return;
627639
} else if (defaultSpecifier) {
628640
if (

‎packages/eslint-plugin/tests/rules/consistent-type-imports.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ ruleTester.run('consistent-type-imports', rule, {
125125
`,
126126
options: [{ prefer: 'no-type-imports' }],
127127
},
128+
{
129+
code: `
130+
import * as Type from 'foo' assert { type: 'json' };
131+
const a: typeof Type = Type;
132+
`,
133+
options: [{ prefer: 'no-type-imports' }],
134+
dependencyConstraints: {
135+
typescript: '4.5',
136+
},
137+
},
128138
`
129139
import { type A } from 'foo';
130140
type T = A;

0 commit comments

Comments
 (0)