From 1b75e283946882859171e0f66d94bd4250ae64c5 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Tue, 29 Dec 2020 03:34:06 +0900 Subject: [PATCH] Refactor attributes-order (#1392) --- lib/rules/attributes-order.js | 38 +++++++++----------- tests/lib/rules/attributes-order.js | 56 ++++++++++++++--------------- 2 files changed, 44 insertions(+), 50 deletions(-) diff --git a/lib/rules/attributes-order.js b/lib/rules/attributes-order.js index 2441ee497..96fb1ac1d 100644 --- a/lib/rules/attributes-order.js +++ b/lib/rules/attributes-order.js @@ -96,9 +96,8 @@ function getDirectiveKeyName(directiveKey, sourceCode) { /** * @param {VAttribute | VDirective} attribute - * @param {SourceCode} sourceCode */ -function getAttributeType(attribute, sourceCode) { +function getAttributeType(attribute) { let propName if (attribute.directive) { if (!isVBind(attribute)) { @@ -129,9 +128,10 @@ function getAttributeType(attribute, sourceCode) { return ATTRS.OTHER_DIRECTIVES } } - propName = attribute.key.argument - ? sourceCode.getText(attribute.key.argument) - : '' + propName = + attribute.key.argument && attribute.key.argument.type === 'VIdentifier' + ? attribute.key.argument.rawName + : '' } else { propName = attribute.key.name } @@ -154,11 +154,10 @@ function getAttributeType(attribute, sourceCode) { /** * @param {VAttribute | VDirective} attribute * @param { { [key: string]: number } } attributePosition - * @param {SourceCode} sourceCode */ -function getPosition(attribute, attributePosition, sourceCode) { - const attributeType = getAttributeType(attribute, sourceCode) - return attributePosition.hasOwnProperty(attributeType) +function getPosition(attribute, attributePosition) { + const attributeType = getAttributeType(attribute) + return attributePosition[attributeType] != null ? attributePosition[attributeType] : -1 } @@ -209,9 +208,9 @@ function create(context) { const attributePosition = {} attributeOrder.forEach((item, i) => { if (Array.isArray(item)) { - item.forEach((attr) => { + for (const attr of item) { attributePosition[attr] = i - }) + } } else attributePosition[item] = i }) @@ -223,8 +222,7 @@ function create(context) { const currentNode = sourceCode.getText(node.key) const prevNode = sourceCode.getText(previousNode.key) context.report({ - node: node.key, - loc: node.loc, + node, message: `Attribute "${currentNode}" should go before "${prevNode}".`, data: { currentNode @@ -250,18 +248,14 @@ function create(context) { attributes.indexOf(previousNode), attributes.indexOf(node) ) - const moveUpNodes = [node] - const moveDownNodes = [] - let index = 0 - while (previousNodes[index]) { - const node = previousNodes[index++] + const moveNodes = [node] + for (const node of previousNodes) { if (isMoveUp(node)) { - moveUpNodes.unshift(node) + moveNodes.unshift(node) } else { - moveDownNodes.push(node) + moveNodes.push(node) } } - const moveNodes = [...moveUpNodes, ...moveDownNodes] return moveNodes.map((moveNode, index) => { const text = sourceCode.getText(moveNode) @@ -337,7 +331,7 @@ function create(context) { } } } - return getPosition(node, attributePosition, sourceCode) + return getPosition(node, attributePosition) } } }) diff --git a/tests/lib/rules/attributes-order.js b/tests/lib/rules/attributes-order.js index 399a859b4..97c3b6e73 100644 --- a/tests/lib/rules/attributes-order.js +++ b/tests/lib/rules/attributes-order.js @@ -432,7 +432,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "is" should go before "v-cloak".', - type: 'VIdentifier' + type: 'VAttribute' } ] }, @@ -443,7 +443,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "v-cloak" should go before "id".', - type: 'VDirectiveKey' + type: 'VAttribute' } ] }, @@ -468,11 +468,11 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "v-model" should go before "model".', - type: 'VDirectiveKey' + type: 'VAttribute' }, { message: 'Attribute ":id" should go before "propOne".', - type: 'VDirectiveKey' + type: 'VAttribute' } ] }, @@ -499,11 +499,11 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "v-model" should go before "v-on".', - type: 'VDirectiveKey' + type: 'VAttribute' }, { message: 'Attribute "propOne" should go before "v-on".', - type: 'VIdentifier' + type: 'VAttribute' } ] }, @@ -516,7 +516,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "is" should go before "aria-test".', - type: 'VIdentifier' + type: 'VAttribute' } ] }, @@ -546,7 +546,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "is" should go before "propone".', - type: 'VIdentifier' + type: 'VAttribute' } ] }, @@ -565,7 +565,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "is" should go before "v-cloak".', - type: 'VIdentifier' + type: 'VAttribute' } ] }, @@ -604,27 +604,27 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "v-for" should go before "v-if".', - type: 'VDirectiveKey' + type: 'VAttribute' }, { message: 'Attribute "is" should go before "v-once".', - type: 'VIdentifier' + type: 'VAttribute' }, { message: 'Attribute "ref" should go before "v-on:click".', - type: 'VIdentifier' + type: 'VAttribute' }, { message: 'Attribute ":prop" should go before "v-on:click".', - type: 'VDirectiveKey' + type: 'VAttribute' }, { message: 'Attribute "id" should go before "v-text".', - type: 'VIdentifier' + type: 'VAttribute' }, { message: 'Attribute "myProp" should go before "v-text".', - type: 'VIdentifier' + type: 'VAttribute' } ] }, @@ -680,23 +680,23 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "is" should go before "v-once".', - type: 'VIdentifier' + type: 'VAttribute' }, { message: 'Attribute "v-on:click" should go before "v-once".', - type: 'VDirectiveKey' + type: 'VAttribute' }, { message: 'Attribute "ref" should go before "v-once".', - type: 'VIdentifier' + type: 'VAttribute' }, { message: 'Attribute "id" should go before "v-text".', - type: 'VIdentifier' + type: 'VAttribute' }, { message: 'Attribute "myProp" should go before "v-text".', - type: 'VIdentifier' + type: 'VAttribute' } ] }, @@ -737,7 +737,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "v-if" should go before "class".', - type: 'VDirectiveKey' + type: 'VAttribute' } ] }, @@ -761,7 +761,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "v-slot" should go before "v-model".', - type: 'VDirectiveKey' + type: 'VAttribute' } ] }, @@ -783,7 +783,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "a-prop" should go before "z-prop".', - type: 'VIdentifier' + type: 'VAttribute' } ] }, @@ -805,7 +805,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute ":a-prop" should go before ":z-prop".', - type: 'VDirectiveKey' + type: 'VAttribute' } ] }, @@ -827,7 +827,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "@change" should go before "@input".', - type: 'VDirectiveKey' + type: 'VAttribute' } ] }, @@ -849,7 +849,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "boolean-prop" should go before "z-prop".', - type: 'VIdentifier' + type: 'VAttribute' } ] }, @@ -871,7 +871,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "v-on:[c]" should go before "v-on:click".', - type: 'VDirectiveKey' + type: 'VAttribute' } ] }, @@ -893,7 +893,7 @@ tester.run('attributes-order', rule, { errors: [ { message: 'Attribute "v-on:click" should go before "v-text".', - type: 'VDirectiveKey' + type: 'VAttribute' } ] },