fix(inline-type): ignore default import

This commit is contained in:
Anthony Fu 2022-04-07 06:06:50 +08:00
parent 0af15d4bf4
commit 07a15be095
2 changed files with 30 additions and 0 deletions

View File

@ -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' }],
})),
})
})

View File

@ -26,6 +26,9 @@ export default createEslintRule<Options, MessageIds>({
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) {