mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
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 { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
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);
const checkStatement = (node: n.Node): node is K.StatementKind =>
  n.Statement.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;
  let inLoop = false;
  let replaceReturn = false;
  const visitLoop = (...args: string[]) =>
    function(this: Context, path: NodePath) {
      for (const arg of args) {
        this.visit(path.get(arg));
      }
      const old = { inLoop };
      inLoop = true;
      this.visit(path.get("body"));
      inLoop = old.inLoop;
      return false;
    };
  recast.visit(ast, {
    visitDoWhileStatement: visitLoop("test"),
    visitExpressionStatement(path) {
      const { expression, comments } = path.node;
      let valueOnly;
      if (
        n.CallExpression.check(expression) &&
        n.MemberExpression.check(expression.callee) &&
        !expression.callee.computed &&
        n.Identifier.check(expression.callee.object) &&
        expression.callee.object.name === "_" &&
        n.Identifier.check(expression.callee.property) &&
        ["each", "forEach"].includes(expression.callee.property.name) &&
        [2, 3].includes(expression.arguments.length) &&
        checkExpression(expression.arguments[0]) &&
        (n.FunctionExpression.check(expression.arguments[1]) ||
          n.ArrowFunctionExpression.check(expression.arguments[1])) &&
        [1, 2].includes(expression.arguments[1].params.length) &&
        n.Identifier.check(expression.arguments[1].params[0]) &&
        ((valueOnly = expression.arguments[1].params[1] === undefined) ||
          n.Identifier.check(expression.arguments[1].params[1])) &&
        (expression.arguments[2] === undefined ||
          n.ThisExpression.check(expression.arguments[2]))
      ) {
        const old = { inLoop, replaceReturn };
        inLoop = false;
        replaceReturn = true;
        this.visit(
          path
            .get("expression")
            .get("arguments")
            .get(1)
            .get("body")
        );
        inLoop = old.inLoop;
        replaceReturn = old.replaceReturn;
        const [right, { body, params }] = expression.arguments;
        const loop = b.forOfStatement(
          b.variableDeclaration("let", [
            b.variableDeclarator(
              valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
            ),
          ]),
          valueOnly
            ? right
            : b.callExpression(
                b.memberExpression(right, b.identifier("entries")),
                []
              ),
          checkStatement(body) ? body : b.expressionStatement(body)
        );
        loop.comments = comments;
        path.replace(loop);
        changed = true;
      }
      this.traverse(path);
    },
    visitForStatement: visitLoop("init", "test", "update"),
    visitForInStatement: visitLoop("left", "right"),
    visitForOfStatement: visitLoop("left", "right"),
    visitFunction(path) {
      this.visit(path.get("params"));
      const old = { replaceReturn };
      replaceReturn = false;
      this.visit(path.get("body"));
      replaceReturn = old.replaceReturn;
      return false;
    },
    visitReturnStatement(path) {
      if (replaceReturn) {
        assert(!inLoop); // could use labeled continue if this ever fires
        const { argument, comments } = path.node;
        if (argument === null) {
          const s = b.continueStatement();
          s.comments = comments;
          path.replace(s);
        } else {
          const s = b.expressionStatement(argument);
          s.comments = comments;
          path.replace(s, b.continueStatement());
        }
        return false;
      }
      this.traverse(path);
    },
    visitWhileStatement: visitLoop("test"),
  });
  if (changed) {
    console.log("Writing", file);
    fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
  }
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
		
	
		
			
				
	
	
		
			251 lines
		
	
	
		
			7.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			251 lines
		
	
	
		
			7.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
let focused_recipient;
 | 
						|
let normal_display = false;
 | 
						|
 | 
						|
exports.should_fade_message =  function (message) {
 | 
						|
    return !util.same_recipient(focused_recipient, message);
 | 
						|
};
 | 
						|
 | 
						|
exports.set_focused_recipient = function (msg_type) {
 | 
						|
    if (msg_type === undefined) {
 | 
						|
        focused_recipient = undefined;
 | 
						|
    }
 | 
						|
 | 
						|
    // Construct focused_recipient as a mocked up element which has all the
 | 
						|
    // fields of a message used by util.same_recipient()
 | 
						|
    focused_recipient = {
 | 
						|
        type: msg_type,
 | 
						|
    };
 | 
						|
 | 
						|
    if (focused_recipient.type === "stream") {
 | 
						|
        const stream_name = $('#stream_message_recipient_stream').val();
 | 
						|
        focused_recipient.topic = $('#stream_message_recipient_topic').val();
 | 
						|
        focused_recipient.stream = stream_name;
 | 
						|
        const sub = stream_data.get_sub(stream_name);
 | 
						|
        if (sub) {
 | 
						|
            focused_recipient.stream_id = sub.stream_id;
 | 
						|
        }
 | 
						|
    } else {
 | 
						|
        // Normalize the recipient list so it matches the one used when
 | 
						|
        // adding the message (see message_store.add_message_metadata()).
 | 
						|
        const reply_to = util.normalize_recipients(compose_state.private_message_recipient());
 | 
						|
        focused_recipient.reply_to = reply_to;
 | 
						|
        focused_recipient.to_user_ids = people.reply_to_to_user_ids_string(reply_to);
 | 
						|
    }
 | 
						|
};
 | 
						|
 | 
						|
