/
custom-event-name-casing.js
221 lines (207 loc) · 6.44 KB
/
custom-event-name-casing.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
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
/**
* @typedef {import('vue-eslint-parser').AST.ESLintLiteral} Literal
* @typedef {import('vue-eslint-parser').AST.ESLintCallExpression} CallExpression
*/
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const { findVariable } = require('eslint-utils')
const utils = require('../utils')
const { isKebabCase } = require('../utils/casing')
// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
/**
* Check whether the given event name is valid.
* @param {string} name The name to check.
* @returns {boolean} `true` if the given event name is valid.
*/
function isValidEventName(name) {
return isKebabCase(name) || name.startsWith('update:')
}
/**
* Get the name param node from the given CallExpression
* @param {CallExpression} node CallExpression
* @returns { Literal & { value: string } }
*/
function getNameParamNode(node) {
const nameLiteralNode = node.arguments[0]
if (
!nameLiteralNode ||
nameLiteralNode.type !== 'Literal' ||
typeof nameLiteralNode.value !== 'string'
) {
// cannot check
return null
}
return nameLiteralNode
}
/**
* Get the callee member node from the given CallExpression
* @param {CallExpression} node CallExpression
*/
function getCalleeMemberNode(node) {
const callee = node.callee
if (callee.type === 'MemberExpression') {
const name = utils.getStaticPropertyName(callee)
if (name) {
return { name, member: callee }
}
}
return null
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce custom event names always use "kebab-case"',
categories: ['vue3-essential', 'essential'],
url: 'https://eslint.vuejs.org/rules/custom-event-name-casing.html'
},
fixable: null,
schema: [],
messages: {
unexpected: "Custom event name '{{name}}' must be kebab-case."
}
},
create(context) {
const setupContexts = new Map()
/**
* @param { Literal & { value: string } } nameLiteralNode
*/
function verify(nameLiteralNode) {
const name = nameLiteralNode.value
if (isValidEventName(name)) {
return
}
context.report({
node: nameLiteralNode,
messageId: 'unexpected',
data: {
name
}
})
}
return utils.defineTemplateBodyVisitor(
context,
{
CallExpression(node) {
const callee = node.callee
const nameLiteralNode = getNameParamNode(node)
if (!nameLiteralNode) {
// cannot check
return
}
if (callee.type === 'Identifier' && callee.name === '$emit') {
verify(nameLiteralNode)
}
}
},
utils.compositingVisitors(
utils.defineVueVisitor(context, {
onSetupFunctionEnter(node, { node: vueNode }) {
const contextParam = node.params[1]
if (!contextParam) {
// no arguments
return
}
if (contextParam.type === 'RestElement') {
// cannot check
return
}
if (contextParam.type === 'ArrayPattern') {
// cannot check
return
}
const contextReferenceIds = new Set()
const emitReferenceIds = new Set()
if (contextParam.type === 'ObjectPattern') {
const emitProperty = contextParam.properties.find(
(p) =>
p.type === 'Property' &&
utils.getStaticPropertyName(p) === 'emit'
)
if (!emitProperty) {
return
}
const emitParam = emitProperty.value
// `setup(props, {emit})`
const variable = findVariable(context.getScope(), emitParam)
if (!variable) {
return
}
for (const reference of variable.references) {
emitReferenceIds.add(reference.identifier)
}
} else {
// `setup(props, context)`
const variable = findVariable(context.getScope(), contextParam)
if (!variable) {
return
}
for (const reference of variable.references) {
contextReferenceIds.add(reference.identifier)
}
}
setupContexts.set(vueNode, {
contextReferenceIds,
emitReferenceIds
})
},
CallExpression(node, { node: vueNode }) {
const nameLiteralNode = getNameParamNode(node)
if (!nameLiteralNode) {
// cannot check
return
}
// verify setup context
const setupContext = setupContexts.get(vueNode)
if (setupContext) {
const { contextReferenceIds, emitReferenceIds } = setupContext
if (emitReferenceIds.has(node.callee)) {
// verify setup(props,{emit}) {emit()}
verify(nameLiteralNode)
} else {
const emit = getCalleeMemberNode(node)
if (
emit &&
emit.name === 'emit' &&
contextReferenceIds.has(emit.member.object)
) {
// verify setup(props,context) {context.emit()}
verify(nameLiteralNode)
}
}
}
},
onVueObjectExit(node) {
setupContexts.delete(node)
}
}),
{
CallExpression(node) {
const nameLiteralNode = getNameParamNode(node)
if (!nameLiteralNode) {
// cannot check
return
}
const emit = getCalleeMemberNode(node)
// verify $emit
if (emit && emit.name === '$emit') {
// verify this.$emit()
verify(nameLiteralNode)
}
}
}
)
)
}
}