zulip/static/js
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
..
analytics js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
billing js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
bundles babel: Enable loose mode. 2020-02-05 11:52:52 -08:00
portico js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
stats minor: Replace Math.min() with Infinity for easier to read code. 2019-12-06 11:27:58 -08:00
types dependencies: Upgrade stacktrace-gps from 3.0.3 to 3.0.4. 2020-02-04 22:13:33 -08:00
activity.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
admin.js settings_org: Use actual value for value attribute of create-stream-policy. 2020-01-31 12:42:26 +05:30
alert_words.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
alert_words_ui.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
archive.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
attachments_ui.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
avatar.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
blueslip.js blueslip: Use modern spread arguments syntax. 2020-02-10 14:08:12 -08:00
blueslip_stacktrace.ts dependencies: Upgrade stacktrace-gps from 3.0.3 to 3.0.4. 2020-02-04 22:13:33 -08:00
bot_data.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
buddy_data.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
buddy_list.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
channel.js js: Convert _.indexOf(a, …) to a.indexOf(…). 2020-02-10 14:08:12 -08:00
click_handlers.js bots: Remove FEEDBACK_BOT implementation. 2020-01-25 22:41:39 -08:00
color_data.js streams: Simplify claim_colors. 2019-12-30 09:50:22 -08:00
colorspace.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
common.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
components.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
compose.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
compose_actions.js people: Rename method to get_by_user_id(). 2020-02-05 12:04:56 -08:00
compose_fade.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
compose_pm_pill.js people: Rename method to get_by_user_id(). 2020-02-05 12:04:56 -08:00
compose_state.js compose_state: Rename compost_state.recipient to be about PMs only. 2019-12-02 08:53:55 -08:00
compose_ui.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
composebox_typeahead.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
condense.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
confirm_dialog.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
copy_and_paste.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
csrf.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
debug.js lint: Delegate console.log check to ESLint. 2020-02-07 14:09:47 -08:00
dict.ts dict: Remove each method. 2020-02-04 12:22:03 -08:00
drafts.js drafts: Iterate over drafts with Object.entries. 2020-02-07 14:09:47 -08:00
echo.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
emoji.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
emoji_picker.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
favicon.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
feature_flags.js js: Purge useless IIFEs. 2019-10-25 13:51:21 -07:00
feedback_widget.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
fenced_code.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
fetch_status.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
filter.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
floating_recipient_bar.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
fold_dict.ts dict: Remove each method. 2020-02-04 12:22:03 -08:00
gear_menu.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
global.d.ts dependencies: Remove WebSockets system for sending messages. 2020-01-14 22:34:00 -08:00
hash_util.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
hashchange.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
hbs.d.ts blueslip: Make stack trace more readable. 2019-10-31 13:47:54 -07:00
hotkey.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
hotspots.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
info_overlay.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
input_pill.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
int_dict.ts dict: Remove each method. 2020-02-04 12:22:03 -08:00
integration_bot_widget.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
invite.js invitations: Avoid adding to notifications stream unconditionally. 2020-01-27 15:36:59 -08:00
keydown_util.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
lazy_set.js dict, lazy_set: Return an iterator from keys method. 2020-02-04 12:22:03 -08:00
lightbox.js lightbox: Don't blow up for messages not in the message store. 2020-02-10 14:00:40 -08:00
lightbox_canvas.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
list_cursor.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
list_render.js blueslip: Add feature to time common operations. 2020-01-15 12:01:16 -08:00
list_util.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
loading.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
local_message.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
localstorage.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
markdown.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
message_edit.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
message_events.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
message_fetch.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
message_flags.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
message_list.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
message_list_data.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
message_list_view.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
message_live_update.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
message_scroll.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
message_store.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
message_util.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
message_viewport.js message_viewport: Convert _.every used as loop to for…of. 2020-02-10 14:08:12 -08:00
muting.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
muting_ui.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
narrow.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
narrow_state.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
navigate.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
night_mode.js js: Purge useless IIFEs. 2019-10-25 13:51:21 -07:00
notifications.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
overlays.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
padded_widget.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
page_params.js page_params: Record page_params_parse_time. 2020-01-15 12:01:14 -08:00
panels.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
people.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
pm_conversations.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
pm_list.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
pm_list_dom.js pm_list: Simplify redraws for Private Messages. 2020-01-30 13:11:32 -08:00
pointer.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
poll_widget.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
popovers.js popovers: Check for undefined message.edit_history. 2020-02-10 14:08:12 -08:00
presence.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
reactions.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
ready.js templates: Make the Loading… message more robust. 2019-09-20 10:34:44 -07:00
realm_icon.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
realm_logo.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
recent_senders.js recent_senders: Replace topic_senders and stream_senders with IntDict. 2020-02-02 20:37:41 -08:00
reload.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
reload_state.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
reminder.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
resize.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
rows.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
rtl.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
schema.js schema: Convert _.find used as loop to for…of. 2020-02-10 14:08:12 -08:00
scroll_bar.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
scroll_util.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
search.js search performance: Stop at max_items. 2019-12-28 11:09:28 -08:00
search_pill.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
search_pill_widget.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
search_suggestion.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
search_util.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
sent_messages.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
server_events.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
server_events_dispatch.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
settings.js settings: Add notification settings checkboxes for wildcard mentions. 2019-11-20 16:58:46 -08:00
settings_account.js js: Convert jQuery.each to for…of. 2020-02-10 14:08:12 -08:00
settings_bots.js js: Convert jQuery.each to for…of. 2020-02-10 14:08:12 -08:00
settings_display.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
settings_emoji.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
settings_exports.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
settings_invites.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
settings_linkifiers.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
settings_muting.js js: Clean up stream_id type confusion. 2020-01-16 13:23:47 -08:00
settings_notifications.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
settings_org.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
settings_panel_menu.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
settings_profile_fields.js settings_profile_fields: Iterate over field_types with Object.values. 2020-02-10 14:08:12 -08:00
settings_sections.js settings_sections: Replace is_loaded Dict with loaded_groups Set. 2020-02-02 20:37:41 -08:00
settings_streams.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
settings_toggle.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
settings_ui.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
settings_user_groups.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
settings_users.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
setup.js dependencies: Remove WebSockets system for sending messages. 2020-01-14 22:34:00 -08:00
starred_messages.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
stream_color.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
stream_create.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
stream_data.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
stream_edit.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
stream_events.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
stream_list.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
stream_muting.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
stream_popover.js Convert more stream_ids to ints. 2020-01-12 11:27:26 -08:00
stream_sort.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
stream_ui_updates.js settings: Migrate to stream_post_policy structure. 2020-02-04 17:08:08 -08:00
submessage.js submessage: Move submessages === undefined check up. 2020-02-10 14:08:12 -08:00
subs.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
tab_bar.js js: Automatically convert var to let and const in remaining files. 2019-11-20 23:04:01 -08:00
templates.js templates: Cache translations. 2020-01-15 18:01:59 -08:00
tictactoe_widget.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
timerender.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
todo_widget.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
top_left_corner.js pm_list: Set active-sub-filter in template. 2020-01-06 10:21:23 -08:00
topic_data.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
topic_generator.js js: Convert _.indexOf(a, …) to a.indexOf(…). 2020-02-10 14:08:12 -08:00
topic_list.js topic list: Use vdom techniques. 2020-02-05 13:04:16 -08:00
topic_list_data.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
topic_zoom.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
translations.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
transmit.js dependencies: Remove WebSockets system for sending messages. 2020-01-14 22:34:00 -08:00
tslint.json typescript: Move TS files into JS directory. 2019-03-25 12:11:37 -07:00
tutorial.js js: Purge useless IIFEs. 2019-10-25 13:51:21 -07:00
typeahead_helper.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
typing.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
typing_data.js js: Replace [...x] with Array.from(x). 2020-02-05 11:52:52 -08:00
typing_events.js people: Rename method to get_by_user_id(). 2020-02-05 12:04:56 -08:00
ui.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
ui_init.js blueslip: Add feature to time common operations. 2020-01-15 12:01:16 -08:00
ui_report.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
ui_util.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
unread.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
unread_ops.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
unread_ui.js topic list: Use vdom techniques. 2020-02-05 13:04:16 -08:00
upload.js performance: Use startsWith in many places. 2020-01-28 12:47:37 -08:00
upload_widget.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
user_events.js people: Rename method to get_by_user_id(). 2020-02-05 12:04:56 -08:00
user_groups.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
user_pill.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
user_search.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
user_status.js user_status: Iterate over page_params.user_status with Object.entries. 2020-02-07 14:09:47 -08:00
user_status_ui.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
util.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
vdom.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
widgetize.js widgetize: Convert widget_contents from object to Map. 2020-02-06 17:24:43 -08:00
zcommand.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
zform.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
zulip.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00