-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
comma-dangle.ts
179 lines (162 loc) · 4.98 KB
/
comma-dangle.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
import * as util from '../util';
import { getESLintCoreRule } from '../util/getESLintCoreRule';
import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/utils';
const baseRule = getESLintCoreRule('comma-dangle');
export type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
export type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;
type Option = Options[0];
type NormalizedOptions = Required<
Pick<Exclude<Option, string>, 'enums' | 'generics' | 'tuples'>
>;
const OPTION_VALUE_SCHEME = [
'always-multiline',
'always',
'never',
'only-multiline',
];
const DEFAULT_OPTION_VALUE = 'never';
function normalizeOptions(options: Option): NormalizedOptions {
if (typeof options === 'string') {
return {
enums: options,
generics: options,
tuples: options,
};
}
return {
enums: options.enums ?? DEFAULT_OPTION_VALUE,
generics: options.generics ?? DEFAULT_OPTION_VALUE,
tuples: options.tuples ?? DEFAULT_OPTION_VALUE,
};
}
export default util.createRule<Options, MessageIds>({
name: 'comma-dangle',
meta: {
type: 'layout',
docs: {
description: 'Require or disallow trailing commas',
recommended: false,
extendsBaseRule: true,
},
schema: {
$defs: {
value: {
enum: OPTION_VALUE_SCHEME,
},
valueWithIgnore: {
enum: [...OPTION_VALUE_SCHEME, 'ignore'],
},
},
type: 'array',
items: [
{
oneOf: [
{
$ref: '#/$defs/value',
},
{
type: 'object',
properties: {
arrays: { $ref: '#/$defs/valueWithIgnore' },
objects: { $ref: '#/$defs/valueWithIgnore' },
imports: { $ref: '#/$defs/valueWithIgnore' },
exports: { $ref: '#/$defs/valueWithIgnore' },
functions: { $ref: '#/$defs/valueWithIgnore' },
enums: { $ref: '#/$defs/valueWithIgnore' },
generics: { $ref: '#/$defs/valueWithIgnore' },
tuples: { $ref: '#/$defs/valueWithIgnore' },
},
additionalProperties: false,
},
],
},
],
additionalProperties: false,
},
fixable: 'code',
hasSuggestions: baseRule.meta.hasSuggestions,
messages: baseRule.meta.messages,
},
defaultOptions: ['never'],
create(context, [options]) {
const rules = baseRule.create(context);
const sourceCode = context.getSourceCode();
const normalizedOptions = normalizeOptions(options);
const predicate = {
always: forceComma,
'always-multiline': forceCommaIfMultiline,
'only-multiline': allowCommaIfMultiline,
never: forbidComma,
ignore: (): void => {},
};
function last(nodes: TSESTree.Node[]): TSESTree.Node | null {
return nodes[nodes.length - 1] ?? null;
}
function getLastItem(node: TSESTree.Node): TSESTree.Node | null {
switch (node.type) {
case AST_NODE_TYPES.TSEnumDeclaration:
return last(node.members);
case AST_NODE_TYPES.TSTypeParameterDeclaration:
return last(node.params);
case AST_NODE_TYPES.TSTupleType:
return last(node.elementTypes);
default:
return null;
}
}
function getTrailingToken(node: TSESTree.Node): TSESTree.Token | null {
const last = getLastItem(node);
const trailing = last && sourceCode.getTokenAfter(last);
return trailing;
}
function isMultiline(node: TSESTree.Node): boolean {
const last = getLastItem(node);
const lastToken = sourceCode.getLastToken(node);
return last?.loc.end.line !== lastToken?.loc.end.line;
}
function forbidComma(node: TSESTree.Node): void {
const last = getLastItem(node);
const trailing = getTrailingToken(node);
if (last && trailing && util.isCommaToken(trailing)) {
context.report({
node,
messageId: 'unexpected',
fix(fixer) {
return fixer.remove(trailing);
},
});
}
}
function forceComma(node: TSESTree.Node): void {
const last = getLastItem(node);
const trailing = getTrailingToken(node);
if (last && trailing && !util.isCommaToken(trailing)) {
context.report({
node,
messageId: 'missing',
fix(fixer) {
return fixer.insertTextAfter(last, ',');
},
});
}
}
function allowCommaIfMultiline(node: TSESTree.Node): void {
if (!isMultiline(node)) {
forbidComma(node);
}
}
function forceCommaIfMultiline(node: TSESTree.Node): void {
if (isMultiline(node)) {
forceComma(node);
} else {
forbidComma(node);
}
}
return {
...rules,
TSEnumDeclaration: predicate[normalizedOptions.enums],
TSTypeParameterDeclaration: predicate[normalizedOptions.generics],
TSTupleType: predicate[normalizedOptions.tuples],
};
},
});