From 56647f560395ba24fd54f05f4a8c3739688c2e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=B4=E9=87=8C=E5=88=87=E7=BD=97?= Date: Mon, 26 Feb 2018 18:19:58 +0800 Subject: [PATCH 1/6] Fix: v-bind order in `vue/attribute-order` rule --- docs/rules/attributes-order.md | 18 +++++++++--------- lib/rules/attributes-order.js | 13 +++++++++---- tests/lib/rules/attributes-order.js | 14 +++++++------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/docs/rules/attributes-order.md b/docs/rules/attributes-order.md index 999a19523..926eef1e9 100644 --- a/docs/rules/attributes-order.md +++ b/docs/rules/attributes-order.md @@ -5,7 +5,7 @@ ## :book: Rule Details -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: +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: - DEFINITION ex: 'is' - LIST_RENDERING @@ -18,8 +18,8 @@ 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_ATTR ex: 'customProp="foo"' - EVENTS @@ -49,7 +49,7 @@ ex: 'v-text', 'v-html' v-for="item in items" v-if="!visible" propOne="prop" - propTwo="prop" + :propTwo="prop" propThree="prop" @click="functionCall" v-text="textContent"> @@ -59,7 +59,7 @@ ex: 'v-text', 'v-html' ```html
``` @@ -87,7 +87,7 @@ Specify custom order of attribute groups :+1: Examples of **correct** code with custom order`: ```html - +
+
+
-``` +``` \ No newline at end of file diff --git a/lib/rules/attributes-order.js b/lib/rules/attributes-order.js index 0ae63b95a..7446ffd62 100644 --- a/lib/rules/attributes-order.js +++ b/lib/rules/attributes-order.js @@ -17,8 +17,8 @@ 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') { @@ -37,13 +37,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_ATTR', 'EVENTS', 'CONTENT'] if (context.options[0] && context.options[0].order) { attributeOrder = context.options[0].order } diff --git a/tests/lib/rules/attributes-order.js b/tests/lib/rules/attributes-order.js index 9366ac130..4d9096e97 100644 --- a/tests/lib/rules/attributes-order.js +++ b/tests/lib/rules/attributes-order.js @@ -105,7 +105,7 @@ tester.run('attributes-order', rule, { { filename: 'test.vue', code: - ``, @@ -464,7 +465,7 @@ tester.run('attributes-order', rule, { { order: [ 'EVENTS', - 'BINDING', + 'TWO_WAY_BINDING', 'UNIQUE', 'DEFINITION', 'CONDITIONALS', @@ -472,6 +473,7 @@ tester.run('attributes-order', rule, { 'RENDER_MODIFIERS', 'GLOBAL', 'OTHER_ATTR', + 'OTHER_DIRECTIVES', 'CONTENT' ] }], @@ -504,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'