2020-07-15 01:29:15 +02:00
|
|
|
zrequire("message_events");
|
|
|
|
zrequire("message_store");
|
|
|
|
zrequire("muting");
|
|
|
|
zrequire("people");
|
|
|
|
zrequire("recent_senders");
|
|
|
|
zrequire("stream_data");
|
|
|
|
zrequire("stream_topic_history");
|
|
|
|
zrequire("unread");
|
|
|
|
|
|
|
|
set_global("$", global.make_zjquery());
|
|
|
|
set_global("alert_words", {});
|
|
|
|
set_global("condense", {});
|
|
|
|
set_global("current_msg_list", {});
|
|
|
|
set_global("message_edit", {});
|
|
|
|
set_global("message_list", {});
|
|
|
|
set_global("notifications", {});
|
|
|
|
set_global("page_params", {});
|
|
|
|
set_global("pm_list", {});
|
|
|
|
set_global("stream_list", {});
|
|
|
|
set_global("unread_ui", {});
|
2018-09-11 17:16:52 +02:00
|
|
|
|
|
|
|
alert_words.process_message = () => {};
|
|
|
|
|
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@example.com",
|
2018-09-11 17:16:52 +02:00
|
|
|
user_id: 32,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice Patel",
|
2018-09-11 17:16:52 +02:00
|
|
|
};
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(alice);
|
2018-09-11 17:16:52 +02:00
|
|
|
|
2020-04-11 02:15:41 +02:00
|
|
|
const denmark = {
|
|
|
|
subscribed: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Denmark",
|
2020-04-11 02:15:41 +02:00
|
|
|
stream_id: 101,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(denmark);
|
|
|
|
|
2018-09-11 17:16:52 +02:00
|
|
|
function test_helper(side_effects) {
|
|
|
|
const events = [];
|
|
|
|
|
js: Automatically convert _.each to for…of.
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>
2020-02-06 06:19:47 +01:00
|
|
|
for (const side_effect of side_effects) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const parts = side_effect.split(".");
|
2018-09-11 17:16:52 +02:00
|
|
|
const module = parts[0];
|
|
|
|
const field = parts[1];
|
|
|
|
|
|
|
|
global[module][field] = () => {
|
|
|
|
events.push(side_effect);
|
|
|
|
};
|
js: Automatically convert _.each to for…of.
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>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2018-09-11 17:16:52 +02:00
|
|
|
|
|
|
|
const self = {};
|
|
|
|
|
|
|
|
self.verify = () => {
|
|
|
|
assert.deepEqual(side_effects, events);
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("update_messages", () => {
|
2018-09-11 17:16:52 +02:00
|
|
|
const original_message = {
|
|
|
|
id: 111,
|
2020-04-11 02:15:41 +02:00
|
|
|
display_recipient: denmark.name,
|
2020-07-15 01:29:15 +02:00
|
|
|
flags: ["mentioned"],
|
2018-09-11 17:16:52 +02:00
|
|
|
sender_id: alice.user_id,
|
2020-04-11 02:15:41 +02:00
|
|
|
stream_id: denmark.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunch",
|
|
|
|
type: "stream",
|
2018-09-11 17:16:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
message_store.add_message_metadata(original_message);
|
2020-04-11 02:15:41 +02:00
|
|
|
message_store.set_message_booleans(original_message);
|
|
|
|
|
2020-04-11 02:51:45 +02:00
|
|
|
assert.equal(original_message.mentioned, true);
|
2020-04-11 02:15:41 +02:00
|
|
|
assert.equal(original_message.unread, true);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(stream_topic_history.get_recent_topic_names(denmark.stream_id), ["lunch"]);
|
2018-09-11 17:16:52 +02:00
|
|
|
|
2020-04-11 02:51:45 +02:00
|
|
|
unread.update_message_for_mention(original_message);
|
|
|
|
assert(unread.unread_mentions_counter.has(original_message.id));
|
|
|
|
|
2018-09-11 17:16:52 +02:00
|
|
|
const events = [
|
|
|
|
{
|
2020-04-11 02:15:41 +02:00
|
|
|
message_id: original_message.id,
|
2018-09-11 17:16:52 +02:00
|
|
|
flags: [],
|
2020-07-15 01:29:15 +02:00
|
|
|
orig_content: "old stuff",
|
|
|
|
content: "**new content**",
|
|
|
|
rendered_content: "<b>new content</b>",
|
2018-09-11 17:16:52 +02:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
current_msg_list.get_row = (message_id) => {
|
|
|
|
assert.equal(message_id, 111);
|
2020-07-15 01:29:15 +02:00
|
|
|
return ["row-stub"];
|
2018-09-11 17:16:52 +02:00
|
|
|
};
|
|
|
|
current_msg_list.view = {};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let rendered_mgs;
|
2018-09-11 17:16:52 +02:00
|
|
|
|
|
|
|
current_msg_list.view.rerender_messages = (msgs_to_rerender, message_content_edited) => {
|
|
|
|
rendered_mgs = msgs_to_rerender;
|
|
|
|
assert.equal(message_content_edited, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const side_effects = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"condense.un_cache_message_content_height",
|
|
|
|
"message_edit.end_message_row_edit",
|
|
|
|
"notifications.received_messages",
|
|
|
|
"unread_ui.update_unread_counts",
|
|
|
|
"stream_list.update_streams_sidebar",
|
|
|
|
"pm_list.update_private_messages",
|
2018-09-11 17:16:52 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
const helper = test_helper(side_effects);
|
|
|
|
|
|
|
|
page_params.realm_allow_edit_history = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
message_list.narrowed = "stub-to-ignore";
|
2018-09-11 17:16:52 +02:00
|
|
|
|
|
|
|
// TEST THIS:
|
|
|
|
message_events.update_messages(events);
|
|
|
|
|
2020-04-11 02:51:45 +02:00
|
|
|
assert(!unread.unread_mentions_counter.has(original_message.id));
|
|
|
|
|
2018-09-11 17:16:52 +02:00
|
|
|
helper.verify();
|
|
|
|
|
2020-07-16 23:29:01 +02:00
|
|
|
assert.deepEqual(rendered_mgs, [
|
2018-09-11 17:16:52 +02:00
|
|
|
{
|
|
|
|
alerted: false,
|
2020-04-11 02:15:41 +02:00
|
|
|
collapsed: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "<b>new content</b>",
|
2020-04-11 02:15:41 +02:00
|
|
|
display_recipient: denmark.name,
|
|
|
|
historical: false,
|
2018-09-11 17:16:52 +02:00
|
|
|
id: 111,
|
2020-04-11 02:15:41 +02:00
|
|
|
is_stream: true,
|
2018-09-11 17:16:52 +02:00
|
|
|
last_edit_timestamp: undefined,
|
|
|
|
mentioned: false,
|
|
|
|
mentioned_me_directly: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
raw_content: "**new content**",
|
2018-09-11 17:16:52 +02:00
|
|
|
reactions: [],
|
2020-04-11 02:15:41 +02:00
|
|
|
reply_to: alice.email,
|
|
|
|
sender_email: alice.email,
|
|
|
|
sender_full_name: alice.full_name,
|
2018-09-11 17:16:52 +02:00
|
|
|
sender_id: 32,
|
|
|
|
sent_by_me: false,
|
2020-04-11 02:15:41 +02:00
|
|
|
starred: false,
|
|
|
|
stream: denmark.name,
|
|
|
|
stream_id: denmark.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunch",
|
|
|
|
type: "stream",
|
2020-04-11 02:15:41 +02:00
|
|
|
unread: true,
|
2018-09-11 17:16:52 +02:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|