-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-unused-emit-declarations.js
364 lines (336 loc) · 10.6 KB
/
no-unused-emit-declarations.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* @author ItMaga
* See LICENSE file in root directory for full license.
*/
'use strict'
const { findVariable } = require('@eslint-community/eslint-utils')
const utils = require('../utils')
/**
* @typedef {import('../utils').ComponentEmit} ComponentEmit
* @typedef {import('../utils').VueObjectData} VueObjectData
*/
/**
* @typedef {object} SetupContext
* @property {Set<Identifier>} contextReferenceIds
* @property {Set<Identifier>} emitReferenceIds
*/
/**
* @typedef {object} NameWithLoc
* @property {string} name
* @property {SourceLocation} loc
* @property {Range} range
*/
/**
* Get the name param node from the given CallExpression
* @param {CallExpression} node CallExpression
* @returns {NameWithLoc | null}
*/
function getNameParamNode(node) {
const nameLiteralNode = node.arguments[0]
if (nameLiteralNode && utils.isStringLiteral(nameLiteralNode)) {
const name = utils.getStringLiteralValue(nameLiteralNode)
if (name != null) {
return { name, loc: nameLiteralNode.loc, range: nameLiteralNode.range }
}
}
return null
}
/**
* Check if the given node is a reference of setup context
* @param {Expression | Super | SpreadElement} value
* @param {SetupContext} setupContext
* @returns {boolean}
* */
function hasReferenceId(value, setupContext) {
const { contextReferenceIds, emitReferenceIds } = setupContext
return (
value.type === 'Identifier' &&
(emitReferenceIds.has(value) || contextReferenceIds.has(value))
)
}
/**
* Check if the given name matches emitReferenceIds variable name
* @param {string} name
* @param {Set<Identifier>} emitReferenceIds
* @returns {boolean}
*/
function isEmitVariableName(name, emitReferenceIds) {
const emitVariable = emitReferenceIds.values().next().value.name
return emitVariable === name
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow unused emit declarations',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-unused-emit-declarations.html'
},
fixable: null,
schema: [],
messages: {
unused: '`{{name}}` is defined as emit but never used.'
}
},
/** @param {RuleContext} context */
create(context) {
/** @type {Map<string, ComponentEmit>} */
const emitDeclarations = new Map()
/** @type {Map<string, Expression>} */
const emitCalls = new Map()
/** @type {Map<ObjectExpression | Program, SetupContext>} */
const setupContexts = new Map()
const programNode = context.getSourceCode().ast
/**
* @param {CallExpression} node
* */
function addEmitCall(node) {
const nameParamNode = getNameParamNode(node)
if (nameParamNode) {
emitCalls.set(nameParamNode.name, node)
}
}
function clearEmits() {
emitCalls.clear()
emitDeclarations.clear()
}
/**
* @param {Expression | SpreadElement} expression
* @param {SetupContext} setupContext
* @returns {boolean}
* */
function checkExpressionReference(expression, setupContext) {
if (expression.type === 'MemberExpression') {
const memObject = utils.skipChainExpression(expression.object)
if (hasReferenceId(memObject, setupContext)) {
clearEmits()
return true
}
}
if (hasReferenceId(expression, setupContext)) {
clearEmits()
return true
}
return false
}
/**
*
* @param {Array<Expression | SpreadElement>} args
* @param {SetupContext} setupContext
* @returns {boolean}
*/
function verifyArgumentsReferences(args, setupContext) {
for (const argument of args) {
if (argument.type === 'ObjectExpression') {
for (const property of argument.properties) {
if (
property.type === 'Property' &&
checkExpressionReference(property.value, setupContext)
) {
return true
}
}
}
if (argument.type === 'ArrayExpression') {
for (const element of argument.elements) {
if (!element) {
continue
}
if (checkExpressionReference(element, setupContext)) {
return true
}
}
}
if (checkExpressionReference(argument, setupContext)) {
return true
}
}
return false
}
/**
* @param {Expression | Super} callee
* @param {Set<Identifier>} referenceIds
* @param {CallExpression} node
* */
function addEmitCallByReference(callee, referenceIds, node) {
if (callee.type === 'Identifier' && referenceIds.has(callee)) {
addEmitCall(node)
}
}
const callVisitor = {
/**
* @param {CallExpression} node
* @param {VueObjectData} [info]
*/
CallExpression(node, info) {
const callee = utils.skipChainExpression(node.callee)
let emit = null
if (callee.type === 'MemberExpression') {
const name = utils.getStaticPropertyName(callee)
if (name === 'emit' || name === '$emit') {
emit = { name, member: callee }
}
}
const vueDefineNode = info ? info.node : programNode
const setupContext = setupContexts.get(vueDefineNode)
if (setupContext) {
if (
callee.parent.type === 'CallExpression' &&
callee.parent.arguments &&
verifyArgumentsReferences(callee.parent.arguments, setupContext)
) {
// skip if the emit passed as argument
return
}
const { contextReferenceIds, emitReferenceIds } = setupContext
// verify defineEmits variable in template
if (
callee.type === 'Identifier' &&
isEmitVariableName(callee.name, emitReferenceIds)
) {
addEmitCall(node)
}
// verify setup(props,{emit}) {emit()}
addEmitCallByReference(callee, emitReferenceIds, node)
if (emit && emit.name === 'emit') {
const memObject = utils.skipChainExpression(emit.member.object)
// verify setup(props,context) {context.emit()}
addEmitCallByReference(memObject, contextReferenceIds, node)
}
}
if (emit && emit.name === '$emit') {
const memObject = utils.skipChainExpression(emit.member.object)
// verify this.$emit()
if (utils.isThis(memObject, context)) {
addEmitCall(node)
}
}
// verify $emit() in template
if (callee.type === 'Identifier' && callee.name === '$emit') {
addEmitCall(node)
}
}
}
return utils.compositingVisitors(
utils.defineTemplateBodyVisitor(context, callVisitor),
utils.defineVueVisitor(context, {
...callVisitor,
onVueObjectEnter(node) {
for (const emit of utils.getComponentEmitsFromOptions(node)) {
if (emit.emitName) {
emitDeclarations.set(emit.emitName, emit)
}
}
},
onSetupFunctionEnter(node, { node: vueNode }) {
const contextParam = node.params[1]
if (
!contextParam ||
contextParam.type === 'RestElement' ||
contextParam.type === 'ArrayPattern'
) {
// no arguments or cannot check
return
}
/** @type {Set<Identifier>} */
const contextReferenceIds = new Set()
/** @type {Set<Identifier>} */
const emitReferenceIds = new Set()
if (contextParam.type === 'ObjectPattern') {
const emitProperty = utils.findAssignmentProperty(
contextParam,
'emit'
)
if (!emitProperty) {
return
}
const emitParam = emitProperty.value
// `setup(props, {emit})`
const variable =
emitParam.type === 'Identifier'
? findVariable(utils.getScope(context, emitParam), emitParam)
: null
if (!variable) {
return
}
for (const reference of variable.references) {
emitReferenceIds.add(reference.identifier)
}
} else if (contextParam.type === 'Identifier') {
// `setup(props, context)`
const variable = findVariable(
utils.getScope(context, contextParam),
contextParam
)
for (const reference of variable.references) {
contextReferenceIds.add(reference.identifier)
}
}
setupContexts.set(vueNode, {
contextReferenceIds,
emitReferenceIds
})
}
}),
utils.defineScriptSetupVisitor(context, {
onDefineEmitsEnter(node, emits) {
for (const emit of emits) {
if (emit.emitName) {
emitDeclarations.set(emit.emitName, emit)
}
}
if (
!node.parent ||
node.parent.type !== 'VariableDeclarator' ||
node.parent.init !== node
) {
return
}
const emitParam = node.parent.id
const variable =
emitParam.type === 'Identifier'
? findVariable(utils.getScope(context, emitParam), emitParam)
: null
if (!variable) {
return
}
/** @type {Set<Identifier>} */
const emitReferenceIds = new Set()
for (const reference of variable.references) {
emitReferenceIds.add(reference.identifier)
}
setupContexts.set(programNode, {
contextReferenceIds: new Set(),
emitReferenceIds
})
},
onDefineModelEnter(node, model) {
if (
node.parent &&
node.parent.type === 'VariableDeclarator' &&
node.parent.init === node
) {
// If store the return of defineModel() in a variable, we can mark the 'update:modelName' event as used if use that variable.
// If that variable is unused it will already be reported by `no-unused-var` rule.
emitCalls.set(`update:${model.name.modelName}`, node)
}
},
...callVisitor
}),
{
'Program:exit'() {
for (const [name, emit] of emitDeclarations) {
if (!emitCalls.has(name) && emit.node) {
context.report({
node: emit.node,
loc: emit.node.loc,
messageId: 'unused',
data: { name }
})
}
}
}
}
)
}
}