зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1339461 - add an eslint rule to detect when indexOf should be replaced with includes, r=Standard8.
This commit is contained in:
Родитель
2b1c8dccb6
Коммит
fcd9a3a0c8
|
@ -79,7 +79,7 @@ function checkSort(aTree, aSortingMode, aSortingAnno) {
|
|||
// Check sortingAnnotation, but only if sortingMode is ANNOTATION.
|
||||
if ([Ci.nsINavHistoryQueryOptions.SORT_BY_ANNOTATION_ASCENDING,
|
||||
Ci.nsINavHistoryQueryOptions.SORT_BY_ANNOTATION_DESCENDING].
|
||||
indexOf(aSortingMode) >= 0) {
|
||||
includes(aSortingMode)) {
|
||||
is(res.sortingAnnotation, aSortingAnno,
|
||||
"column should now have sorting annotation " + aSortingAnno);
|
||||
}
|
||||
|
|
|
@ -274,6 +274,11 @@ use-ownerGlobal
|
|||
|
||||
Require .ownerGlobal instead of .ownerDocument.defaultView.
|
||||
|
||||
use-includes-instead-of-indexOf
|
||||
-------------------------------
|
||||
|
||||
Use .includes instead of .indexOf to check if something is in an array or string.
|
||||
|
||||
use-services
|
||||
------------
|
||||
|
||||
|
|
|
@ -171,6 +171,7 @@ module.exports = {
|
|||
"mozilla/no-useless-removeEventListener": "error",
|
||||
"mozilla/use-chromeutils-import": "error",
|
||||
"mozilla/use-default-preference-values": "error",
|
||||
"mozilla/use-includes-instead-of-indexOf": "error",
|
||||
"mozilla/use-ownerGlobal": "error",
|
||||
"mozilla/use-services": "error",
|
||||
|
||||
|
|
|
@ -61,6 +61,7 @@ module.exports = {
|
|||
"use-default-preference-values":
|
||||
require("../lib/rules/use-default-preference-values"),
|
||||
"use-ownerGlobal": require("../lib/rules/use-ownerGlobal"),
|
||||
"use-includes-instead-of-indexOf": require("../lib/rules/use-includes-instead-of-indexOf"),
|
||||
"use-services": require("../lib/rules/use-services"),
|
||||
"var-only-at-top-level": require("../lib/rules/var-only-at-top-level")
|
||||
},
|
||||
|
@ -87,6 +88,7 @@ module.exports = {
|
|||
"use-chromeutils-import": "off",
|
||||
"use-default-preference-values": "off",
|
||||
"use-ownerGlobal": "off",
|
||||
"use-includes-instead-of-indexOf": "off",
|
||||
"use-services": "off",
|
||||
"var-only-at-top-level": "off"
|
||||
}
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* @fileoverview Use .includes instead of .indexOf
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Rule Definition
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
module.exports = function(context) {
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
return {
|
||||
"BinaryExpression": function(node) {
|
||||
if (node.left.type != "CallExpression" ||
|
||||
node.left.callee.type != "MemberExpression" ||
|
||||
node.left.callee.property.type != "Identifier" ||
|
||||
node.left.callee.property.name != "indexOf") {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((["!=", "!==", "==", "==="].includes(node.operator) &&
|
||||
node.right.type == "UnaryExpression" &&
|
||||
node.right.operator == "-" &&
|
||||
node.right.argument.type == "Literal" &&
|
||||
node.right.argument.value == 1) ||
|
||||
([">=", "<"].includes(node.operator) &&
|
||||
node.right.type == "Literal" &&
|
||||
node.right.value == 0)) {
|
||||
context.report(node, "use .includes instead of .indexOf");
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
|
@ -0,0 +1,38 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
"use strict";
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
var rule = require("../lib/rules/use-includes-instead-of-indexOf");
|
||||
var RuleTester = require("eslint/lib/testers/rule-tester");
|
||||
|
||||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
function invalidCode(code) {
|
||||
let message = "use .includes instead of .indexOf";
|
||||
return {code, errors: [{message, type: "BinaryExpression"}]};
|
||||
}
|
||||
|
||||
ruleTester.run("use-includes-instead-of-indexOf", rule, {
|
||||
valid: [
|
||||
"let a = foo.includes(bar);",
|
||||
"let a = foo.indexOf(bar) > 0;",
|
||||
"let a = foo.indexOf(bar) != 0;"
|
||||
],
|
||||
invalid: [
|
||||
invalidCode("let a = foo.indexOf(bar) >= 0;"),
|
||||
invalidCode("let a = foo.indexOf(bar) != -1;"),
|
||||
invalidCode("let a = foo.indexOf(bar) !== -1;"),
|
||||
invalidCode("let a = foo.indexOf(bar) == -1;"),
|
||||
invalidCode("let a = foo.indexOf(bar) === -1;"),
|
||||
invalidCode("let a = foo.indexOf(bar) < 0;")
|
||||
]
|
||||
});
|
Загрузка…
Ссылка в новой задаче