From f6d2fa0d31cea3dc72a2462a08213fa4874fd959 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Mon, 17 Feb 2020 18:40:49 +0900 Subject: [PATCH 1/4] Fixed false positives for binded and unbinded attrs in 'vue/attributes-order' with `alphabetical` option. --- docs/rules/attributes-order.md | 16 ++++++--- lib/rules/attributes-order.js | 10 ++++-- tests/lib/rules/attributes-order.js | 54 +++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/docs/rules/attributes-order.md b/docs/rules/attributes-order.md index 879ec1d57..59941462a 100644 --- a/docs/rules/attributes-order.md +++ b/docs/rules/attributes-order.md @@ -106,12 +106,13 @@ This rule aims to enforce ordering of component attributes. The default order is "EVENTS", "CONTENT" ], - "alphabetical": true + "alphabetical": false }] } ``` -### Alphabetical order +### `"alphabetical": true` + ```vue @@ -122,6 +123,8 @@ This rule aims to enforce ordering of component attributes. The default order is :another-custom-prop="value" :blue-color="false" boolean-prop + class="foo" + :class="bar" z-prop="Z" v-on:[c]="functionCall" @change="functionCall" @@ -147,8 +150,13 @@ This rule aims to enforce ordering of component attributes. The default order is
+ :z-prop="Z" + :a-prop="A"> +
+ +
diff --git a/lib/rules/attributes-order.js b/lib/rules/attributes-order.js index ac8afe283..56aee134b 100644 --- a/lib/rules/attributes-order.js +++ b/lib/rules/attributes-order.js @@ -24,7 +24,6 @@ const ATTRS = { function getAttributeName (attribute, sourceCode) { const isBind = attribute.directive && attribute.key.name.name === 'bind' - debugger return isBind ? (attribute.key.argument ? sourceCode.getText(attribute.key.argument) : '') : (attribute.directive ? sourceCode.getText(attribute.key.argument) : attribute.key.name) @@ -75,7 +74,14 @@ function getPosition (attribute, attributePosition, sourceCode) { function isAlphabetical (prevNode, currNode, sourceCode) { const isSameType = getAttributeType(prevNode, sourceCode) === getAttributeType(currNode, sourceCode) if (isSameType) { - return getAttributeName(prevNode, sourceCode) < getAttributeName(currNode, sourceCode) + const prevName = getAttributeName(prevNode, sourceCode) + const currName = getAttributeName(currNode, sourceCode) + if (prevName === currName) { + const prevIsBind = Boolean(prevNode.directive && prevNode.key.name.name === 'bind') + const currIsBind = Boolean(currNode.directive && currNode.key.name.name === 'bind') + return prevIsBind <= currIsBind + } + return prevName < currName } return true } diff --git a/tests/lib/rules/attributes-order.js b/tests/lib/rules/attributes-order.js index 481148f9d..c5579aa05 100644 --- a/tests/lib/rules/attributes-order.js +++ b/tests/lib/rules/attributes-order.js @@ -312,6 +312,39 @@ tester.run('attributes-order', rule, { `, options: [{ alphabetical: true }] + }, + { + filename: 'test.vue', + code: + ``, + options: [{ alphabetical: true }] + }, + { + filename: 'duplicate.vue', + code: + ``, + options: [{ alphabetical: true }] + }, + { + filename: 'duplicate.vue', + code: + ``, + options: [{ alphabetical: true }] } ], @@ -778,6 +811,27 @@ tester.run('attributes-order', rule, { message: 'Attribute "v-on:click" should go before "v-text".', type: 'VDirectiveKey' }] + }, + { + filename: 'test.vue', + code: + ``, + options: [{ alphabetical: true }], + output: + ``, + errors: [{ + message: 'Attribute "class" should go before ":class".' + }] } ] }) From b4152231fa185d0cbba960c5cb461d05ca44334c Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Mon, 17 Feb 2020 20:16:47 +0900 Subject: [PATCH 2/4] Fixed #1056 --- lib/rules/attributes-order.js | 13 +++- tests/lib/rules/attributes-order.js | 96 +++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/lib/rules/attributes-order.js b/lib/rules/attributes-order.js index 56aee134b..32b1cb102 100644 --- a/lib/rules/attributes-order.js +++ b/lib/rules/attributes-order.js @@ -26,7 +26,18 @@ function getAttributeName (attribute, sourceCode) { const isBind = attribute.directive && attribute.key.name.name === 'bind' return isBind ? (attribute.key.argument ? sourceCode.getText(attribute.key.argument) : '') - : (attribute.directive ? sourceCode.getText(attribute.key.argument) : attribute.key.name) + : (attribute.directive ? getDirectiveKeyName(attribute.key, sourceCode) : attribute.key.name) +} + +function getDirectiveKeyName (directiveKey, sourceCode) { + let text = 'v-' + directiveKey.name.name + if (directiveKey.argument) { + text += ':' + sourceCode.getText(directiveKey.argument) + } + for (const modifier of directiveKey.modifiers) { + text += '.' + modifier.name + } + return text } function getAttributeType (attribute, sourceCode) { diff --git a/tests/lib/rules/attributes-order.js b/tests/lib/rules/attributes-order.js index c5579aa05..5583c9a1c 100644 --- a/tests/lib/rules/attributes-order.js +++ b/tests/lib/rules/attributes-order.js @@ -345,6 +345,39 @@ tester.run('attributes-order', rule, { `, options: [{ alphabetical: true }] + }, + { + filename: 'test.vue', + code: + ``, + options: [{ alphabetical: true }] + }, + { + filename: 'test.vue', + code: + ``, + options: [{ alphabetical: true }] + }, + { + filename: 'test.vue', + code: + ``, + options: [{ alphabetical: true }] } ], @@ -832,6 +865,69 @@ tester.run('attributes-order', rule, { errors: [{ message: 'Attribute "class" should go before ":class".' }] + }, + { + filename: 'test.vue', + code: + ``, + options: [{ alphabetical: true }], + output: + ``, + errors: [{ + message: 'Attribute "v-if" should go before "v-show".' + }] + }, + { + filename: 'test.vue', + code: + ``, + options: [{ alphabetical: true }], + output: + ``, + errors: [{ + message: 'Attribute "v-bar" should go before "v-foo".' + }] + }, + { + filename: 'test.vue', + code: + ``, + options: [{ alphabetical: true }], + output: + ``, + errors: [{ + message: 'Attribute "v-foo.a" should go before "v-foo.b".' + }] } ] }) From 4cd48f4cd480e56f2e2ec2646c45d85dbbd42558 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Mon, 17 Feb 2020 21:31:53 +0900 Subject: [PATCH 3/4] test timeout to 300000ms --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1212002bf..c64f9b29c 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "npm run test:base -- --watch --growl", "test:base": "mocha \"tests/lib/**/*.js\" --reporter dot", - "test": "nyc npm run test:base -- \"tests/integrations/*.js\" --timeout 60000", + "test": "nyc npm run test:base -- \"tests/integrations/*.js\" --timeout 300000", "debug": "mocha --inspect-brk \"tests/lib/**/*.js\" --reporter dot --timeout 60000", "lint": "eslint . --rulesdir eslint-internal-rules", "pretest": "npm run lint", From 97cba15c3fcb53077d81bfed5e0d97d90422c9b9 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Mon, 17 Feb 2020 22:11:25 +0900 Subject: [PATCH 4/4] revert timeout --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c64f9b29c..1212002bf 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "npm run test:base -- --watch --growl", "test:base": "mocha \"tests/lib/**/*.js\" --reporter dot", - "test": "nyc npm run test:base -- \"tests/integrations/*.js\" --timeout 300000", + "test": "nyc npm run test:base -- \"tests/integrations/*.js\" --timeout 60000", "debug": "mocha --inspect-brk \"tests/lib/**/*.js\" --reporter dot --timeout 60000", "lint": "eslint . --rulesdir eslint-internal-rules", "pretest": "npm run lint",