forked from vuejs/eslint-plugin-vue
/
no-unregistered-components.js
196 lines (180 loc) · 6.03 KB
/
no-unregistered-components.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* @fileoverview Report used components that are not registered
* @author Jesús Ángel González Novez
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const utils = require('eslint-plugin-vue/lib/utils')
const casing = require('eslint-plugin-vue/lib/utils/casing')
// ------------------------------------------------------------------------------
// Rule helpers
// ------------------------------------------------------------------------------
const VUE_BUILT_IN_COMPONENTS = [
'component',
'suspense',
'teleport',
'transition',
'transition-group',
'keep-alive',
'slot'
]
/**
* Check whether the given node is a built-in component or not.
*
* Includes `suspense` and `teleport` from Vue 3.
*
* @param {VElement} node The start tag node to check.
* @returns {boolean} `true` if the node is a built-in component.
*/
const isBuiltInComponent = (node) => {
const rawName = node && casing.kebabCase(node.rawName)
return (
utils.isHtmlElementNode(node) &&
!utils.isHtmlWellKnownElementName(node.rawName) &&
VUE_BUILT_IN_COMPONENTS.indexOf(rawName) > -1
)
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description:
'disallow using components that are not registered inside templates',
categories: null,
recommended: false,
url: 'https://eslint.vuejs.org/rules/no-unregistered-components.html'
},
fixable: null,
schema: [
{
type: 'object',
properties: {
ignorePatterns: {
type: 'array'
}
},
additionalProperties: false
}
]
},
/** @param {RuleContext} context */
create(context) {
const options = context.options[0] || {}
/** @type {string[]} */
const ignorePatterns = options.ignorePatterns || []
/** @type { { node: VElement | VDirective | VAttribute, name: string }[] } */
const usedComponentNodes = []
/** @type { { node: Property, name: string }[] } */
const registeredComponents = []
return utils.defineTemplateBodyVisitor(
context,
{
/** @param {VElement} node */
VElement(node) {
if (
(!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) ||
utils.isHtmlWellKnownElementName(node.rawName) ||
utils.isSvgWellKnownElementName(node.rawName) ||
isBuiltInComponent(node)
) {
return
}
usedComponentNodes.push({ node, name: node.rawName })
},
/** @param {VDirective} node */
"VAttribute[directive=true][key.name.name='bind'][key.argument.name='is'], VAttribute[directive=true][key.name.name='is']"(
node
) {
if (
!node.value ||
node.value.type !== 'VExpressionContainer' ||
!node.value.expression
)
return
if (node.value.expression.type === 'Literal') {
if (
utils.isHtmlWellKnownElementName(`${node.value.expression.value}`)
)
return
usedComponentNodes.push({
node,
name: `${node.value.expression.value}`
})
}
},
/** @param {VAttribute} node */
"VAttribute[directive=false][key.name='is']"(node) {
if (
!node.value || // `<component is />`
utils.isHtmlWellKnownElementName(node.value.value)
)
return
usedComponentNodes.push({ node, name: node.value.value })
},
/** @param {VElement} node */
"VElement[name='template']:exit"() {
// All registered components, transformed to kebab-case
const registeredComponentNames = registeredComponents.map(
({ name }) => casing.kebabCase(name)
)
// All registered components using kebab-case syntax
const componentsRegisteredAsKebabCase = registeredComponents
.filter(({ name }) => name === casing.kebabCase(name))
.map(({ name }) => name)
usedComponentNodes
.filter(({ name }) => {
const kebabCaseName = casing.kebabCase(name)
// Check ignored patterns in first place
if (
ignorePatterns.find((pattern) => {
const regExp = new RegExp(pattern)
return (
regExp.test(kebabCaseName) ||
regExp.test(casing.pascalCase(name)) ||
regExp.test(casing.camelCase(name)) ||
regExp.test(casing.snakeCase(name)) ||
regExp.test(name)
)
})
)
return false
// Component registered as `foo-bar` cannot be used as `FooBar`
if (
casing.isPascalCase(name) &&
componentsRegisteredAsKebabCase.indexOf(kebabCaseName) !== -1
) {
return true
}
// Otherwise
return registeredComponentNames.indexOf(kebabCaseName) === -1
})
.forEach(({ node, name }) =>
context.report({
node,
message:
'The "{{name}}" component has been used but not registered.',
data: {
name
}
})
)
}
},
utils.executeOnVue(context, (obj) => {
registeredComponents.push(...utils.getRegisteredComponents(obj))
const nameProperty = utils.findProperty(obj, 'name')
if (nameProperty) {
registeredComponents.push({
node: nameProperty,
name: nameProperty.value.value
})
}
})
)
}
}