forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
no-explicit-any.ts
209 lines (196 loc) · 6.89 KB
/
no-explicit-any.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
199
200
201
202
203
204
205
206
207
208
209
import { AST_NODE_TYPES, TSESLint, TSESTree } from '@typescript-eslint/utils';
import * as util from '../util';
export type Options = [
{
fixToUnknown?: boolean;
ignoreRestArgs?: boolean;
},
];
export type MessageIds = 'unexpectedAny' | 'suggestUnknown' | 'suggestNever';
export default util.createRule<Options, MessageIds>({
name: 'no-explicit-any',
meta: {
type: 'suggestion',
docs: {
description: 'Disallow the `any` type',
recommended: 'warn',
suggestion: true,
},
fixable: 'code',
hasSuggestions: true,
messages: {
unexpectedAny: 'Unexpected any. Specify a different type.',
suggestUnknown:
'Use `unknown` instead, this will force you to explicitly, and safely assert the type is correct.',
suggestNever:
"Use `never` instead, this is useful when instantiating generic type parameters that you don't need to know the type of.",
},
schema: [
{
type: 'object',
additionalProperties: false,
properties: {
fixToUnknown: {
type: 'boolean',
},
ignoreRestArgs: {
type: 'boolean',
},
},
},
],
},
defaultOptions: [
{
fixToUnknown: false,
ignoreRestArgs: false,
},
],
create(context, [{ ignoreRestArgs, fixToUnknown }]) {
/**
* Checks if the node is an arrow function, function/constructor declaration or function expression
* @param node the node to be validated.
* @returns true if the node is any kind of function declaration or expression
* @private
*/
function isNodeValidFunction(node: TSESTree.Node): boolean {
return [
AST_NODE_TYPES.ArrowFunctionExpression, // const x = (...args: any[]) => {};
AST_NODE_TYPES.FunctionDeclaration, // function f(...args: any[]) {}
AST_NODE_TYPES.FunctionExpression, // const x = function(...args: any[]) {};
AST_NODE_TYPES.TSEmptyBodyFunctionExpression, // declare class A { f(...args: any[]): unknown; }
AST_NODE_TYPES.TSFunctionType, // type T = (...args: any[]) => unknown;
AST_NODE_TYPES.TSConstructorType, // type T = new (...args: any[]) => unknown
AST_NODE_TYPES.TSCallSignatureDeclaration, // type T = {(...args: any[]): unknown};
AST_NODE_TYPES.TSConstructSignatureDeclaration, // type T = {new (...args: any[]): unknown};
AST_NODE_TYPES.TSMethodSignature, // type T = {f(...args: any[]): unknown};
AST_NODE_TYPES.TSDeclareFunction, // declare function _8(...args: any[]): unknown;
].includes(node.type);
}
/**
* Checks if the node is a rest element child node of a function
* @param node the node to be validated.
* @returns true if the node is a rest element child node of a function
* @private
*/
function isNodeRestElementInFunction(node: TSESTree.Node): boolean {
return (
node.type === AST_NODE_TYPES.RestElement &&
isNodeValidFunction(node.parent)
);
}
/**
* Checks if the node is a TSTypeOperator node with a readonly operator
* @param node the node to be validated.
* @returns true if the node is a TSTypeOperator node with a readonly operator
* @private
*/
function isNodeReadonlyTSTypeOperator(node: TSESTree.Node): boolean {
return (
node.type === AST_NODE_TYPES.TSTypeOperator &&
node.operator === 'readonly'
);
}
/**
* Checks if the node is a TSTypeReference node with an Array identifier
* @param node the node to be validated.
* @returns true if the node is a TSTypeReference node with an Array identifier
* @private
*/
function isNodeValidArrayTSTypeReference(node: TSESTree.Node): boolean {
return (
node.type === AST_NODE_TYPES.TSTypeReference &&
typeof node.typeName !== 'undefined' &&
node.typeName.type === AST_NODE_TYPES.Identifier &&
['Array', 'ReadonlyArray'].includes(node.typeName.name)
);
}
/**
* Checks if the node is a valid TSTypeOperator or TSTypeReference node
* @param node the node to be validated.
* @returns true if the node is a valid TSTypeOperator or TSTypeReference node
* @private
*/
function isNodeValidTSType(node: TSESTree.Node): boolean {
return (
isNodeReadonlyTSTypeOperator(node) ||
isNodeValidArrayTSTypeReference(node)
);
}
/**
* Checks if the great grand-parent node is a RestElement node in a function
* @param node the node to be validated.
* @returns true if the great grand-parent node is a RestElement node in a function
* @private
*/
function isGreatGrandparentRestElement(node: TSESTree.Node): boolean {
return (
node?.parent?.parent?.parent != null &&
isNodeRestElementInFunction(node.parent.parent.parent)
);
}
/**
* Checks if the great great grand-parent node is a valid RestElement node in a function
* @param node the node to be validated.
* @returns true if the great great grand-parent node is a valid RestElement node in a function
* @private
*/
function isGreatGreatGrandparentRestElement(node: TSESTree.Node): boolean {
return (
node.parent?.parent?.parent?.parent != null &&
isNodeValidTSType(node.parent.parent) &&
isNodeRestElementInFunction(node.parent.parent.parent.parent)
);
}
/**
* Checks if the great grand-parent or the great great grand-parent node is a RestElement node
* @param node the node to be validated.
* @returns true if the great grand-parent or the great great grand-parent node is a RestElement node
* @private
*/
function isNodeDescendantOfRestElementInFunction(
node: TSESTree.Node,
): boolean {
return (
isGreatGrandparentRestElement(node) ||
isGreatGreatGrandparentRestElement(node)
);
}
return {
TSAnyKeyword(node): void {
if (ignoreRestArgs && isNodeDescendantOfRestElementInFunction(node)) {
return;
}
const fixOrSuggest: {
fix: TSESLint.ReportFixFunction | null;
suggest: TSESLint.ReportSuggestionArray<MessageIds> | null;
} = {
fix: null,
suggest: [
{
messageId: 'suggestUnknown',
fix(fixer): TSESLint.RuleFix {
return fixer.replaceText(node, 'unknown');
},
},
{
messageId: 'suggestNever',
fix(fixer): TSESLint.RuleFix {
return fixer.replaceText(node, 'never');
},
},
],
};
if (fixToUnknown) {
fixOrSuggest.fix = (fixer): TSESLint.RuleFix =>
fixer.replaceText(node, 'unknown');
}
context.report({
node,
messageId: 'unexpectedAny',
...fixOrSuggest,
});
},
};
},
});