Anders Kaseorg
719546641f
js: Convert a.indexOf(…) !== -1 to a.includes(…).
Babel polyfills this for us for Internet Explorer.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import K from "ast-types/gen/kinds";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
recast.visit(ast, {
visitBinaryExpression(path) {
const { operator, left, right } = path.node;
if (
n.CallExpression.check(left) &&
n.MemberExpression.check(left.callee) &&
!left.callee.computed &&
n.Identifier.check(left.callee.property) &&
left.callee.property.name === "indexOf" &&
left.arguments.length === 1 &&
checkExpression(left.arguments[0]) &&
((["===", "!==", "==", "!=", ">", "<="].includes(operator) &&
n.UnaryExpression.check(right) &&
right.operator == "-" &&
n.Literal.check(right.argument) &&
right.argument.value === 1) ||
([">=", "<"].includes(operator) &&
n.Literal.check(right) &&
right.value === 0))
) {
const test = b.callExpression(
b.memberExpression(left.callee.object, b.identifier("includes")),
[left.arguments[0]]
);
path.replace(
["!==", "!=", ">", ">="].includes(operator)
? test
: b.unaryExpression("!", test)
);
changed = true;
}
this.traverse(path);
},
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-10 14:08:12 -08:00
..
2019-11-03 12:42:39 -08:00
2019-11-03 12:42:39 -08:00
2020-02-05 11:52:52 -08:00
2020-02-10 14:08:12 -08:00
2019-12-06 11:27:58 -08:00
2020-02-04 22:13:33 -08:00
2020-02-10 14:08:12 -08:00
2020-01-31 12:42:26 +05:30
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:08:12 -08:00
2019-11-03 12:42:39 -08:00
2020-02-04 22:13:33 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2020-01-25 22:41:39 -08:00
2019-12-30 09:50:22 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:08:12 -08:00
2019-11-03 12:42:39 -08:00
2020-02-05 12:04:56 -08:00
2020-02-07 14:09:47 -08:00
2020-02-05 12:04:56 -08:00
2019-12-02 08:53:55 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:08:12 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2019-11-03 12:42:39 -08:00
2020-02-07 14:09:47 -08:00
2020-02-04 12:22:03 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2020-02-10 14:08:12 -08:00
2019-11-03 12:42:39 -08:00
2019-10-25 13:51:21 -07:00
2019-11-03 12:42:39 -08:00
2020-02-07 14:09:47 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-02-04 12:22:03 -08:00
2019-11-03 12:42:39 -08:00
2020-01-14 22:34:00 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2019-10-31 13:47:54 -07:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2019-11-03 12:42:39 -08:00
2020-02-04 12:22:03 -08:00
2019-11-03 12:42:39 -08:00
2020-01-27 15:36:59 -08:00
2019-11-03 12:42:39 -08:00
2020-02-04 12:22:03 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:00:40 -08:00
2019-11-03 12:42:39 -08:00
2020-01-15 12:01:16 -08:00
2019-11-03 12:42:39 -08:00
2019-11-03 12:42:39 -08:00
2019-11-03 12:42:39 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2020-02-10 14:08:12 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2019-11-03 12:42:39 -08:00
2020-02-07 14:09:47 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:08:12 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2019-11-03 12:42:39 -08:00
2019-10-25 13:51:21 -07:00
2020-02-10 14:08:12 -08:00
2019-11-03 12:42:39 -08:00
2019-11-03 12:42:39 -08:00
2020-01-15 12:01:14 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-01-30 13:11:32 -08:00
2020-02-10 14:08:12 -08:00
2019-11-03 12:42:39 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2019-09-20 10:34:44 -07:00
2019-11-03 12:42:39 -08:00
2019-11-03 12:42:39 -08:00
2020-02-02 20:37:41 -08:00
2019-11-03 12:42:39 -08:00
2020-02-07 14:09:47 -08:00
2019-11-03 12:42:39 -08:00
2020-02-07 14:09:47 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:08:12 -08:00
2020-02-10 14:08:12 -08:00
2019-11-03 12:42:39 -08:00
2019-11-03 12:42:39 -08:00
2019-11-03 12:42:39 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:08:12 -08:00
2020-02-10 14:08:12 -08:00
2019-12-28 11:09:28 -08:00
2019-11-03 12:42:39 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2020-02-10 14:08:12 -08:00
2020-02-10 14:08:12 -08:00
2020-02-10 14:08:12 -08:00
2020-02-10 14:08:12 -08:00
2020-02-10 14:08:12 -08:00
2020-02-10 14:08:12 -08:00
2020-01-16 13:23:47 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:08:12 -08:00
2020-02-02 20:37:41 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2019-11-03 12:42:39 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2019-11-20 16:58:46 -08:00
2020-01-14 22:34:00 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2020-02-09 22:08:50 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2019-11-03 12:42:39 -08:00
2020-01-12 11:27:26 -08:00
2020-02-07 14:09:47 -08:00
2020-02-04 17:08:08 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2019-11-20 23:04:01 -08:00
2020-01-15 18:01:59 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2020-02-07 14:09:47 -08:00
2020-01-06 10:21:23 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-02-05 13:04:16 -08:00
2019-11-03 12:42:39 -08:00
2019-11-03 12:42:39 -08:00
2020-01-14 22:34:00 -08:00
2019-10-25 13:51:21 -07:00
2020-02-07 14:09:47 -08:00
2020-02-05 11:52:52 -08:00
2020-02-05 12:04:56 -08:00
2019-11-03 12:42:39 -08:00
2020-01-15 12:01:16 -08:00
2019-11-03 12:42:39 -08:00
2019-11-03 12:42:39 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-02-05 13:04:16 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2020-01-28 12:47:37 -08:00
2020-02-05 12:04:56 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2019-11-03 12:42:39 -08:00
2019-11-03 12:42:39 -08:00
2020-02-07 14:09:47 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2020-02-06 17:24:43 -08:00
2020-02-10 14:08:12 -08:00
2020-02-07 14:09:47 -08:00
2019-11-03 12:42:39 -08:00