Skip to content

Commit

Permalink
Refactor attributes-order (#1392)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Dec 28, 2020
1 parent c387fc3 commit 1b75e28
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 50 deletions.
38 changes: 16 additions & 22 deletions lib/rules/attributes-order.js
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
})

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -337,7 +331,7 @@ function create(context) {
}
}
}
return getPosition(node, attributePosition, sourceCode)
return getPosition(node, attributePosition)
}
}
})
Expand Down
56 changes: 28 additions & 28 deletions tests/lib/rules/attributes-order.js
Expand Up @@ -432,7 +432,7 @@ tester.run('attributes-order', rule, {
errors: [
{
message: 'Attribute "is" should go before "v-cloak".',
type: 'VIdentifier'
type: 'VAttribute'
}
]
},
Expand All @@ -443,7 +443,7 @@ tester.run('attributes-order', rule, {
errors: [
{
message: 'Attribute "v-cloak" should go before "id".',
type: 'VDirectiveKey'
type: 'VAttribute'
}
]
},
Expand All @@ -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'
}
]
},
Expand All @@ -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'
}
]
},
Expand All @@ -516,7 +516,7 @@ tester.run('attributes-order', rule, {
errors: [
{
message: 'Attribute "is" should go before "aria-test".',
type: 'VIdentifier'
type: 'VAttribute'
}
]
},
Expand Down Expand Up @@ -546,7 +546,7 @@ tester.run('attributes-order', rule, {
errors: [
{
message: 'Attribute "is" should go before "propone".',
type: 'VIdentifier'
type: 'VAttribute'
}
]
},
Expand All @@ -565,7 +565,7 @@ tester.run('attributes-order', rule, {
errors: [
{
message: 'Attribute "is" should go before "v-cloak".',
type: 'VIdentifier'
type: 'VAttribute'
}
]
},
Expand Down Expand Up @@ -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'
}
]
},
Expand Down Expand Up @@ -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'
}
]
},
Expand Down Expand Up @@ -737,7 +737,7 @@ tester.run('attributes-order', rule, {
errors: [
{
message: 'Attribute "v-if" should go before "class".',
type: 'VDirectiveKey'
type: 'VAttribute'
}
]
},
Expand All @@ -761,7 +761,7 @@ tester.run('attributes-order', rule, {
errors: [
{
message: 'Attribute "v-slot" should go before "v-model".',
type: 'VDirectiveKey'
type: 'VAttribute'
}
]
},
Expand All @@ -783,7 +783,7 @@ tester.run('attributes-order', rule, {
errors: [
{
message: 'Attribute "a-prop" should go before "z-prop".',
type: 'VIdentifier'
type: 'VAttribute'
}
]
},
Expand All @@ -805,7 +805,7 @@ tester.run('attributes-order', rule, {
errors: [
{
message: 'Attribute ":a-prop" should go before ":z-prop".',
type: 'VDirectiveKey'
type: 'VAttribute'
}
]
},
Expand All @@ -827,7 +827,7 @@ tester.run('attributes-order', rule, {
errors: [
{
message: 'Attribute "@change" should go before "@input".',
type: 'VDirectiveKey'
type: 'VAttribute'
}
]
},
Expand All @@ -849,7 +849,7 @@ tester.run('attributes-order', rule, {
errors: [
{
message: 'Attribute "boolean-prop" should go before "z-prop".',
type: 'VIdentifier'
type: 'VAttribute'
}
]
},
Expand All @@ -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'
}
]
},
Expand All @@ -893,7 +893,7 @@ tester.run('attributes-order', rule, {
errors: [
{
message: 'Attribute "v-on:click" should go before "v-text".',
type: 'VDirectiveKey'
type: 'VAttribute'
}
]
},
Expand Down

0 comments on commit 1b75e28

Please sign in to comment.