Skip to content

Commit 8cd3a41

Browse files
authored
adding in alphabetical ordering within groupings (#1022)
1 parent b394ca6 commit 8cd3a41

File tree

3 files changed

+392
-175
lines changed

3 files changed

+392
-175
lines changed

docs/rules/attributes-order.md

+47-1
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,57 @@ This rule aims to enforce ordering of component attributes. The default order is
105105
"OTHER_ATTR",
106106
"EVENTS",
107107
"CONTENT"
108-
]
108+
],
109+
"alphabetical": true
109110
}]
110111
}
111112
```
112113

114+
### Alphabetical order
115+
<eslint-code-block fix :rules="{'vue/attributes-order': ['error', {alphabetical: true}]}">
116+
117+
```vue
118+
<template>
119+
<!-- ✓ GOOD -->
120+
<div
121+
a-custom-prop="value"
122+
:another-custom-prop="value"
123+
:blue-color="false"
124+
boolean-prop
125+
z-prop="Z"
126+
v-on:[c]="functionCall"
127+
@change="functionCall"
128+
v-on:click="functionCall"
129+
@input="functionCall"
130+
v-text="textContent">
131+
</div>
132+
133+
<!-- ✗ BAD -->
134+
<div
135+
z-prop="Z"
136+
a-prop="A">
137+
</div>
138+
139+
<div
140+
@input="bar"
141+
@change="foo">
142+
</div>
143+
144+
<div
145+
v-on:click="functionCall"
146+
v-on:[c]="functionCall">
147+
</div>
148+
149+
<div
150+
:a-prop="A"
151+
:z-prop="Z">
152+
</div>
153+
154+
</template>
155+
```
156+
157+
</eslint-code-block>
158+
113159
### Custom orders
114160

115161
#### `['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'TWO_WAY_BINDING', 'DEFINITION', 'OTHER_DIRECTIVES', 'OTHER_ATTR', 'EVENTS', 'CONTENT']`

lib/rules/attributes-order.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ const ATTRS = {
2222
CONTENT: 'CONTENT'
2323
}
2424

25+
function getAttributeName (attribute, sourceCode) {
26+
const isBind = attribute.directive && attribute.key.name.name === 'bind'
27+
debugger
28+
return isBind
29+
? (attribute.key.argument ? sourceCode.getText(attribute.key.argument) : '')
30+
: (attribute.directive ? sourceCode.getText(attribute.key.argument) : attribute.key.name)
31+
}
32+
2533
function getAttributeType (attribute, sourceCode) {
2634
const isBind = attribute.directive && attribute.key.name.name === 'bind'
2735
const name = isBind
@@ -64,6 +72,14 @@ function getPosition (attribute, attributePosition, sourceCode) {
6472
return attributePosition.hasOwnProperty(attributeType) ? attributePosition[attributeType] : -1
6573
}
6674

75+
function isAlphabetical (prevNode, currNode, sourceCode) {
76+
const isSameType = getAttributeType(prevNode, sourceCode) === getAttributeType(currNode, sourceCode)
77+
if (isSameType) {
78+
return getAttributeName(prevNode, sourceCode) < getAttributeName(currNode, sourceCode)
79+
}
80+
return true
81+
}
82+
6783
function create (context) {
6884
const sourceCode = context.getSourceCode()
6985
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]
@@ -110,7 +126,11 @@ function create (context) {
110126
previousNode = null
111127
},
112128
'VAttribute' (node) {
113-
if ((currentPosition === -1) || (currentPosition <= getPosition(node, attributePosition, sourceCode))) {
129+
let inAlphaOrder = true
130+
if (currentPosition !== -1 && (context.options[0] && context.options[0].alphabetical)) {
131+
inAlphaOrder = isAlphabetical(previousNode, node, sourceCode)
132+
}
133+
if ((currentPosition === -1) || ((currentPosition <= getPosition(node, attributePosition, sourceCode)) && inAlphaOrder)) {
114134
currentPosition = getPosition(node, attributePosition, sourceCode)
115135
previousNode = node
116136
} else {

0 commit comments

Comments
 (0)