prefer-includes.js
9.28 KB
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
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const experimental_utils_1 = require("@typescript-eslint/experimental-utils");
const regexpp_1 = require("regexpp");
const ts = __importStar(require("typescript"));
const util_1 = require("../util");
exports.default = util_1.createRule({
name: 'prefer-includes',
defaultOptions: [],
meta: {
type: 'suggestion',
docs: {
description: 'Enforce `includes` method over `indexOf` method',
category: 'Best Practices',
recommended: false,
requiresTypeChecking: true,
},
fixable: 'code',
messages: {
preferIncludes: "Use 'includes()' method instead.",
preferStringIncludes: 'Use `String#includes()` method with a string instead.',
},
schema: [],
},
create(context) {
const globalScope = context.getScope();
const services = util_1.getParserServices(context);
const types = services.program.getTypeChecker();
function isNumber(node, value) {
const evaluated = util_1.getStaticValue(node, globalScope);
return evaluated !== null && evaluated.value === value;
}
function isPositiveCheck(node) {
switch (node.operator) {
case '!==':
case '!=':
case '>':
return isNumber(node.right, -1);
case '>=':
return isNumber(node.right, 0);
default:
return false;
}
}
function isNegativeCheck(node) {
switch (node.operator) {
case '===':
case '==':
case '<=':
return isNumber(node.right, -1);
case '<':
return isNumber(node.right, 0);
default:
return false;
}
}
function hasSameParameters(nodeA, nodeB) {
if (!ts.isFunctionLike(nodeA) || !ts.isFunctionLike(nodeB)) {
return false;
}
const paramsA = nodeA.parameters;
const paramsB = nodeB.parameters;
if (paramsA.length !== paramsB.length) {
return false;
}
for (let i = 0; i < paramsA.length; ++i) {
const paramA = paramsA[i];
const paramB = paramsB[i];
// Check name, type, and question token once.
if (paramA.getText() !== paramB.getText()) {
return false;
}
}
return true;
}
/**
* Parse a given node if it's a `RegExp` instance.
* @param node The node to parse.
*/
function parseRegExp(node) {
const evaluated = util_1.getStaticValue(node, globalScope);
if (evaluated == null || !(evaluated.value instanceof RegExp)) {
return null;
}
const { pattern, flags } = regexpp_1.parseRegExpLiteral(evaluated.value);
if (pattern.alternatives.length !== 1 ||
flags.ignoreCase ||
flags.global) {
return null;
}
// Check if it can determine a unique string.
const chars = pattern.alternatives[0].elements;
if (!chars.every(c => c.type === 'Character')) {
return null;
}
// To string.
return String.fromCodePoint(...chars.map(c => c.value));
}
return {
[[
// a.indexOf(b) !== 1
"BinaryExpression > CallExpression.left > MemberExpression.callee[property.name='indexOf'][computed=false]",
// a?.indexOf(b) !== 1
"BinaryExpression > ChainExpression.left > CallExpression > MemberExpression.callee[property.name='indexOf'][computed=false]",
].join(', ')](node) {
var _a, _b, _c;
// Check if the comparison is equivalent to `includes()`.
const callNode = node.parent;
const compareNode = (((_a = callNode.parent) === null || _a === void 0 ? void 0 : _a.type) ===
experimental_utils_1.AST_NODE_TYPES.ChainExpression
? callNode.parent.parent
: callNode.parent);
const negative = isNegativeCheck(compareNode);
if (!negative && !isPositiveCheck(compareNode)) {
return;
}
// Get the symbol of `indexOf` method.
const tsNode = services.esTreeNodeToTSNodeMap.get(node.property);
const indexofMethodDeclarations = (_b = types
.getSymbolAtLocation(tsNode)) === null || _b === void 0 ? void 0 : _b.getDeclarations();
if (indexofMethodDeclarations == null ||
indexofMethodDeclarations.length === 0) {
return;
}
// Check if every declaration of `indexOf` method has `includes` method
// and the two methods have the same parameters.
for (const instanceofMethodDecl of indexofMethodDeclarations) {
const typeDecl = instanceofMethodDecl.parent;
const type = types.getTypeAtLocation(typeDecl);
const includesMethodDecl = (_c = type
.getProperty('includes')) === null || _c === void 0 ? void 0 : _c.getDeclarations();
if (includesMethodDecl == null ||
!includesMethodDecl.some(includesMethodDecl => hasSameParameters(includesMethodDecl, instanceofMethodDecl))) {
return;
}
}
// Report it.
context.report({
node: compareNode,
messageId: 'preferIncludes',
*fix(fixer) {
if (negative) {
yield fixer.insertTextBefore(callNode, '!');
}
yield fixer.replaceText(node.property, 'includes');
yield fixer.removeRange([callNode.range[1], compareNode.range[1]]);
},
});
},
// /bar/.test(foo)
'CallExpression > MemberExpression.callee[property.name="test"][computed=false]'(node) {
var _a;
const callNode = node.parent;
const text = callNode.arguments.length === 1 ? parseRegExp(node.object) : null;
if (text == null) {
return;
}
//check the argument type of test methods
const argument = callNode.arguments[0];
const tsNode = services.esTreeNodeToTSNodeMap.get(argument);
const type = util_1.getConstrainedTypeAtLocation(types, tsNode);
const includesMethodDecl = (_a = type
.getProperty('includes')) === null || _a === void 0 ? void 0 : _a.getDeclarations();
if (includesMethodDecl == null) {
return;
}
context.report({
node: callNode,
messageId: 'preferStringIncludes',
*fix(fixer) {
const argNode = callNode.arguments[0];
const needsParen = argNode.type !== experimental_utils_1.AST_NODE_TYPES.Literal &&
argNode.type !== experimental_utils_1.AST_NODE_TYPES.TemplateLiteral &&
argNode.type !== experimental_utils_1.AST_NODE_TYPES.Identifier &&
argNode.type !== experimental_utils_1.AST_NODE_TYPES.MemberExpression &&
argNode.type !== experimental_utils_1.AST_NODE_TYPES.CallExpression;
yield fixer.removeRange([callNode.range[0], argNode.range[0]]);
if (needsParen) {
yield fixer.insertTextBefore(argNode, '(');
yield fixer.insertTextAfter(argNode, ')');
}
yield fixer.insertTextAfter(argNode, `${node.optional ? '?.' : '.'}includes(${JSON.stringify(text)}`);
},
});
},
};
},
});
//# sourceMappingURL=prefer-includes.js.map