-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathvalid-v-model.js
233 lines (211 loc) · 6.48 KB
/
valid-v-model.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
/**
* @author Toru Nagashima
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
const VALID_MODIFIERS = new Set(['lazy', 'number', 'trim'])
/**
* Check whether the given node is valid or not.
* @param {VElement} node The element node to check.
* @returns {boolean} `true` if the node is valid.
*/
function isValidElement(node) {
const name = node.name
return (
name === 'input' ||
name === 'select' ||
name === 'textarea' ||
(name !== 'keep-alive' &&
name !== 'slot' &&
name !== 'transition' &&
name !== 'transition-group' &&
utils.isCustomComponent(node))
)
}
/**
* Check whether the given node is a MemberExpression containing an optional chaining.
* e.g.
* - `a?.b`
* - `a?.b.c`
* @param {ASTNode} node The node to check.
* @returns {boolean} `true` if the node is a MemberExpression containing an optional chaining.
*/
function isOptionalChainingMemberExpression(node) {
return (
node.type === 'ChainExpression' &&
node.expression.type === 'MemberExpression'
)
}
/**
* Check whether the given node can be LHS (left-hand side).
* @param {ASTNode} node The node to check.
* @returns {boolean} `true` if the node can be LHS.
*/
function isLhs(node) {
if (node.type === 'TSAsExpression' || node.type === 'TSNonNullExpression') {
return isLhs(node.expression)
}
return node.type === 'Identifier' || node.type === 'MemberExpression'
}
/**
* Check whether the given node is a MemberExpression of a possibly null object.
* e.g.
* - `(a?.b).c`
* - `(null).foo`
* @param {ASTNode} node The node to check.
* @returns {boolean} `true` if the node is a MemberExpression of a possibly null object.
*/
function maybeNullObjectMemberExpression(node) {
if (node.type !== 'MemberExpression') {
return false
}
const { object } = node
if (object.type === 'ChainExpression') {
// `(a?.b).c`
return true
}
if (object.type === 'Literal' && object.value === null && !object.bigint) {
// `(null).foo`
return true
}
if (object.type === 'MemberExpression') {
return maybeNullObjectMemberExpression(object)
}
return false
}
/**
* Get the variable by names.
* @param {string} name The variable name to find.
* @param {VElement} leafNode The node to look up.
* @returns {VVariable|null} The found variable or null.
*/
function getVariable(name, leafNode) {
let node = leafNode
while (node != null) {
const variables = node.variables
const variable = variables && variables.find((v) => v.id.name === name)
if (variable != null) {
return variable
}
if (node.parent.type === 'VDocumentFragment') {
break
}
node = node.parent
}
return null
}
/** @type {RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'enforce valid `v-model` directives',
categories: ['vue3-essential', 'vue2-essential'],
url: 'https://eslint.vuejs.org/rules/valid-v-model.html'
},
fixable: null,
schema: [],
messages: {
unexpectedInvalidElement:
"'v-model' directives aren't supported on <{{name}}> elements.",
unexpectedInputFile:
"'v-model' directives don't support 'file' input type.",
unexpectedArgument: "'v-model' directives require no argument.",
unexpectedModifier:
"'v-model' directives don't support the modifier '{{name}}'.",
missingValue: "'v-model' directives require that attribute value.",
unexpectedOptionalChaining:
"Optional chaining cannot appear in 'v-model' directives.",
unexpectedNonLhsExpression:
"'v-model' directives require the attribute value which is valid as LHS.",
unexpectedNullObject:
"'v-model' directive has potential null object property access.",
unexpectedUpdateIterationVariable:
"'v-model' directives cannot update the iteration variable '{{varName}}' itself."
}
},
/** @param {RuleContext} context */
create(context) {
return utils.defineTemplateBodyVisitor(context, {
/** @param {VDirective} node */
"VAttribute[directive=true][key.name.name='model']"(node) {
const element = node.parent.parent
const name = element.name
if (!isValidElement(element)) {
context.report({
node,
messageId: 'unexpectedInvalidElement',
data: { name }
})
}
if (name === 'input' && utils.hasAttribute(element, 'type', 'file')) {
context.report({
node,
messageId: 'unexpectedInputFile'
})
}
if (!utils.isCustomComponent(element)) {
if (node.key.argument) {
context.report({
node: node.key.argument,
messageId: 'unexpectedArgument'
})
}
for (const modifier of node.key.modifiers) {
if (!VALID_MODIFIERS.has(modifier.name)) {
context.report({
node: modifier,
messageId: 'unexpectedModifier',
data: { name: modifier.name }
})
}
}
}
if (!node.value || utils.isEmptyValueDirective(node, context)) {
context.report({
node,
messageId: 'missingValue'
})
return
}
const expression = node.value.expression
if (!expression) {
// Parsing error
return
}
if (isOptionalChainingMemberExpression(expression)) {
context.report({
node: expression,
messageId: 'unexpectedOptionalChaining'
})
} else if (!isLhs(expression)) {
context.report({
node: expression,
messageId: 'unexpectedNonLhsExpression'
})
} else if (maybeNullObjectMemberExpression(expression)) {
context.report({
node: expression,
messageId: 'unexpectedNullObject'
})
}
for (const reference of node.value.references) {
const id = reference.id
if (id.parent.type !== 'VExpressionContainer') {
continue
}
const variable = getVariable(id.name, element)
if (variable != null) {
context.report({
node: expression,
messageId: 'unexpectedUpdateIterationVariable',
data: { varName: id.name }
})
}
}
}
})
}
}