Skip to content

Commit ae2180c

Browse files
author
lihongda03
committed
fix(no-undef-components): report error on type-only imports
1 parent 9b55f3c commit ae2180c

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

lib/rules/no-undef-components.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,16 @@ module.exports = {
130130
(scope) => scope.type === 'module'
131131
)
132132
for (const variable of (moduleScope && moduleScope.variables) || []) {
133-
scriptVariableNames.add(variable.name)
133+
const definition = variable.defs[0]
134+
if (
135+
!(
136+
(definition.parent?.type === 'ImportDeclaration' &&
137+
definition.parent?.importKind === 'type') ||
138+
definition.node.importKind === 'type'
139+
)
140+
) {
141+
scriptVariableNames.add(variable.name)
142+
}
134143
}
135144
}
136145
/**

tests/lib/rules/no-undef-components.js

+59
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,65 @@ tester.run('no-undef-components', rule, {
661661
}
662662
]
663663
},
664+
{
665+
filename: 'test.vue',
666+
code: `
667+
<script setup lang="ts">
668+
import type Foo from './Foo.vue'
669+
import type {HelloWorld1} from './components/HelloWorld'
670+
import { type HelloWorld2 } from './components/HelloWorld2'
671+
import type {HelloWorld as HelloWorld3} from './components/HelloWorld3'
672+
import { type HelloWorld as HelloWorld4 } from './components/HelloWorld4';
673+
import { type default as HelloWorld5 } from './components/HelloWorld5';
674+
</script>
675+
676+
<template>
677+
<Foo />
678+
<HelloWorld1 />
679+
<HelloWorld2 />
680+
<HelloWorld3 />
681+
<HelloWorld4 />
682+
<HelloWorld5 />
683+
</template>
684+
`,
685+
parserOptions: {
686+
ecmaVersion: 6,
687+
sourceType: 'module',
688+
parser: require.resolve('@typescript-eslint/parser')
689+
},
690+
parser: require.resolve('vue-eslint-parser'),
691+
errors: [
692+
{
693+
message: "The '<Foo>' component has been used, but not defined.",
694+
line: 12
695+
},
696+
{
697+
message:
698+
"The '<HelloWorld1>' component has been used, but not defined.",
699+
line: 13
700+
},
701+
{
702+
message:
703+
"The '<HelloWorld2>' component has been used, but not defined.",
704+
line: 14
705+
},
706+
{
707+
message:
708+
"The '<HelloWorld3>' component has been used, but not defined.",
709+
line: 15
710+
},
711+
{
712+
message:
713+
"The '<HelloWorld4>' component has been used, but not defined.",
714+
line: 16
715+
},
716+
{
717+
message:
718+
"The '<HelloWorld5>' component has been used, but not defined.",
719+
line: 17
720+
}
721+
]
722+
},
664723

665724
// options API
666725
{

typings/eslint-plugin-vue/util-types/ast/es-ast.ts

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ export interface ImportDeclaration extends HasParentNode {
268268
| ImportNamespaceSpecifier
269269
)[]
270270
source: Literal & { value: string }
271+
importKind?: 'type' | 'value'
271272
}
272273
export interface ImportSpecifier extends HasParentNode {
273274
type: 'ImportSpecifier'

0 commit comments

Comments
 (0)