Skip to content

Commit 5d06597

Browse files
committed
Fix: v-bind order in vue/attribute-order rule
1 parent 92c3609 commit 5d06597

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

docs/rules/attributes-order.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## :book: Rule Details
66

7-
This rule aims to enforce ordering of component attributes. The default order is specified in the [Vue styleguide](https://vuejs.org/v2/style-guide/#Element-attribute-order-recommended) and is:
7+
This rule aims to enfore ordering of component attributes. The default order is specified in the [Vue styleguide](https://vuejs.org/v2/style-guide/#Element-attribute-order-recommended) and is:
88
- DEFINITION
99
ex: 'is'
1010
- LIST_RENDERING
@@ -17,8 +17,8 @@ ex: 'v-once', 'v-pre'
1717
ex: 'id'
1818
- UNIQUE
1919
ex: 'ref', 'key', 'slot'
20-
- BINDING
21-
ex: 'v-model', 'v-bind', ':property="foo"'
20+
- TWO\_WAY\_BINDING
21+
ex: 'v-model'
2222
- OTHER_ATTR
2323
ex: 'customProp="foo"'
2424
- EVENTS
@@ -48,7 +48,7 @@ ex: 'v-text', 'v-html'
4848
v-for="item in items"
4949
v-if="!visible"
5050
propOne="prop"
51-
propTwo="prop"
51+
:propTwo="prop"
5252
propThree="prop"
5353
@click="functionCall"
5454
v-text="textContent">
@@ -58,7 +58,7 @@ ex: 'v-text', 'v-html'
5858
```html
5959
<div
6060
propOne="prop"
61-
propTwo="prop"
61+
:propTwo="prop"
6262
propThree="prop">
6363
</div>
6464
```
@@ -86,7 +86,7 @@ Specify custom order of attribute groups
8686
:+1: Examples of **correct** code with custom order`:
8787

8888
```html
89-
<!-- 'vue/attributes-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'BINDING', 'OTHER_ATTR', 'EVENTS', 'CONTENT', 'DEFINITION'] }] -->
89+
<!-- 'vue/attribute-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'TWO_WAY_BINDING', 'OTHER_ATTR', 'EVENTS', 'CONTENT', 'DEFINITION'] }] -->
9090
<div
9191
propOne="prop"
9292
propTwo="prop"
@@ -95,7 +95,7 @@ Specify custom order of attribute groups
9595
```
9696

9797
```html
98-
<!-- 'vue/attributes-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'BINDING', 'DEFINITION', 'OTHER_ATTR', 'EVENTS', 'CONTENT'] }] -->
98+
<!-- 'vue/attribute-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'TWO_WAY_BINDING', 'DEFINITION', 'OTHER_ATTR', 'EVENTS', 'CONTENT'] }] -->
9999
<div
100100
ref="header"
101101
is="header"
@@ -107,10 +107,10 @@ Specify custom order of attribute groups
107107
:-1: Examples of **incorrect** code with custom order`:
108108

109109
```html
110-
<!-- 'vue/attributes-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'BINDING', 'DEFINITION', 'OTHER_ATTR', 'EVENTS', 'CONTENT'] }] -->
110+
<!-- 'vue/attribute-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'TWO_WAY_BINDING', 'DEFINITION', 'OTHER_ATTR', 'EVENTS', 'CONTENT'] }] -->
111111
<div
112112
ref="header"
113113
propOne="prop"
114114
is="header">
115115
</div>
116-
```
116+
```

lib/rules/attributes-order.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ function getAttributeType (name, isDirective) {
1717
return 'CONDITIONALS'
1818
} else if (name === 'pre' || name === 'once') {
1919
return 'RENDER_MODIFIERS'
20-
} else if (name === 'model' || name === 'bind') {
21-
return 'BINDING'
20+
} else if (name === 'model') {
21+
return 'TWO_WAY_BINDING'
2222
} else if (name === 'on') {
2323
return 'EVENTS'
2424
} else if (name === 'html' || name === 'text') {
@@ -37,13 +37,18 @@ function getAttributeType (name, isDirective) {
3737
}
3838
}
3939
function getPosition (attribute, attributeOrder) {
40-
const attributeType = getAttributeType(attribute.key.name, attribute.directive)
40+
let attributeType
41+
if (attribute.directive && attribute.key.name === 'bind') {
42+
attributeType = getAttributeType(attribute.key.argument, false)
43+
} else {
44+
attributeType = getAttributeType(attribute.key.name, attribute.directive)
45+
}
4146
return attributeOrder.indexOf(attributeType)
4247
}
4348

4449
function create (context) {
4550
const sourceCode = context.getSourceCode()
46-
let attributeOrder = ['DEFINITION', 'LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'BINDING', 'OTHER_ATTR', 'EVENTS', 'CONTENT']
51+
let attributeOrder = ['DEFINITION', 'LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'TWO_WAY_BINDING', 'OTHER_ATTR', 'EVENTS', 'CONTENT']
4752
if (context.options[0] && context.options[0].order) {
4853
attributeOrder = context.options[0].order
4954
}

tests/lib/rules/attributes-order.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ tester.run('attributes-order', rule, {
105105
{
106106
filename: 'test.vue',
107107
code:
108-
`<template>
108+
`<template>
109109
<div
110110
is="header"
111111
v-for="item in items"
@@ -164,7 +164,7 @@ tester.run('attributes-order', rule, {
164164
v-for="item in items"
165165
v-if="!visible"
166166
propone="prop"
167-
proptwo="prop"
167+
:proptwo="prop"
168168
propthree="prop"
169169
@click="functionCall"
170170
v-text="textContent">
@@ -185,7 +185,7 @@ tester.run('attributes-order', rule, {
185185
'RENDER_MODIFIERS',
186186
'GLOBAL',
187187
'UNIQUE',
188-
'BINDING',
188+
'TWO_WAY_BINDING',
189189
'OTHER_ATTR',
190190
'EVENTS',
191191
'CONTENT',
@@ -202,7 +202,7 @@ tester.run('attributes-order', rule, {
202202
'RENDER_MODIFIERS',
203203
'GLOBAL',
204204
'UNIQUE',
205-
'BINDING',
205+
'TWO_WAY_BINDING',
206206
'DEFINITION',
207207
'OTHER_ATTR',
208208
'EVENTS',
@@ -236,15 +236,15 @@ tester.run('attributes-order', rule, {
236236
model="baz"
237237
v-model="toggle"
238238
propOne="bar"
239-
:bindingProp="foo">
239+
:id="foo">
240240
</div>
241241
</template>`,
242242
errors: [{
243243
message: 'Attribute "v-model" should go before "model".',
244244
type: 'VDirectiveKey'
245245
},
246246
{
247-
message: 'Attribute ":bindingProp" should go before "propOne".',
247+
message: 'Attribute ":id" should go before "propOne".',
248248
type: 'VDirectiveKey'
249249
}]
250250
},
@@ -287,7 +287,7 @@ tester.run('attributes-order', rule, {
287287
'RENDER_MODIFIERS',
288288
'GLOBAL',
289289
'UNIQUE',
290-
'BINDING',
290+
'TWO_WAY_BINDING',
291291
'DEFINITION',
292292
'OTHER_ATTR',
293293
'EVENTS',

0 commit comments

Comments
 (0)