-
-
Notifications
You must be signed in to change notification settings - Fork 663
/
valid-v-memo.js
119 lines (117 loc) · 3.5 KB
/
valid-v-memo.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
/**
* @author Yosuke Ota <https://github.com/ota-meshi>
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'enforce valid `v-memo` directives',
categories: ['vue3-essential'],
url: 'https://eslint.vuejs.org/rules/valid-v-memo.html'
},
fixable: null,
schema: [],
messages: {
unexpectedArgument: "'v-memo' directives require no argument.",
unexpectedModifier: "'v-memo' directives require no modifier.",
expectedValue: "'v-memo' directives require that attribute value.",
expectedArray:
"'v-memo' directives require the attribute value to be an array.",
insideVFor: "'v-memo' directive does not work inside 'v-for'."
}
},
/** @param {RuleContext} context */
create(context) {
/** @type {VElement | null} */
let vForElement = null
return utils.defineTemplateBodyVisitor(context, {
VElement(node) {
if (!vForElement && utils.hasDirective(node, 'for')) {
vForElement = node
}
},
'VElement:exit'(node) {
if (vForElement === node) {
vForElement = null
}
},
/** @param {VDirective} node */
"VAttribute[directive=true][key.name.name='memo']"(node) {
if (vForElement && vForElement !== node.parent.parent) {
context.report({
node: node.key,
messageId: 'insideVFor'
})
}
if (node.key.argument) {
context.report({
node: node.key.argument,
messageId: 'unexpectedArgument'
})
}
if (node.key.modifiers.length > 0) {
context.report({
node,
loc: {
start: node.key.modifiers[0].loc.start,
end: node.key.modifiers[node.key.modifiers.length - 1].loc.end
},
messageId: 'unexpectedModifier'
})
}
if (!node.value || utils.isEmptyValueDirective(node, context)) {
context.report({
node,
messageId: 'expectedValue'
})
return
}
if (!node.value.expression) {
return
}
const expressions = [node.value.expression]
let expression
while ((expression = expressions.pop())) {
switch (expression.type) {
case 'ObjectExpression':
case 'ClassExpression':
case 'ArrowFunctionExpression':
case 'FunctionExpression':
case 'Literal':
case 'TemplateLiteral':
case 'UnaryExpression':
case 'BinaryExpression':
case 'UpdateExpression': {
context.report({
node: expression,
messageId: 'expectedArray'
})
break
}
case 'AssignmentExpression': {
expressions.push(expression.right)
break
}
case 'TSAsExpression': {
expressions.push(expression.expression)
break
}
case 'SequenceExpression': {
expressions.push(
expression.expressions[expression.expressions.length - 1]
)
break
}
case 'ConditionalExpression': {
expressions.push(expression.consequent, expression.alternate)
break
}
}
}
}
})
}
}