Skip to content

Commit b19843c

Browse files
shadskiiota-meshi
authored andcommitted
⭐️ New: Add rule no-reserved-component-names (#757)
* ⭐ add rule no-reserved-component-names * Increase test coverage * Checking elements against element lists * 🔨 Update PR with ota-meshi suggestions * 🔨 Lint locally registered components * 👌 Adding tests to validate slot and template * 🔨 Linting for deprecated elements
1 parent 06dc4a2 commit b19843c

File tree

4 files changed

+529
-0
lines changed

4 files changed

+529
-0
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# vue/no-reserved-component-names
2+
> disallow the use of reserved names in component definitions
3+
4+
- :gear: This rule is included in all of `"plugin:vue/essential"`, `"plugin:vue/recommended"`, and `"plugin:vue/strongly-recommended"`.
5+
6+
## :book: Rule Details
7+
8+
This rule prevents name collisions between vue components and standard html elements.
9+
10+
<eslint-code-block :rules="{'vue/no-reserved-component-names': ['error']}">
11+
12+
```vue
13+
<script>
14+
/* ✗ BAD */
15+
export default {
16+
name: 'div'
17+
}
18+
</script>
19+
```
20+
21+
</eslint-code-block>
22+
23+
## :books: Further reading
24+
25+
- [List of html elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element)
26+
- [List of SVG elements](https://developer.mozilla.org/en-US/docs/Web/SVG/Element)
27+
- [Kebab case elements](https://stackoverflow.com/questions/22545621/do-custom-elements-require-a-dash-in-their-name/22545622#22545622)
28+
- [Valid custom element name](https://w3c.github.io/webcomponents/spec/custom/#valid-custom-element-name)
+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* @fileoverview disallow the use of reserved names in component definitions
3+
* @author Jake Hassel <https://github.com/shadskii>
4+
*/
5+
'use strict'
6+
7+
const utils = require('../utils')
8+
const casing = require('../utils/casing')
9+
10+
const htmlElements = require('../utils/html-elements.json')
11+
const deprecatedHtmlElements = require('../utils/deprecated-html-elements.json')
12+
const svgElements = require('../utils/svg-elements.json')
13+
14+
const kebabCaseElements = [
15+
'annotation-xml',
16+
'color-profile',
17+
'font-face',
18+
'font-face-src',
19+
'font-face-uri',
20+
'font-face-format',
21+
'font-face-name',
22+
'missing-glyph'
23+
]
24+
25+
const isLowercase = (word) => /^[a-z]*$/.test(word)
26+
const capitalizeFirstLetter = (word) => word[0].toUpperCase() + word.substring(1, word.length)
27+
28+
const RESERVED_NAMES = new Set(
29+
[
30+
...kebabCaseElements,
31+
...kebabCaseElements.map(casing.pascalCase),
32+
...htmlElements,
33+
...htmlElements.map(capitalizeFirstLetter),
34+
...deprecatedHtmlElements,
35+
...deprecatedHtmlElements.map(capitalizeFirstLetter),
36+
...svgElements,
37+
...svgElements.filter(isLowercase).map(capitalizeFirstLetter)
38+
])
39+
40+
// ------------------------------------------------------------------------------
41+
// Rule Definition
42+
// ------------------------------------------------------------------------------
43+
44+
module.exports = {
45+
meta: {
46+
type: 'suggestion',
47+
docs: {
48+
description: 'disallow the use of reserved names in component definitions',
49+
category: undefined, // 'essential'
50+
url: 'https://eslint.vuejs.org/rules/no-reserved-component-names.html'
51+
},
52+
fixable: null,
53+
schema: []
54+
},
55+
56+
create (context) {
57+
function canVerify (node) {
58+
return node.type === 'Literal' || (
59+
node.type === 'TemplateLiteral' &&
60+
node.expressions.length === 0 &&
61+
node.quasis.length === 1
62+
)
63+
}
64+
65+
function reportIfInvalid (node) {
66+
let name
67+
if (node.type === 'TemplateLiteral') {
68+
const quasis = node.quasis[0]
69+
name = quasis.value.cooked
70+
} else {
71+
name = node.value
72+
}
73+
if (RESERVED_NAMES.has(name)) {
74+
report(node, name)
75+
}
76+
}
77+
78+
function report (node, name) {
79+
context.report({
80+
node: node,
81+
message: 'Name "{{name}}" is reserved.',
82+
data: {
83+
name: name
84+
}
85+
})
86+
}
87+
88+
return Object.assign({},
89+
{
90+
"CallExpression > MemberExpression > Identifier[name='component']" (node) {
91+
const parent = node.parent.parent
92+
const calleeObject = utils.unwrapTypes(parent.callee.object)
93+
94+
if (calleeObject.type === 'Identifier' &&
95+
calleeObject.name === 'Vue' &&
96+
parent.arguments &&
97+
parent.arguments.length === 2
98+
) {
99+
const argument = parent.arguments[0]
100+
101+
if (canVerify(argument)) {
102+
reportIfInvalid(argument)
103+
}
104+
}
105+
}
106+
},
107+
utils.executeOnVue(context, (obj) => {
108+
// Report if a component has been registered locally with a reserved name.
109+
utils.getRegisteredComponents(obj)
110+
.filter(({ name }) => RESERVED_NAMES.has(name))
111+
.forEach(({ node, name }) => report(node, name))
112+
113+
const node = obj.properties
114+
.find(item => (
115+
item.type === 'Property' &&
116+
item.key.name === 'name' &&
117+
canVerify(item.value)
118+
))
119+
120+
if (!node) return
121+
reportIfInvalid(node.value)
122+
})
123+
)
124+
}
125+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["acronym","applet","basefont","bgsound","big","blink","center","command","content","dir","element","font","frame","frameset","image","isindex","keygen","listing","marquee","menuitem","multicol","nextid","nobr","noembed","noframes","plaintext","shadow","spacer","strike","tt","xmp"]

0 commit comments

Comments
 (0)