forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcan-convert-to-v-slot.js
222 lines (212 loc) · 6.42 KB
/
can-convert-to-v-slot.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
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../../../utils')
/**
* @typedef {object} SlotVForVariables
* @property {VForExpression} expr
* @property {VVariable[]} variables
*/
/**
* @typedef {object} SlotContext
* @property {VElement} element
* @property {VAttribute | VDirective | null} slot
* @property {VDirective | null} vFor
* @property {SlotVForVariables | null} slotVForVars
* @property {string} normalizedName
*/
/**
* Checks whether the given element can use v-slot.
* @param {VElement} element
* @param {SourceCode} sourceCode
* @param {ParserServices.TokenStore} tokenStore
*/
module.exports = function canConvertToVSlot(element, sourceCode, tokenStore) {
const ownerElement = element.parent
if (
ownerElement.type === 'VDocumentFragment' ||
!utils.isCustomComponent(ownerElement)
) {
return false
}
const slot = getSlotContext(element, sourceCode)
if (slot.vFor && !slot.slotVForVars) {
// E.g., <template v-for="x of xs" #one></template>
return false
}
if (hasSameSlotDirective(ownerElement, slot, sourceCode, tokenStore)) {
return false
}
return true
}
/**
* @param {VElement} element
* @param {SourceCode} sourceCode
* @returns {SlotContext}
*/
function getSlotContext(element, sourceCode) {
const slot =
utils.getAttribute(element, 'slot') ||
utils.getDirective(element, 'bind', 'slot')
const vFor = utils.getDirective(element, 'for')
const slotVForVars = getSlotVForVariableIfUsingIterationVars(slot, vFor)
return {
element,
slot,
vFor,
slotVForVars,
normalizedName: getNormalizedName(slot, sourceCode)
}
}
/**
* Gets the `v-for` directive and variable that provide the variables used by the given `slot` attribute.
* @param {VAttribute | VDirective | null} slot The current `slot` attribute node.
* @param {VDirective | null} [vFor] The current `v-for` directive node.
* @returns { SlotVForVariables | null } The SlotVForVariables.
*/
function getSlotVForVariableIfUsingIterationVars(slot, vFor) {
if (!slot || !slot.directive) {
return null
}
const expr =
vFor && vFor.value && /** @type {VForExpression} */ (vFor.value.expression)
const variables =
expr && getUsingIterationVars(slot.value, slot.parent.parent)
return expr && variables && variables.length > 0 ? { expr, variables } : null
}
/**
* Gets iterative variables if a given expression node is using iterative variables that the element defined.
* @param {VExpressionContainer|null} expression The expression node to check.
* @param {VElement} element The element node which has the expression.
* @returns {VVariable[]} The expression node is using iteration variables.
*/
function getUsingIterationVars(expression, element) {
const vars = []
if (expression && expression.type === 'VExpressionContainer') {
for (const { variable } of expression.references) {
if (
variable != null &&
variable.kind === 'v-for' &&
variable.id.range[0] > element.startTag.range[0] &&
variable.id.range[1] < element.startTag.range[1]
) {
vars.push(variable)
}
}
}
return vars
}
/**
* Get the normalized name of a given `slot` attribute node.
* @param {VAttribute | VDirective | null} slotAttr node of `slot`
* @param {SourceCode} sourceCode The source code.
* @returns {string} The normalized name.
*/
function getNormalizedName(slotAttr, sourceCode) {
if (!slotAttr) {
return 'default'
}
if (!slotAttr.directive) {
return slotAttr.value ? slotAttr.value.value : 'default'
}
return slotAttr.value ? `[${sourceCode.getText(slotAttr.value)}]` : '[null]'
}
/**
* Checks whether parent element has the same slot as the given slot.
* @param {VElement} ownerElement The parent element.
* @param {SlotContext} targetSlot The SlotContext with a slot to check if they are the same.
* @param {SourceCode} sourceCode
* @param {ParserServices.TokenStore} tokenStore
*/
function hasSameSlotDirective(
ownerElement,
targetSlot,
sourceCode,
tokenStore
) {
for (const group of utils.iterateChildElementsChains(ownerElement)) {
if (group.includes(targetSlot.element)) {
continue
}
for (const childElement of group) {
const slot = getSlotContext(childElement, sourceCode)
if (!targetSlot.slotVForVars || !slot.slotVForVars) {
if (
!targetSlot.slotVForVars &&
!slot.slotVForVars &&
targetSlot.normalizedName === slot.normalizedName
) {
return true
}
continue
}
if (
equalSlotVForVariables(
targetSlot.slotVForVars,
slot.slotVForVars,
tokenStore
)
) {
return true
}
}
}
return false
}
/**
* Determines whether the two given `v-slot` variables are considered to be equal.
* @param {SlotVForVariables} a First element.
* @param {SlotVForVariables} b Second element.
* @param {ParserServices.TokenStore} tokenStore The token store.
* @returns {boolean} `true` if the elements are considered to be equal.
*/
function equalSlotVForVariables(a, b, tokenStore) {
if (a.variables.length !== b.variables.length) {
return false
}
if (!equal(a.expr.right, b.expr.right)) {
return false
}
const checkedVarNames = new Set()
const len = Math.min(a.expr.left.length, b.expr.left.length)
for (let index = 0; index < len; index++) {
const aPtn = a.expr.left[index]
const bPtn = b.expr.left[index]
const aVar = a.variables.find(
(v) => aPtn.range[0] <= v.id.range[0] && v.id.range[1] <= aPtn.range[1]
)
const bVar = b.variables.find(
(v) => bPtn.range[0] <= v.id.range[0] && v.id.range[1] <= bPtn.range[1]
)
if (aVar && bVar) {
if (aVar.id.name !== bVar.id.name) {
return false
}
if (!equal(aPtn, bPtn)) {
return false
}
checkedVarNames.add(aVar.id.name)
} else if (aVar || bVar) {
return false
}
}
return a.variables.every(
(v) =>
checkedVarNames.has(v.id.name) ||
b.variables.some((bv) => v.id.name === bv.id.name)
)
/**
* Determines whether the two given nodes are considered to be equal.
* @param {ASTNode} a First node.
* @param {ASTNode} b Second node.
* @returns {boolean} `true` if the nodes are considered to be equal.
*/
function equal(a, b) {
if (a.type !== b.type) {
return false
}
return utils.equalTokens(a, b, tokenStore)
}
}