forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid-component-name.js
101 lines (92 loc) · 2.33 KB
/
valid-component-name.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* @author Wayne Zhang
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
const { toRegExp } = require('../utils/regexp')
const htmlElements = require('../utils/html-elements.json')
const deprecatedHtmlElements = require('../utils/deprecated-html-elements.json')
const svgElements = require('../utils/svg-elements.json')
const RESERVED_NAMES_IN_VUE = new Set(
require('../utils/vue2-builtin-components')
)
const RESERVED_NAMES_IN_VUE3 = new Set(
require('../utils/vue3-builtin-components')
)
const kebabCaseElements = [
'annotation-xml',
'color-profile',
'font-face',
'font-face-src',
'font-face-uri',
'font-face-format',
'font-face-name',
'missing-glyph'
]
const RESERVED_NAMES_IN_HTML = new Set(htmlElements)
const RESERVED_NAMES_IN_OTHERS = new Set([
...deprecatedHtmlElements,
...kebabCaseElements,
...svgElements
])
const reservedNames = new Set([
...RESERVED_NAMES_IN_HTML,
...RESERVED_NAMES_IN_VUE,
...RESERVED_NAMES_IN_VUE3,
...RESERVED_NAMES_IN_OTHERS
])
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'enforce consistency in component names',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/valid-component-name.html'
},
fixable: null,
schema: [
{
type: 'object',
additionalProperties: false,
properties: {
allow: {
type: 'array',
items: { type: 'string' },
uniqueItems: true,
additionalItems: false
}
}
}
],
messages: {
invalidName: 'Component name "{{name}}" is not valid.'
}
},
/** @param {RuleContext} context */
create(context) {
const options = context.options[0] || {}
/** @type {RegExp[]} */
const allow = (options.allow || []).map(toRegExp)
/** @param {string} name */
function isAllowedTarget(name) {
return reservedNames.has(name) || allow.some((re) => re.test(name))
}
return utils.defineTemplateBodyVisitor(context, {
VElement(node) {
const name = node.rawName
if (isAllowedTarget(name)) {
return
}
context.report({
node,
loc: node.loc,
messageId: 'invalidName',
data: {
name
}
})
}
})
}
}