mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 14:03:30 +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>
216 lines
6.3 KiB
JavaScript
216 lines
6.3 KiB
JavaScript
set_global('i18n', global.stub_i18n);
|
|
|
|
zrequire('keydown_util');
|
|
zrequire('components');
|
|
|
|
const noop = function () {};
|
|
|
|
const LEFT_KEY = { which: 37, preventDefault: noop, stopPropagation: noop };
|
|
const RIGHT_KEY = { which: 39, preventDefault: noop, stopPropagation: noop };
|
|
|
|
run_test('basics', () => {
|
|
let keydown_f;
|
|
let click_f;
|
|
const tabs = [];
|
|
let focused_tab;
|
|
let callback_args;
|
|
|
|
function make_tab(i) {
|
|
const self = {};
|
|
|
|
assert.equal(tabs.length, i);
|
|
|
|
self.stub = true;
|
|
self.class = [];
|
|
|
|
self.addClass = function (c) {
|
|
self.class += ' ' + c;
|
|
const tokens = self.class.trim().split(/ +/);
|
|
self.class = _.uniq(tokens).join(' ');
|
|
};
|
|
|
|
self.removeClass = function (c) {
|
|
const tokens = self.class.trim().split(/ +/);
|
|
self.class = _.without(tokens, c).join(' ');
|
|
};
|
|
|
|
self.hasClass = function (c) {
|
|
const tokens = self.class.trim().split(/ +/);
|
|
return tokens.includes(c);
|
|
};
|
|
|
|
self.data = function (name) {
|
|
assert.equal(name, 'tab-id');
|
|
return i;
|
|
};
|
|
|
|
self.focus = function () {
|
|
focused_tab = i;
|
|
};
|
|
|
|
tabs.push(self);
|
|
|
|
return self;
|
|
}
|
|
|
|
const ind_tab = (function () {
|
|
const self = {};
|
|
|
|
self.stub = true;
|
|
|
|
self.click = function (f) {
|
|
click_f = f;
|
|
};
|
|
|
|
self.keydown = function (f) {
|
|
keydown_f = f;
|
|
};
|
|
|
|
self.removeClass = function (c) {
|
|
for (const tab of tabs) {
|
|
tab.removeClass(c);
|
|
}
|
|
};
|
|
|
|
self.eq = function (idx) {
|
|
return tabs[idx];
|
|
};
|
|
|
|
return self;
|
|
}());
|
|
|
|
const switcher = (function () {
|
|
const self = {};
|
|
|
|
self.stub = true;
|
|
|
|
self.children = [];
|
|
|
|
self.append = function (child) {
|
|
self.children.push(child);
|
|
};
|
|
|
|
self.find = function (sel) {
|
|
switch (sel) {
|
|
case ".ind-tab":
|
|
return ind_tab;
|
|
default:
|
|
throw Error('unknown selector: ' + sel);
|
|
}
|
|
};
|
|
|
|
return self;
|
|
}());
|
|
|
|
set_global('$', function (sel) {
|
|
if (sel.stub) {
|
|
// The component often redundantly re-wraps objects.
|
|
return sel;
|
|
}
|
|
|
|
switch (sel) {
|
|
case "<div class='tab-switcher'></div>":
|
|
return switcher;
|
|
case "<div class='ind-tab' data-tab-key='keyboard-shortcuts' data-tab-id='0' tabindex='0'>translated: Keyboard shortcuts</div>":
|
|
return make_tab(0);
|
|
case "<div class='ind-tab' data-tab-key='message-formatting' data-tab-id='1' tabindex='0'>translated: Message formatting</div>":
|
|
return make_tab(1);
|
|
case "<div class='ind-tab' data-tab-key='search-operators' data-tab-id='2' tabindex='0'>translated: Search operators</div>":
|
|
return make_tab(2);
|
|
default:
|
|
throw Error('unknown selector: ' + sel);
|
|
}
|
|
});
|
|
|
|
let callback_value;
|
|
|
|
let widget = null;
|
|
widget = components.toggle({
|
|
selected: 0,
|
|
values: [
|
|
{ label: i18n.t("Keyboard shortcuts"), key: "keyboard-shortcuts" },
|
|
{ label: i18n.t("Message formatting"), key: "message-formatting" },
|
|
{ label: i18n.t("Search operators"), key: "search-operators" },
|
|
],
|
|
callback: function (name, key) {
|
|
assert.equal(callback_args, undefined);
|
|
callback_args = [name, key];
|
|
|
|
// The subs code tries to get a widget value in the middle of a
|
|
// callback, which can lead to obscure bugs.
|
|
if (widget) {
|
|
callback_value = widget.value();
|
|
}
|
|
},
|
|
});
|
|
|
|
assert.equal(widget.get(), switcher);
|
|
|
|
assert.deepEqual(switcher.children, tabs);
|
|
|
|
assert.equal(focused_tab, 0);
|
|
assert.equal(tabs[0].class, 'first selected');
|
|
assert.equal(tabs[1].class, 'middle');
|
|
assert.equal(tabs[2].class, 'last');
|
|
assert.deepEqual(callback_args, ['translated: Keyboard shortcuts', 'keyboard-shortcuts']);
|
|
assert.equal(widget.value(), 'translated: Keyboard shortcuts');
|
|
|
|
callback_args = undefined;
|
|
|
|
widget.goto('message-formatting');
|
|
assert.equal(focused_tab, 1);
|
|
assert.equal(tabs[0].class, 'first');
|
|
assert.equal(tabs[1].class, 'middle selected');
|
|
assert.equal(tabs[2].class, 'last');
|
|
assert.deepEqual(callback_args, ['translated: Message formatting', 'message-formatting']);
|
|
assert.equal(widget.value(), 'translated: Message formatting');
|
|
|
|
// Go to same tab twice and make sure we get callback.
|
|
callback_args = undefined;
|
|
widget.goto('message-formatting');
|
|
assert.deepEqual(callback_args, ['translated: Message formatting', 'message-formatting']);
|
|
|
|
callback_args = undefined;
|
|
keydown_f.call(tabs[focused_tab], RIGHT_KEY);
|
|
assert.equal(focused_tab, 2);
|
|
assert.equal(tabs[0].class, 'first');
|
|
assert.equal(tabs[1].class, 'middle');
|
|
assert.equal(tabs[2].class, 'last selected');
|
|
assert.deepEqual(callback_args, ['translated: Search operators', 'search-operators']);
|
|
assert.equal(widget.value(), 'translated: Search operators');
|
|
assert.equal(widget.value(), callback_value);
|
|
|
|
// try to crash the key handler
|
|
keydown_f.call(tabs[focused_tab], RIGHT_KEY);
|
|
assert.equal(widget.value(), 'translated: Search operators');
|
|
|
|
callback_args = undefined;
|
|
|
|
keydown_f.call(tabs[focused_tab], LEFT_KEY);
|
|
assert.equal(widget.value(), 'translated: Message formatting');
|
|
|
|
callback_args = undefined;
|
|
|
|
keydown_f.call(tabs[focused_tab], LEFT_KEY);
|
|
assert.equal(widget.value(), 'translated: Keyboard shortcuts');
|
|
|
|
// try to crash the key handler
|
|
keydown_f.call(tabs[focused_tab], LEFT_KEY);
|
|
assert.equal(widget.value(), 'translated: Keyboard shortcuts');
|
|
|
|
callback_args = undefined;
|
|
|
|
click_f.call(tabs[1]);
|
|
assert.equal(widget.value(), 'translated: Message formatting');
|
|
|
|
callback_args = undefined;
|
|
widget.disable_tab("search-operators");
|
|
assert.equal(tabs[2].hasClass('disabled'), true);
|
|
assert.equal(tabs[2].class, "last disabled");
|
|
|
|
widget.goto('keyboard-shortcuts');
|
|
assert.equal(focused_tab, 0);
|
|
widget.goto("search-operators");
|
|
assert.equal(focused_tab, 0);
|
|
});
|