function display_messages_normally() {
 | 
						|
    const table = rows.get_table(current_msg_list.table_name);
 | 
						|
    table.find('.recipient_row').removeClass("message-fade");
 | 
						|
 | 
						|
    normal_display = true;
 | 
						|
    floating_recipient_bar.update();
 | 
						|
}
 | 
						|
 | 
						|
function change_fade_state(elt, should_fade_group) {
 | 
						|
    if (should_fade_group) {
 | 
						|
        elt.addClass("message-fade");
 | 
						|
    } else {
 | 
						|
        elt.removeClass("message-fade");
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
function fade_messages() {
 | 
						|
    let i;
 | 
						|
    let first_message;
 | 
						|
    let first_row;
 | 
						|
    let should_fade_group = false;
 | 
						|
    const visible_groups = message_viewport.visible_groups(false);
 | 
						|
 | 
						|
    normal_display = false;
 | 
						|
 | 
						|
    // Update the visible messages first, before the compose box opens
 | 
						|
    for (i = 0; i < visible_groups.length; i += 1) {
 | 
						|
        first_row = rows.first_message_in_group(visible_groups[i]);
 | 
						|
        first_message = current_msg_list.get(rows.id(first_row));
 | 
						|
        should_fade_group = exports.should_fade_message(first_message);
 | 
						|
 | 
						|
        change_fade_state($(visible_groups[i]), should_fade_group);
 | 
						|
    }
 | 
						|
 | 
						|
    // Defer updating all message groups so that the compose box can open sooner
 | 
						|
    setTimeout(function (expected_msg_list, expected_recipient) {
 | 
						|
        const all_groups = rows.get_table(current_msg_list.table_name).find(".recipient_row");
 | 
						|
 | 
						|
        if (current_msg_list !== expected_msg_list ||
 | 
						|
            !compose_state.composing() ||
 | 
						|
            compose_state.private_message_recipient() !== expected_recipient) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
 | 
						|
        should_fade_group = false;
 | 
						|
 | 
						|
        // Note: The below algorithm relies on the fact that all_elts is
 | 
						|
        // sorted as it would be displayed in the message view
 | 
						|
        for (i = 0; i < all_groups.length; i += 1) {
 | 
						|
            const group_elt = $(all_groups[i]);
 | 
						|
            should_fade_group = exports.should_fade_message(rows.recipient_from_group(group_elt));
 | 
						|
            change_fade_state(group_elt, should_fade_group);
 | 
						|
        }
 | 
						|
 | 
						|
        floating_recipient_bar.update();
 | 
						|
    }, 0, current_msg_list, compose_state.private_message_recipient());
 | 
						|
}
 | 
						|
 | 
						|
exports.would_receive_message = function (email) {
 | 
						|
    if (focused_recipient.type === 'stream') {
 | 
						|
        const user = people.get_active_user_for_email(email);
 | 
						|
        const sub = stream_data.get_sub(focused_recipient.stream);
 | 
						|
        if (!sub || !user) {
 | 
						|
            // If the stream or user isn't valid, there is no risk of a mix
 | 
						|
            // yet, so we sort of "lie" and say they would receive a
 | 
						|
            // message.
 | 
						|
            return true;
 | 
						|
        }
 | 
						|
 | 
						|
        return stream_data.is_user_subscribed(focused_recipient.stream, user.user_id);
 | 
						|
    }
 | 
						|
 | 
						|
    // PM, so check if the given email is in the recipients list.
 | 
						|
    return util.is_pm_recipient(email, focused_recipient);
 | 
						|
};
 | 
						|
 | 
						|
const user_fade_config = {
 | 
						|
    get_user_id: function (li) {
 | 
						|
        return buddy_list.get_key_from_li({li: li});
 | 
						|
    },
 | 
						|
    fade: function (li) {
 | 
						|
        return li.addClass('user-fade');
 | 
						|
    },
 | 
						|
    unfade: function (li) {
 | 
						|
        return li.removeClass('user-fade');
 | 
						|
    },
 | 
						|
};
 | 
						|
 | 
						|
function update_user_row_when_fading(li, conf) {
 | 
						|
    const user_id = conf.get_user_id(li);
 | 
						|
    const email = people.get_by_user_id(user_id).email;
 | 
						|
    const would_receive = exports.would_receive_message(email);
 | 
						|
 | 
						|
    if (would_receive || people.is_my_user_id(user_id)) {
 | 
						|
        conf.unfade(li);
 | 
						|
    } else {
 | 
						|
        conf.fade(li);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
function display_users_normally(items, conf) {
 | 
						|
    for (const li of items) {
 | 
						|
        conf.unfade(li);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
function fade_users(items, conf) {
 | 
						|
    for (const li of items) {
 | 
						|
        update_user_row_when_fading(li, conf);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
function want_normal_display() {
 | 
						|
    // If we're not composing show a normal display.
 | 
						|
    if (focused_recipient === undefined) {
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    // If the user really hasn't specified anything let, then we want a normal display
 | 
						|
    if (focused_recipient.type === "stream") {
 | 
						|
        // If a stream doesn't exist, there is no real chance of a mix, so fading
 | 
						|
        // is just noise to the user.
 | 
						|
        if (!stream_data.get_sub(focused_recipient.stream)) {
 | 
						|
            return true;
 | 
						|
        }
 | 
						|
 | 
						|
        // This is kind of debatable.  If the topic is empty, it could be that
 | 
						|
        // the user simply hasn't started typing it yet, but disabling fading here
 | 
						|
        // means the feature doesn't help realms where topics aren't mandatory
 | 
						|
        // (which is most realms as of this writing).
 | 
						|
        if (focused_recipient.topic === "") {
 | 
						|
            return true;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    return focused_recipient.type === "private" && focused_recipient.reply_to === "";
 | 
						|
}
 | 
						|
 | 
						|
function do_update_all() {
 | 
						|
    const user_items = buddy_list.get_items();
 | 
						|
 | 
						|
    if (want_normal_display()) {
 | 
						|
        if (!normal_display) {
 | 
						|
            display_messages_normally();
 | 
						|
            display_users_normally(user_items, user_fade_config);
 | 
						|
        }
 | 
						|
    } else {
 | 
						|
        fade_messages();
 | 
						|
        fade_users(user_items, user_fade_config);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
// This one only updates the users, not both, like update_faded_messages.
 | 
						|
// This is for when new presence information comes in, redrawing the presence
 | 
						|
// list.
 | 
						|
exports.update_faded_users = function () {
 | 
						|
    const user_items = buddy_list.get_items();
 | 
						|
 | 
						|
    exports.update_user_info(user_items, user_fade_config);
 | 
						|
};
 | 
						|
 | 
						|
exports.update_user_info = function (items, conf) {
 | 
						|
    if (want_normal_display()) {
 | 
						|
        display_users_normally(items, conf);
 | 
						|
    } else {
 | 
						|
        fade_users(items, conf);
 | 
						|
    }
 | 
						|
};
 | 
						|
 | 
						|
// This gets called on keyup events, hence the throttling.
 | 
						|
exports.update_all = _.debounce(do_update_all, 50);
 | 
						|
 | 
						|
exports.start_compose = function (msg_type) {
 | 
						|
    exports.set_focused_recipient(msg_type);
 | 
						|
    do_update_all();
 | 
						|
};
 | 
						|
 | 
						|
exports.clear_compose = function () {
 | 
						|
    focused_recipient = undefined;
 | 
						|
    display_messages_normally();
 | 
						|
    exports.update_faded_users();
 | 
						|
};
 | 
						|
 | 
						|
exports.update_message_list = function () {
 | 
						|
    if (want_normal_display()) {
 | 
						|
        display_messages_normally();
 | 
						|
    } else {
 | 
						|
        fade_messages();
 | 
						|
    }
 | 
						|
};
 | 
						|
 | 
						|
exports.update_rendered_message_groups = function (message_groups, get_element) {
 | 
						|
    if (want_normal_display()) {
 | 
						|
        return;
 | 
						|
    }
 | 
						|
 | 
						|
    // This loop is superficially similar to some code in fade_messages, but an
 | 
						|
    // important difference here is that we look at each message individually, whereas
 | 
						|
    // the other code takes advantage of blocks beneath recipient bars.
 | 
						|
    for (const message_group of message_groups) {
 | 
						|
        const elt = get_element(message_group);
 | 
						|
        const first_message = message_group.message_containers[0].msg;
 | 
						|
        const should_fade = exports.should_fade_message(first_message);
 | 
						|
        change_fade_state(elt, should_fade);
 | 
						|
    }
 | 
						|
};
 | 
						|
 | 
						|
exports.initialize = function () {
 | 
						|
    $(document).on('peer_subscribe.zulip peer_unsubscribe.zulip', function () {
 | 
						|
        exports.update_faded_users();
 | 
						|
    });
 | 
						|
};
 | 
						|
 | 
						|
 | 
						|
window.compose_fade = exports;
 |