UNPKG

1.45 kBJavaScriptView Raw
1/**
2 * @fileoverview Use .includes instead of .indexOf
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9"use strict";
10
11// -----------------------------------------------------------------------------
12// Rule Definition
13// -----------------------------------------------------------------------------
14
15module.exports = function(context) {
16
17 // ---------------------------------------------------------------------------
18 // Public
19 // --------------------------------------------------------------------------
20
21 return {
22 "BinaryExpression": function(node) {
23 if (node.left.type != "CallExpression" ||
24 node.left.callee.type != "MemberExpression" ||
25 node.left.callee.property.type != "Identifier" ||
26 node.left.callee.property.name != "indexOf") {
27 return;
28 }
29
30 if ((["!=", "!==", "==", "==="].includes(node.operator) &&
31 node.right.type == "UnaryExpression" &&
32 node.right.operator == "-" &&
33 node.right.argument.type == "Literal" &&
34 node.right.argument.value == 1) ||
35 ([">=", "<"].includes(node.operator) &&
36 node.right.type == "Literal" &&
37 node.right.value == 0)) {
38 context.report(node, "use .includes instead of .indexOf");
39 }
40 }
41 };
42};