Skip to content

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

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

Merged
merged 6 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/component-name-in-template-casing.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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))) {
return true
}

Expand Down
84 changes: 84 additions & 0 deletions tests/lib/rules/component-name-in-template-casing.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@ tester.run('component-name-in-template-casing', rule, {
<keep-alive />
</template>
`
},

// globals
{
code: `
<template>
<div>
<RouterView />
<RouterLink />
</div>
</template>
`,
options: ['PascalCase', { globals: ['RouterView', 'router-link'] }]
},
{
code: `
<template>
<div>
<router-view />
<router-link />
</div>
</template>
`,
options: ['kebab-case', { globals: ['RouterView', 'router-link'] }]
}
],
invalid: [
Expand Down Expand Up @@ -854,6 +878,66 @@ 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: [
{
message: 'Component name "router-view" is not PascalCase.',
line: 3,
column: 11
}
]
},
{
code: `
<template>
<RouterView />
</template>
`,
options: ['kebab-case', { globals: ['RouterView'] }],
output: `
<template>
<router-view />
</template>
`,
errors: [
{
message: 'Component name "RouterView" is not kebab-case.',
line: 3,
column: 11
}
]
},
{
code: `
<template>
<RouterView />
</template>
`,
options: ['kebab-case', { globals: ['router-view'] }],
output: `
<template>
<router-view />
</template>
`,
errors: [
{
message: 'Component name "RouterView" is not kebab-case.',
line: 3,
column: 11
}
]
}
]
})