zulip/frontend_tests/node_tests
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
..
.eslintrc.json lint: Add JS indentation eslint rules for node_tests. 2018-05-06 19:35:18 -07:00
activity.js presence: Iterate over presence info with Object.entries. 2020-02-07 14:09:47 -08:00
alert_words.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
alert_words_ui.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
billing.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
billing_helpers.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
blueslip_stacktrace.js blueslip: Make stack trace more readable. 2019-10-31 13:47:54 -07:00
bot_data.js tests: Fix more undefined mocks. 2020-02-10 14:08:12 -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 var to let and const in most files. 2019-11-03 12:42:39 -08:00
channel.js node tests: Consolidate some set_global() calls. 2018-08-02 08:02:12 -04:00
color_data.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -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: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
components.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
compose.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
compose_actions.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
compose_fade.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
compose_pm_pill.js people: Rename method to get_by_user_id(). 2020-02-05 12:04:56 -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 stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
copy_and_paste.js zjsunit: Make window a Proxy for global. 2019-11-13 14:27:13 -08:00
dict.js js: Replace [...x] with Array.from(x). 2020-02-05 11:52:52 -08:00
dispatch.js people: Rename method to get_by_user_id(). 2020-02-05 12:04:56 -08:00
drafts.js compose_state: Rename compost_state.recipient to be about PMs only. 2019-12-02 08:53:55 -08:00
echo.js api: Rename subject_links to topic_links. 2020-02-07 14:35:22 -08:00
emoji.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
emoji_picker.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
fenced_code.js compose: Add fences of unused length in quote-and-reply. 2020-02-04 18:17: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 stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
fold_dict.js js: Replace [...x] with Array.from(x). 2020-02-05 11:52:52 -08:00
general.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
hash_util.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
hashchange.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
hotkey.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
i18n.js zjsunit: Remove set_global side effect from zrequire. 2019-11-13 14:29:17 -08:00
input_pill.js zjsunit: Make window a Proxy for global. 2019-11-13 14:27:13 -08:00
int_dict.js js: Replace [...x] with Array.from(x). 2020-02-05 11:52:52 -08:00
keydown_util.js keydown_util: Ignore alt-arrow and similar things. 2018-12-04 12:24:39 -08:00
lazy_set.js lazy_set: Convert LazySet to a real class. 2020-02-04 12:22:03 -08:00
lightbox.js node: Fix lightbox tests to use message_store.get(). 2020-02-10 14:00:10 -08:00
list_cursor.js node tests: Bring list_cursor to 100% coverage. 2018-08-24 10:00:04 -07:00
list_render.js blueslip: Add feature to time common operations. 2020-01-15 12:01:16 -08:00
markdown.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
message_edit.js zjsunit: Remove set_global side effect from zrequire. 2019-11-13 14:29:17 -08:00
message_events.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
message_fetch.js tests: Fix predicate mock in simulate_narrow. 2020-02-10 14:08:12 -08:00
message_flags.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -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_store.js people: Rename method to get_by_user_id(). 2020-02-05 12:04:56 -08:00
muting.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
narrow.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
narrow_activate.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
narrow_local.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
narrow_state.js tests: Fix more undefined mocks. 2020-02-10 14:08:12 -08:00
narrow_unread.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
notifications.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
password.js zjsunit: Remove set_global side effect from zrequire. 2019-11-13 14:29:17 -08:00
people.js node tests: Always enforce blueslip warn/error/fatal. 2020-02-07 14:15:44 -08:00
people_errors.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
pm_conversations.js bug fix: Fix huddles in "Private Messages". 2020-01-02 11:59:58 -08:00
pm_list.js vdom: Add replace_content/find parameters. 2020-02-05 13:04:16 -08:00
popovers.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
presence.js presence: Convert presence_info from object to Map. 2020-02-06 17:24:43 -08:00
reactions.js emoji: Convert active_realm_emojis from object to Map. 2020-02-06 17:24:43 -08:00
recent_senders.js tests: Avoid _.uniqueId when a number is needed. 2020-02-02 20:37:41 -08:00
rtl.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
schema.js lint: Fix comma spacing in node tests. 2018-12-07 13:14:28 -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_legacy.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_suggestion.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
search_suggestion_legacy.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
server_events.js zjsunit: Make window a Proxy for global. 2019-11-13 14:27:13 -08:00
settings_bots.js tests: Fix more undefined mocks. 2020-02-10 14:08:12 -08:00
settings_muting.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
settings_org.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
settings_profile_fields.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
stream_data.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
stream_events.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
stream_list.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
stream_search.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
stream_sort.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
submessage.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
subs.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
support.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
templates.js js: Convert a.indexOf(…) !== -1 to a.includes(…). 2020-02-10 14:08:12 -08:00
timerender.js zjsunit: Remove Dict dependency. 2020-01-03 17:19:59 -08:00
top_left_corner.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
topic_data.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
topic_generator.js settings: Migrate to stream_post_policy structure. 2020-02-04 17:08:08 -08:00
topic_list_data.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
transmit.js dependencies: Remove WebSockets system for sending messages. 2020-01-14 22:34:00 -08:00
typeahead.js emojis: Make it easier to type smiley icons. 2020-01-28 12:48:02 -08:00
typeahead_helper.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
typing_data.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
typing_status.js typing: Fix invalid typing notifications for stream messages. 2019-12-02 09:31:16 -08:00
ui.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
ui_init.js js: Automatically convert _.each to for…of. 2020-02-07 14:09:47 -08:00
unread.js stream_data: Remove stream_name param from add_sub(). 2020-02-09 22:08:50 -08:00
upgrade.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
upload.js zjsunit: Make window a Proxy for global. 2019-11-13 14:27:13 -08:00
user_events.js people: Rename method to get_by_user_id(). 2020-02-05 12:04:56 -08:00
user_groups.js user_groups: Convert members from Dict to Set. 2020-01-16 13:23:47 -08:00
user_pill.js js: Automatically convert var to let and const in most files. 2019-11-03 12:42:39 -08:00
user_status.js refactor: Use a Set for away_user_ids. 2020-01-14 17:52:25 +00:00
util.js node tests: Always enforce blueslip warn/error/fatal. 2020-02-07 14:15:44 -08:00
vdom.js tests: Fix more undefined mocks. 2020-02-10 14:08:12 -08:00
voting_widget.js tests: Add missing options argument to poll_data_holder. 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
zblueslip.js node tests: Always enforce blueslip warn/error/fatal. 2020-02-07 14:15:44 -08:00
zjquery.js zjsquery: Add data support. 2020-01-05 12:28:37 -08:00