Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions docs/rules/attributes-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ ex: 'v-once', 'v-pre'
ex: 'id'
- UNIQUE
ex: 'ref', 'key', 'slot'
- BINDING
ex: 'v-model', 'v-bind', ':property="foo"'
- TWO\_WAY\_BINDING
ex: 'v-model'
- OTHER_DIRECTIVES
ex: 'v-custom-directive'
- OTHER_ATTR
ex: 'customProp="foo"'
ex: 'custom-prop="foo"', 'v-bind:prop="foo"', ':prop="foo"'
- EVENTS
ex: '@click="functionCall"', 'v-on="event"'
- CONTENT
Expand All @@ -38,7 +40,7 @@ ex: 'v-text', 'v-html'
id="uniqueID"
ref="header"
v-model="headerData"
myProp="prop"
my-prop="prop"
@click="functionCall"
v-text="textContent">
</div>
Expand All @@ -48,19 +50,19 @@ ex: 'v-text', 'v-html'
<div
v-for="item in items"
v-if="!visible"
propOne="prop"
propTwo="prop"
propThree="prop"
prop-one="prop"
:prop-two="prop"
prop-three="prop"
@click="functionCall"
v-text="textContent">
</div>
```

```html
<div
propOne="prop"
propTwo="prop"
propThree="prop">
prop-one="prop"
:prop-two="prop"
prop-three="prop">
</div>
```

Expand All @@ -70,9 +72,10 @@ ex: 'v-text', 'v-html'
<div
ref="header"
v-for="item in items"
v-once id="uniqueID"
v-once
id="uniqueID"
v-model="headerData"
myProp="prop"
my-prop="prop"
v-if="!visible"
is="header"
@click="functionCall"
Expand All @@ -87,31 +90,31 @@ Specify custom order of attribute groups
:+1: Examples of **correct** code with custom order`:

```html
<!-- 'vue/attributes-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'BINDING', 'OTHER_ATTR', 'EVENTS', 'CONTENT', 'DEFINITION'] }] -->
<!-- 'vue/attributes-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'TWO_WAY_BINDING', 'OTHER_DIRECTIVES', 'OTHER_ATTR', 'EVENTS', 'CONTENT', 'DEFINITION'] }] -->
<div
propOne="prop"
propTwo="prop"
prop-one="prop"
prop-two="prop"
is="header">
</div>
```

```html
<!-- 'vue/attributes-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'BINDING', 'DEFINITION', 'OTHER_ATTR', 'EVENTS', 'CONTENT'] }] -->
<!-- 'vue/attributes-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'TWO_WAY_BINDING', 'DEFINITION', 'OTHER_DIRECTIVES', 'OTHER_ATTR', 'EVENTS', 'CONTENT'] }] -->
<div
ref="header"
is="header"
propOne="prop"
propTwo="prop">
prop-one="prop"
prop-two="prop">
</div>
```

