Skip to content

Commit

Permalink
fix(eslint-plugin): [consistent-type-imports] import assertion checks…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
rielAsh24 committed Oct 11, 2023
1 parent e5ea1d0 commit afdae37
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
36 changes: 24 additions & 12 deletions packages/eslint-plugin/src/rules/consistent-type-imports.ts
Expand Up @@ -271,17 +271,25 @@ export default 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 @@ -622,7 +630,11 @@ export default 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
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

0 comments on commit afdae37

Please sign in to comment.