/
comma-spacing.ts
189 lines (176 loc) · 5.09 KB
/
comma-spacing.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import {
TSESTree,
AST_TOKEN_TYPES,
} from '@typescript-eslint/experimental-utils';
import {
isClosingParenToken,
isCommaToken,
isTokenOnSameLine,
createRule,
} from '../util';
type Options = [
{
before: boolean;
after: boolean;
},
];
type MessageIds = 'unexpected' | 'missing';
export default createRule<Options, MessageIds>({
name: 'comma-spacing',
meta: {
type: 'suggestion',
docs: {
description: 'Enforces consistent spacing before and after commas',
category: 'Stylistic Issues',
recommended: false,
extendsBaseRule: true,
},
fixable: 'whitespace',
schema: [
{
type: 'object',
properties: {
before: {
type: 'boolean',
default: false,
},
after: {
type: 'boolean',
default: true,
},
},
additionalProperties: false,
},
],
messages: {
unexpected: `There should be no space {{loc}} ','.`,
missing: `A space is required {{loc}} ','.`,
},
},
defaultOptions: [
{
before: false,
after: true,
},
],
create(context, [{ before: spaceBefore, after: spaceAfter }]) {
const sourceCode = context.getSourceCode();
const tokensAndComments = sourceCode.tokensAndComments;
const ignoredTokens = new Set<TSESTree.PunctuatorToken>();
/**
* Adds null elements of the ArrayExpression or ArrayPattern node to the ignore list
* @param node node to evaluate
*/
function addNullElementsToIgnoreList(
node: TSESTree.ArrayExpression | TSESTree.ArrayPattern,
): void {
let previousToken = sourceCode.getFirstToken(node);
for (const element of node.elements) {
let token: TSESTree.Token | null;
if (element === null) {
token = sourceCode.getTokenAfter(previousToken!);
if (token && isCommaToken(token)) {
ignoredTokens.add(token);
}
} else {
token = sourceCode.getTokenAfter(element);
}
previousToken = token;
}
}
/**
* Adds type parameters trailing comma token to the ignore list
* @param node node to evaluate
*/
function addTypeParametersTrailingCommaToIgnoreList(
node: TSESTree.TSTypeParameterDeclaration,
): void {
const param = node.params[node.params.length - 1];
const afterToken = sourceCode.getTokenAfter(param);
if (afterToken && isCommaToken(afterToken)) {
ignoredTokens.add(afterToken);
}
}
/**
* Validates the spacing around a comma token.
* @param commaToken The token representing the comma
* @param prevToken The last token before the comma
* @param nextToken The first token after the comma
*/
function validateCommaSpacing(
commaToken: TSESTree.PunctuatorToken,
prevToken: TSESTree.Token | TSESTree.Comment | null,
nextToken: TSESTree.Token | TSESTree.Comment | null,
): void {
if (
prevToken &&
isTokenOnSameLine(prevToken, commaToken) &&
spaceBefore !== sourceCode.isSpaceBetweenTokens(prevToken, commaToken)
) {
context.report({
node: commaToken,
data: {
loc: 'before',
},
messageId: spaceBefore ? 'missing' : 'unexpected',
fix: fixer =>
spaceBefore
? fixer.insertTextBefore(commaToken, ' ')
: fixer.replaceTextRange(
[prevToken.range[1], commaToken.range[0]],
'',
),
});
}
if (nextToken && isClosingParenToken(nextToken)) {
return;
}
if (!spaceAfter && nextToken && nextToken.type === AST_TOKEN_TYPES.Line) {
return;
}
if (
nextToken &&
isTokenOnSameLine(commaToken, nextToken) &&
spaceAfter !== sourceCode.isSpaceBetweenTokens(commaToken, nextToken)
) {
context.report({
node: commaToken,
data: {
loc: 'after',
},
messageId: spaceAfter ? 'missing' : 'unexpected',
fix: fixer =>
spaceAfter
? fixer.insertTextAfter(commaToken, ' ')
: fixer.replaceTextRange(
[commaToken.range[1], nextToken.range[0]],
'',
),
});
}
}
return {
TSTypeParameterDeclaration: addTypeParametersTrailingCommaToIgnoreList,
ArrayExpression: addNullElementsToIgnoreList,
ArrayPattern: addNullElementsToIgnoreList,
'Program:exit'(): void {
tokensAndComments.forEach((token, i) => {
if (!isCommaToken(token)) {
return;
}
const prevToken = tokensAndComments[i - 1];
const nextToken = tokensAndComments[i + 1];
validateCommaSpacing(
token,
isCommaToken(prevToken) || ignoredTokens.has(token)
? null
: prevToken,
isCommaToken(nextToken) || ignoredTokens.has(token)
? null
: nextToken,
);
});
},
};
},
});