Skip to content

Commit ea158e1

Browse files
authored
fix(component-name-in-template-casing): update rule to support <script setup> (#1934)
1 parent 32da3e4 commit ea158e1

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

lib/rules/component-name-in-template-casing.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,22 @@ module.exports = {
6868
context.parserServices.getTemplateBodyTokenStore &&
6969
context.parserServices.getTemplateBodyTokenStore()
7070

71-
/** @type { string[] } */
72-
const registeredComponents = []
71+
/** @type { Set<string> } */
72+
const registeredComponents = new Set()
7373

74+
if (utils.isScriptSetup(context)) {
75+
// For <script setup>
76+
const globalScope = context.getSourceCode().scopeManager.globalScope
77+
if (globalScope) {
78+
// Only check find the import module
79+
const moduleScope = globalScope.childScopes.find(
80+
(scope) => scope.type === 'module'
81+
)
82+
for (const variable of (moduleScope && moduleScope.variables) || []) {
83+
registeredComponents.add(variable.name)
84+
}
85+
}
86+
}
7487
/**
7588
* Checks whether the given node is the verification target node.
7689
* @param {VElement} node element node
@@ -95,7 +108,7 @@ module.exports = {
95108
}
96109
// We only verify the components registered in the component.
97110
if (
98-
registeredComponents
111+
[...registeredComponents]
99112
.filter((name) => casing.isPascalCase(name)) // When defining a component with PascalCase, you can use either case
100113
.some(
101114
(name) =>
@@ -153,9 +166,9 @@ module.exports = {
153166
},
154167
...(registeredComponentsOnly
155168
? utils.executeOnVue(context, (obj) => {
156-
registeredComponents.push(
157-
...utils.getRegisteredComponents(obj).map((n) => n.name)
158-
)
169+
for (const n of utils.getRegisteredComponents(obj)) {
170+
registeredComponents.add(n.name)
171+
}
159172
})
160173
: {})
161174
}

tests/lib/rules/component-name-in-template-casing.js

+40
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,46 @@ tester.run('component-name-in-template-casing', rule, {
814814
'Component name "client-only" is not PascalCase.',
815815
'Component name "keep-alive" is not PascalCase.'
816816
]
817+
},
818+
{
819+
code: `
820+
<template>
821+
<the-component />
822+
</template>
823+
<script setup>
824+
import TheComponent from '@/components/theComponent.vue'
825+
</script>
826+
`,
827+
options: ['PascalCase'],
828+
output: `
829+
<template>
830+
<TheComponent />
831+
</template>
832+
<script setup>
833+
import TheComponent from '@/components/theComponent.vue'
834+
</script>
835+
`,
836+
errors: ['Component name "the-component" is not PascalCase.']
837+
},
838+
{
839+
code: `
840+
<template>
841+
<TheComponent />
842+
</template>
843+
<script setup>
844+
import TheComponent from '@/components/theComponent.vue'
845+
</script>
846+
`,
847+
options: ['kebab-case'],
848+
output: `
849+
<template>
850+
<the-component />
851+
</template>
852+
<script setup>
853+
import TheComponent from '@/components/theComponent.vue'
854+
</script>
855+
`,
856+
errors: ['Component name "TheComponent" is not kebab-case.']
817857
}
818858
]
819859
})

0 commit comments

Comments
 (0)