Skip to content

Commit c4f77f1

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

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-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)

0 commit comments

Comments
 (0)