Skip to content

Format some files #1205

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 1 commit into from
Jun 9, 2020
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
11 changes: 0 additions & 11 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ module.exports = {
// Introduce prettier. but ignore files to avoid conflicts with PR.
{
files: [
// https://github.com/vuejs/eslint-plugin-vue/pull/1107
'lib/rules/order-in-components.js',
'tests/lib/rules/order-in-components.js',
// https://github.com/vuejs/eslint-plugin-vue/pull/1090
'lib/rules/require-direct-export.js',
'tests/lib/rules/require-direct-export.js',
'lib/utils/index.js',
'tests/lib/utils/vue-component.js',
// https://github.com/vuejs/eslint-plugin-vue/pull/982
'lib/rules/attributes-order.js',
'tests/lib/rules/attributes-order.js',
// https://github.com/vuejs/eslint-plugin-vue/pull/819
'lib/rules/attributes-order.js',
'tests/lib/rules/attributes-order.js'
Expand Down
74 changes: 43 additions & 31 deletions lib/rules/order-in-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,15 @@ const groups = {
'renderTriggered', // for Vue.js 3.x
'errorCaptured' // for Vue.js 2.5.0+
],
ROUTER_GUARDS: [
'beforeRouteEnter',
'beforeRouteUpdate',
'beforeRouteLeave'
]
ROUTER_GUARDS: ['beforeRouteEnter', 'beforeRouteUpdate', 'beforeRouteLeave']
}

function getOrderMap (order) {
function getOrderMap(order) {
const orderMap = new Map()

order.forEach((property, i) => {
if (Array.isArray(property)) {
property.forEach(p => orderMap.set(p, i))
property.forEach((p) => orderMap.set(p, i))
} else {
orderMap.set(property, i)
}
Expand All @@ -110,11 +106,11 @@ function getOrderMap (order) {
return orderMap
}

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

const ARITHMETIC_OPERATORS = ['+', '-', '*', '/', '%', '**'/* es2016 */]
const ARITHMETIC_OPERATORS = ['+', '-', '*', '/', '%', '**' /* es2016 */]
const BITWISE_OPERATORS = ['&', '|', '^', '~', '<<', '>>', '>>>']
const COMPARISON_OPERATORS = ['==', '!=', '===', '!==', '>', '>=', '<', '<=']
const RELATIONAL_OPERATORS = ['in', 'instanceof']
Expand All @@ -124,7 +120,7 @@ const ALL_BINARY_OPERATORS = [].concat(
COMPARISON_OPERATORS,
RELATIONAL_OPERATORS
)
const LOGICAL_OPERATORS = ['&&', '||', '??'/* es2020 */]
const LOGICAL_OPERATORS = ['&&', '||', '??' /* es2020 */]

/*
* Result `true` if the node is sure that there are no side effects
Expand All @@ -142,12 +138,12 @@ const LOGICAL_OPERATORS = ['&&', '||', '??'/* es2020 */]
* @param {Object} visitorKeys sourceCode.visitorKey
* @returns {Boolean} no side effects
*/
function isNotSideEffectsNode (node, visitorKeys) {
function isNotSideEffectsNode(node, visitorKeys) {
let result = true
let skipNode = false
traverseNodes(node, {
visitorKeys,
enterNode (node) {
enterNode(node) {
if (!result || skipNode) {
return
}
Expand All @@ -166,9 +162,12 @@ function isNotSideEffectsNode (node, visitorKeys) {
node.type !== 'Property' &&
node.type !== 'ObjectExpression' &&
node.type !== 'ArrayExpression' &&
(node.type !== 'UnaryExpression' || !['!', '~', '+', '-', 'typeof'].includes(node.operator)) &&
(node.type !== 'BinaryExpression' || !ALL_BINARY_OPERATORS.includes(node.operator)) &&
(node.type !== 'LogicalExpression' || !LOGICAL_OPERATORS.includes(node.operator)) &&
(node.type !== 'UnaryExpression' ||
!['!', '~', '+', '-', 'typeof'].includes(node.operator)) &&
(node.type !== 'BinaryExpression' ||
!ALL_BINARY_OPERATORS.includes(node.operator)) &&
(node.type !== 'LogicalExpression' ||
!LOGICAL_OPERATORS.includes(node.operator)) &&
node.type !== 'MemberExpression' &&
node.type !== 'ConditionalExpression' &&
// es2015
Expand All @@ -179,7 +178,7 @@ function isNotSideEffectsNode (node, visitorKeys) {
result = false
}
},
leaveNode (node) {
leaveNode(node) {
if (skipNode === node) {
skipNode = null
}
Expand Down Expand Up @@ -215,23 +214,25 @@ module.exports = {
]
},

create (context) {
create(context) {
const options = context.options[0] || {}
const order = options.order || defaultOrder
const extendedOrder = order.map(property => groups[property] || property)
const extendedOrder = order.map((property) => groups[property] || property)
const orderMap = getOrderMap(extendedOrder)
const sourceCode = context.getSourceCode()

function checkOrder (propertiesNodes, orderMap) {
function checkOrder(propertiesNodes, orderMap) {
const properties = propertiesNodes
.filter(property => property.type === 'Property')
.map(property => property.key)
.filter((property) => property.type === 'Property')
.map((property) => property.key)

properties.forEach((property, i) => {
const propertiesAbove = properties.slice(0, i)
const unorderedProperties = propertiesAbove
.filter(p => orderMap.get(p.name) > orderMap.get(property.name))
.sort((p1, p2) => orderMap.get(p1.name) > orderMap.get(p2.name) ? 1 : -1)
.filter((p) => orderMap.get(p.name) > orderMap.get(property.name))
.sort((p1, p2) =>
orderMap.get(p1.name) > orderMap.get(p2.name) ? 1 : -1
)

const firstUnorderedProperty = unorderedProperties[0]

Expand All @@ -245,15 +246,18 @@ module.exports = {
firstUnorderedPropertyName: firstUnorderedProperty.name,
line
},
fix (fixer) {
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))
.some(
(property) =>
!isNotSideEffectsNode(property, sourceCode.visitorKeys)
)
if (hasSideEffectsPossibility) {
return undefined
}
Expand All @@ -262,12 +266,20 @@ module.exports = {

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

const propertyCode = sourceCode.text.slice(codeStart, codeEnd) + (hasAfterComma ? '' : ',')
const insertTarget = sourceCode.getTokenBefore(firstUnorderedPropertyNode)

const removeStart = hasAfterComma ? codeStart : beforeComma.range[0]
const codeEnd = hasAfterComma
? afterComma.range[1]
: propertyNode.range[1]

const propertyCode =
sourceCode.text.slice(codeStart, codeEnd) +
(hasAfterComma ? '' : ',')
const insertTarget = sourceCode.getTokenBefore(
firstUnorderedPropertyNode
)

const removeStart = hasAfterComma
? codeStart
: beforeComma.range[0]

return [
fixer.removeRange([removeStart, codeEnd]),
Expand Down
99 changes: 57 additions & 42 deletions lib/rules/require-direct-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,50 @@ module.exports = {
categories: undefined,
url: 'https://eslint.vuejs.org/rules/require-direct-export.html'
},
fixable: null, // or "code" or "whitespace"
schema: [{
type: 'object',
properties: {
disallowFunctionalComponentFunction: { type: 'boolean' }
},
additionalProperties: false
}]
fixable: null, // or "code" or "whitespace"
schema: [
{
type: 'object',
properties: {
disallowFunctionalComponentFunction: { type: 'boolean' }
},
additionalProperties: false
}
]
},

create (context) {
create(context) {
const filePath = context.getFilename()
if (!utils.isVueFile(filePath)) return {}

const disallowFunctional = (context.options[0] || {}).disallowFunctionalComponentFunction
const disallowFunctional = (context.options[0] || {})
.disallowFunctionalComponentFunction

let maybeVue3Functional
let scopeStack = null

return {
/** @param {Declaration | Expression} node */
'ExportDefaultDeclaration > *' (node) {
'ExportDefaultDeclaration > *'(node) {
if (node.type === 'ObjectExpression') {
// OK
return
}
if (!disallowFunctional) {
if (node.type === 'ArrowFunctionExpression') {
if (node.body.type !== 'BlockStatement') {
// OK
// OK
return
}
maybeVue3Functional = {
body: node.body
}
return
}
if (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration') {
if (
node.type === 'FunctionExpression' ||
node.type === 'FunctionDeclaration'
) {
maybeVue3Functional = {
body: node.body
}
Expand All @@ -75,35 +81,44 @@ module.exports = {
message: `Expected the component literal to be directly exported.`
})
},
...(disallowFunctional ? {} : {
':function > BlockStatement' (node) {
if (!maybeVue3Functional) {
return
}
scopeStack = { upper: scopeStack, withinVue3FunctionalBody: maybeVue3Functional.body === node }
},
/** @param {ReturnStatement} node */
ReturnStatement (node) {
if (scopeStack && scopeStack.withinVue3FunctionalBody && node.argument) {
maybeVue3Functional.hasReturnArgument = true
}
},
':function > BlockStatement:exit' (node) {
scopeStack = scopeStack && scopeStack.upper
},
/** @param {ExportDefaultDeclaration} node */
'ExportDefaultDeclaration:exit' (node) {
if (!maybeVue3Functional) {
return
}
if (!maybeVue3Functional.hasReturnArgument) {
context.report({
node,
message: `Expected the component literal to be directly exported.`
})
}
}
})
...(disallowFunctional
? {}
: {
':function > BlockStatement'(node) {
if (!maybeVue3Functional) {
return
}
scopeStack = {
upper: scopeStack,
withinVue3FunctionalBody: maybeVue3Functional.body === node
}
},
/** @param {ReturnStatement} node */
ReturnStatement(node) {
if (
scopeStack &&
scopeStack.withinVue3FunctionalBody &&
node.argument
) {
maybeVue3Functional.hasReturnArgument = true
}
},
':function > BlockStatement:exit'(node) {
scopeStack = scopeStack && scopeStack.upper
},
/** @param {ExportDefaultDeclaration} node */
'ExportDefaultDeclaration:exit'(node) {
if (!maybeVue3Functional) {
return
}
if (!maybeVue3Functional.hasReturnArgument) {
context.report({
node,
message: `Expected the component literal to be directly exported.`
})
}
}
})
}
}
}
Loading