-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathattributes-order.js
378 lines (351 loc) · 10.5 KB
/
attributes-order.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* @fileoverview enforce ordering of attributes
* @author Erin Depew
*/
'use strict'
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
/**
* @typedef { VDirective & { key: VDirectiveKey & { name: VIdentifier & { name: 'bind' } } } } VBindDirective
*/
const ATTRS = {
DEFINITION: 'DEFINITION',
LIST_RENDERING: 'LIST_RENDERING',
CONDITIONALS: 'CONDITIONALS',
RENDER_MODIFIERS: 'RENDER_MODIFIERS',
GLOBAL: 'GLOBAL',
UNIQUE: 'UNIQUE',
TWO_WAY_BINDING: 'TWO_WAY_BINDING',
OTHER_DIRECTIVES: 'OTHER_DIRECTIVES',
OTHER_ATTR: 'OTHER_ATTR',
EVENTS: 'EVENTS',
CONTENT: 'CONTENT'
}
/**
* Check whether the given attribute is `v-bind` directive.
* @param {VAttribute | VDirective | undefined | null} node
* @returns { node is VBindDirective }
*/
function isVBind(node) {
return Boolean(node && node.directive && node.key.name.name === 'bind')
}
/**
* Check whether the given attribute is plain attribute.
* @param {VAttribute | VDirective | undefined | null} node
* @returns { node is VAttribute }
*/
function isVAttribute(node) {
return Boolean(node && !node.directive)
}
/**
* Check whether the given attribute is plain attribute or `v-bind` directive.
* @param {VAttribute | VDirective | undefined | null} node
* @returns { node is VAttribute }
*/
function isVAttributeOrVBind(node) {
return isVAttribute(node) || isVBind(node)
}
/**
* Check whether the given attribute is `v-bind="..."` directive.
* @param {VAttribute | VDirective | undefined | null} node
* @returns { node is VBindDirective }
*/
function isVBindObject(node) {
return isVBind(node) && node.key.argument == null
}
/**
* @param {VAttribute | VDirective} attribute
* @param {SourceCode} sourceCode
*/
function getAttributeName(attribute, sourceCode) {
if (attribute.directive) {
if (isVBind(attribute)) {
return attribute.key.argument
? sourceCode.getText(attribute.key.argument)
: ''
} else {
return getDirectiveKeyName(attribute.key, sourceCode)
}
} else {
return attribute.key.name
}
}
/**
* @param {VDirectiveKey} directiveKey
* @param {SourceCode} sourceCode
*/
function getDirectiveKeyName(directiveKey, sourceCode) {
let text = `v-${directiveKey.name.name}`
if (directiveKey.argument) {
text += `:${sourceCode.getText(directiveKey.argument)}`
}
for (const modifier of directiveKey.modifiers) {
text += `.${modifier.name}`
}
return text
}
/**
* @param {VAttribute | VDirective} attribute
*/
function getAttributeType(attribute) {
let propName
if (attribute.directive) {
if (!isVBind(attribute)) {
const name = attribute.key.name.name
if (name === 'for') {
return ATTRS.LIST_RENDERING
} else if (
name === 'if' ||
name === 'else-if' ||
name === 'else' ||
name === 'show' ||
name === 'cloak'
) {
return ATTRS.CONDITIONALS
} else if (name === 'pre' || name === 'once') {
return ATTRS.RENDER_MODIFIERS
} else if (name === 'model') {
return ATTRS.TWO_WAY_BINDING
} else if (name === 'on') {
return ATTRS.EVENTS
} else if (name === 'html' || name === 'text') {
return ATTRS.CONTENT
} else if (name === 'slot') {
return ATTRS.UNIQUE
} else if (name === 'is') {
return ATTRS.DEFINITION
} else {
return ATTRS.OTHER_DIRECTIVES
}
}
propName =
attribute.key.argument && attribute.key.argument.type === 'VIdentifier'
? attribute.key.argument.rawName
: ''
} else {
propName = attribute.key.name
}
if (propName === 'is') {
return ATTRS.DEFINITION
} else if (propName === 'id') {
return ATTRS.GLOBAL
} else if (
propName === 'ref' ||
propName === 'key' ||
propName === 'slot' ||
propName === 'slot-scope'
) {
return ATTRS.UNIQUE
} else {
return ATTRS.OTHER_ATTR
}
}
/**
* @param {VAttribute | VDirective} attribute
* @param { { [key: string]: number } } attributePosition
*/
function getPosition(attribute, attributePosition) {
const attributeType = getAttributeType(attribute)
return attributePosition[attributeType] != null
? attributePosition[attributeType]
: -1
}
/**
* @param {VAttribute | VDirective} prevNode
* @param {VAttribute | VDirective} currNode
* @param {SourceCode} sourceCode
*/
function isAlphabetical(prevNode, currNode, sourceCode) {
const prevName = getAttributeName(prevNode, sourceCode)
const currName = getAttributeName(currNode, sourceCode)
if (prevName === currName) {
const prevIsBind = isVBind(prevNode)
const currIsBind = isVBind(currNode)
return prevIsBind <= currIsBind
}
return prevName < currName
}
/**
* @param {RuleContext} context - The rule context.
* @returns {RuleListener} AST event handlers.
*/
function create(context) {
const sourceCode = context.getSourceCode()
let attributeOrder = [
ATTRS.DEFINITION,
ATTRS.LIST_RENDERING,
ATTRS.CONDITIONALS,
ATTRS.RENDER_MODIFIERS,
ATTRS.GLOBAL,
ATTRS.UNIQUE,
ATTRS.TWO_WAY_BINDING,
ATTRS.OTHER_DIRECTIVES,
ATTRS.OTHER_ATTR,
ATTRS.EVENTS,
ATTRS.CONTENT
]
if (context.options[0] && context.options[0].order) {
attributeOrder = context.options[0].order
}
const alphabetical = Boolean(
context.options[0] && context.options[0].alphabetical
)
/** @type { { [key: string]: number } } */
const attributePosition = {}
attributeOrder.forEach((item, i) => {
if (Array.isArray(item)) {
for (const attr of item) {
attributePosition[attr] = i
}
} else attributePosition[item] = i
})
/**
* @param {VAttribute | VDirective} node
* @param {VAttribute | VDirective} previousNode
*/
function reportIssue(node, previousNode) {
const currentNode = sourceCode.getText(node.key)
const prevNode = sourceCode.getText(previousNode.key)
context.report({
node,
message: `Attribute "${currentNode}" should go before "${prevNode}".`,
data: {
currentNode
},
fix(fixer) {
const attributes = node.parent.attributes
/** @type { (node: VAttribute | VDirective | undefined) => boolean } */
let isMoveUp
if (isVBindObject(node)) {
// prev, v-bind:foo, v-bind -> v-bind:foo, v-bind, prev
isMoveUp = isVAttributeOrVBind
} else if (isVAttributeOrVBind(node)) {
// prev, v-bind, v-bind:foo -> v-bind, v-bind:foo, prev
isMoveUp = isVBindObject
} else {
isMoveUp = () => false
}
const previousNodes = attributes.slice(
attributes.indexOf(previousNode),
attributes.indexOf(node)
)
const moveNodes = [node]
for (const node of previousNodes) {
if (isMoveUp(node)) {
moveNodes.unshift(node)
} else {
moveNodes.push(node)
}
}
return moveNodes.map((moveNode, index) => {
const text = sourceCode.getText(moveNode)
return fixer.replaceText(previousNodes[index] || node, text)
})
}
})
}
return utils.defineTemplateBodyVisitor(context, {
VStartTag(node) {
const attributes = node.attributes.filter((node, index, attributes) => {
if (
isVBindObject(node) &&
(isVAttributeOrVBind(attributes[index - 1]) ||
isVAttributeOrVBind(attributes[index + 1]))
) {
// In Vue 3, ignore the `v-bind:foo=" ... "` and `v-bind ="object"` syntax
// as they behave differently if you change the order.
return false
}
return true
})
if (attributes.length <= 1) {
return
}
let previousNode = attributes[0]
let previousPosition = getPositionFromAttrIndex(0)
for (let index = 1; index < attributes.length; index++) {
const node = attributes[index]
const position = getPositionFromAttrIndex(index)
let valid = previousPosition <= position
if (valid && alphabetical && previousPosition === position) {
valid = isAlphabetical(previousNode, node, sourceCode)
}
if (valid) {
previousNode = node
previousPosition = position
} else {
reportIssue(node, previousNode)
}
}
/**
* @param {number} index
* @returns {number}
*/
function getPositionFromAttrIndex(index) {
const node = attributes[index]
if (isVBindObject(node)) {
// node is `v-bind ="object"` syntax
// In Vue 3, if change the order of `v-bind:foo=" ... "` and `v-bind ="object"`,
// the behavior will be different, so adjust so that there is no change in behavior.
const len = attributes.length
for (let nextIndex = index + 1; nextIndex < len; nextIndex++) {
const next = attributes[nextIndex]
if (isVAttributeOrVBind(next) && !isVBindObject(next)) {
// It is considered to be in the same order as the next bind prop node.
return getPositionFromAttrIndex(nextIndex)
}
}
for (let prevIndex = index - 1; prevIndex >= 0; prevIndex--) {
const prev = attributes[prevIndex]
if (isVAttributeOrVBind(prev) && !isVBindObject(prev)) {
// It is considered to be in the same order as the prev bind prop node.
return getPositionFromAttrIndex(prevIndex)
}
}
}
return getPosition(node, attributePosition)
}
}
})
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce order of attributes',
categories: ['vue3-recommended', 'recommended'],
url: 'https://eslint.vuejs.org/rules/attributes-order.html'
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
order: {
type: 'array',
items: {
anyOf: [
{ enum: Object.values(ATTRS) },
{
type: 'array',
items: {
enum: Object.values(ATTRS),
uniqueItems: true,
additionalItems: false
}
}
]
},
uniqueItems: true,
additionalItems: false
},
alphabetical: { type: 'boolean' }
},
additionalProperties: false
}
]
},
create
}