forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-undef-components.js
275 lines (253 loc) · 8.16 KB
/
no-undef-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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const utils = require('../utils')
const casing = require('../utils/casing')
// ------------------------------------------------------------------------------
// Rule helpers
// ------------------------------------------------------------------------------
/**
* `casing.camelCase()` converts the beginning to lowercase,
* but does not convert the case of the beginning character when converting with Vue3.
* @see https://github.com/vuejs/core/blob/ae4b0783d78670b6e942ae2a4e3ec6efbbffa158/packages/shared/src/index.ts#L105
* @param {string} str
*/
function camelize(str) {
return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow use of undefined components in `<template>`',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-undef-components.html'
},
fixable: null,
schema: [
{
type: 'object',
properties: {
ignorePatterns: {
type: 'array'
}
},
additionalProperties: false
}
],
messages: {
undef: "The '<{{name}}>' component has been used, but not defined.",
typeOnly:
"The '<{{name}}>' component has been used, but '{{name}}' only refers to a type."
}
},
/** @param {RuleContext} context */
create(context) {
const options = context.options[0] || {}
/** @type {string[]} */
const ignorePatterns = options.ignorePatterns || []
/**
* Check whether the given element name is a verify target or not.
*
* @param {string} rawName The element name.
* @returns {boolean}
*/
function isVerifyTargetComponent(rawName) {
const kebabCaseName = casing.kebabCase(rawName)
if (
utils.isHtmlWellKnownElementName(rawName) ||
utils.isSvgWellKnownElementName(rawName) ||
utils.isBuiltInComponentName(kebabCaseName)
) {
return false
}
const pascalCaseName = casing.pascalCase(rawName)
// Check ignored patterns
if (
ignorePatterns.some((pattern) => {
const regExp = new RegExp(pattern)
return (
regExp.test(rawName) ||
regExp.test(kebabCaseName) ||
regExp.test(pascalCaseName)
)
})
) {
return false
}
return true
}
/** @type { (rawName:string, reportNode: ASTNode) => void } */
let verifyName
/** @type {RuleListener} */
let scriptVisitor = {}
/** @type {TemplateListener} */
const templateBodyVisitor = {
VElement(node) {
if (!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) {
return
}
verifyName(node.rawName, node.startTag)
},
/** @param {VAttribute} node */
"VAttribute[directive=false][key.name='is']"(node) {
if (
!node.value // `<component is />`
)
return
const value = node.value.value.startsWith('vue:') // Usage on native elements 3.1+
? node.value.value.slice(4)
: node.value.value
verifyName(value, node)
}
}
if (utils.isScriptSetup(context)) {
// For <script setup>
/** @type {Set<string>} */
const scriptVariableNames = new Set()
const scriptTypeOnlyNames = new Set()
const globalScope = context.getSourceCode().scopeManager.globalScope
if (globalScope) {
for (const variable of globalScope.variables) {
scriptVariableNames.add(variable.name)
}
const moduleScope = globalScope.childScopes.find(
(scope) => scope.type === 'module'
)
for (const variable of (moduleScope && moduleScope.variables) || []) {
if (variable.isTypeVariable) {
scriptTypeOnlyNames.add(variable.name)
} else {
scriptVariableNames.add(variable.name)
}
}
}
/**
* @see https://github.com/vuejs/core/blob/ae4b0783d78670b6e942ae2a4e3ec6efbbffa158/packages/compiler-core/src/transforms/transformElement.ts#L334
* @param {string} name
*/
const existsSetupReference = (name) => {
if (scriptVariableNames.has(name)) {
return true
}
const camelName = camelize(name)
if (scriptVariableNames.has(camelName)) {
return true
}
const pascalName = casing.capitalize(camelName)
if (scriptVariableNames.has(pascalName)) {
return true
}
return false
}
verifyName = (rawName, reportNode) => {
if (!isVerifyTargetComponent(rawName)) {
return
}
if (existsSetupReference(rawName)) {
return
}
// Check namespace
// https://github.com/vuejs/core/blob/ae4b0783d78670b6e942ae2a4e3ec6efbbffa158/packages/compiler-core/src/transforms/transformElement.ts#L305
const dotIndex = rawName.indexOf('.')
if (dotIndex > 0 && existsSetupReference(rawName.slice(0, dotIndex))) {
return
}
context.report({
node: reportNode,
messageId: scriptTypeOnlyNames.has(rawName) ? 'typeOnly' : 'undef',
data: {
name: rawName
}
})
}
} else {
// For Options API
/**
* All registered components
* @type {string[]}
*/
const registeredComponentNames = []
/**
* All registered components, transformed to kebab-case
* @type {string[]}
*/
const registeredComponentKebabCaseNames = []
/**
* All registered components using kebab-case syntax
* @type {string[]}
*/
const componentsRegisteredAsKebabCase = []
scriptVisitor = utils.executeOnVue(context, (obj) => {
registeredComponentNames.push(
...utils.getRegisteredComponents(obj).map(({ name }) => name)
)
const nameProperty = utils.findProperty(obj, 'name')
if (nameProperty && utils.isStringLiteral(nameProperty.value)) {
const name = utils.getStringLiteralValue(nameProperty.value)
if (name) {
registeredComponentNames.push(name)
}
}
registeredComponentKebabCaseNames.push(
...registeredComponentNames.map((name) => casing.kebabCase(name))
)
componentsRegisteredAsKebabCase.push(
...registeredComponentNames.filter(
(name) => name === casing.kebabCase(name)
)
)
})
verifyName = (rawName, reportNode) => {
if (!isVerifyTargetComponent(rawName)) {
return
}
if (registeredComponentNames.includes(rawName)) {
return
}
const kebabCaseName = casing.kebabCase(rawName)
if (
registeredComponentKebabCaseNames.includes(kebabCaseName) &&
!casing.isPascalCase(rawName)
) {
// Component registered as `foo-bar` cannot be used as `FooBar`
return
}
context.report({
node: reportNode,
messageId: 'undef',
data: {
name: rawName
}
})
}
/** @param {VDirective} node */
templateBodyVisitor[
"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') {
verifyName(`${node.value.expression.value}`, node)
}
}
}
return utils.defineTemplateBodyVisitor(
context,
templateBodyVisitor,
scriptVisitor
)
}
}