From fad89c71c39b5c0f62ea87099687e0ce97f82f32 Mon Sep 17 00:00:00 2001 From: Ashriel Date: Sun, 1 Oct 2023 23:59:47 +0530 Subject: [PATCH 1/2] Updated consistent-type-imports Added type assertion for json files --- .../tests/rules/consistent-type-imports.test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/consistent-type-imports.test.ts b/packages/eslint-plugin/tests/rules/consistent-type-imports.test.ts index ae171e0d87e8..055f8473ee28 100644 --- a/packages/eslint-plugin/tests/rules/consistent-type-imports.test.ts +++ b/packages/eslint-plugin/tests/rules/consistent-type-imports.test.ts @@ -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; From 81f63d6ab06660e4b29fd2ef94e7775443805730 Mon Sep 17 00:00:00 2001 From: Ashriel Date: Wed, 4 Oct 2023 18:19:03 +0530 Subject: [PATCH 2/2] Import assertion checks added The linter checks for import assertions before suggesting fixes --- .../src/rules/consistent-type-imports.ts | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/packages/eslint-plugin/src/rules/consistent-type-imports.ts b/packages/eslint-plugin/src/rules/consistent-type-imports.ts index 9aaa296a4b0c..c23323411c66 100644 --- a/packages/eslint-plugin/src/rules/consistent-type-imports.ts +++ b/packages/eslint-plugin/src/rules/consistent-type-imports.ts @@ -261,17 +261,25 @@ export default util.createRule({ 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'; @@ -612,7 +620,11 @@ export default util.createRule({ 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 (