2018-08-01 21:17:03 +02:00
|
|
|
const _page_params = {};
|
|
|
|
|
|
|
|
set_global('page_params', _page_params);
|
2019-08-04 14:57:32 +02:00
|
|
|
set_global('$', global.make_zjquery());
|
2018-04-20 18:22:28 +02:00
|
|
|
zrequire('people');
|
|
|
|
zrequire('presence');
|
2018-12-19 21:16:03 +01:00
|
|
|
zrequire('user_status');
|
2019-08-04 14:57:32 +02:00
|
|
|
|
2018-04-20 18:22:28 +02:00
|
|
|
zrequire('buddy_data');
|
2019-02-28 16:09:03 +01:00
|
|
|
set_global('timerender', {});
|
2018-04-20 18:22:28 +02:00
|
|
|
|
|
|
|
// The buddy_data module is mostly tested indirectly through
|
|
|
|
// activity.js, but we should feel free to add direct tests
|
|
|
|
// here.
|
|
|
|
|
2018-12-18 19:25:14 +01:00
|
|
|
const selma = {
|
|
|
|
user_id: 1000,
|
|
|
|
full_name: 'Human Selma',
|
|
|
|
email: 'selma@example.com',
|
|
|
|
};
|
|
|
|
|
|
|
|
const me = {
|
|
|
|
user_id: 1001,
|
|
|
|
full_name: 'Human Myself',
|
|
|
|
email: 'self@example.com',
|
|
|
|
};
|
|
|
|
|
2019-02-28 16:09:03 +01:00
|
|
|
const old_user = {
|
|
|
|
user_id: 9999,
|
|
|
|
full_name: 'Old User',
|
|
|
|
email: 'old_user@example.com',
|
|
|
|
};
|
|
|
|
|
2018-12-18 19:25:14 +01:00
|
|
|
const bot = {
|
|
|
|
user_id: 55555,
|
|
|
|
full_name: 'Red Herring Bot',
|
|
|
|
email: 'bot@example.com',
|
|
|
|
is_bot: true,
|
2019-08-04 14:57:32 +02:00
|
|
|
bot_owner_id: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const bot_with_owner = {
|
|
|
|
user_id: 55556,
|
|
|
|
full_name: 'Blue Herring Bot',
|
|
|
|
email: 'bot_with_owner@example.com',
|
|
|
|
is_bot: true,
|
|
|
|
bot_owner_id: 1001,
|
|
|
|
bot_owner_full_name: 'Human Myself',
|
2018-12-18 19:25:14 +01:00
|
|
|
};
|
|
|
|
|
2018-09-08 14:41:41 +02:00
|
|
|
function make_people() {
|
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 i of _.range(1002, 2000)) {
|
2018-04-20 18:22:28 +02:00
|
|
|
const person = {
|
|
|
|
user_id: i,
|
2018-04-23 23:13:52 +02:00
|
|
|
full_name: `Human ${i}`,
|
2018-04-20 18:22:28 +02:00
|
|
|
email: `person${i}@example.com`,
|
|
|
|
};
|
2020-03-21 20:19:30 +01:00
|
|
|
people.add(person);
|
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-08 14:41:41 +02:00
|
|
|
|
2020-03-21 20:19:30 +01:00
|
|
|
people.add(bot);
|
|
|
|
people.add(bot_with_owner);
|
|
|
|
people.add(selma);
|
|
|
|
people.add(me);
|
|
|
|
people.add(old_user);
|
2018-12-18 18:50:58 +01:00
|
|
|
|
2018-12-18 19:25:14 +01:00
|
|
|
people.initialize_current_user(me.user_id);
|
2018-09-08 14:41:41 +02:00
|
|
|
}
|
|
|
|
|
2018-12-18 19:25:14 +01:00
|
|
|
function activate_people() {
|
2020-02-07 17:27:47 +01:00
|
|
|
presence.presence_info.clear();
|
|
|
|
|
|
|
|
function set_presence(user_id, status) {
|
|
|
|
presence.presence_info.set(user_id, {
|
|
|
|
status: status,
|
|
|
|
last_active: 9999,
|
|
|
|
});
|
|
|
|
}
|
2018-04-20 18:22:28 +02:00
|
|
|
|
|
|
|
// Make 400 of the users active
|
2020-02-07 17:27:47 +01:00
|
|
|
set_presence(selma.user_id, 'active');
|
|
|
|
set_presence(me.user_id, 'active');
|
2018-12-18 19:25:14 +01:00
|
|
|
|
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 user_id of _.range(1000, 1400)) {
|
2020-02-07 17:27:47 +01:00
|
|
|
set_presence(user_id, 'active');
|
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-12-18 19:25:14 +01:00
|
|
|
|
2018-04-20 18:22:28 +02:00
|
|
|
// And then 300 not active
|
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 user_id of _.range(1400, 1700)) {
|
2020-02-07 17:27:47 +01:00
|
|
|
set_presence(user_id, 'offline');
|
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-12-18 19:25:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
make_people();
|
|
|
|
activate_people();
|
|
|
|
|
2019-02-17 02:10:42 +01:00
|
|
|
run_test('user_circle', () => {
|
|
|
|
assert.equal(buddy_data.get_user_circle_class(selma.user_id), 'user_circle_green');
|
|
|
|
user_status.set_away(selma.user_id);
|
2019-03-01 19:06:23 +01:00
|
|
|
assert.equal(buddy_data.get_user_circle_class(selma.user_id), 'user_circle_empty_line');
|
2019-02-17 02:10:42 +01:00
|
|
|
user_status.revoke_away(selma.user_id);
|
|
|
|
assert.equal(buddy_data.get_user_circle_class(selma.user_id), 'user_circle_green');
|
|
|
|
|
|
|
|
assert.equal(buddy_data.get_user_circle_class(me.user_id), 'user_circle_green');
|
|
|
|
user_status.set_away(me.user_id);
|
2019-03-01 19:06:23 +01:00
|
|
|
assert.equal(buddy_data.get_user_circle_class(me.user_id), 'user_circle_empty_line');
|
2019-02-17 02:10:42 +01:00
|
|
|
user_status.revoke_away(me.user_id);
|
|
|
|
assert.equal(buddy_data.get_user_circle_class(me.user_id), 'user_circle_green');
|
|
|
|
});
|
|
|
|
|
2018-12-19 21:16:03 +01:00
|
|
|
run_test('buddy_status', () => {
|
|
|
|
assert.equal(buddy_data.buddy_status(selma.user_id), 'active');
|
|
|
|
user_status.set_away(selma.user_id);
|
|
|
|
assert.equal(buddy_data.buddy_status(selma.user_id), 'away_them');
|
|
|
|
user_status.revoke_away(selma.user_id);
|
|
|
|
assert.equal(buddy_data.buddy_status(selma.user_id), 'active');
|
|
|
|
|
|
|
|
assert.equal(buddy_data.buddy_status(me.user_id), 'active');
|
|
|
|
user_status.set_away(me.user_id);
|
|
|
|
assert.equal(buddy_data.buddy_status(me.user_id), 'away_me');
|
|
|
|
user_status.revoke_away(me.user_id);
|
|
|
|
assert.equal(buddy_data.buddy_status(me.user_id), 'active');
|
|
|
|
});
|
|
|
|
|
2019-08-04 14:57:32 +02:00
|
|
|
run_test('title_data', () => {
|
|
|
|
// Groups
|
2019-11-02 00:06:25 +01:00
|
|
|
let is_group = true;
|
|
|
|
const user_ids_string = "9999,1000";
|
|
|
|
let expected_group_data = {
|
2019-11-20 23:39:10 +01:00
|
|
|
first_line: 'Human Selma, Old User',
|
|
|
|
second_line: '',
|
|
|
|
third_line: '',
|
2019-08-04 14:57:32 +02:00
|
|
|
};
|
|
|
|
assert.deepEqual(buddy_data.get_title_data(user_ids_string, is_group), expected_group_data);
|
|
|
|
|
|
|
|
is_group = '';
|
|
|
|
|
|
|
|
// Bots with owners.
|
|
|
|
expected_group_data = {
|
2019-11-20 23:39:10 +01:00
|
|
|
first_line: 'Blue Herring Bot',
|
|
|
|
second_line: 'translated: Owner: Human Myself',
|
|
|
|
third_line: '',
|
2019-08-04 14:57:32 +02:00
|
|
|
};
|
|
|
|
assert.deepEqual(buddy_data.get_title_data(bot_with_owner.user_id, is_group),
|
|
|
|
expected_group_data);
|
|
|
|
|
|
|
|
// Bots without owners.
|
|
|
|
expected_group_data = {
|
2019-11-20 23:39:10 +01:00
|
|
|
first_line: 'Red Herring Bot',
|
|
|
|
second_line: '',
|
|
|
|
third_line: '',
|
2019-08-04 14:57:32 +02:00
|
|
|
};
|
|
|
|
assert.deepEqual(buddy_data.get_title_data(bot.user_id, is_group), expected_group_data);
|
|
|
|
|
|
|
|
// Individual Users.
|
2019-01-25 16:40:18 +01:00
|
|
|
user_status.set_status_text({
|
|
|
|
user_id: me.user_id,
|
|
|
|
status_text: 'out to lunch',
|
|
|
|
});
|
2019-08-04 14:57:32 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let expected_data = {
|
2019-11-20 23:39:10 +01:00
|
|
|
first_line: 'Human Myself',
|
|
|
|
second_line: 'out to lunch',
|
|
|
|
third_line: 'translated: Active now',
|
|
|
|
|
2019-08-04 14:57:32 +02:00
|
|
|
};
|
|
|
|
assert.deepEqual(buddy_data.get_title_data(me.user_id, is_group), expected_data);
|
|
|
|
|
|
|
|
expected_data = {
|
2019-11-20 23:39:10 +01:00
|
|
|
first_line: 'Old User',
|
|
|
|
second_line: 'translated: Last active: translated: More than 2 weeks ago',
|
|
|
|
third_line: '',
|
2019-08-04 14:57:32 +02:00
|
|
|
};
|
|
|
|
assert.deepEqual(buddy_data.get_title_data(old_user.user_id, is_group), expected_data);
|
2019-01-25 16:40:18 +01:00
|
|
|
});
|
|
|
|
|
2018-12-18 19:25:14 +01:00
|
|
|
run_test('simple search', () => {
|
|
|
|
const user_ids = buddy_data.get_filtered_and_sorted_user_ids('sel');
|
|
|
|
|
2018-12-18 19:29:08 +01:00
|
|
|
assert.deepEqual(user_ids, [selma.user_id]);
|
2018-06-02 01:42:05 +02:00
|
|
|
});
|
2018-04-20 18:22:28 +02:00
|
|
|
|
2018-12-18 19:25:14 +01:00
|
|
|
run_test('bulk_data_hacks', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_ids;
|
2018-04-20 18:22:28 +02:00
|
|
|
|
2018-04-23 23:13:52 +02:00
|
|
|
// Even though we have 1000 users, we only get the 400 active
|
2018-04-20 18:22:28 +02:00
|
|
|
// users. This is a consequence of buddy_data.maybe_shrink_list.
|
2018-04-23 23:13:52 +02:00
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids();
|
2018-04-20 18:22:28 +02:00
|
|
|
assert.equal(user_ids.length, 400);
|
2018-04-23 23:13:52 +02:00
|
|
|
|
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids('');
|
|
|
|
assert.equal(user_ids.length, 400);
|
|
|
|
|
2018-12-18 19:25:14 +01:00
|
|
|
// We don't match on "so", because it's not at the start of a
|
2018-04-23 23:13:52 +02:00
|
|
|
// word in the name/email.
|
2018-12-18 19:25:14 +01:00
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids('so');
|
2018-04-23 23:13:52 +02:00
|
|
|
assert.equal(user_ids.length, 0);
|
|
|
|
|
|
|
|
// We match on "h" for the first name, and the result limit
|
2018-12-18 19:29:08 +01:00
|
|
|
// is relaxed for searches. (We exclude "me", though.)
|
2018-04-23 23:13:52 +02:00
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids('h');
|
2018-12-18 19:29:08 +01:00
|
|
|
assert.equal(user_ids.length, 999);
|
2018-04-23 23:13:52 +02:00
|
|
|
|
|
|
|
// We match on "p" for the email.
|
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids('p');
|
2018-12-18 19:25:14 +01:00
|
|
|
assert.equal(user_ids.length, 998);
|
2018-04-23 23:13:52 +02:00
|
|
|
|
|
|
|
// Make our shrink limit higher, and go back to an empty search.
|
|
|
|
// We won't get all 1000 users, just the present ones.
|
|
|
|
buddy_data.max_size_before_shrinking = 50000;
|
|
|
|
|
|
|
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids('');
|
|
|
|
assert.equal(user_ids.length, 700);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-12-20 15:55:20 +01:00
|
|
|
|
2018-12-21 20:10:27 +01:00
|
|
|
run_test('level', () => {
|
|
|
|
assert.equal(buddy_data.my_user_status(me.user_id), 'translated: (you)');
|
|
|
|
user_status.set_away(me.user_id);
|
2019-02-14 10:14:35 +01:00
|
|
|
assert.equal(buddy_data.my_user_status(me.user_id), 'translated: (unavailable)');
|
2018-12-21 20:10:27 +01:00
|
|
|
user_status.revoke_away(me.user_id);
|
|
|
|
assert.equal(buddy_data.my_user_status(me.user_id), 'translated: (you)');
|
|
|
|
});
|
|
|
|
|
2018-12-20 15:55:20 +01:00
|
|
|
run_test('level', () => {
|
2020-02-06 04:21:07 +01:00
|
|
|
presence.presence_info.clear();
|
2018-12-20 15:55:20 +01:00
|
|
|
assert.equal(buddy_data.level(me.user_id), 0);
|
|
|
|
assert.equal(buddy_data.level(selma.user_id), 3);
|
|
|
|
|
|
|
|
const server_time = 9999;
|
|
|
|
const info = {
|
|
|
|
website: {
|
|
|
|
status: "active",
|
|
|
|
timestamp: server_time,
|
|
|
|
},
|
|
|
|
};
|
2020-02-07 17:19:03 +01:00
|
|
|
presence.update_info_from_event(me.user_id, info, server_time);
|
|
|
|
presence.update_info_from_event(selma.user_id, info, server_time);
|
2018-12-20 15:55:20 +01:00
|
|
|
|
|
|
|
assert.equal(buddy_data.level(me.user_id), 0);
|
|
|
|
assert.equal(buddy_data.level(selma.user_id), 1);
|
|
|
|
|
|
|
|
user_status.set_away(me.user_id);
|
|
|
|
user_status.set_away(selma.user_id);
|
|
|
|
|
2018-12-19 21:16:03 +01:00
|
|
|
// Selma gets demoted to level 3, but "me"
|
|
|
|
// stays in level 0.
|
2018-12-20 15:55:20 +01:00
|
|
|
assert.equal(buddy_data.level(me.user_id), 0);
|
2018-12-19 21:16:03 +01:00
|
|
|
assert.equal(buddy_data.level(selma.user_id), 3);
|
2018-12-20 15:55:20 +01:00
|
|
|
});
|
2019-02-28 16:09:03 +01:00
|
|
|
|
|
|
|
run_test('user_last_seen_time_status', () => {
|
|
|
|
assert.equal(buddy_data.user_last_seen_time_status(selma.user_id),
|
2019-03-23 15:53:21 +01:00
|
|
|
'translated: Active now');
|
2019-02-28 16:09:03 +01:00
|
|
|
|
|
|
|
page_params.realm_is_zephyr_mirror_realm = true;
|
|
|
|
assert.equal(buddy_data.user_last_seen_time_status(old_user.user_id),
|
|
|
|
'translated: Unknown');
|
|
|
|
page_params.realm_is_zephyr_mirror_realm = false;
|
|
|
|
assert.equal(buddy_data.user_last_seen_time_status(old_user.user_id),
|
|
|
|
'translated: More than 2 weeks ago');
|
|
|
|
|
|
|
|
presence.last_active_date = (user_id) => {
|
|
|
|
assert.equal(user_id, old_user.user_id);
|
|
|
|
|
|
|
|
return {
|
|
|
|
clone: () => 'date-stub',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
timerender.last_seen_status_from_date = (date) => {
|
|
|
|
assert.equal(date, 'date-stub');
|
|
|
|
return 'May 12';
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.equal(buddy_data.user_last_seen_time_status(old_user.user_id),
|
|
|
|
'May 12');
|
|
|
|
|
|
|
|
});
|
2020-02-04 15:38:37 +01:00
|
|
|
|
|
|
|
run_test('error handling', () => {
|
|
|
|
presence.get_user_ids = () => [42];
|
2020-04-03 17:18:04 +02:00
|
|
|
blueslip.expect('error', 'Unknown user_id in get_by_user_id: 42');
|
|
|
|
blueslip.expect('warn', 'Got user_id in presence but not people: 42');
|
2020-02-04 15:38:37 +01:00
|
|
|
buddy_data.get_filtered_and_sorted_user_ids();
|
|
|
|
});
|