Skip to content

[Update] Make vue/order-in-components fixable #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 101 additions & 1 deletion lib/rules/order-in-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'use strict'

const utils = require('../utils')
const Traverser = require('eslint/lib/util/traverser')

const defaultOrder = [
'el',
Expand Down Expand Up @@ -56,6 +57,75 @@ function getOrderMap (order) {
return orderMap
}

function isComma (node) {
return node.type === 'Punctuator' && node.value === ','
}

const ARITHMETIC_OPERATORS = ['+', '-', '*', '/', '%', '**']
const BITWISE_OPERATORS = ['&', '|', '^', '~', '<<', '>>', '>>>']
const COMPARISON_OPERATORS = ['==', '!=', '===', '!==', '>', '>=', '<', '<=']
const RELATIONAL_OPERATORS = ['in', 'instanceof']
const ALL_BINARY_OPERATORS = [].concat(
ARITHMETIC_OPERATORS,
BITWISE_OPERATORS,
COMPARISON_OPERATORS,
RELATIONAL_OPERATORS
)
const LOGICAL_OPERATORS = ['&&', '||']

/*
* Result `true` if the node is sure that there are no side effects
*
* Currently known side effects types
*
* node.type === 'CallExpression'
* node.type === 'NewExpression'
* node.type === 'UpdateExpression'
* node.type === 'AssignmentExpression'
* node.type === 'TaggedTemplateExpression'
* node.type === 'UnaryExpression' && node.operator === 'delete'
*
* @param {ASTNode} node target node
* @param {Object} visitorKeys sourceCode.visitorKey
* @returns {Boolean} no side effects
*/
function isNotSideEffectsNode (node, visitorKeys) {
let result = true
new Traverser().traverse(node, {
visitorKeys,
enter (node, parent) {
if (
node.type === 'FunctionExpression' ||
node.type === 'Identifier' ||
node.type === 'Literal' ||
// es2015
node.type === 'ArrowFunctionExpression' ||
node.type === 'TemplateElement'
) {
// no side effects node
this.skip()
} else if (
node.type !== 'Property' &&
node.type !== 'ObjectExpression' &&
node.type !== 'ArrayExpression' &&
(node.type !== 'UnaryExpression' || ['!', '~', '+', '-', 'typeof'].indexOf(node.operator) < 0) &&
(node.type !== 'BinaryExpression' || ALL_BINARY_OPERATORS.indexOf(node.operator) < 0) &&
(node.type !== 'LogicalExpression' || LOGICAL_OPERATORS.indexOf(node.operator) < 0) &&
node.type !== 'MemberExpression' &&
node.type !== 'ConditionalExpression' &&
// es2015
node.type !== 'SpreadElement' &&
node.type !== 'TemplateLiteral'
) {
// Can not be sure that a node has no side effects
result = false
this.break()
}
}
})
return result
}

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
Expand All @@ -67,7 +137,7 @@ module.exports = {
category: 'recommended',
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.2.2/docs/rules/order-in-components.md'
},
fixable: null,
fixable: 'code', // null or "code" or "whitespace"
schema: [
{
type: 'object',
Expand All @@ -86,6 +156,7 @@ module.exports = {
const order = options.order || defaultOrder
const extendedOrder = order.map(property => groups[property] || property)
const orderMap = getOrderMap(extendedOrder)
const sourceCode = context.getSourceCode()

function checkOrder (propertiesNodes, orderMap) {
const properties = propertiesNodes
Expand All @@ -109,6 +180,35 @@ module.exports = {
name: property.name,
firstUnorderedPropertyName: firstUnorderedProperty.name,
line
},
fix (fixer) {
const propertyNode = property.parent
const firstUnorderedPropertyNode = firstUnorderedProperty.parent
const hasSideEffectsPossibility = propertiesNodes
.slice(
propertiesNodes.indexOf(firstUnorderedPropertyNode),
propertiesNodes.indexOf(propertyNode) + 1
)
.some((property) => !isNotSideEffectsNode(property, sourceCode.visitorKeys))
if (hasSideEffectsPossibility) {
return undefined
}
const comma = sourceCode.getTokenAfter(propertyNode)
const hasAfterComma = isComma(comma)

const codeStart = sourceCode.getTokenBefore(propertyNode).range[1] // to include comments
const codeEnd = hasAfterComma ? comma.range[1] : propertyNode.range[1]

const propertyCode = sourceCode.text.slice(codeStart, codeEnd) + (hasAfterComma ? '' : ',')
const insertTarget = sourceCode.getTokenBefore(firstUnorderedPropertyNode)
// If we can upgrade requirements to `eslint@>4.1.0`, this code can be replaced by:
// return [
// fixer.removeRange([codeStart, codeEnd]),
// fixer.insertTextAfter(insertTarget, propertyCode)
// ]
const insertStart = insertTarget.range[1]
const newCode = propertyCode + sourceCode.text.slice(insertStart, codeStart)
return fixer.replaceTextRange([insertStart, codeEnd], newCode)
}
})
}
Expand Down
Loading