forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-inferrable-types.js
191 lines (169 loc) · 6.14 KB
/
no-inferrable-types.js
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
/**
* @fileoverview Disallows explicit type declarations for inferrable types
* @author James Garbutt <https://github.com/43081j>
*/
"use strict";
const util = require("../util");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description:
"Disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean.",
extraDescription: [util.tslintRule("no-inferrable-types")],
category: "TypeScript",
url:
"https://github.com/nzakas/eslint-plugin-typescript/blob/master/docs/rules/no-inferrable-types.md"
},
fixable: "code",
schema: [
{
type: "object",
properties: {
ignoreParameters: {
type: "boolean"
},
ignoreProperties: {
type: "boolean"
}
},
additionalProperties: false
}
]
},
create(context) {
const ignoreParameters = context.options[0]
? context.options[0].ignoreParameters
: false;
const ignoreProperties = context.options[0]
? context.options[0].ignoreProperties
: false;
/**
* Returns whether a node has an inferrable value or not
* @param {ASTNode} node the node to check
* @param {ASTNode} init the initializer
* @returns {boolean} whether the node has an inferrable type
*/
function isInferrable(node, init) {
if (node.type !== "TSTypeAnnotation" || !node.typeAnnotation) {
return false;
}
if (!init) {
return false;
}
const annotation = node.typeAnnotation;
if (annotation.type === "TSStringKeyword") {
return (
(init.type === "Literal" &&
typeof init.value === "string") ||
(init.type === "TemplateElement" &&
(!init.expressions || init.expressions.length === 0))
);
}
if (annotation.type === "TSBooleanKeyword") {
return init.type === "Literal";
}
if (annotation.type === "TSNumberKeyword") {
// Infinity is special
if (
(init.type === "UnaryExpression" &&
init.operator === "-" &&
init.argument.type === "Identifier" &&
init.argument.name === "Infinity") ||
(init.type === "Identifier" && init.name === "Infinity")
) {
return true;
}
return (
init.type === "Literal" && typeof init.value === "number"
);
}
return false;
}
/**
* Reports an inferrable type declaration, if any
* @param {ASTNode} node the node being visited
* @param {ASTNode} typeNode the type annotation node
* @param {ASTNode} initNode the initializer node
* @returns {void}
*/
function reportInferrableType(node, typeNode, initNode) {
if (!typeNode || !initNode || !typeNode.typeAnnotation) {
return;
}
if (!isInferrable(typeNode, initNode)) {
return;
}
const typeMap = {
TSBooleanKeyword: "boolean",
TSNumberKeyword: "number",
TSStringKeyword: "string"
};
const type = typeMap[typeNode.typeAnnotation.type];
context.report({
node,
message: `Type ${type} trivially inferred from a ${type} literal, remove type annotation`,
fix: fixer => fixer.remove(typeNode)
});
}
/**
* Visits variables
* @param {ASTNode} node the node to be visited
* @returns {void}
*/
function inferrableVariableVisitor(node) {
if (!node.id) {
return;
}
reportInferrableType(node, node.id.typeAnnotation, node.init);
}
/**
* Visits parameters
* @param {ASTNode} node the node to be visited
* @returns {void}
*/
function inferrableParameterVisitor(node) {
if (ignoreParameters || !node.params) {
return;
}
node.params
.filter(
param =>
param.type === "AssignmentPattern" &&
param.left &&
param.right
)
.forEach(param => {
reportInferrableType(
param,
param.left.typeAnnotation,
param.right
);
});
}
/**
* Visits properties
* @param {ASTNode} node the node to be visited
* @returns {void}
*/
function inferrablePropertyVisitor(node) {
// We ignore `readonly` because of Microsoft/TypeScript#14416
// Essentially a readonly property without a type
// will result in its value being the type, leading to
// compile errors if the type is stripped.
if (ignoreProperties || node.readonly) {
return;
}
reportInferrableType(node, node.typeAnnotation, node.value);
}
return {
VariableDeclarator: inferrableVariableVisitor,
FunctionExpression: inferrableParameterVisitor,
FunctionDeclaration: inferrableParameterVisitor,
ArrowFunctionExpression: inferrableParameterVisitor,
ClassProperty: inferrablePropertyVisitor
};
}
};