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

Add globals option to component-name-in-template-casing #1989

Merged
merged 6 commits into from Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions docs/rules/component-name-in-template-casing.md
Expand Up @@ -33,6 +33,7 @@ This rule aims to warn the tag names other than the configured casing in Vue.js
- `registeredComponentsOnly` ... If `true`, only registered components (in PascalCase) are checked. If `false`, check all.
default `true`
- `ignores` (`string[]`) ... The element names to ignore. Sets the element name to allow. For example, custom elements or Vue components with special name. You can set the regexp by writing it like `"/^name/"`.
- `globals` (`string[]`) ... Globally registered component names to check. For example, `RouterView` and `RouterLink` are globally registered by `vue-router` and can't be detected as registered in a SFC file.

### `"PascalCase", { registeredComponentsOnly: true }` (default)

Expand Down
20 changes: 10 additions & 10 deletions lib/rules/component-name-in-template-casing.js
Expand Up @@ -40,6 +40,11 @@ module.exports = {
{
type: 'object',
properties: {
globals: {
type: 'array',
items: { type: 'string' },
uniqueItems: true
},
ignores: {
type: 'array',
items: { type: 'string' },
Expand All @@ -63,13 +68,15 @@ module.exports = {
: defaultCase
/** @type {RegExp[]} */
const ignores = (options.ignores || []).map(toRegExp)
/** @type {string[]} */
const globals = (options.globals || []).map(casing.pascalCase)
const registeredComponentsOnly = options.registeredComponentsOnly !== false
const tokens =
context.parserServices.getTemplateBodyTokenStore &&
context.parserServices.getTemplateBodyTokenStore()

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

if (utils.isScriptSetup(context)) {
// For <script setup>
Expand Down Expand Up @@ -106,15 +113,8 @@ module.exports = {
}
return true
}
// We only verify the components registered in the component.
if (
[...registeredComponents]
.filter((name) => casing.isPascalCase(name)) // When defining a component with PascalCase, you can use either case
.some(
(name) =>
node.rawName === name || casing.pascalCase(node.rawName) === name
)
) {
// We only verify the registered components.
if (registeredComponents.has(casing.pascalCase(node.rawName))) {
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved
return true
}

Expand Down
76 changes: 76 additions & 0 deletions tests/lib/rules/component-name-in-template-casing.js
Expand Up @@ -168,6 +168,40 @@ tester.run('component-name-in-template-casing', rule, {
<keep-alive />
</template>
`
},

// globals
{
code: `
<template>
<RouterView />
</template>
`,
options: ['PascalCase', { globals: ['RouterView'] }]
},
{
code: `
<template>
<RouterView />
</template>
`,
options: ['PascalCase', { globals: ['router-view'] }]
lsdsjy marked this conversation as resolved.
Show resolved Hide resolved
},
{
code: `
<template>
<router-view />
</template>
`,
options: ['kebab-case', { globals: ['RouterView'] }]
},
{
code: `
<template>
<router-view />
</template>
`,
options: ['kebab-case', { globals: ['router-view'] }]
}
lsdsjy marked this conversation as resolved.
Show resolved Hide resolved
],
invalid: [
Expand Down Expand Up @@ -854,6 +888,48 @@ tester.run('component-name-in-template-casing', rule, {
</script>
`,
errors: ['Component name "TheComponent" is not kebab-case.']
},
{
code: `
<template>
<router-view />
</template>
`,
options: ['PascalCase', { globals: ['RouterView'] }],
output: `
<template>
<RouterView />
</template>
`,
errors: ['Component name "router-view" is not PascalCase.']
lsdsjy marked this conversation as resolved.
Show resolved Hide resolved
},
{
code: `
<template>
<RouterView />
</template>
`,
options: ['kebab-case', { globals: ['RouterView'] }],
output: `
<template>
<router-view />
</template>
`,
errors: ['Component name "RouterView" is not kebab-case.']
},
{
code: `
<template>
<RouterView />
</template>
`,
options: ['kebab-case', { globals: ['router-view'] }],
output: `
<template>
<router-view />
</template>
`,
errors: ['Component name "RouterView" is not kebab-case.']
}
]
})