Skip to content

Commit 0115e46

Browse files
committed
Add same order support on multiple types of attributes.
1 parent 3f88f28 commit 0115e46

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

Diff for: docs/rules/attributes-order.md

+9
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ Specify custom order of attribute groups
8686

8787
:+1: Examples of **correct** code with custom order`:
8888

89+
```html
90+
<!-- 'vue/attributes-order': [2, { order: ['DEFINITION', 'LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', ['BINDING', 'OTHER_ATTR'], 'EVENTS', 'CONTENT'] }] -->
91+
<div
92+
is="header"
93+
propOne="prop"
94+
:propTwo="prop">
95+
</div>
96+
```
97+
8998
```html
9099
<!-- 'vue/attributes-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'BINDING', 'OTHER_ATTR', 'EVENTS', 'CONTENT', 'DEFINITION'] }] -->
91100
<div

Diff for: lib/rules/attributes-order.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ function getAttributeType (name, isDirective) {
3636
}
3737
}
3838
}
39-
function getPosition (attribute, attributeOrder) {
39+
function getPosition (attribute, attributePosition) {
4040
const attributeType = getAttributeType(attribute.key.name, attribute.directive)
41-
return attributeOrder.indexOf(attributeType)
41+
return attributePosition.hasOwnProperty(attributeType) ? attributePosition[attributeType] : -1
4242
}
4343

4444
function create (context) {
@@ -47,6 +47,14 @@ function create (context) {
4747
if (context.options[0] && context.options[0].order) {
4848
attributeOrder = context.options[0].order
4949
}
50+
const attributePosition = {}
51+
attributeOrder.forEach((item, i) => {
52+
if (item instanceof Array) {
53+
item.forEach((attr) => {
54+
attributePosition[attr] = i
55+
})
56+
} else attributePosition[item] = i
57+
})
5058
let currentPosition
5159
let previousNode
5260

@@ -94,8 +102,8 @@ function create (context) {
94102
previousNode = null
95103
},
96104
'VAttribute' (node) {
97-
if ((currentPosition === -1) || (currentPosition <= getPosition(node, attributeOrder))) {
98-
currentPosition = getPosition(node, attributeOrder)
105+
if ((currentPosition === -1) || (currentPosition <= getPosition(node, attributePosition))) {
106+
currentPosition = getPosition(node, attributePosition)
99107
previousNode = node
100108
} else {
101109
reportIssue(node, previousNode)

Diff for: tests/lib/rules/attributes-order.js

+69
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,33 @@ tester.run('attributes-order', rule, {
242242
'OTHER_ATTR'
243243
]
244244
}]
245+
},
246+
{
247+
filename: 'test.vue',
248+
code:
249+
`<template>
250+
<div
251+
v-if="!visible"
252+
class="content"
253+
:class="className"
254+
v-text="textContent"
255+
>
256+
</div>
257+
</template>`,
258+
options: [
259+
{ order:
260+
[
261+
'CONDITIONALS',
262+
'LIST_RENDERING',
263+
'RENDER_MODIFIERS',
264+
'DEFINITION',
265+
'EVENTS',
266+
'UNIQUE',
267+
['BINDING', 'OTHER_ATTR'],
268+
'CONTENT',
269+
'GLOBAL'
270+
]
271+
}]
245272
}
246273
],
247274

@@ -510,6 +537,48 @@ tester.run('attributes-order', rule, {
510537
nodeType: 'VIdentifier'
511538
}
512539
]
540+
},
541+
{
542+
code:
543+
`<template>
544+
<div
545+
class="content"
546+
v-if="!visible"
547+
:class="className"
548+
v-text="textContent"
549+
>
550+
</div>
551+
</template>`,
552+
options: [
553+
{ order:
554+
[
555+
'CONDITIONALS',
556+
'LIST_RENDERING',
557+
'RENDER_MODIFIERS',
558+
'DEFINITION',
559+
'EVENTS',
560+
'UNIQUE',
561+
['BINDING', 'OTHER_ATTR'],
562+
'CONTENT',
563+
'GLOBAL'
564+
]
565+
}],
566+
output:
567+
`<template>
568+
<div
569+
v-if="!visible"
570+
class="content"
571+
:class="className"
572+
v-text="textContent"
573+
>
574+
</div>
575+
</template>`,
576+
errors: [
577+
{
578+
message: 'Attribute "v-if" should go before "class".',
579+
nodeType: 'VIdentifier'
580+
}
581+
]
513582
}
514583
]
515584
})

0 commit comments

Comments
 (0)