/
consistent-generic-constructors.ts
148 lines (143 loc) · 4.94 KB
/
consistent-generic-constructors.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
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { createRule, nullThrows, NullThrowsReasons } from '../util';
type MessageIds = 'preferConstructor' | 'preferTypeAnnotation';
type Options = ['constructor' | 'type-annotation'];
export default createRule<Options, MessageIds>({
name: 'consistent-generic-constructors',
meta: {
type: 'suggestion',
docs: {
description:
'Enforce specifying generic type arguments on type annotation or constructor name of a constructor call',
recommended: 'stylistic',
},
messages: {
preferTypeAnnotation:
'The generic type arguments should be specified as part of the type annotation.',
preferConstructor:
'The generic type arguments should be specified as part of the constructor type arguments.',
},
fixable: 'code',
schema: [
{
type: 'string',
enum: ['type-annotation', 'constructor'],
},
],
},
defaultOptions: ['constructor'],
create(context, [mode]) {
return {
'VariableDeclarator,PropertyDefinition,:matches(FunctionDeclaration,FunctionExpression) > AssignmentPattern'(
node:
| TSESTree.AssignmentPattern
| TSESTree.PropertyDefinition
| TSESTree.VariableDeclarator,
): void {
function getLHSRHS(): [
TSESTree.BindingName | TSESTree.PropertyDefinition,
TSESTree.Expression | null,
] {
switch (node.type) {
case AST_NODE_TYPES.VariableDeclarator:
return [node.id, node.init];
case AST_NODE_TYPES.PropertyDefinition:
return [node, node.value];
case AST_NODE_TYPES.AssignmentPattern:
return [node.left, node.right];
default:
throw new Error(
`Unhandled node type: ${(node as { type: string }).type}`,
);
}
}
const [lhsName, rhs] = getLHSRHS();
const lhs = lhsName.typeAnnotation?.typeAnnotation;
if (
!rhs ||
rhs.type !== AST_NODE_TYPES.NewExpression ||
rhs.callee.type !== AST_NODE_TYPES.Identifier
) {
return;
}
if (
lhs &&
(lhs.type !== AST_NODE_TYPES.TSTypeReference ||
lhs.typeName.type !== AST_NODE_TYPES.Identifier ||
lhs.typeName.name !== rhs.callee.name)
) {
return;
}
if (mode === 'type-annotation') {
if (!lhs && rhs.typeArguments) {
const { typeArguments, callee } = rhs;
const typeAnnotation =
context.sourceCode.getText(callee) +
context.sourceCode.getText(typeArguments);
context.report({
node,
messageId: 'preferTypeAnnotation',
fix(fixer) {
function getIDToAttachAnnotation():
| TSESTree.Node
| TSESTree.Token {
if (node.type !== AST_NODE_TYPES.PropertyDefinition) {
return lhsName;
}
if (!node.computed) {
return node.key;
}
// If the property's computed, we have to attach the
// annotation after the square bracket, not the enclosed expression
return nullThrows(
context.sourceCode.getTokenAfter(node.key),
NullThrowsReasons.MissingToken(']', 'key'),
);
}
return [
fixer.remove(typeArguments),
fixer.insertTextAfter(
getIDToAttachAnnotation(),
': ' + typeAnnotation,
),
];
},
});
}
return;
}
if (lhs?.typeArguments && !rhs.typeArguments) {
const hasParens =
context.sourceCode.getTokenAfter(rhs.callee)?.value === '(';
const extraComments = new Set(
context.sourceCode.getCommentsInside(lhs.parent),
);
context.sourceCode
.getCommentsInside(lhs.typeArguments)
.forEach(c => extraComments.delete(c));
context.report({
node,
messageId: 'preferConstructor',
*fix(fixer) {
yield fixer.remove(lhs.parent);
for (const comment of extraComments) {
yield fixer.insertTextAfter(
rhs.callee,
context.sourceCode.getText(comment),
);
}
yield fixer.insertTextAfter(
rhs.callee,
context.sourceCode.getText(lhs.typeArguments),
);
if (!hasParens) {
yield fixer.insertTextAfter(rhs.callee, '()');
}
},
});
}
},
};
},
});