Skip to content

Commit 1b75e28

Browse files
authored
Refactor attributes-order (#1392)
1 parent c387fc3 commit 1b75e28

File tree

2 files changed

+44
-50
lines changed

2 files changed

+44
-50
lines changed

lib/rules/attributes-order.js

+16-22
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ function getDirectiveKeyName(directiveKey, sourceCode) {
9696

9797
/**
9898
* @param {VAttribute | VDirective} attribute
99-
* @param {SourceCode} sourceCode
10099
*/
101-
function getAttributeType(attribute, sourceCode) {
100+
function getAttributeType(attribute) {
102101
let propName
103102
if (attribute.directive) {
104103
if (!isVBind(attribute)) {
@@ -129,9 +128,10 @@ function getAttributeType(attribute, sourceCode) {
129128
return ATTRS.OTHER_DIRECTIVES
130129
}
131130
}
132-
propName = attribute.key.argument
133-
? sourceCode.getText(attribute.key.argument)
134-
: ''
131+
propName =
132+
attribute.key.argument && attribute.key.argument.type === 'VIdentifier'
133+
? attribute.key.argument.rawName
134+
: ''
135135
} else {
136136
propName = attribute.key.name
137137
}
@@ -154,11 +154,10 @@ function getAttributeType(attribute, sourceCode) {
154154
/**
155155
* @param {VAttribute | VDirective} attribute
156156
* @param { { [key: string]: number } } attributePosition
157-
* @param {SourceCode} sourceCode
158157
*/
159-
function getPosition(attribute, attributePosition, sourceCode) {
160-
const attributeType = getAttributeType(attribute, sourceCode)
161-
return attributePosition.hasOwnProperty(attributeType)
158+
function getPosition(attribute, attributePosition) {
159+
const attributeType = getAttributeType(attribute)
160+
return attributePosition[attributeType] != null
162161
? attributePosition[attributeType]
163162
: -1
164163
}
@@ -209,9 +208,9 @@ function create(context) {
209208
const attributePosition = {}
210209
attributeOrder.forEach((item, i) => {
211210
if (Array.isArray(item)) {
212-
item.forEach((attr) => {
211+
for (const attr of item) {
213212
attributePosition[attr] = i
214-
})
213+
}
215214
} else attributePosition[item] = i
216215
})
217216

@@ -223,8 +222,7 @@ function create(context) {
223222
const currentNode = sourceCode.getText(node.key)
224223
const prevNode = sourceCode.getText(previousNode.key)
225224
context.report({
226-
node: node.key,
227-
loc: node.loc,
225+
node,
228226
message: `Attribute "${currentNode}" should go before "${prevNode}".`,
229227
data: {
230228
currentNode
@@ -250,18 +248,14 @@ function create(context) {
250248
attributes.indexOf(previousNode),
251249
attributes.indexOf(node)
252250
)
253-
const moveUpNodes = [node]
254-
const moveDownNodes = []
255-
let index = 0
256-
while (previousNodes[index]) {
257-
const node = previousNodes[index++]
251+
const moveNodes = [node]
252+
for (const node of previousNodes) {
258253
if (isMoveUp(node)) {
259-
moveUpNodes.unshift(node)
254+
moveNodes.unshift(node)
260255
} else {
261-
moveDownNodes.push(node)
256+
moveNodes.push(node)
262257
}
263258
}
264-
const moveNodes = [...moveUpNodes, ...moveDownNodes]
265259

266260
return moveNodes.map((moveNode, index) => {
267261
const text = sourceCode.getText(moveNode)
@@ -337,7 +331,7 @@ function create(context) {
337331
}
338332
}
339333
}
340-
return getPosition(node, attributePosition, sourceCode)
334+
return getPosition(node, attributePosition)
341335
}
342336
}
343337
})

tests/lib/rules/attributes-order.js

+28-28
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ tester.run('attributes-order', rule, {
432432
errors: [
433433
{
434434
message: 'Attribute "is" should go before "v-cloak".',
435-
type: 'VIdentifier'
435+
type: 'VAttribute'
436436
}
437437
]
438438
},
@@ -443,7 +443,7 @@ tester.run('attributes-order', rule, {
443443
errors: [
444444
{
445445
message: 'Attribute "v-cloak" should go before "id".',
446-
type: 'VDirectiveKey'
446+
type: 'VAttribute'
447447
}
448448
]
449449
},
@@ -468,11 +468,11 @@ tester.run('attributes-order', rule, {
468468
errors: [
469469
{
470470
message: 'Attribute "v-model" should go before "model".',
471-
type: 'VDirectiveKey'
471+
type: 'VAttribute'
472472
},
473473
{
474474
message: 'Attribute ":id" should go before "propOne".',
475-
type: 'VDirectiveKey'
475+
type: 'VAttribute'
476476
}
477477
]
478478
},
@@ -499,11 +499,11 @@ tester.run('attributes-order', rule, {
499499
errors: [
500500
{
501501
message: 'Attribute "v-model" should go before "v-on".',
502-
type: 'VDirectiveKey'
502+
type: 'VAttribute'
503503
},
504504
{
505505
message: 'Attribute "propOne" should go before "v-on".',
506-
type: 'VIdentifier'
506+
type: 'VAttribute'
507507
}
508508
]
509509
},
@@ -516,7 +516,7 @@ tester.run('attributes-order', rule, {
516516
errors: [
517517
{
518518
message: 'Attribute "is" should go before "aria-test".',
519-
type: 'VIdentifier'
519+
type: 'VAttribute'
520520
}
521521
]
522522
},
@@ -546,7 +546,7 @@ tester.run('attributes-order', rule, {
546546
errors: [
547547
{
548548
message: 'Attribute "is" should go before "propone".',
549-
type: 'VIdentifier'
549+
type: 'VAttribute'
550550
}
551551
]
552552
},
@@ -565,7 +565,7 @@ tester.run('attributes-order', rule, {
565565
errors: [
566566
{
567567
message: 'Attribute "is" should go before "v-cloak".',
568-
type: 'VIdentifier'
568+
type: 'VAttribute'
569569
}
570570
]
571571
},
@@ -604,27 +604,27 @@ tester.run('attributes-order', rule, {
604604
errors: [
605605
{
606606
message: 'Attribute "v-for" should go before "v-if".',
607-
type: 'VDirectiveKey'
607+
type: 'VAttribute'
608608
},
609609
{
610610
message: 'Attribute "is" should go before "v-once".',
611-
type: 'VIdentifier'
611+
type: 'VAttribute'
612612
},
613613
{
614614
message: 'Attribute "ref" should go before "v-on:click".',
615-
type: 'VIdentifier'
615+
type: 'VAttribute'
616616
},
617617
{
618618
message: 'Attribute ":prop" should go before "v-on:click".',
619-
type: 'VDirectiveKey'
619+
type: 'VAttribute'
620620
},
621621
{
622622
message: 'Attribute "id" should go before "v-text".',
623-
type: 'VIdentifier'
623+
type: 'VAttribute'
624624
},
625625
{
626626
message: 'Attribute "myProp" should go before "v-text".',
627-
type: 'VIdentifier'
627+
type: 'VAttribute'
628628
}
629629
]
630630
},
@@ -680,23 +680,23 @@ tester.run('attributes-order', rule, {
680680
errors: [
681681
{
682682
message: 'Attribute "is" should go before "v-once".',
683-
type: 'VIdentifier'
683+
type: 'VAttribute'
684684
},
685685
{
686686
message: 'Attribute "v-on:click" should go before "v-once".',
687-
type: 'VDirectiveKey'
687+
type: 'VAttribute'
688688
},
689689
{
690690
message: 'Attribute "ref" should go before "v-once".',
691-
type: 'VIdentifier'
691+
type: 'VAttribute'
692692
},
693693
{
694694
message: 'Attribute "id" should go before "v-text".',
695-
type: 'VIdentifier'
695+
type: 'VAttribute'
696696
},
697697
{
698698
message: 'Attribute "myProp" should go before "v-text".',
699-
type: 'VIdentifier'
699+
type: 'VAttribute'
700700
}
701701
]
702702
},
@@ -737,7 +737,7 @@ tester.run('attributes-order', rule, {
737737
errors: [
738738
{
739739
message: 'Attribute "v-if" should go before "class".',
740-
type: 'VDirectiveKey'
740+
type: 'VAttribute'
741741
}
742742
]
743743
},
@@ -761,7 +761,7 @@ tester.run('attributes-order', rule, {
761761
errors: [
762762
{
763763
message: 'Attribute "v-slot" should go before "v-model".',
764-
type: 'VDirectiveKey'
764+
type: 'VAttribute'
765765
}
766766
]
767767
},
@@ -783,7 +783,7 @@ tester.run('attributes-order', rule, {
783783
errors: [
784784
{
785785
message: 'Attribute "a-prop" should go before "z-prop".',
786-
type: 'VIdentifier'
786+
type: 'VAttribute'
787787
}
788788
]
789789
},
@@ -805,7 +805,7 @@ tester.run('attributes-order', rule, {
805805
errors: [
806806
{
807807
message: 'Attribute ":a-prop" should go before ":z-prop".',
808-
type: 'VDirectiveKey'
808+
type: 'VAttribute'
809809
}
810810
]
811811
},
@@ -827,7 +827,7 @@ tester.run('attributes-order', rule, {
827827
errors: [
828828
{
829829
message: 'Attribute "@change" should go before "@input".',
830-
type: 'VDirectiveKey'
830+
type: 'VAttribute'
831831
}
832832
]
833833
},
@@ -849,7 +849,7 @@ tester.run('attributes-order', rule, {
849849
errors: [
850850
{
851851
message: 'Attribute "boolean-prop" should go before "z-prop".',
852-
type: 'VIdentifier'
852+
type: 'VAttribute'
853853
}
854854
]
855855
},
@@ -871,7 +871,7 @@ tester.run('attributes-order', rule, {
871871
errors: [
872872
{
873873
message: 'Attribute "v-on:[c]" should go before "v-on:click".',
874-
type: 'VDirectiveKey'
874+
type: 'VAttribute'
875875
}
876876
]
877877
},
@@ -893,7 +893,7 @@ tester.run('attributes-order', rule, {
893893
errors: [
894894
{
895895
message: 'Attribute "v-on:click" should go before "v-text".',
896-
type: 'VDirectiveKey'
896+
type: 'VAttribute'
897897
}
898898
]
899899
},

0 commit comments

Comments
 (0)