diff --git a/packages/eslint-plugin-antfu/src/rules/prefer-inline-type-import.test.ts b/packages/eslint-plugin-antfu/src/rules/prefer-inline-type-import.test.ts new file mode 100644 index 0000000..f2e92fb --- /dev/null +++ b/packages/eslint-plugin-antfu/src/rules/prefer-inline-type-import.test.ts @@ -0,0 +1,27 @@ +import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint' +import { it } from 'vitest' +import rule, { RULE_NAME } from './prefer-inline-type-import' + +const valids = [ + 'import { type Foo } from \'foo\'', + 'import type Foo from \'foo\'', + 'import type * as Foo from \'foo\'', +] +const invalids = [ + ['import type { Foo } from \'foo\'', 'import { type Foo } from \'foo\''], +] + +it('runs', () => { + const ruleTester: RuleTester = new RuleTester({ + parser: require.resolve('@typescript-eslint/parser'), + }) + + ruleTester.run(RULE_NAME, rule, { + valid: valids, + invalid: invalids.map(i => ({ + code: i[0], + output: i[1].trim(), + errors: [{ messageId: 'preferInlineTypeImport' }], + })), + }) +}) diff --git a/packages/eslint-plugin-antfu/src/rules/prefer-inline-type-import.ts b/packages/eslint-plugin-antfu/src/rules/prefer-inline-type-import.ts index 34f076d..072e906 100644 --- a/packages/eslint-plugin-antfu/src/rules/prefer-inline-type-import.ts +++ b/packages/eslint-plugin-antfu/src/rules/prefer-inline-type-import.ts @@ -26,6 +26,9 @@ export default createEslintRule({ const sourceCode = context.getSourceCode() return { ImportDeclaration: (node) => { + // ignore bare type imports + if (node.specifiers.length === 1 && ['ImportNamespaceSpecifier', 'ImportDefaultSpecifier'].includes(node.specifiers[0].type)) + return if (node.importKind === 'type') { context.report({ *fix(fixer) {