/
consistent-generic-constructors.ts
87 lines (85 loc) · 2.54 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
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
import { createRule } from '../util';
type MessageIds = 'preferLHS' | 'preferRHS';
type Options = ['lhs' | 'rhs'];
export default createRule<Options, MessageIds>({
name: 'consistent-generic-constructors',
meta: {
type: 'suggestion',
docs: {
description:
'Enforce specifying generic type arguments on LHS or RHS of constructor call',
recommended: false,
},
messages: {
preferLHS:
'The generic type arguments should be specified on the left-hand side of the constructor call.',
preferRHS:
'The generic type arguments should be specified on the right-hand side of the constructor call.',
},
fixable: 'code',
schema: [
{
enum: ['lhs', 'rhs'],
},
],
},
defaultOptions: ['rhs'],
create(context, [mode]) {
return {
VariableDeclarator(node: TSESTree.VariableDeclarator): void {
const sourceCode = context.getSourceCode();
const lhs = node.id.typeAnnotation?.typeAnnotation;
const rhs = node.init;
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)
) {
return;
}
if (mode === 'lhs' && !lhs && rhs.typeParameters) {
context.report({
node,
messageId: 'preferLHS',
fix(fixer) {
const { typeParameters, callee } = rhs;
const typeAnnotation =
sourceCode.getText(callee) + sourceCode.getText(typeParameters);
return [
fixer.remove(typeParameters!),
fixer.insertTextAfter(node.id, ': ' + typeAnnotation),
];
},
});
} else if (
mode === 'rhs' &&
lhs?.typeParameters &&
!rhs.typeParameters &&
(lhs.typeName as TSESTree.Identifier).name === rhs.callee.name
) {
context.report({
node,
messageId: 'preferRHS',
fix(fixer) {
return [
fixer.remove(lhs.parent!),
fixer.insertTextAfter(
rhs.callee,
sourceCode.getText(lhs.typeParameters),
),
];
},
});
}
},
};
},
});