mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 13:33:24 +00:00
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>
This commit is contained in:
committed by
Tim Abbott
parent
788c5afbcf
commit
719546641f
@@ -127,7 +127,7 @@ function ajaxSubscribeForCreation(stream_name, description, user_ids, invite_onl
|
||||
},
|
||||
error: function (xhr) {
|
||||
const msg = JSON.parse(xhr.responseText).msg;
|
||||
if (msg.indexOf('access') >= 0) {
|
||||
if (msg.includes('access')) {
|
||||
// If we can't access the stream, we can safely assume it's
|
||||
// a duplicate stream that we are not invited to.
|
||||
stream_name_error.report_already_exists(stream_name);
|
||||
@@ -210,7 +210,7 @@ function create_stream() {
|
||||
// Even though we already check to make sure that while typing the user cannot enter
|
||||
// newline characters (by pressing the enter key) it would still be possible to copy
|
||||
// and paste over a description with newline characters in it. Prevent that.
|
||||
if (description.indexOf('\n') !== -1) {
|
||||
if (description.includes('\n')) {
|
||||
ui_report.message(i18n.t("The stream description cannot contain newline characters."), $(".stream_create_info"), 'alert-error');
|
||||
return;
|
||||
}
|
||||
@@ -409,7 +409,7 @@ exports.set_up_handlers = function () {
|
||||
stream_subscription_error.report_no_subs_to_stream();
|
||||
return;
|
||||
}
|
||||
if (principals.indexOf(people.my_current_user_id()) < 0 && !page_params.is_admin) {
|
||||
if (!principals.includes(people.my_current_user_id()) && !page_params.is_admin) {
|
||||
stream_subscription_error.cant_create_stream_without_susbscribing();
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user