Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update vue/component-name-in-template-casing rule to support <script setup> #1934

Merged
merged 1 commit into from Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.']
}
]
})