-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathreturn-in-emits-validator.js
151 lines (141 loc) · 4.05 KB
/
return-in-emits-validator.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
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
/**
* @typedef {import('../utils').ComponentEmit} ComponentEmit
* @typedef {import('../utils').ComponentObjectEmit} ComponentObjectEmit
*/
/**
* Checks if the given node value is falsy.
* @param {Expression} node The node to check
* @returns {boolean} If `true`, the given node value is falsy.
*/
function isFalsy(node) {
if (node.type === 'Literal') {
if (node.bigint) {
return node.bigint === '0'
}
if (!node.value) {
return true
}
}
return (
node.type === 'Identifier' &&
(node.name === 'undefined' || node.name === 'NaN')
)
}
module.exports = {
meta: {
type: 'problem',
docs: {
description:
'enforce that a return statement is present in emits validator',
categories: ['vue3-essential', 'vue2-essential'],
url: 'https://eslint.vuejs.org/rules/return-in-emits-validator.html'
},
fixable: null,
schema: [],
messages: {
expectedTrue:
'Expected to return a true value in "{{name}}" emits validator.',
expectedBoolean:
'Expected to return a boolean value in "{{name}}" emits validator.'
}
},
/** @param {RuleContext} context */
create(context) {
/** @type {ComponentObjectEmit[]} */
const emitsValidators = []
/**
* @typedef {object} ScopeStack
* @property {ScopeStack | null} upper
* @property {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} functionNode
* @property {boolean} hasReturnValue
* @property {boolean} possibleOfReturnTrue
*/
/**
* @type {ScopeStack | null}
*/
let scopeStack = null
/**
* @param {ComponentEmit[]} emits
*/
function processEmits(emits) {
for (const emit of emits) {
if (emit.type !== 'object' || !emit.value) {
continue
}
if (
emit.value.type !== 'FunctionExpression' &&
emit.value.type !== 'ArrowFunctionExpression'
) {
continue
}
emitsValidators.push(emit)
}
}
return utils.compositingVisitors(
utils.defineScriptSetupVisitor(context, {
onDefineEmitsEnter(_node, emits) {
processEmits(emits)
}
}),
utils.defineVueVisitor(context, {
/** @param {ObjectExpression} obj */
onVueObjectEnter(obj) {
processEmits(utils.getComponentEmitsFromOptions(obj))
}
}),
{
/** @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node */
':function'(node) {
scopeStack = {
upper: scopeStack,
functionNode: node,
hasReturnValue: false,
possibleOfReturnTrue: false
}
if (node.type === 'ArrowFunctionExpression' && node.expression) {
scopeStack.hasReturnValue = true
if (!isFalsy(node.body)) {
scopeStack.possibleOfReturnTrue = true
}
}
},
/** @param {ReturnStatement} node */
ReturnStatement(node) {
if (!scopeStack) {
return
}
if (node.argument) {
scopeStack.hasReturnValue = true
if (!isFalsy(node.argument)) {
scopeStack.possibleOfReturnTrue = true
}
}
},
/** @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node */
':function:exit'(node) {
if (scopeStack && !scopeStack.possibleOfReturnTrue) {
const emits = emitsValidators.find((e) => e.value === node)
if (emits) {
context.report({
node,
messageId: scopeStack.hasReturnValue
? 'expectedTrue'
: 'expectedBoolean',
data: {
name: emits.emitName || 'Unknown'
}
})
}
}
scopeStack = scopeStack && scopeStack.upper
}
}
)
}
}