Skip to content

Commit

Permalink
fix(component-name-in-template-casing): ignore vue template syntax (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Oct 24, 2023
1 parent 467631e commit 66a678f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/rules/component-name-in-template-casing.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ module.exports = {
if (
(!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) ||
utils.isHtmlWellKnownElementName(node.rawName) ||
utils.isSvgWellKnownElementName(node.rawName)
utils.isSvgWellKnownElementName(node.rawName) ||
utils.isVueBuiltInElementName(node.rawName)
) {
return false
}
Expand Down
10 changes: 10 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const VUE2_BUILTIN_COMPONENT_NAMES = new Set(
const VUE3_BUILTIN_COMPONENT_NAMES = new Set(
require('./vue3-builtin-components')
)
const VUE_BUILTIN_ELEMENT_NAMES = new Set(require('./vue-builtin-elements'))
const path = require('path')
const vueEslintParser = require('vue-eslint-parser')
const { traverseNodes, getFallbackKeys, NS } = vueEslintParser.AST
Expand Down Expand Up @@ -867,6 +868,15 @@ module.exports = {
)
},

/**
* Check whether the given name is Vue builtin element name or not.
* @param {string} name The name to check.
* @returns {boolean} `true` if the name is a builtin Vue element name
*/
isVueBuiltInElementName(name) {
return VUE_BUILTIN_ELEMENT_NAMES.has(name.toLowerCase())
},

/**
* Check whether the given name is Vue builtin directive name or not.
* @param {string} name The name to check.
Expand Down
1 change: 1 addition & 0 deletions lib/utils/vue-builtin-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = ['template', 'slot', 'component']
18 changes: 10 additions & 8 deletions tests/lib/rules/component-name-in-template-casing.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ tester.run('component-name-in-template-casing', rule, {
code: '<template><circle cx="0" cy="0" :d="radius"></template>',
options: ['PascalCase', { registeredComponentsOnly: false }]
},
{
code: '<template><component is="div" /></template>',
options: ['PascalCase', { registeredComponentsOnly: false }]
},

// kebab-case
{
Expand All @@ -108,6 +112,10 @@ tester.run('component-name-in-template-casing', rule, {
code: '<template><math><mspace/></math></template>',
options: ['kebab-case', { registeredComponentsOnly: false }]
},
{
code: '<template><component is="div" /></template>',
options: ['kebab-case', { registeredComponentsOnly: false }]
},

// ignores
{
Expand Down Expand Up @@ -859,7 +867,7 @@ tester.run('component-name-in-template-casing', rule, {
`,
output: `
<template>
<Component />
<component />
<Suspense />
<Teleport />
<ClientOnly />
Expand All @@ -868,7 +876,6 @@ tester.run('component-name-in-template-casing', rule, {
`,
options: ['PascalCase', { registeredComponentsOnly: false }],
errors: [
'Component name "component" is not PascalCase.',
'Component name "suspense" is not PascalCase.',
'Component name "teleport" is not PascalCase.',
'Component name "client-only" is not PascalCase.',
Expand Down Expand Up @@ -1025,7 +1032,7 @@ tester.run('component-name-in-template-casing', rule, {
<HelloWorld3 />
<HelloWorld4 />
<HelloWorld5 />
<Component />
<component />
</template>
`,
errors: [
Expand Down Expand Up @@ -1058,11 +1065,6 @@ tester.run('component-name-in-template-casing', rule, {
message: 'Component name "hello-world5" is not PascalCase.',
line: 18,
column: 17
},
{
message: 'Component name "component" is not PascalCase.',
line: 19,
column: 17
}
]
}
Expand Down

0 comments on commit 66a678f

Please sign in to comment.