Skip to content

Commit

Permalink
fix(component-name-in-template-casing): update rule to support <scrip…
Browse files Browse the repository at this point in the history
…t setup> (#1934)
  • Loading branch information
BJ0815 committed Jul 21, 2022
1 parent 32da3e4 commit ea158e1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/rules/component-name-in-template-casing.js
Expand Up @@ -68,9 +68,22 @@ module.exports = {
context.parserServices.getTemplateBodyTokenStore &&
context.parserServices.getTemplateBodyTokenStore()

/** @type { string[] } */
const registeredComponents = []
/** @type { Set<string> } */
const registeredComponents = new Set()

if (utils.isScriptSetup(context)) {
// For <script setup>
const globalScope = context.getSourceCode().scopeManager.globalScope
if (globalScope) {
// Only check find the import module
const moduleScope = globalScope.childScopes.find(
(scope) => scope.type === 'module'
)
for (const variable of (moduleScope && moduleScope.variables) || []) {
registeredComponents.add(variable.name)
}
}
}
/**
* Checks whether the given node is the verification target node.
* @param {VElement} node element node
Expand All @@ -95,7 +108,7 @@ module.exports = {
}
// We only verify the components registered in the component.
if (
registeredComponents
[...registeredComponents]
.filter((name) => casing.isPascalCase(name)) // When defining a component with PascalCase, you can use either case
.some(
(name) =>
Expand Down Expand Up @@ -153,9 +166,9 @@ module.exports = {
},
...(registeredComponentsOnly
? utils.executeOnVue(context, (obj) => {
registeredComponents.push(
...utils.getRegisteredComponents(obj).map((n) => n.name)
)
for (const n of utils.getRegisteredComponents(obj)) {
registeredComponents.add(n.name)
}
})
: {})
}
Expand Down
40 changes: 40 additions & 0 deletions tests/lib/rules/component-name-in-template-casing.js
Expand Up @@ -814,6 +814,46 @@ tester.run('component-name-in-template-casing', rule, {
'Component name "client-only" is not PascalCase.',
'Component name "keep-alive" is not PascalCase.'
]
},
{
code: `
<template>
<the-component />
</template>
<script setup>
import TheComponent from '@/components/theComponent.vue'
</script>
`,
options: ['PascalCase'],
output: `
<template>
<TheComponent />
</template>
<script setup>
import TheComponent from '@/components/theComponent.vue'
</script>
`,
errors: ['Component name "the-component" is not PascalCase.']
},
{
code: `
<template>
<TheComponent />
</template>
<script setup>
import TheComponent from '@/components/theComponent.vue'
</script>
`,
options: ['kebab-case'],
output: `
<template>
<the-component />
</template>
<script setup>
import TheComponent from '@/components/theComponent.vue'
</script>
`,
errors: ['Component name "TheComponent" is not kebab-case.']
}
]
})

0 comments on commit ea158e1

Please sign in to comment.