From 9d9c716dd236ffa0ecf302a1a9328d5d84df77e8 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sat, 27 Feb 2021 16:04:35 -0800 Subject: [PATCH] js: Convert static/js/search_suggestion.js to ES6 module. Signed-off-by: Anders Kaseorg --- .eslintrc.json | 1 - frontend_tests/node_tests/search.js | 3 +- frontend_tests/node_tests/search_legacy.js | 3 +- .../node_tests/search_suggestion.js | 2 +- .../node_tests/search_suggestion_legacy.js | 2 +- frontend_tests/node_tests/ui_init.js | 1 - static/js/bundles/app.js | 1 - static/js/global.d.ts | 1 - static/js/search.js | 1 + static/js/search_suggestion.js | 44 +++++++++---------- 10 files changed, 27 insertions(+), 32 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index de59e69a25..c7cb483bf0 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -178,7 +178,6 @@ "reminder": false, "search": false, "search_pill_widget": false, - "search_suggestion": false, "server_events": false, "settings": false, "settings_account": false, diff --git a/frontend_tests/node_tests/search.js b/frontend_tests/node_tests/search.js index daec8c2101..d18fdc742e 100644 --- a/frontend_tests/node_tests/search.js +++ b/frontend_tests/node_tests/search.js @@ -19,7 +19,8 @@ const narrow_state = { filter: () => false, }; rewiremock("../../static/js/narrow_state").with(narrow_state); -const search_suggestion = set_global("search_suggestion", {}); +const search_suggestion = {__esModule: true}; +rewiremock("../../static/js/search_suggestion").with(search_suggestion); rewiremock("../../static/js/ui_util").with({ change_tab_to: noop, place_caret_at_end: noop, diff --git a/frontend_tests/node_tests/search_legacy.js b/frontend_tests/node_tests/search_legacy.js index f8193b1a29..c605d607ea 100644 --- a/frontend_tests/node_tests/search_legacy.js +++ b/frontend_tests/node_tests/search_legacy.js @@ -16,7 +16,8 @@ const noop = () => {}; const narrow_state = {__esModule: true}; rewiremock("../../static/js/narrow_state").with(narrow_state); -const search_suggestion = set_global("search_suggestion", {}); +const search_suggestion = {__esModule: true}; +rewiremock("../../static/js/search_suggestion").with(search_suggestion); rewiremock("../../static/js/ui_util").with({ change_tab_to: noop, }); diff --git a/frontend_tests/node_tests/search_suggestion.js b/frontend_tests/node_tests/search_suggestion.js index c25513a6ff..2358eff23c 100644 --- a/frontend_tests/node_tests/search_suggestion.js +++ b/frontend_tests/node_tests/search_suggestion.js @@ -28,7 +28,7 @@ const people = zrequire("people"); zrequire("unread"); const search = zrequire("search_suggestion"); -search.max_num_of_search_results = 15; +search.__Rewire__("max_num_of_search_results", 15); const me = { email: "myself@zulip.com", diff --git a/frontend_tests/node_tests/search_suggestion_legacy.js b/frontend_tests/node_tests/search_suggestion_legacy.js index e2833ceb06..bbe33d0d29 100644 --- a/frontend_tests/node_tests/search_suggestion_legacy.js +++ b/frontend_tests/node_tests/search_suggestion_legacy.js @@ -26,7 +26,7 @@ const people = zrequire("people"); zrequire("unread"); const search = zrequire("search_suggestion"); -search.max_num_of_search_results = 15; +search.__Rewire__("max_num_of_search_results", 15); const me = { email: "myself@zulip.com", diff --git a/frontend_tests/node_tests/ui_init.js b/frontend_tests/node_tests/ui_init.js index c5b01f9a62..b6fdf42d0d 100644 --- a/frontend_tests/node_tests/ui_init.js +++ b/frontend_tests/node_tests/ui_init.js @@ -119,7 +119,6 @@ const upload = zrequire("upload"); const compose = zrequire("compose"); zrequire("composebox_typeahead"); zrequire("narrow"); -zrequire("search_suggestion"); zrequire("search"); zrequire("notifications"); zrequire("stream_list"); diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js index 9fbdfe2c8f..4f2a51db52 100644 --- a/static/js/bundles/app.js +++ b/static/js/bundles/app.js @@ -27,7 +27,6 @@ import "../reload"; import "../compose_actions"; import "../subs"; import "../ui"; -import "../search_suggestion"; import "../search"; import "../composebox_typeahead"; import "../navigate"; diff --git a/static/js/global.d.ts b/static/js/global.d.ts index 0f86c110cf..67d3ec43e9 100644 --- a/static/js/global.d.ts +++ b/static/js/global.d.ts @@ -51,7 +51,6 @@ declare let recent_topics: any; declare let reminder: any; declare let search: any; declare let search_pill_widget: any; -declare let search_suggestion: any; declare let server_events: any; declare let settings: any; declare let settings_account: any; diff --git a/static/js/search.js b/static/js/search.js index 42f60e793c..42f78753d8 100644 --- a/static/js/search.js +++ b/static/js/search.js @@ -3,6 +3,7 @@ const {Filter} = require("./filter"); const narrow_state = require("./narrow_state"); const search_pill = require("./search_pill"); +const search_suggestion = require("./search_suggestion"); const ui_util = require("./ui_util"); // Exported for unit testing diff --git a/static/js/search_suggestion.js b/static/js/search_suggestion.js index 24a1dfcc37..fc8c00779d 100644 --- a/static/js/search_suggestion.js +++ b/static/js/search_suggestion.js @@ -1,18 +1,16 @@ -"use strict"; +import Handlebars from "handlebars/runtime"; -const Handlebars = require("handlebars/runtime"); +import * as common from "./common"; +import {Filter} from "./filter"; +import * as huddle_data from "./huddle_data"; +import * as narrow_state from "./narrow_state"; +import * as people from "./people"; +import * as settings_data from "./settings_data"; +import * as stream_data from "./stream_data"; +import * as stream_topic_history from "./stream_topic_history"; +import * as typeahead_helper from "./typeahead_helper"; -const common = require("./common"); -const {Filter} = require("./filter"); -const huddle_data = require("./huddle_data"); -const narrow_state = require("./narrow_state"); -const people = require("./people"); -const settings_data = require("./settings_data"); -const stream_data = require("./stream_data"); -const stream_topic_history = require("./stream_topic_history"); -const typeahead_helper = require("./typeahead_helper"); - -exports.max_num_of_search_results = 12; +export const max_num_of_search_results = 12; function stream_matches_query(stream_name, q) { return common.phrase_match(q, stream_name); @@ -603,7 +601,7 @@ class Attacher { } } -exports.get_search_result = function (base_query, query) { +export function get_search_result(base_query, query) { let suggestion; let all_operators; @@ -699,7 +697,7 @@ exports.get_search_result = function (base_query, query) { all_operators = search_operators; } const base_operators = all_operators.slice(0, -1); - const max_items = exports.max_num_of_search_results; + const max_items = max_num_of_search_results; for (const filterer of filterers) { if (attacher.result.length < max_items) { @@ -722,14 +720,14 @@ exports.get_search_result = function (base_query, query) { } return attacher.result.slice(0, max_items); -}; +} -exports.get_suggestions = function (base_query, query) { - const result = exports.get_search_result(base_query, query); - return exports.finalize_search_result(result); -}; +export function get_suggestions(base_query, query) { + const result = get_search_result(base_query, query); + return finalize_search_result(result); +} -exports.finalize_search_result = function (result) { +export function finalize_search_result(result) { for (const sug of result) { const first = sug.description.charAt(0).toUpperCase(); sug.description = first + sug.description.slice(1); @@ -748,6 +746,4 @@ exports.finalize_search_result = function (result) { strings, lookup_table, }; -}; - -window.search_suggestion = exports; +}