Skip to content

Commit

Permalink
fix(no-undef-components): report error on type-only imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lihongda03 committed Sep 10, 2022
1 parent 9b55f3c commit ae2180c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/rules/no-undef-components.js
Expand Up @@ -130,7 +130,16 @@ module.exports = {
(scope) => scope.type === 'module'
)
for (const variable of (moduleScope && moduleScope.variables) || []) {
scriptVariableNames.add(variable.name)
const definition = variable.defs[0]
if (
!(
(definition.parent?.type === 'ImportDeclaration' &&
definition.parent?.importKind === 'type') ||
definition.node.importKind === 'type'
)
) {
scriptVariableNames.add(variable.name)
}
}
}
/**
Expand Down
59 changes: 59 additions & 0 deletions tests/lib/rules/no-undef-components.js
Expand Up @@ -661,6 +661,65 @@ tester.run('no-undef-components', rule, {
}
]
},
{
filename: 'test.vue',
code: `
<script setup lang="ts">
import type Foo from './Foo.vue'
import type {HelloWorld1} from './components/HelloWorld'
import { type HelloWorld2 } from './components/HelloWorld2'
import type {HelloWorld as HelloWorld3} from './components/HelloWorld3'
import { type HelloWorld as HelloWorld4 } from './components/HelloWorld4';
import { type default as HelloWorld5 } from './components/HelloWorld5';
</script>
<template>
<Foo />
<HelloWorld1 />
<HelloWorld2 />
<HelloWorld3 />
<HelloWorld4 />
<HelloWorld5 />
</template>
`,
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
parser: require.resolve('@typescript-eslint/parser')
},
parser: require.resolve('vue-eslint-parser'),
errors: [
{
message: "The '<Foo>' component has been used, but not defined.",
line: 12
},
{
message:
"The '<HelloWorld1>' component has been used, but not defined.",
line: 13
},
{
message:
"The '<HelloWorld2>' component has been used, but not defined.",
line: 14
},
{
message:
"The '<HelloWorld3>' component has been used, but not defined.",
line: 15
},
{
message:
"The '<HelloWorld4>' component has been used, but not defined.",
line: 16
},
{
message:
"The '<HelloWorld5>' component has been used, but not defined.",
line: 17
}
]
},

// options API
{
Expand Down
1 change: 1 addition & 0 deletions typings/eslint-plugin-vue/util-types/ast/es-ast.ts
Expand Up @@ -268,6 +268,7 @@ export interface ImportDeclaration extends HasParentNode {
| ImportNamespaceSpecifier
)[]
source: Literal & { value: string }
importKind?: 'type' | 'value'
}
export interface ImportSpecifier extends HasParentNode {
type: 'ImportSpecifier'
Expand Down

0 comments on commit ae2180c

Please sign in to comment.