forked from typescript-eslint/typescript-eslint
/
type-annotation-spacing.ts
198 lines (184 loc) · 5.3 KB
/
type-annotation-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
190
191
192
193
194
195
196
197
198
import { TSESTree } from '@typescript-eslint/experimental-utils';
import * as util from '../util';
type Options = [
{
before?: boolean;
after?: boolean;
overrides?: {
colon?: {
before?: boolean;
after?: boolean;
};
arrow?: {
before?: boolean;
after?: boolean;
};
};
}?,
];
type MessageIds =
| 'expectedSpaceAfter'
| 'expectedSpaceBefore'
| 'unexpectedSpaceAfter'
| 'unexpectedSpaceBefore';
const definition = {
type: 'object',
properties: {
before: { type: 'boolean' },
after: { type: 'boolean' },
},
additionalProperties: false,
};
export default util.createRule<Options, MessageIds>({
name: 'type-annotation-spacing',
meta: {
type: 'layout',
docs: {
description: 'Require consistent spacing around type annotations',
category: 'Stylistic Issues',
recommended: 'error',
},
fixable: 'whitespace',
messages: {
expectedSpaceAfter: "Expected a space after the '{{type}}'.",
expectedSpaceBefore: "Expected a space before the '{{type}}'.",
unexpectedSpaceAfter: "Unexpected space after the '{{type}}'.",
unexpectedSpaceBefore: "Unexpected space before the '{{type}}'.",
},
schema: [
{
type: 'object',
properties: {
before: { type: 'boolean' },
after: { type: 'boolean' },
overrides: {
type: 'object',
properties: {
colon: definition,
arrow: definition,
},
additionalProperties: false,
},
},
additionalProperties: false,
},
],
},
defaultOptions: [
// technically there is a default, but the overrides mean
// that if we apply them here, it will break the no override case.
{},
],
create(context, [options]) {
const punctuators = [':', '=>'];
const sourceCode = context.getSourceCode();
const overrides = options?.overrides ?? { colon: {}, arrow: {} };
const colonOptions = Object.assign(
{},
{ before: false, after: true },
options,
overrides.colon,
);
const arrowOptions = Object.assign(
{},
{ before: true, after: true },
options,
overrides.arrow,
);
/**
* Checks if there's proper spacing around type annotations (no space
* before colon, one space after).
*/
function checkTypeAnnotationSpacing(
typeAnnotation: TSESTree.TypeNode,
): void {
const nextToken = typeAnnotation;
const punctuatorTokenEnd = sourceCode.getTokenBefore(nextToken)!;
let punctuatorTokenStart = punctuatorTokenEnd;
let previousToken = sourceCode.getTokenBefore(punctuatorTokenEnd)!;
let type = punctuatorTokenEnd.value;
if (!punctuators.includes(type)) {
return;
}
const before = type === ':' ? colonOptions.before : arrowOptions.before;
const after = type === ':' ? colonOptions.after : arrowOptions.after;
if (type === ':' && previousToken.value === '?') {
// shift the start to the ?
type = '?:';
punctuatorTokenStart = previousToken;
previousToken = sourceCode.getTokenBefore(previousToken)!;
// handle the +/- modifiers for optional modification operators
if (previousToken.value === '+' || previousToken.value === '-') {
type = `${previousToken.value}?:`;
punctuatorTokenStart = previousToken;
previousToken = sourceCode.getTokenBefore(previousToken)!;
}
}
const previousDelta =
punctuatorTokenStart.range[0] - previousToken.range[1];
const nextDelta = nextToken.range[0] - punctuatorTokenEnd.range[1];
if (after && nextDelta === 0) {
context.report({
node: punctuatorTokenEnd,
messageId: 'expectedSpaceAfter',
data: {
type,
},
fix(fixer) {
return fixer.insertTextAfter(punctuatorTokenEnd, ' ');
},
});
} else if (!after && nextDelta > 0) {
context.report({
node: punctuatorTokenEnd,
messageId: 'unexpectedSpaceAfter',
data: {
type,
},
fix(fixer) {
return fixer.removeRange([
punctuatorTokenEnd.range[1],
nextToken.range[0],
]);
},
});
}
if (before && previousDelta === 0) {
context.report({
node: punctuatorTokenStart,
messageId: 'expectedSpaceBefore',
data: {
type,
},
fix(fixer) {
return fixer.insertTextAfter(previousToken, ' ');
},
});
} else if (!before && previousDelta > 0) {
context.report({
node: punctuatorTokenStart,
messageId: 'unexpectedSpaceBefore',
data: {
type,
},
fix(fixer) {
return fixer.removeRange([
previousToken.range[1],
punctuatorTokenStart.range[0],
]);
},
});
}
}
return {
TSMappedType(node): void {
if (node.typeAnnotation) {
checkTypeAnnotationSpacing(node.typeAnnotation);
}
},
TSTypeAnnotation(node): void {
checkTypeAnnotationSpacing(node.typeAnnotation);
},
};
},
});