:-1: Examples of **incorrect** code with custom order`:

```html
<!-- 'vue/attributes-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'BINDING', 'DEFINITION', 'OTHER_ATTR', 'EVENTS', 'CONTENT'] }] -->
<!-- 'vue/attributes-order': [2, { order: ['LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'TWO_WAY_BINDING', 'DEFINITION', 'OTHER_DIRECTIVES', 'OTHER_ATTR', 'EVENTS', 'CONTENT'] }] -->
<div
ref="header"
propOne="prop"
prop-one="prop"
is="header">
</div>
```
Expand Down
15 changes: 11 additions & 4 deletions lib/rules/attributes-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ function getAttributeType (name, isDirective) {
return 'CONDITIONALS'
} else if (name === 'pre' || name === 'once') {
return 'RENDER_MODIFIERS'
} else if (name === 'model' || name === 'bind') {
return 'BINDING'
} else if (name === 'model') {
return 'TWO_WAY_BINDING'
} else if (name === 'on') {
return 'EVENTS'
} else if (name === 'html' || name === 'text') {
return 'CONTENT'
} else {
return 'OTHER_DIRECTIVES'
}
} else {
if (name === 'is') {
Expand All @@ -37,13 +39,18 @@ function getAttributeType (name, isDirective) {
}
}
function getPosition (attribute, attributeOrder) {
const attributeType = getAttributeType(attribute.key.name, attribute.directive)
let attributeType
if (attribute.directive && attribute.key.name === 'bind') {
attributeType = getAttributeType(attribute.key.argument, false)
} else {
attributeType = getAttributeType(attribute.key.name, attribute.directive)
}
return attributeOrder.indexOf(attributeType)
}

function create (context) {
const sourceCode = context.getSourceCode()
let attributeOrder = ['DEFINITION', 'LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'BINDING', 'OTHER_ATTR', 'EVENTS', 'CONTENT']
let attributeOrder = ['DEFINITION', 'LIST_RENDERING', 'CONDITIONALS', 'RENDER_MODIFIERS', 'GLOBAL', 'UNIQUE', 'TWO_WAY_BINDING', 'OTHER_DIRECTIVES', 'OTHER_ATTR', 'EVENTS', 'CONTENT']
if (context.options[0] && context.options[0].order) {
attributeOrder = context.options[0].order
}
Expand Down
37 changes: 21 additions & 16 deletions tests/lib/rules/attributes-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ tester.run('attributes-order', rule, {
filename: 'test.vue',
code: '<template><div v-model="toggle"></div></template>'
},
{
filename: 'test.vue',
code: '<template><div v-custom-directive></div></template>'
},
{
filename: 'test.vue',
code:
Expand All @@ -105,7 +109,7 @@ tester.run('attributes-order', rule, {
{
filename: 'test.vue',
code:
`<template>
`<template>
<div
is="header"
v-for="item in items"
Expand Down Expand Up @@ -164,7 +168,7 @@ tester.run('attributes-order', rule, {
v-for="item in items"
v-if="!visible"
propone="prop"
proptwo="prop"
:proptwo="prop"
propthree="prop"
@click="functionCall"
v-text="textContent">
Expand All @@ -185,7 +189,8 @@ tester.run('attributes-order', rule, {
'RENDER_MODIFIERS',
'GLOBAL',
'UNIQUE',
'BINDING',
'TWO_WAY_BINDING',
'OTHER_DIRECTIVES',
'OTHER_ATTR',
'EVENTS',
'CONTENT',
Expand All @@ -202,8 +207,9 @@ tester.run('attributes-order', rule, {
'RENDER_MODIFIERS',
'GLOBAL',
'UNIQUE',
'BINDING',
'TWO_WAY_BINDING',
'DEFINITION',
'OTHER_DIRECTIVES',
'OTHER_ATTR',
'EVENTS',
'CONTENT']
Expand All @@ -220,9 +226,9 @@ tester.run('attributes-order', rule, {
is="header"
v-on:click="functionCall"
ref="header"
:prop="headerData"
v-text="textContent"
id="uniqueID"
:prop="headerData"
myProp="prop"
>
</div>
Expand All @@ -236,10 +242,11 @@ tester.run('attributes-order', rule, {
'DEFINITION',
'EVENTS',
'UNIQUE',
'BINDING',
'TWO_WAY_BINDING',
'CONTENT',
'GLOBAL',
'OTHER_ATTR'
'OTHER_ATTR',
'OTHER_DIRECTIVES'
]
}]
}
Expand Down Expand Up @@ -272,15 +279,15 @@ tester.run('attributes-order', rule, {
model="baz"
v-model="toggle"
propOne="bar"
:bindingProp="foo">
:id="foo">
</div>
</template>`,
output:
`<template>
<div
v-model="toggle"
model="baz"
:bindingProp="foo"
:id="foo"
propOne="bar">
</div>
</template>`,
Expand All @@ -289,7 +296,7 @@ tester.run('attributes-order', rule, {
type: 'VDirectiveKey'
},
{
message: 'Attribute ":bindingProp" should go before "propOne".',
message: 'Attribute ":id" should go before "propOne".',
type: 'VDirectiveKey'
}]
},
Expand Down Expand Up @@ -343,8 +350,9 @@ tester.run('attributes-order', rule, {
'RENDER_MODIFIERS',
'GLOBAL',
'UNIQUE',
'BINDING',
'TWO_WAY_BINDING',
'DEFINITION',
'OTHER_DIRECTIVES',
'OTHER_ATTR',
'EVENTS',
'CONTENT']
Expand Down Expand Up @@ -457,14 +465,15 @@ tester.run('attributes-order', rule, {
{ order:
[
'EVENTS',
'BINDING',
'TWO_WAY_BINDING',
'UNIQUE',
'DEFINITION',
'CONDITIONALS',
'LIST_RENDERING',
'RENDER_MODIFIERS',
'GLOBAL',
'OTHER_ATTR',
'OTHER_DIRECTIVES',
'CONTENT'
]
}],
Expand Down Expand Up @@ -497,10 +506,6 @@ tester.run('attributes-order', rule, {
message: 'Attribute "ref" should go before "v-once".',
nodeType: 'VIdentifier'
},
{
message: 'Attribute ":prop" should go before "v-once".',
nodeType: 'VDirectiveKey'
},
{
message: 'Attribute "id" should go before "v-text".',
nodeType: 'VIdentifier'
Expand Down