Skip to content

Commit 6def98a

Browse files
authored
Format some files (vuejs#1205)
Format the files for which the PR has been finished.
1 parent 1823583 commit 6def98a

File tree

7 files changed

+701
-441
lines changed

7 files changed

+701
-441
lines changed

.eslintrc.js

-11
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ module.exports = {
4040
// Introduce prettier. but ignore files to avoid conflicts with PR.
4141
{
4242
files: [
43-
// https://github.com/vuejs/eslint-plugin-vue/pull/1107
44-
'lib/rules/order-in-components.js',
45-
'tests/lib/rules/order-in-components.js',
46-
// https://github.com/vuejs/eslint-plugin-vue/pull/1090
47-
'lib/rules/require-direct-export.js',
48-
'tests/lib/rules/require-direct-export.js',
49-
'lib/utils/index.js',
50-
'tests/lib/utils/vue-component.js',
51-
// https://github.com/vuejs/eslint-plugin-vue/pull/982
52-
'lib/rules/attributes-order.js',
53-
'tests/lib/rules/attributes-order.js',
5443
// https://github.com/vuejs/eslint-plugin-vue/pull/819
5544
'lib/rules/attributes-order.js',
5645
'tests/lib/rules/attributes-order.js'

lib/rules/order-in-components.js

+43-31
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,15 @@ const groups = {
8989
'renderTriggered', // for Vue.js 3.x
9090
'errorCaptured' // for Vue.js 2.5.0+
9191
],
92-
ROUTER_GUARDS: [
93-
'beforeRouteEnter',
94-
'beforeRouteUpdate',
95-
'beforeRouteLeave'
96-
]
92+
ROUTER_GUARDS: ['beforeRouteEnter', 'beforeRouteUpdate', 'beforeRouteLeave']
9793
}
9894

99-
function getOrderMap (order) {
95+
function getOrderMap(order) {
10096
const orderMap = new Map()
10197

10298
order.forEach((property, i) => {
10399
if (Array.isArray(property)) {
104-
property.forEach(p => orderMap.set(p, i))
100+
property.forEach((p) => orderMap.set(p, i))
105101
} else {
106102
orderMap.set(property, i)
107103
}
@@ -110,11 +106,11 @@ function getOrderMap (order) {
110106
return orderMap
111107
}
112108

113-
function isComma (node) {
109+
function isComma(node) {
114110
return node.type === 'Punctuator' && node.value === ','
115111
}
116112

117-
const ARITHMETIC_OPERATORS = ['+', '-', '*', '/', '%', '**'/* es2016 */]
113+
const ARITHMETIC_OPERATORS = ['+', '-', '*', '/', '%', '**' /* es2016 */]
118114
const BITWISE_OPERATORS = ['&', '|', '^', '~', '<<', '>>', '>>>']
119115
const COMPARISON_OPERATORS = ['==', '!=', '===', '!==', '>', '>=', '<', '<=']
120116
const RELATIONAL_OPERATORS = ['in', 'instanceof']
@@ -124,7 +120,7 @@ const ALL_BINARY_OPERATORS = [].concat(
124120
COMPARISON_OPERATORS,
125121
RELATIONAL_OPERATORS
126122
)
127-
const LOGICAL_OPERATORS = ['&&', '||', '??'/* es2020 */]
123+
const LOGICAL_OPERATORS = ['&&', '||', '??' /* es2020 */]
128124

129125
/*
130126
* Result `true` if the node is sure that there are no side effects
@@ -142,12 +138,12 @@ const LOGICAL_OPERATORS = ['&&', '||', '??'/* es2020 */]
142138
* @param {Object} visitorKeys sourceCode.visitorKey
143139
* @returns {Boolean} no side effects
144140
*/
145-
function isNotSideEffectsNode (node, visitorKeys) {
141+
function isNotSideEffectsNode(node, visitorKeys) {
146142
let result = true
147143
let skipNode = false
148144
traverseNodes(node, {
149145
visitorKeys,
150-
enterNode (node) {
146+
enterNode(node) {
151147
if (!result || skipNode) {
152148
return
153149
}
@@ -166,9 +162,12 @@ function isNotSideEffectsNode (node, visitorKeys) {
166162
node.type !== 'Property' &&
167163
node.type !== 'ObjectExpression' &&
168164
node.type !== 'ArrayExpression' &&
169-
(node.type !== 'UnaryExpression' || !['!', '~', '+', '-', 'typeof'].includes(node.operator)) &&
170-
(node.type !== 'BinaryExpression' || !ALL_BINARY_OPERATORS.includes(node.operator)) &&
171-
(node.type !== 'LogicalExpression' || !LOGICAL_OPERATORS.includes(node.operator)) &&
165+
(node.type !== 'UnaryExpression' ||
166+
!['!', '~', '+', '-', 'typeof'].includes(node.operator)) &&
167+
(node.type !== 'BinaryExpression' ||
168+
!ALL_BINARY_OPERATORS.includes(node.operator)) &&
169+
(node.type !== 'LogicalExpression' ||
170+
!LOGICAL_OPERATORS.includes(node.operator)) &&
172171
node.type !== 'MemberExpression' &&
173172
node.type !== 'ConditionalExpression' &&
174173
// es2015
@@ -179,7 +178,7 @@ function isNotSideEffectsNode (node, visitorKeys) {
179178
result = false
180179
}
181180
},
182-
leaveNode (node) {
181+
leaveNode(node) {
183182
if (skipNode === node) {
184183
skipNode = null
185184
}
@@ -215,23 +214,25 @@ module.exports = {
215214
]
216215
},
217216

218-
create (context) {
217+
create(context) {
219218
const options = context.options[0] || {}
220219
const order = options.order || defaultOrder
221-
const extendedOrder = order.map(property => groups[property] || property)
220+
const extendedOrder = order.map((property) => groups[property] || property)
222221
const orderMap = getOrderMap(extendedOrder)
223222
const sourceCode = context.getSourceCode()
224223

225-
function checkOrder (propertiesNodes, orderMap) {
224+
function checkOrder(propertiesNodes, orderMap) {
226225
const properties = propertiesNodes
227-
.filter(property => property.type === 'Property')
228-
.map(property => property.key)
226+
.filter((property) => property.type === 'Property')
227+
.map((property) => property.key)
229228

230229
properties.forEach((property, i) => {
231230
const propertiesAbove = properties.slice(0, i)
232231
const unorderedProperties = propertiesAbove
233-
.filter(p => orderMap.get(p.name) > orderMap.get(property.name))
234-
.sort((p1, p2) => orderMap.get(p1.name) > orderMap.get(p2.name) ? 1 : -1)
232+
.filter((p) => orderMap.get(p.name) > orderMap.get(property.name))
233+
.sort((p1, p2) =>
234+
orderMap.get(p1.name) > orderMap.get(p2.name) ? 1 : -1
235+
)
235236

236237
const firstUnorderedProperty = unorderedProperties[0]
237238

@@ -245,15 +246,18 @@ module.exports = {
245246
firstUnorderedPropertyName: firstUnorderedProperty.name,
246247
line
247248
},
248-
fix (fixer) {
249+
fix(fixer) {
249250
const propertyNode = property.parent
250251
const firstUnorderedPropertyNode = firstUnorderedProperty.parent
251252
const hasSideEffectsPossibility = propertiesNodes
252253
.slice(
253254
propertiesNodes.indexOf(firstUnorderedPropertyNode),
254255
propertiesNodes.indexOf(propertyNode) + 1
255256
)
256-
.some((property) => !isNotSideEffectsNode(property, sourceCode.visitorKeys))
257+
.some(
258+
(property) =>
259+
!isNotSideEffectsNode(property, sourceCode.visitorKeys)
260+
)
257261
if (hasSideEffectsPossibility) {
258262
return undefined
259263
}
@@ -262,12 +266,20 @@ module.exports = {
262266

263267
const beforeComma = sourceCode.getTokenBefore(propertyNode)
264268
const codeStart = beforeComma.range[1] // to include comments
265-
const codeEnd = hasAfterComma ? afterComma.range[1] : propertyNode.range[1]
266-
267-
const propertyCode = sourceCode.text.slice(codeStart, codeEnd) + (hasAfterComma ? '' : ',')
268-
const insertTarget = sourceCode.getTokenBefore(firstUnorderedPropertyNode)
269-
270-
const removeStart = hasAfterComma ? codeStart : beforeComma.range[0]
269+
const codeEnd = hasAfterComma
270+
? afterComma.range[1]
271+
: propertyNode.range[1]
272+
273+
const propertyCode =
274+
sourceCode.text.slice(codeStart, codeEnd) +
275+
(hasAfterComma ? '' : ',')
276+
const insertTarget = sourceCode.getTokenBefore(
277+
firstUnorderedPropertyNode
278+
)
279+
280+
const removeStart = hasAfterComma
281+
? codeStart
282+
: beforeComma.range[0]
271283

272284
return [
273285
fixer.removeRange([removeStart, codeEnd]),

lib/rules/require-direct-export.js

+57-42
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,50 @@ module.exports = {
2525
categories: undefined,
2626
url: 'https://eslint.vuejs.org/rules/require-direct-export.html'
2727
},
28-
fixable: null, // or "code" or "whitespace"
29-
schema: [{
30-
type: 'object',
31-
properties: {
32-
disallowFunctionalComponentFunction: { type: 'boolean' }
33-
},
34-
additionalProperties: false
35-
}]
28+
fixable: null, // or "code" or "whitespace"
29+
schema: [
30+
{
31+
type: 'object',
32+
properties: {
33+
disallowFunctionalComponentFunction: { type: 'boolean' }
34+
},
35+
additionalProperties: false
36+
}
37+
]
3638
},
3739

38-
create (context) {
40+
create(context) {
3941
const filePath = context.getFilename()
4042
if (!utils.isVueFile(filePath)) return {}
4143

42-
const disallowFunctional = (context.options[0] || {}).disallowFunctionalComponentFunction
44+
const disallowFunctional = (context.options[0] || {})
45+
.disallowFunctionalComponentFunction
4346

4447
let maybeVue3Functional
4548
let scopeStack = null
4649

4750
return {
4851
/** @param {Declaration | Expression} node */
49-
'ExportDefaultDeclaration > *' (node) {
52+
'ExportDefaultDeclaration > *'(node) {
5053
if (node.type === 'ObjectExpression') {
5154
// OK
5255
return
5356
}
5457
if (!disallowFunctional) {
5558
if (node.type === 'ArrowFunctionExpression') {
5659
if (node.body.type !== 'BlockStatement') {
57-
// OK
60+
// OK
5861
return
5962
}
6063
maybeVue3Functional = {
6164
body: node.body
6265
}
6366
return
6467
}
65-
if (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration') {
68+
if (
69+
node.type === 'FunctionExpression' ||
70+
node.type === 'FunctionDeclaration'
71+
) {
6672
maybeVue3Functional = {
6773
body: node.body
6874
}
@@ -75,35 +81,44 @@ module.exports = {
7581
message: `Expected the component literal to be directly exported.`
7682
})
7783
},
78-
...(disallowFunctional ? {} : {
79-
':function > BlockStatement' (node) {
80-
if (!maybeVue3Functional) {
81-
return
82-
}
83-
scopeStack = { upper: scopeStack, withinVue3FunctionalBody: maybeVue3Functional.body === node }
84-
},
85-
/** @param {ReturnStatement} node */
86-
ReturnStatement (node) {
87-
if (scopeStack && scopeStack.withinVue3FunctionalBody && node.argument) {
88-
maybeVue3Functional.hasReturnArgument = true
89-
}
90-
},
91-
':function > BlockStatement:exit' (node) {
92-
scopeStack = scopeStack && scopeStack.upper
93-
},
94-
/** @param {ExportDefaultDeclaration} node */
95-
'ExportDefaultDeclaration:exit' (node) {
96-
if (!maybeVue3Functional) {
97-
return
98-
}
99-
if (!maybeVue3Functional.hasReturnArgument) {
100-
context.report({
101-
node,
102-
message: `Expected the component literal to be directly exported.`
103-
})
104-
}
105-
}
106-
})
84+
...(disallowFunctional
85+
? {}
86+
: {
87+
':function > BlockStatement'(node) {
88+
if (!maybeVue3Functional) {
89+
return
90+
}
91+
scopeStack = {
92+
upper: scopeStack,
93+
withinVue3FunctionalBody: maybeVue3Functional.body === node
94+
}
95+
},
96+
/** @param {ReturnStatement} node */
97+
ReturnStatement(node) {
98+
if (
99+
scopeStack &&
100+
scopeStack.withinVue3FunctionalBody &&
101+
node.argument
102+
) {
103+
maybeVue3Functional.hasReturnArgument = true
104+
}
105+
},
106+
':function > BlockStatement:exit'(node) {
107+
scopeStack = scopeStack && scopeStack.upper
108+
},
109+
/** @param {ExportDefaultDeclaration} node */
110+
'ExportDefaultDeclaration:exit'(node) {
111+
if (!maybeVue3Functional) {
112+
return
113+
}
114+
if (!maybeVue3Functional.hasReturnArgument) {
115+
context.report({
116+
node,
117+
message: `Expected the component literal to be directly exported.`
118+
})
119+
}
120+
}
121+
})
107122
}
108123
}
109124
}

0 commit comments

Comments
 (0)