-
Notifications
You must be signed in to change notification settings - Fork 0
/
empty-array.js
84 lines (67 loc) · 1.69 KB
/
empty-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module.exports = {
meta: {
type: 'layout',
docs: {
description: 'Enforce a single space between empty array brackets',
category: 'Stylistic Issues'
},
fixable: 'whitespace',
schema: [
{ enum: [ 'always', 'never' ] }
],
messages: {
singleSpace: 'Brackets for empty arrays should be separated by a single space.',
singleLine: 'Brackets for empty arrays should appear on the same line.'
}
},
create(context) {
const sourceCode = context.getSourceCode()
function reportInvalidLines(node, token) {
context.report({
node,
loc: token.loc.start,
messageId: 'singleLine',
data: {
tokenValue: token.value
},
fix(fixer) {
const nextToken = sourceCode.getTokenAfter(token)
return fixer.removeRange([ token.range[1], nextToken.range[0] ])
}
})
}
function reportInvalidSpacing(node, token) {
context.report({
node,
loc: token.loc.start,
messageId: 'singleSpace',
data: {
tokenValue: token.value
},
fix(fixer) {
const previousToken = sourceCode.getTokenBefore(token)
return fixer.removeRange([ previousToken.range[1], token.range[0] ])
}
})
}
function validateArraySpacing(node) {
if(node.elements.length !== 0) {
return
}
const first = sourceCode.getFirstToken(node)
const last = node.typeAnnotation
? sourceCode.getTokenBefore(node.typeAnnotation)
: sourceCode.getLastToken(node)
if(first.loc.start.line !== last.loc.end.line) {
reportInvalidLines(node, first)
}
if(first.end + 1 !== last.start) {
reportInvalidSpacing(node, last)
}
}
return {
ArrayPattern: validateArraySpacing,
ArrayExpression: validateArraySpacing
}
}
}