Skip to content

Commit 0b51c93

Browse files
authored
fix(no-unused-components): match registered names the way Vue resolves components (#3248)
1 parent 7bb5b40 commit 0b51c93

7 files changed

Lines changed: 269 additions & 51 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-vue": patch
3+
---
4+
5+
Fixed `vue/no-unused-components` to match registered names the same way Vue resolves components, so e.g. a `myComponent` registration used only as `<MyComponent>` (which Vue cannot resolve) is now reported

‎docs/rules/no-unused-components.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export default {
8989
</eslint-code-block>
9090

9191
::: warning Note
92-
Components registered under `PascalCase` or `camelCase` names may be called however you like, except using `snake_case`. Otherwise, they will need to be called directly under the specified name.
92+
A component counts as used when Vue can resolve it from the template, the same way Vue does at runtime. For a tag in the template, Vue looks for a registration named exactly like the tag, then like the tag in `camelCase`, then like the tag in `PascalCase`. So a component registered as `TheButton` is found by `<TheButton>`, `<the-button>` and `<theButton>`. But a component registered as `theButton` is not found by `<TheButton>`, because Vue never lowercases the first letter of a tag.
9393
:::
9494

9595
## :wrench: Options

‎lib/rules/no-undef-components.ts‎

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,7 @@
44
*/
55
import path from 'node:path'
66
import utils from '../utils/index.ts'
7-
import {
8-
capitalize,
9-
isPascalCase,
10-
kebabCase,
11-
pascalCase
12-
} from '../utils/casing.ts'
13-
14-
/**
15-
* `casing.camelCase()` converts the beginning to lowercase,
16-
* but does not convert the case of the beginning character when converting with Vue3.
17-
* @see https://github.com/vuejs/core/blob/ae4b0783d78670b6e942ae2a4e3ec6efbbffa158/packages/shared/src/index.ts#L105
18-
*/
19-
function camelize(str: string) {
20-
return str.replaceAll(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
21-
}
7+
import { isPascalCase, kebabCase, pascalCase } from '../utils/casing.ts'
228

239
class DefinedInSetupComponents {
2410
/**
@@ -36,15 +22,11 @@ class DefinedInSetupComponents {
3622
* @see https://github.com/vuejs/core/blob/ae4b0783d78670b6e942ae2a4e3ec6efbbffa158/packages/compiler-core/src/transforms/transformElement.ts#L334
3723
*/
3824
isDefinedComponent(rawName: string): boolean {
39-
if (this.names.has(rawName)) {
40-
return true
41-
}
42-
const camelName = camelize(rawName)
43-
if (this.names.has(camelName)) {
44-
return true
45-
}
46-
const pascalName = capitalize(camelName)
47-
if (this.names.has(pascalName)) {
25+
if (
26+
utils
27+
.getRegisteredComponentNameCandidates(rawName)
28+
.some((name) => this.names.has(name))
29+
) {
4830
return true
4931
}
5032
// Check namespace

‎lib/rules/no-unused-components.ts‎

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
* @author Michał Sajnóg
44
*/
55
import utils from '../utils/index.ts'
6-
import {
7-
isPascalCase,
8-
isCamelCase,
9-
pascalCase,
10-
camelCase
11-
} from '../utils/casing.ts'
126

137
export default {
148
meta: {
@@ -76,6 +70,8 @@ export default {
7670
: null
7771
if (name !== null) {
7872
usedComponents.add(name)
73+
} else if (expression.type === 'Literal') {
74+
// `:is="null"` renders nothing, so a non-string literal names no component
7975
} else if (ignoreWhenBindingPresent) {
8076
shouldIgnoreReporting = true
8177
}
@@ -100,27 +96,14 @@ export default {
10096
)
10197
return
10298

99+
const resolvableNames = new Set(
100+
[...usedComponents].flatMap((name) =>
101+
utils.getRegisteredComponentNameCandidates(name)
102+
)
103+
)
103104
for (const { node, name } of registeredComponents) {
104-
// If the component name is PascalCase or camelCase
105-
// it can be used in various of ways inside template,
106-
// like "theComponent", "The-component" etc.
107-
// but except snake_case
108-
if (isPascalCase(name) || isCamelCase(name)) {
109-
if (
110-
[...usedComponents].some(
111-
(n) =>
112-
!n.includes('_') &&
113-
(name === pascalCase(n) || name === camelCase(n))
114-
)
115-
) {
116-
continue
117-
}
118-
} else {
119-
// In any other case the used component name must exactly match
120-
// the registered name
121-
if (usedComponents.has(name)) {
122-
continue
123-
}
105+
if (resolvableNames.has(name)) {
106+
continue
124107
}
125108

126109
context.report({

‎lib/utils/index.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import type {
3030
ScriptSetupVisitor
3131
} from '../../typings/eslint-plugin-vue/util-types/utils.ts'
3232
import { getScope } from './scope.ts'
33+
import { capitalize } from './casing.ts'
3334
import {
3435
getComponentPropsFromTypeDefine,
3536
getComponentEmitsFromTypeDefine,
@@ -853,6 +854,19 @@ const utils = {
853854
.filter(isDef)
854855
},
855856

857+
/**
858+
* Get the registered component names that Vue looks up, in order, to resolve
859+
* a component used as `name`: the name itself, then its camelized form,
860+
* then its capitalized camelized form.
861+
* e.g. `my-comp` gives `['my-comp', 'myComp', 'MyComp']`.
862+
* Vue never lowercases the first letter, so `MyComp` does not find `myComp`.
863+
* @see https://github.com/vuejs/core/blob/ae4b0783d78670b6e942ae2a4e3ec6efbbffa158/packages/runtime-core/src/helpers/resolveAssets.ts#L128-L135
864+
*/
865+
getRegisteredComponentNameCandidates(name: string): string[] {
866+
const camelName = camelize(name)
867+
return [name, camelName, capitalize(camelName)]
868+
},
869+
856870
/**
857871
* Check whether the previous sibling element has `if` or `else-if` directive.
858872
*/
@@ -2184,13 +2198,23 @@ export const {
21842198
executeOnVueComponent,
21852199
getComputedProperties,
21862200
getMemberChaining,
2201+
getRegisteredComponentNameCandidates,
21872202
getRegisteredComponents
21882203
} = utils
21892204

21902205
// ------------------------------------------------------------------------------
21912206
// Standard Helpers
21922207
// ------------------------------------------------------------------------------
21932208

2209+
/**
2210+
* Camelize the given string the way Vue does: only `-x` becomes `X`.
2211+
* Unlike `casing.camelCase()`, the first letter keeps its case and `_` is kept.
2212+
* @see https://github.com/vuejs/core/blob/ae4b0783d78670b6e942ae2a4e3ec6efbbffa158/packages/shared/src/index.ts#L105
2213+
*/
2214+
function camelize(str: string): string {
2215+
return str.replaceAll(/-(\w)/gu, (_, c: string) => c.toUpperCase())
2216+
}
2217+
21942218
/**
21952219
* Checks whether the given value is defined.
21962220
*/

‎tests/lib/rules/no-unused-components.test.ts‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,69 @@ tester.run('no-unused-components', rule, {
485485
}
486486
</script>
487487
`
488+
},
489+
490+
// Vue tries the used name, its camelized form and its capitalized camelized form
491+
{
492+
filename: 'test.vue',
493+
code: `
494+
<template>
495+
<myCoolComponent />
496+
</template>
497+
<script>
498+
export default {
499+
components: {
500+
MyCoolComponent
501+
},
502+
}
503+
</script>
504+
`
505+
},
506+
{
507+
filename: 'test.vue',
508+
code: `
509+
<template>
510+
<my-cool-component />
511+
</template>
512+
<script>
513+
export default {
514+
components: {
515+
myCoolComponent
516+
},
517+
}
518+
</script>
519+
`
520+
},
521+
{
522+
filename: 'test.vue',
523+
code: `
524+
<template>
525+
<component is="my-cool-component" />
526+
</template>
527+
<script>
528+
export default {
529+
components: {
530+
myCoolComponent
531+
},
532+
}
533+
</script>
534+
`
535+
},
536+
{
537+
filename: 'test.vue',
538+
code: `
539+
<template>
540+
<component :is="null" />
541+
<my-comp />
542+
</template>
543+
<script>
544+
export default {
545+
components: {
546+
MyComp
547+
},
548+
}
549+
</script>
550+
`
488551
}
489552
],
490553
invalid: [
@@ -747,6 +810,130 @@ tester.run('no-unused-components', rule, {
747810
endColumn: 18
748811
}
749812
]
813+
},
814+
815+
// Vue does not lower the first letter, so these do not resolve to the registered component
816+
{
817+
filename: 'test.vue',
818+
code: `
819+
<template>
820+
<MyCoolComponent />
821+
</template>
822+
<script>
823+
export default {
824+
components: {
825+
myCoolComponent
826+
},
827+
}
828+
</script>
829+
`,
830+
errors: [
831+
{
832+
message:
833+
'The "myCoolComponent" component has been registered but not used.',
834+
line: 8,
835+
column: 15,
836+
endLine: 8,
837+
endColumn: 30
838+
}
839+
]
840+
},
841+
{
842+
filename: 'test.vue',
843+
code: `
844+
<template>
845+
<The-component />
846+
</template>
847+
<script>
848+
export default {
849+
components: {
850+
theComponent
851+
},
852+
}
853+
</script>
854+
`,
855+
errors: [
856+
{
857+
message:
858+
'The "theComponent" component has been registered but not used.',
859+
line: 8,
860+
column: 15,
861+
endLine: 8,
862+
endColumn: 27
863+
}
864+
]
865+
},
866+
{
867+
filename: 'test.vue',
868+
code: `
869+
<template>
870+
<Foo />
871+
</template>
872+
<script>
873+
export default {
874+
components: {
875+
foo
876+
},
877+
}
878+
</script>
879+
`,
880+
errors: [
881+
{
882+
message: 'The "foo" component has been registered but not used.',
883+
line: 8,
884+
column: 15,
885+
endLine: 8,
886+
endColumn: 18
887+
}
888+
]
889+
},
890+
{
891+
filename: 'test.vue',
892+
code: `
893+
<template>
894+
<component is="Foo" />
895+
</template>
896+
<script>
897+
export default {
898+
components: {
899+
foo
900+
},
901+
}
902+
</script>
903+
`,
904+
errors: [
905+
{
906+
message: 'The "foo" component has been registered but not used.',
907+
line: 8,
908+
column: 15,
909+
endLine: 8,
910+
endColumn: 18
911+
}
912+
]
913+
},
914+
{
915+
filename: 'test.vue',
916+
code: `
917+
<template>
918+
<component :is="1" />
919+
</template>
920+
<script>
921+
export default {
922+
components: {
923+
MyComp
924+
},
925+
}
926+
</script>
927+
`,
928+
errors: [
929+
{
930+
message: 'The "MyComp" component has been registered but not used.',
931+
line: 8,
932+
column: 15,
933+
endLine: 8,
934+
endColumn: 21
935+
}
936+
]
750937
}
751938
]
752939
})

0 commit comments

Comments
 (0)