2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-29 01:37:13 +02:00
|
|
|
const Handlebars = require("handlebars/runtime");
|
|
|
|
|
2020-05-26 14:25:21 +02:00
|
|
|
const huddle_data = require("./huddle_data");
|
2020-07-24 06:02:07 +02:00
|
|
|
const settings_data = require("./settings_data");
|
2020-02-25 12:46:14 +01:00
|
|
|
|
2019-12-25 16:58:11 +01:00
|
|
|
exports.max_num_of_search_results = 12;
|
|
|
|
|
2013-07-30 23:02:10 +02:00
|
|
|
function stream_matches_query(stream_name, q) {
|
2018-06-25 17:14:45 +02:00
|
|
|
return common.phrase_match(q, stream_name);
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
2019-12-24 15:31:52 +01:00
|
|
|
function make_person_highlighter(query) {
|
|
|
|
const hilite = typeahead_helper.make_query_highlighter(query);
|
|
|
|
|
|
|
|
return function (person) {
|
2020-02-25 12:46:14 +01:00
|
|
|
if (settings_data.show_email()) {
|
2019-12-24 15:31:52 +01:00
|
|
|
return hilite(person.full_name) + " <" + hilite(person.email) + ">";
|
|
|
|
}
|
|
|
|
return hilite(person.full_name);
|
|
|
|
};
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
function match_criteria(operators, criteria) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const filter = new Filter(operators);
|
2020-07-02 01:39:34 +02:00
|
|
|
return criteria.some((cr) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (Object.prototype.hasOwnProperty.call(cr, "operand")) {
|
2017-06-03 04:32:25 +02:00
|
|
|
return filter.has_operand(cr.operator, cr.operand);
|
|
|
|
}
|
|
|
|
return filter.has_operator(cr.operator);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-01 15:00:00 +02:00
|
|
|
function check_validity(last, operators, valid, invalid) {
|
|
|
|
// valid: list of strings valid for the last operator
|
|
|
|
// invalid: list of operators invalid for any previous operators except last.
|
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-08 04:55:06 +01:00
|
|
|
if (!valid.includes(last.operator)) {
|
2018-06-01 15:00:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (match_criteria(operators, invalid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-01 15:59:17 +02:00
|
|
|
function format_as_suggestion(terms) {
|
|
|
|
return {
|
|
|
|
description: Filter.describe(terms),
|
|
|
|
search_string: Filter.unparse(terms),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-05 02:58:05 +02:00
|
|
|
function compare_by_huddle(huddle) {
|
2020-07-02 01:39:34 +02:00
|
|
|
huddle = huddle.slice(0, -1).map((person) => {
|
2017-07-05 02:58:05 +02:00
|
|
|
person = people.get_by_email(person);
|
js: Convert _.map(a, …) to a.map(…).
And convert the corresponding function expressions to arrow style
while we’re here.
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, {
visitCallExpression(path) {
const { callee, arguments: args } = path.node;
if (
n.MemberExpression.check(callee) &&
!callee.computed &&
n.Identifier.check(callee.object) &&
callee.object.name === "_" &&
n.Identifier.check(callee.property) &&
callee.property.name === "map" &&
args.length === 2 &&
checkExpression(args[0]) &&
checkExpression(args[1])
) {
const [arr, fn] = args;
path.replace(
b.callExpression(b.memberExpression(arr, b.identifier("map")), [
n.FunctionExpression.check(fn) ||
n.ArrowFunctionExpression.check(fn)
? b.arrowFunctionExpression(
fn.params,
n.BlockStatement.check(fn.body) &&
fn.body.body.length === 1 &&
n.ReturnStatement.check(fn.body.body[0])
? fn.body.body[0].argument || b.identifier("undefined")
: fn.body
)
: fn,
])
);
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-08 02:43:49 +01:00
|
|
|
return person && person.user_id;
|
2017-07-05 02:58:05 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Construct dict for all huddles, so we can lookup each's recency
|
2020-05-26 14:25:21 +02:00
|
|
|
const huddles = huddle_data.get_huddles();
|
2020-02-12 07:05:22 +01:00
|
|
|
const huddle_dict = new Map();
|
|
|
|
for (const [i, huddle] of huddles.entries()) {
|
|
|
|
huddle_dict.set(huddle, i + 1);
|
2017-07-05 02:58:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return function (person1, person2) {
|
2020-05-26 18:46:23 +02:00
|
|
|
const huddle1 = people.concat_huddle(huddle, person1.user_id);
|
|
|
|
const huddle2 = people.concat_huddle(huddle, person2.user_id);
|
2017-07-05 02:58:05 +02:00
|
|
|
|
|
|
|
// If not in the dict, assign an arbitrarily high index
|
2020-02-12 07:05:22 +01:00
|
|
|
const score1 = huddle_dict.get(huddle1) || huddles.length + 1;
|
|
|
|
const score2 = huddle_dict.get(huddle2) || huddles.length + 1;
|
2019-11-02 00:06:25 +01:00
|
|
|
const diff = score1 - score2;
|
2017-07-05 02:58:05 +02:00
|
|
|
|
|
|
|
if (diff !== 0) {
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
return typeahead_helper.compare_by_pms(person1, person2);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-03 07:10:13 +02:00
|
|
|
function get_stream_suggestions(last, operators) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const valid = ["stream", "search", ""];
|
2019-11-02 00:06:25 +01:00
|
|
|
const invalid = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "stream"},
|
|
|
|
{operator: "streams"},
|
|
|
|
{operator: "is", operand: "private"},
|
|
|
|
{operator: "pm-with"},
|
2017-06-03 07:10:13 +02:00
|
|
|
];
|
2018-06-01 15:00:00 +02:00
|
|
|
if (!check_validity(last, operators, valid, invalid)) {
|
2013-07-30 23:02:10 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const query = last.operand;
|
|
|
|
let streams = stream_data.subscribed_streams();
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
streams = streams.filter((stream) => stream_matches_query(stream, query));
|
2013-07-30 23:02:10 +02:00
|
|
|
|
|
|
|
streams = typeahead_helper.sorter(query, streams);
|
|
|
|
|
2019-12-25 14:43:48 +01:00
|
|
|
const regex = typeahead_helper.build_highlight_regex(query);
|
|
|
|
const hilite = typeahead_helper.highlight_with_escaping_and_regex;
|
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
const objs = streams.map((stream) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const prefix = "stream";
|
2019-12-25 14:43:48 +01:00
|
|
|
const highlighted_stream = hilite(regex, stream);
|
2020-07-15 01:29:15 +02:00
|
|
|
const verb = last.negated ? "exclude " : "";
|
|
|
|
const description = verb + prefix + " " + highlighted_stream;
|
2019-11-02 00:06:25 +01:00
|
|
|
const term = {
|
2020-07-15 01:29:15 +02:00
|
|
|
operator: "stream",
|
2017-01-12 00:17:43 +01:00
|
|
|
operand: stream,
|
2018-05-21 06:23:08 +02:00
|
|
|
negated: last.negated,
|
2014-02-06 16:35:46 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const search_string = Filter.unparse([term]);
|
2020-07-20 22:18:43 +02:00
|
|
|
return {description, search_string};
|
2013-07-30 23:02:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return objs;
|
|
|
|
}
|
|
|
|
|
2019-12-23 22:42:45 +01:00
|
|
|
function get_group_suggestions(last, operators) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (!check_validity(last, operators, ["pm-with"], [{operator: "stream"}])) {
|
2017-02-05 22:11:18 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const operand = last.operand;
|
|
|
|
const negated = last.negated;
|
2017-02-05 22:11:18 +01:00
|
|
|
|
|
|
|
// The operand has the form "part1,part2,pa", where all but the last part
|
|
|
|
// are emails, and the last part is an arbitrary query.
|
|
|
|
//
|
|
|
|
// We only generate group suggestions when there's more than one part, and
|
|
|
|
// we only use the last part to generate suggestions.
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const last_comma_index = operand.lastIndexOf(",");
|
2017-02-05 22:11:18 +01:00
|
|
|
if (last_comma_index < 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Neither all_but_last_part nor last_part include the final comma.
|
2019-10-26 00:11:05 +02:00
|
|
|
const all_but_last_part = operand.slice(0, last_comma_index);
|
|
|
|
const last_part = operand.slice(last_comma_index + 1);
|
2017-02-05 22:11:18 +01:00
|
|
|
|
|
|
|
// We don't suggest a person if their email is already present in the
|
|
|
|
// operand (not including the last part).
|
2020-07-15 01:29:15 +02:00
|
|
|
const parts = all_but_last_part.split(",").concat(people.my_current_email());
|
2019-12-23 22:42:45 +01:00
|
|
|
|
|
|
|
const person_matcher = people.build_person_matcher(last_part);
|
2020-07-02 01:45:54 +02:00
|
|
|
let persons = people.filter_all_persons((person) => {
|
2020-02-08 04:04:36 +01:00
|
|
|
if (parts.includes(person.email)) {
|
2017-02-05 22:11:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
return last_part === "" || person_matcher(person);
|
2017-02-05 22:11:18 +01:00
|
|
|
});
|
|
|
|
|
2017-07-05 02:58:05 +02:00
|
|
|
persons.sort(compare_by_huddle(parts));
|
2017-02-05 22:11:18 +01:00
|
|
|
|
2017-06-23 08:39:37 +02:00
|
|
|
// Take top 15 persons, since they're ordered by pm_recipient_count.
|
|
|
|
persons = persons.slice(0, 15);
|
2017-02-05 22:11:18 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const prefix = Filter.operator_to_prefix("pm-with", negated);
|
2017-02-05 22:11:18 +01:00
|
|
|
|
2019-12-24 15:31:52 +01:00
|
|
|
const highlight_person = make_person_highlighter(last_part);
|
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
const suggestions = persons.map((person) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const term = {
|
2020-07-15 01:29:15 +02:00
|
|
|
operator: "pm-with",
|
|
|
|
operand: all_but_last_part + "," + person.email,
|
2020-07-20 22:18:43 +02:00
|
|
|
negated,
|
2017-02-05 22:11:18 +01:00
|
|
|
};
|
2019-12-24 15:31:52 +01:00
|
|
|
const name = highlight_person(person);
|
2020-07-15 00:34:28 +02:00
|
|
|
const description =
|
|
|
|
prefix + " " + Handlebars.Utils.escapeExpression(all_but_last_part) + "," + name;
|
2019-11-02 00:06:25 +01:00
|
|
|
let terms = [term];
|
2017-02-05 22:11:18 +01:00
|
|
|
if (negated) {
|
2020-07-15 01:29:15 +02:00
|
|
|
terms = [{operator: "is", operand: "private"}, term];
|
2017-02-05 22:11:18 +01:00
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const search_string = Filter.unparse(terms);
|
2020-07-20 22:18:43 +02:00
|
|
|
return {description, search_string};
|
2017-02-05 22:11:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
2020-01-02 12:53:11 +01:00
|
|
|
function make_people_getter(last) {
|
|
|
|
let persons;
|
|
|
|
|
|
|
|
/* The next function will be called between 0 and 4
|
|
|
|
times for each keystroke in a search, but we will
|
|
|
|
only do real work one time.
|
|
|
|
*/
|
|
|
|
return function () {
|
|
|
|
if (persons !== undefined) {
|
|
|
|
return persons;
|
|
|
|
}
|
|
|
|
|
|
|
|
let query;
|
|
|
|
|
|
|
|
// This next block is designed to match the behavior of the
|
|
|
|
// `is:private` block in get_person_suggestions
|
|
|
|
if (last.operator === "is" && last.operand === "private") {
|
2020-07-15 01:29:15 +02:00
|
|
|
query = "";
|
2020-01-02 12:53:11 +01:00
|
|
|
} else {
|
|
|
|
query = last.operand;
|
|
|
|
}
|
|
|
|
|
|
|
|
persons = people.get_people_for_search_bar(query);
|
|
|
|
persons.sort(typeahead_helper.compare_by_pms);
|
|
|
|
return persons;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-03 09:19:57 +02:00
|
|
|
// Possible args for autocomplete_operator: pm-with, sender, from
|
2020-01-02 12:53:11 +01:00
|
|
|
function get_person_suggestions(people_getter, last, operators, autocomplete_operator) {
|
2017-06-03 09:19:57 +02:00
|
|
|
if (last.operator === "is" && last.operand === "private") {
|
|
|
|
// Interpret 'is:private' as equivalent to 'pm-with:'
|
|
|
|
last = {operator: "pm-with", operand: "", negated: false};
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const query = last.operand;
|
2017-06-03 09:19:57 +02:00
|
|
|
|
|
|
|
// Be especially strict about the less common "from" operator.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (autocomplete_operator === "from" && last.operator !== "from") {
|
2017-06-03 09:19:57 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const valid = ["search", autocomplete_operator];
|
2019-11-02 00:06:25 +01:00
|
|
|
let invalid;
|
2020-07-15 01:29:15 +02:00
|
|
|
if (autocomplete_operator === "pm-with") {
|
|
|
|
invalid = [{operator: "pm-with"}, {operator: "stream"}];
|
2017-06-03 09:19:57 +02:00
|
|
|
} else {
|
|
|
|
// If not pm-with, then this must either be 'sender' or 'from'
|
2020-07-15 01:29:15 +02:00
|
|
|
invalid = [{operator: "sender"}, {operator: "from"}];
|
2017-06-03 09:19:57 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 15:00:00 +02:00
|
|
|
if (!check_validity(last, operators, valid, invalid)) {
|
2013-07-30 23:02:10 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-01-02 12:53:11 +01:00
|
|
|
const persons = people_getter();
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const prefix = Filter.operator_to_prefix(autocomplete_operator, last.negated);
|
2013-10-07 20:48:25 +02:00
|
|
|
|
2019-12-24 15:31:52 +01:00
|
|
|
const highlight_person = make_person_highlighter(query);
|
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
const objs = persons.map((person) => {
|
2019-12-24 15:31:52 +01:00
|
|
|
const name = highlight_person(person);
|
2020-07-15 01:29:15 +02:00
|
|
|
const description = prefix + " " + name;
|
2020-07-15 00:34:28 +02:00
|
|
|
const terms = [
|
|
|
|
{
|
|
|
|
operator: autocomplete_operator,
|
|
|
|
operand: person.email,
|
|
|
|
negated: last.negated,
|
|
|
|
},
|
|
|
|
];
|
2020-07-15 01:29:15 +02:00
|
|
|
if (autocomplete_operator === "pm-with" && last.negated) {
|
2017-06-03 09:19:57 +02:00
|
|
|
// In the special case of '-pm-with', add 'is:private' before it
|
|
|
|
// because we assume the user still wants to narrow to PMs
|
2020-07-15 01:29:15 +02:00
|
|
|
terms.unshift({operator: "is", operand: "private"});
|
2017-06-03 09:19:57 +02:00
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const search_string = Filter.unparse(terms);
|
2020-07-20 22:18:43 +02:00
|
|
|
return {description, search_string};
|
2013-07-30 23:02:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return objs;
|
|
|
|
}
|
|
|
|
|
2013-08-06 19:08:39 +02:00
|
|
|
function get_default_suggestion(operators) {
|
2020-06-13 16:34:23 +02:00
|
|
|
// Here we return the canonical suggestion for the query that the
|
|
|
|
// user typed. (The caller passes us the parsed query as "operators".)
|
2018-07-14 13:08:34 +02:00
|
|
|
if (operators.length === 0) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return {description: "", search_string: ""};
|
2018-07-14 13:08:34 +02:00
|
|
|
}
|
|
|
|
return format_as_suggestion(operators);
|
|
|
|
}
|
|
|
|
|
2017-06-03 22:54:35 +02:00
|
|
|
function get_topic_suggestions(last, operators) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const invalid = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "pm-with"},
|
|
|
|
{operator: "is", operand: "private"},
|
|
|
|
{operator: "topic"},
|
2017-06-03 22:54:35 +02:00
|
|
|
];
|
2020-07-15 01:29:15 +02:00
|
|
|
if (!check_validity(last, operators, ["stream", "topic", "search"], invalid)) {
|
2017-06-03 22:54:35 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const operator = Filter.canonicalize_operator(last.operator);
|
|
|
|
const operand = last.operand;
|
2020-07-15 01:29:15 +02:00
|
|
|
const negated = operator === "topic" && last.negated;
|
2019-11-02 00:06:25 +01:00
|
|
|
let stream;
|
|
|
|
let guess;
|
|
|
|
const filter = new Filter(operators);
|
|
|
|
const suggest_operators = [];
|
2013-07-30 23:02:10 +02:00
|
|
|
|
|
|
|
// stream:Rome -> show all Rome topics
|
|
|
|
// stream:Rome topic: -> show all Rome topics
|
|
|
|
// stream:Rome f -> show all Rome topics with a word starting in f
|
|
|
|
// stream:Rome topic:f -> show all Rome topics with a word starting in f
|
|
|
|
// stream:Rome topic:f -> show all Rome topics with a word starting in f
|
|
|
|
|
|
|
|
// When narrowed to a stream:
|
|
|
|
// topic: -> show all topics in current stream
|
|
|
|
// foo -> show all topics in current stream with words starting with foo
|
|
|
|
|
|
|
|
// If somebody explicitly types search:, then we might
|
|
|
|
// not want to suggest topics, but I feel this is a very
|
2013-08-22 01:29:28 +02:00
|
|
|
// minor issue, and Filter.parse() is currently lossy
|
2013-07-30 23:02:10 +02:00
|
|
|
// in terms of telling us whether they provided the operator,
|
2014-02-10 20:53:38 +01:00
|
|
|
// i.e. "foo" and "search:foo" both become [{operator: 'search', operand: 'foo'}].
|
2013-07-30 23:02:10 +02:00
|
|
|
switch (operator) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "stream":
|
|
|
|
guess = "";
|
|
|
|
stream = operand;
|
|
|
|
suggest_operators.push(last);
|
|
|
|
break;
|
|
|
|
case "topic":
|
|
|
|
case "search":
|
|
|
|
guess = operand;
|
|
|
|
if (filter.has_operator("stream")) {
|
|
|
|
stream = filter.operands("stream")[0];
|
|
|
|
} else {
|
|
|
|
stream = narrow_state.stream();
|
|
|
|
suggest_operators.push({operator: "stream", operand: stream});
|
|
|
|
}
|
|
|
|
break;
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!stream) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = stream_data.get_stream_id(stream);
|
2017-07-24 15:15:28 +02:00
|
|
|
if (!stream_id) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-03-22 18:40:05 +01:00
|
|
|
let topics = stream_topic_history.get_recent_topic_names(stream_id);
|
2013-08-20 16:47:27 +02:00
|
|
|
|
2017-07-24 15:15:28 +02:00
|
|
|
if (!topics || !topics.length) {
|
2013-07-30 23:02:10 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2016-10-28 18:47:57 +02:00
|
|
|
// Be defensive here in case stream_data.get_recent_topics gets
|
|
|
|
// super huge, but still slice off enough topics to find matches.
|
2013-08-08 15:36:57 +02:00
|
|
|
topics = topics.slice(0, 300);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (guess !== "") {
|
2020-07-02 01:39:34 +02:00
|
|
|
topics = topics.filter((topic) => common.phrase_match(guess, topic));
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
2013-11-25 20:26:46 +01:00
|
|
|
topics = topics.slice(0, 10);
|
|
|
|
|
2013-07-30 23:02:10 +02:00
|
|
|
// Just use alphabetical order. While recency and read/unreadness of
|
2016-10-28 19:07:25 +02:00
|
|
|
// topics do matter in some contexts, you can get that from the left sidebar,
|
2013-07-30 23:02:10 +02:00
|
|
|
// and I'm leaning toward high scannability for autocompletion. I also don't
|
|
|
|
// care about case.
|
|
|
|
topics.sort();
|
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
return topics.map((topic) => {
|
2020-07-20 22:18:43 +02:00
|
|
|
const topic_term = {operator: "topic", operand: topic, negated};
|
2019-11-02 00:06:25 +01:00
|
|
|
const operators = suggest_operators.concat([topic_term]);
|
2018-06-01 15:59:17 +02:00
|
|
|
return format_as_suggestion(operators);
|
2013-07-30 23:02:10 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
function get_operator_subset_suggestions(operators) {
|
2013-07-30 23:02:10 +02:00
|
|
|
// For stream:a topic:b search:c, suggest:
|
|
|
|
// stream:a topic:b
|
|
|
|
// stream:a
|
|
|
|
if (operators.length < 1) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let i;
|
|
|
|
const suggestions = [];
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i = operators.length - 1; i >= 1; i -= 1) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const subset = operators.slice(0, i);
|
2018-06-01 15:59:17 +02:00
|
|
|
suggestions.push(format_as_suggestion(subset));
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
2018-05-18 10:10:49 +02:00
|
|
|
function get_special_filter_suggestions(last, operators, suggestions) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const is_search_operand_negated = last.operator === "search" && last.operand[0] === "-";
|
2018-05-20 16:30:06 +02:00
|
|
|
// Negating suggestions on is_search_operand_negated is required for
|
|
|
|
// suggesting negated operators.
|
|
|
|
if (last.negated || is_search_operand_negated) {
|
2020-07-02 01:39:34 +02:00
|
|
|
suggestions = suggestions.map((suggestion) => ({
|
2020-07-15 01:29:15 +02:00
|
|
|
search_string: "-" + suggestion.search_string,
|
|
|
|
description: "exclude " + suggestion.description,
|
js: Convert _.map(a, …) to a.map(…).
And convert the corresponding function expressions to arrow style
while we’re here.
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, {
visitCallExpression(path) {
const { callee, arguments: args } = path.node;
if (
n.MemberExpression.check(callee) &&
!callee.computed &&
n.Identifier.check(callee.object) &&
callee.object.name === "_" &&
n.Identifier.check(callee.property) &&
callee.property.name === "map" &&
args.length === 2 &&
checkExpression(args[0]) &&
checkExpression(args[1])
) {
const [arr, fn] = args;
path.replace(
b.callExpression(b.memberExpression(arr, b.identifier("map")), [
n.FunctionExpression.check(fn) ||
n.ArrowFunctionExpression.check(fn)
? b.arrowFunctionExpression(
fn.params,
n.BlockStatement.check(fn.body) &&
fn.body.body.length === 1 &&
n.ReturnStatement.check(fn.body.body[0])
? fn.body.body[0].argument || b.identifier("undefined")
: fn.body
)
: fn,
])
);
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-08 02:43:49 +01:00
|
|
|
invalid: suggestion.invalid,
|
|
|
|
}));
|
2018-05-20 16:30:06 +02:00
|
|
|
}
|
2018-05-18 10:10:49 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const last_string = Filter.unparse([last]).toLowerCase();
|
2020-07-02 01:39:34 +02:00
|
|
|
suggestions = suggestions.filter((s) => {
|
2018-05-18 10:10:49 +02:00
|
|
|
if (match_criteria(operators, s.invalid)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (last_string === "") {
|
2018-05-18 10:10:49 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-20 16:30:06 +02:00
|
|
|
|
|
|
|
// returns the substring after the ":" symbol.
|
2019-11-02 00:06:25 +01:00
|
|
|
const suggestion_operand = s.search_string.substring(s.search_string.indexOf(":") + 1);
|
2018-05-20 16:30:06 +02:00
|
|
|
// e.g for `att` search query, `has:attachment` should be suggested.
|
2020-07-15 00:34:28 +02:00
|
|
|
const show_operator_suggestions =
|
|
|
|
last.operator === "search" && suggestion_operand.toLowerCase().startsWith(last_string);
|
|
|
|
return (
|
|
|
|
s.search_string.toLowerCase().startsWith(last_string) ||
|
|
|
|
show_operator_suggestions ||
|
|
|
|
s.description.toLowerCase().startsWith(last_string)
|
|
|
|
);
|
2018-05-18 10:10:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Only show home if there's an empty bar
|
2020-07-15 01:29:15 +02:00
|
|
|
if (operators.length === 0 && last_string === "") {
|
|
|
|
suggestions.unshift({search_string: "", description: "All messages"});
|
2018-05-18 10:10:49 +02:00
|
|
|
}
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
2019-08-13 20:20:36 +02:00
|
|
|
function get_streams_filter_suggestions(last, operators) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const suggestions = [
|
2019-08-13 20:20:36 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
search_string: "streams:public",
|
|
|
|
description: "All public streams in organization",
|
2019-08-13 20:20:36 +02:00
|
|
|
invalid: [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "is", operand: "private"},
|
|
|
|
{operator: "stream"},
|
|
|
|
{operator: "group-pm-with"},
|
|
|
|
{operator: "pm-with"},
|
|
|
|
{operator: "in"},
|
|
|
|
{operator: "streams"},
|
2019-08-13 20:20:36 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
return get_special_filter_suggestions(last, operators, suggestions);
|
|
|
|
}
|
2018-05-18 10:10:49 +02:00
|
|
|
function get_is_filter_suggestions(last, operators) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const suggestions = [
|
2013-07-30 23:02:10 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
search_string: "is:private",
|
|
|
|
description: "private messages",
|
2017-06-03 04:32:25 +02:00
|
|
|
invalid: [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "is", operand: "private"},
|
|
|
|
{operator: "stream"},
|
|
|
|
{operator: "pm-with"},
|
|
|
|
{operator: "in"},
|
2017-06-03 04:32:25 +02:00
|
|
|
],
|
2013-07-30 23:02:10 +02:00
|
|
|
},
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
search_string: "is:starred",
|
|
|
|
description: "starred messages",
|
2020-07-15 00:34:28 +02:00
|
|
|
invalid: [{operator: "is", operand: "starred"}],
|
2013-07-30 23:02:10 +02:00
|
|
|
},
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
search_string: "is:mentioned",
|
|
|
|
description: "@-mentions",
|
2020-07-15 00:34:28 +02:00
|
|
|
invalid: [{operator: "is", operand: "mentioned"}],
|
2013-07-30 23:02:10 +02:00
|
|
|
},
|
2013-08-30 21:15:01 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
search_string: "is:alerted",
|
|
|
|
description: "alerted messages",
|
2020-07-15 00:34:28 +02:00
|
|
|
invalid: [{operator: "is", operand: "alerted"}],
|
2013-08-30 21:15:01 +02:00
|
|
|
},
|
2017-06-19 00:33:24 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
search_string: "is:unread",
|
|
|
|
description: "unread messages",
|
2020-07-15 00:34:28 +02:00
|
|
|
invalid: [{operator: "is", operand: "unread"}],
|
2017-06-19 00:33:24 +02:00
|
|
|
},
|
2013-07-30 23:02:10 +02:00
|
|
|
];
|
2018-05-18 10:10:49 +02:00
|
|
|
return get_special_filter_suggestions(last, operators, suggestions);
|
|
|
|
}
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2018-05-18 10:10:49 +02:00
|
|
|
function get_has_filter_suggestions(last, operators) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const suggestions = [
|
2018-05-18 10:10:49 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
search_string: "has:link",
|
|
|
|
description: "messages with one or more link",
|
2020-07-15 00:34:28 +02:00
|
|
|
invalid: [{operator: "has", operand: "link"}],
|
2018-05-18 10:10:49 +02:00
|
|
|
},
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
search_string: "has:image",
|
|
|
|
description: "messages with one or more image",
|
2020-07-15 00:34:28 +02:00
|
|
|
invalid: [{operator: "has", operand: "image"}],
|
2018-05-18 10:10:49 +02:00
|
|
|
},
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
search_string: "has:attachment",
|
|
|
|
description: "messages with one or more attachment",
|
2020-07-15 00:34:28 +02:00
|
|
|
invalid: [{operator: "has", operand: "attachment"}],
|
2018-05-18 10:10:49 +02:00
|
|
|
},
|
|
|
|
];
|
|
|
|
return get_special_filter_suggestions(last, operators, suggestions);
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
2017-06-03 05:33:02 +02:00
|
|
|
function get_sent_by_me_suggestions(last, operators) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const last_string = Filter.unparse([last]).toLowerCase();
|
2020-07-15 00:34:28 +02:00
|
|
|
const negated = last.negated || (last.operator === "search" && last.operand[0] === "-");
|
2020-07-15 01:29:15 +02:00
|
|
|
const negated_symbol = negated ? "-" : "";
|
|
|
|
const verb = negated ? "exclude " : "";
|
2019-11-02 00:06:25 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const sender_query = negated_symbol + "sender:" + people.my_current_email();
|
|
|
|
const from_query = negated_symbol + "from:" + people.my_current_email();
|
|
|
|
const sender_me_query = negated_symbol + "sender:me";
|
|
|
|
const from_me_query = negated_symbol + "from:me";
|
|
|
|
const sent_string = negated_symbol + "sent";
|
|
|
|
const description = verb + "sent by me";
|
2019-11-02 00:06:25 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const invalid = [{operator: "sender"}, {operator: "from"}];
|
2017-01-11 19:20:31 +01:00
|
|
|
|
2017-06-03 05:33:02 +02:00
|
|
|
if (match_criteria(operators, invalid)) {
|
2017-01-11 19:20:31 +01:00
|
|
|
return [];
|
2017-06-03 05:33:02 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
last.operator === "" ||
|
2020-01-28 15:26:02 +01:00
|
|
|
sender_query.startsWith(last_string) ||
|
|
|
|
sender_me_query.startsWith(last_string) ||
|
|
|
|
last_string === sent_string
|
|
|
|
) {
|
2017-01-11 19:20:31 +01:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
search_string: sender_query,
|
2020-07-20 22:18:43 +02:00
|
|
|
description,
|
2017-01-11 19:20:31 +01:00
|
|
|
},
|
|
|
|
];
|
2020-07-15 00:34:28 +02:00
|
|
|
} else if (from_query.startsWith(last_string) || from_me_query.startsWith(last_string)) {
|
2017-01-11 19:20:31 +01:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
search_string: from_query,
|
2020-07-20 22:18:43 +02:00
|
|
|
description,
|
2017-01-11 19:20:31 +01:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-06-15 09:07:12 +02:00
|
|
|
function get_operator_suggestions(last) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (!(last.operator === "search")) {
|
2018-04-06 20:43:34 +02:00
|
|
|
return [];
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
let last_operand = last.operand;
|
2017-06-15 09:07:12 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let negated = false;
|
2020-01-28 15:26:02 +01:00
|
|
|
if (last_operand.startsWith("-")) {
|
2018-04-06 20:43:34 +02:00
|
|
|
negated = true;
|
2018-05-20 16:30:06 +02:00
|
|
|
last_operand = last_operand.slice(1);
|
2018-04-06 20:43:34 +02:00
|
|
|
}
|
2017-06-15 09:07:12 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let choices = ["stream", "topic", "pm-with", "sender", "near", "from", "group-pm-with"];
|
2020-07-02 01:39:34 +02:00
|
|
|
choices = choices.filter((choice) => common.phrase_match(last_operand, choice));
|
2017-06-15 09:07:12 +02:00
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
return choices.map((choice) => {
|
2020-07-20 22:18:43 +02:00
|
|
|
const op = [{operator: choice, operand: "", negated}];
|
2018-06-01 15:59:17 +02:00
|
|
|
return format_as_suggestion(op);
|
2018-04-06 20:43:34 +02:00
|
|
|
});
|
2017-06-15 09:07:12 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 02:09:54 +02:00
|
|
|
class Attacher {
|
|
|
|
result = [];
|
|
|
|
prev = new Set();
|
|
|
|
|
|
|
|
constructor(base) {
|
|
|
|
this.base = base;
|
2019-12-25 18:36:16 +01:00
|
|
|
}
|
|
|
|
|
2020-07-23 02:09:54 +02:00
|
|
|
prepend_base(suggestion) {
|
|
|
|
if (this.base && this.base.description.length > 0) {
|
|
|
|
suggestion.search_string = this.base.search_string + " " + suggestion.search_string;
|
|
|
|
suggestion.description = this.base.description + ", " + suggestion.description;
|
2019-12-25 18:36:16 +01:00
|
|
|
}
|
2020-07-23 02:09:54 +02:00
|
|
|
}
|
2019-12-25 18:36:16 +01:00
|
|
|
|
2020-07-23 02:09:54 +02:00
|
|
|
push(suggestion) {
|
|
|
|
if (!this.prev.has(suggestion.search_string)) {
|
|
|
|
this.prev.add(suggestion.search_string);
|
|
|
|
this.result.push(suggestion);
|
|
|
|
}
|
|
|
|
}
|
2019-12-25 18:36:16 +01:00
|
|
|
|
2020-07-23 02:09:54 +02:00
|
|
|
concat(suggestions) {
|
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 suggestion of suggestions) {
|
2020-07-23 02:09:54 +02:00
|
|
|
this.push(suggestion);
|
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
|
|
|
}
|
2020-07-23 02:09:54 +02:00
|
|
|
}
|
2019-12-25 18:36:16 +01:00
|
|
|
|
2020-07-23 02:09:54 +02:00
|
|
|
attach_many(suggestions) {
|
|
|
|
for (const suggestion of suggestions) {
|
|
|
|
this.prepend_base(suggestion);
|
|
|
|
this.push(suggestion);
|
|
|
|
}
|
|
|
|
}
|
2017-06-03 04:32:25 +02:00
|
|
|
}
|
|
|
|
|
2019-12-25 15:36:51 +01:00
|
|
|
exports.get_search_result = function (base_query, query) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let suggestion;
|
2020-06-01 15:00:42 +02:00
|
|
|
let all_operators;
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2020-06-01 15:00:42 +02:00
|
|
|
// search_operators correspond to the operators for the query in the input.
|
|
|
|
// For search_pills_enabled, this includes just editable query where search pills
|
|
|
|
// have not been created yet.
|
|
|
|
// And for this disabled case, this includes the entire query entered in the searchbox.
|
|
|
|
// operators correspond to the operators for the entire query entered in the searchbox.
|
|
|
|
if (page_params.search_pills_enabled) {
|
|
|
|
all_operators = Filter.parse((base_query + " " + query).trim());
|
|
|
|
}
|
2020-06-01 12:44:23 +02:00
|
|
|
const search_operators = Filter.parse(query);
|
2020-07-15 01:29:15 +02:00
|
|
|
let last = {operator: "", operand: "", negated: false};
|
2020-06-01 12:44:23 +02:00
|
|
|
if (search_operators.length > 0) {
|
|
|
|
last = search_operators.slice(-1)[0];
|
2020-06-01 15:00:42 +02:00
|
|
|
} else if (page_params.search_pills_enabled) {
|
|
|
|
// We push an empty term so that we can get suggestions
|
|
|
|
// on the empty string based on the base query which is
|
|
|
|
// calculated from the created search pills.
|
|
|
|
// Else search results are returned as if the user is still
|
|
|
|
// typing the non-editable last search pill.
|
|
|
|
all_operators.push(last);
|
2020-06-01 12:44:23 +02:00
|
|
|
search_operators.push(last);
|
2017-06-03 04:32:25 +02:00
|
|
|
}
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const person_suggestion_ops = ["sender", "pm-with", "from", "group-pm"];
|
2020-06-01 12:44:23 +02:00
|
|
|
const search_operators_len = search_operators.length;
|
2018-05-25 17:03:13 +02:00
|
|
|
|
2020-06-01 12:44:23 +02:00
|
|
|
// Handle spaces in person name in new suggestions only. Checks if the last operator is 'search'
|
|
|
|
// and the second last operator in search_operators is one out of person_suggestion_ops.
|
2018-05-25 17:03:13 +02:00
|
|
|
// e.g for `sender:Ted sm`, initially last = {operator: 'search', operand: 'sm'....}
|
|
|
|
// and second last is {operator: 'sender', operand: 'sm'....}. If the second last operand
|
|
|
|
// is an email of a user, both of these operators remain unchanged. Otherwise search operator
|
|
|
|
// will be deleted and new last will become {operator:'sender', operand: 'Ted sm`....}.
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
search_operators_len > 1 &&
|
2020-07-15 01:29:15 +02:00
|
|
|
last.operator === "search" &&
|
2020-07-15 00:34:28 +02:00
|
|
|
person_suggestion_ops.includes(search_operators[search_operators_len - 2].operator)
|
|
|
|
) {
|
2020-06-01 12:44:23 +02:00
|
|
|
const person_op = search_operators[search_operators_len - 2];
|
2019-06-27 19:16:55 +02:00
|
|
|
if (!people.reply_to_to_user_ids_string(person_op.operand)) {
|
2018-05-25 17:03:13 +02:00
|
|
|
last = {
|
|
|
|
operator: person_op.operator,
|
2020-07-15 01:29:15 +02:00
|
|
|
operand: person_op.operand + " " + last.operand,
|
2018-05-25 17:03:13 +02:00
|
|
|
negated: person_op.negated,
|
|
|
|
};
|
2020-06-01 15:00:42 +02:00
|
|
|
if (page_params.search_pills_enabled) {
|
|
|
|
all_operators[all_operators.length - 2] = last;
|
|
|
|
all_operators.splice(-1, 1);
|
|
|
|
}
|
2020-06-01 12:44:23 +02:00
|
|
|
search_operators[search_operators_len - 2] = last;
|
|
|
|
search_operators.splice(-1, 1);
|
2018-05-25 17:03:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:44:23 +02:00
|
|
|
const base = get_default_suggestion(search_operators.slice(0, -1));
|
2020-07-23 02:09:54 +02:00
|
|
|
const attacher = new Attacher(base);
|
2019-12-25 18:36:16 +01:00
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
// Display the default first
|
2018-05-22 14:37:51 +02:00
|
|
|
// `has` and `is` operators work only on predefined categories. Default suggestion
|
2018-05-18 06:33:13 +02:00
|
|
|
// is not displayed in that case. e.g. `messages with one or more abc` as
|
|
|
|
// a suggestion for `has:abc`does not make sense.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (last.operator !== "" && last.operator !== "has" && last.operator !== "is") {
|
2020-06-01 12:44:23 +02:00
|
|
|
suggestion = get_default_suggestion(search_operators);
|
2020-06-13 16:34:23 +02:00
|
|
|
attacher.push(suggestion);
|
2017-06-03 04:32:25 +02:00
|
|
|
}
|
|
|
|
|
2020-01-02 12:53:11 +01:00
|
|
|
// only make one people_getter to avoid duplicate work
|
|
|
|
const people_getter = make_people_getter(last);
|
|
|
|
|
2020-01-07 14:21:35 +01:00
|
|
|
function get_people(flavor) {
|
|
|
|
return function (last, base_operators) {
|
|
|
|
return get_person_suggestions(people_getter, last, base_operators, flavor);
|
|
|
|
};
|
|
|
|
}
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2020-01-07 14:21:35 +01:00
|
|
|
const filterers = [
|
|
|
|
get_streams_filter_suggestions,
|
|
|
|
get_is_filter_suggestions,
|
|
|
|
get_sent_by_me_suggestions,
|
|
|
|
get_stream_suggestions,
|
2020-07-15 01:29:15 +02:00
|
|
|
get_people("sender"),
|
|
|
|
get_people("pm-with"),
|
|
|
|
get_people("from"),
|
|
|
|
get_people("group-pm-with"),
|
2020-01-07 14:21:35 +01:00
|
|
|
get_group_suggestions,
|
|
|
|
get_topic_suggestions,
|
|
|
|
get_operator_suggestions,
|
|
|
|
get_has_filter_suggestions,
|
|
|
|
];
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2020-06-01 15:00:42 +02:00
|
|
|
if (!page_params.search_pills_enabled) {
|
|
|
|
all_operators = search_operators;
|
|
|
|
}
|
|
|
|
const base_operators = all_operators.slice(0, -1);
|
2020-01-07 14:21:35 +01:00
|
|
|
const max_items = exports.max_num_of_search_results;
|
2017-06-15 09:07:12 +02: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 filterer of filterers) {
|
2020-01-07 14:21:35 +01:00
|
|
|
if (attacher.result.length < max_items) {
|
|
|
|
const suggestions = filterer(last, base_operators);
|
|
|
|
attacher.attach_many(suggestions);
|
|
|
|
}
|
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
|
|
|
}
|
2017-06-05 09:54:56 +02:00
|
|
|
|
2020-06-01 15:00:42 +02:00
|
|
|
if (!page_params.search_pills_enabled) {
|
|
|
|
// This is unique to the legacy search system. With pills
|
|
|
|
// it is difficult to "suggest" a subset of operators,
|
|
|
|
// and there's a more natural mechanism under that paradigm,
|
|
|
|
// where the user just deletes one or more pills. So you
|
|
|
|
// won't see this is in the new code.
|
2019-12-25 16:58:11 +01:00
|
|
|
if (attacher.result.length < max_items) {
|
2020-06-01 15:00:42 +02:00
|
|
|
const subset_suggestions = get_operator_subset_suggestions(search_operators);
|
|
|
|
attacher.concat(subset_suggestions);
|
2019-12-25 16:58:11 +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
|
|
|
}
|
2018-07-14 13:01:21 +02:00
|
|
|
|
2019-12-25 16:58:11 +01:00
|
|
|
return attacher.result.slice(0, max_items);
|
2019-12-25 15:36:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_suggestions = function (base_query, query) {
|
|
|
|
const result = exports.get_search_result(base_query, query);
|
|
|
|
return exports.finalize_search_result(result);
|
|
|
|
};
|
2018-07-14 13:01:21 +02:00
|
|
|
|
2019-12-25 15:36:51 +01:00
|
|
|
exports.finalize_search_result = function (result) {
|
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 sug of result) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const first = sug.description.charAt(0).toUpperCase();
|
2018-07-14 13:01:21 +02:00
|
|
|
sug.description = first + sug.description.slice(1);
|
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-07-14 13:01:21 +02:00
|
|
|
|
2019-12-25 18:36:16 +01:00
|
|
|
// Typeahead expects us to give it strings, not objects,
|
|
|
|
// so we maintain our own hash back to our objects
|
2020-02-12 06:58:20 +01:00
|
|
|
const lookup_table = new Map();
|
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 obj of result) {
|
2020-02-12 06:58:20 +01:00
|
|
|
lookup_table.set(obj.search_string, obj);
|
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
|
|
|
}
|
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
const strings = result.map((obj) => obj.search_string);
|
2018-07-14 13:01:21 +02:00
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
strings,
|
|
|
|
lookup_table,
|
2018-07-14 13:01:21 +02:00
|
|
|
};
|
|
|
|
};
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.search_suggestion = exports;
|