Skip to content

Commit 2d1da7c

Browse files
authored
Rollup merge of #145742 - lolbinarycat:rustdoc-search-type-cleanup-continued, r=GuillaumeGomez
rustdoc js: Even more typechecking improvments made another pass at eliminating typescript errors, this time mainly focused on adding fields to `window` that weren't declared before. r? `@notriddle`
2 parents ca6415d + 771c641 commit 2d1da7c

File tree

4 files changed

+18
-43
lines changed

4 files changed

+18
-43
lines changed

src/librustdoc/html/static/js/main.js

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,7 @@ function preLoadCss(cssUrl) {
407407
function loadSearch() {
408408
if (!searchLoaded) {
409409
searchLoaded = true;
410-
// @ts-expect-error
411410
window.rr_ = data => {
412-
// @ts-expect-error
413411
window.searchIndex = data;
414412
};
415413
if (!window.StringdexOnload) {
@@ -1277,13 +1275,11 @@ function preLoadCss(cssUrl) {
12771275
}
12781276

12791277
window.addEventListener("resize", () => {
1280-
// @ts-expect-error
12811278
if (window.CURRENT_TOOLTIP_ELEMENT) {
12821279
// As a workaround to the behavior of `contains: layout` used in doc togglers,
12831280
// tooltip popovers are positioned using javascript.
12841281
//
12851282
// This means when the window is resized, we need to redo the layout.
1286-
// @ts-expect-error
12871283
const base = window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE;
12881284
const force_visible = base.TOOLTIP_FORCE_VISIBLE;
12891285
hideTooltip(false);
@@ -1329,26 +1325,25 @@ function preLoadCss(cssUrl) {
13291325
*/
13301326
function showTooltip(e) {
13311327
const notable_ty = e.getAttribute("data-notable-ty");
1332-
// @ts-expect-error
13331328
if (!window.NOTABLE_TRAITS && notable_ty) {
13341329
const data = document.getElementById("notable-traits-data");
13351330
if (data) {
1336-
// @ts-expect-error
13371331
window.NOTABLE_TRAITS = JSON.parse(data.innerText);
13381332
} else {
13391333
throw new Error("showTooltip() called with notable without any notable traits!");
13401334
}
13411335
}
13421336
// Make this function idempotent. If the tooltip is already shown, avoid doing extra work
13431337
// and leave it alone.
1344-
// @ts-expect-error
13451338
if (window.CURRENT_TOOLTIP_ELEMENT && window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE === e) {
1346-
// @ts-expect-error
13471339
clearTooltipHoverTimeout(window.CURRENT_TOOLTIP_ELEMENT);
13481340
return;
13491341
}
13501342
window.hideAllModals(false);
1351-
const wrapper = document.createElement("div");
1343+
// use Object.assign to make sure the object has the correct type
1344+
// with all of the correct fields before it is assigned to a variable,
1345+
// as typescript has no way to change the type of a variable once it is initialized.
1346+
const wrapper = Object.assign(document.createElement("div"), {TOOLTIP_BASE: e});
13521347
if (notable_ty) {
13531348
wrapper.innerHTML = "<div class=\"content\">" +
13541349
// @ts-expect-error
@@ -1394,11 +1389,7 @@ function preLoadCss(cssUrl) {
13941389
);
13951390
}
13961391
wrapper.style.visibility = "";
1397-
// @ts-expect-error
13981392
window.CURRENT_TOOLTIP_ELEMENT = wrapper;
1399-
// @ts-expect-error
1400-
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE = e;
1401-
// @ts-expect-error
14021393
clearTooltipHoverTimeout(window.CURRENT_TOOLTIP_ELEMENT);
14031394
wrapper.onpointerenter = ev => {
14041395
// If this is a synthetic touch event, ignore it. A click event will be along shortly.
@@ -1433,19 +1424,15 @@ function preLoadCss(cssUrl) {
14331424
*/
14341425
function setTooltipHoverTimeout(element, show) {
14351426
clearTooltipHoverTimeout(element);
1436-
// @ts-expect-error
14371427
if (!show && !window.CURRENT_TOOLTIP_ELEMENT) {
14381428
// To "hide" an already hidden element, just cancel its timeout.
14391429
return;
14401430
}
1441-
// @ts-expect-error
14421431
if (show && window.CURRENT_TOOLTIP_ELEMENT) {
14431432
// To "show" an already visible element, just cancel its timeout.
14441433
return;
14451434
}
1446-
// @ts-expect-error
14471435
if (window.CURRENT_TOOLTIP_ELEMENT &&
1448-
// @ts-expect-error
14491436
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE !== element) {
14501437
// Don't do anything if another tooltip is already visible.
14511438
return;
@@ -1468,24 +1455,20 @@ function preLoadCss(cssUrl) {
14681455
*/
14691456
function clearTooltipHoverTimeout(element) {
14701457
if (element.TOOLTIP_HOVER_TIMEOUT !== undefined) {
1471-
// @ts-expect-error
14721458
removeClass(window.CURRENT_TOOLTIP_ELEMENT, "fade-out");
14731459
clearTimeout(element.TOOLTIP_HOVER_TIMEOUT);
14741460
delete element.TOOLTIP_HOVER_TIMEOUT;
14751461
}
14761462
}
14771463

1478-
// @ts-expect-error
1464+
/**
1465+
* @param {Event & { relatedTarget: Node }} event
1466+
*/
14791467
function tooltipBlurHandler(event) {
1480-
// @ts-expect-error
14811468
if (window.CURRENT_TOOLTIP_ELEMENT &&
1482-
// @ts-expect-error
14831469
!window.CURRENT_TOOLTIP_ELEMENT.contains(document.activeElement) &&
1484-
// @ts-expect-error
14851470
!window.CURRENT_TOOLTIP_ELEMENT.contains(event.relatedTarget) &&
1486-
// @ts-expect-error
14871471
!window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.contains(document.activeElement) &&
1488-
// @ts-expect-error
14891472
!window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.contains(event.relatedTarget)
14901473
) {
14911474
// Work around a difference in the focus behaviour between Firefox, Chrome, and Safari.
@@ -1507,30 +1490,22 @@ function preLoadCss(cssUrl) {
15071490
* If set to `false`, leave keyboard focus alone.
15081491
*/
15091492
function hideTooltip(focus) {
1510-
// @ts-expect-error
15111493
if (window.CURRENT_TOOLTIP_ELEMENT) {
1512-
// @ts-expect-error
15131494
if (window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.TOOLTIP_FORCE_VISIBLE) {
15141495
if (focus) {
1515-
// @ts-expect-error
15161496
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.focus();
15171497
}
1518-
// @ts-expect-error
15191498
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.TOOLTIP_FORCE_VISIBLE = false;
15201499
}
1521-
// @ts-expect-error
15221500
document.body.removeChild(window.CURRENT_TOOLTIP_ELEMENT);
1523-
// @ts-expect-error
15241501
clearTooltipHoverTimeout(window.CURRENT_TOOLTIP_ELEMENT);
1525-
// @ts-expect-error
1526-
window.CURRENT_TOOLTIP_ELEMENT = null;
1502+
window.CURRENT_TOOLTIP_ELEMENT = undefined;
15271503
}
15281504
}
15291505

15301506
onEachLazy(document.getElementsByClassName("tooltip"), e => {
15311507
e.onclick = () => {
15321508
e.TOOLTIP_FORCE_VISIBLE = e.TOOLTIP_FORCE_VISIBLE ? false : true;
1533-
// @ts-expect-error
15341509
if (window.CURRENT_TOOLTIP_ELEMENT && !e.TOOLTIP_FORCE_VISIBLE) {
15351510
hideTooltip(true);
15361511
} else {
@@ -1566,9 +1541,7 @@ function preLoadCss(cssUrl) {
15661541
if (ev.pointerType !== "mouse") {
15671542
return;
15681543
}
1569-
// @ts-expect-error
15701544
if (!e.TOOLTIP_FORCE_VISIBLE && window.CURRENT_TOOLTIP_ELEMENT &&
1571-
// @ts-expect-error
15721545
!window.CURRENT_TOOLTIP_ELEMENT.contains(ev.relatedTarget)) {
15731546
// Tooltip pointer leave gesture:
15741547
//
@@ -1601,7 +1574,6 @@ function preLoadCss(cssUrl) {
16011574
// * https://www.nngroup.com/articles/tooltip-guidelines/
16021575
// * https://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown
16031576
setTooltipHoverTimeout(e, false);
1604-
// @ts-expect-error
16051577
addClass(window.CURRENT_TOOLTIP_ELEMENT, "fade-out");
16061578
}
16071579
};
@@ -1707,8 +1679,7 @@ function preLoadCss(cssUrl) {
17071679
if (isHelpPage) {
17081680
const help_section = document.createElement("section");
17091681
help_section.appendChild(container);
1710-
// @ts-expect-error
1711-
document.getElementById("main-content").appendChild(help_section);
1682+
nonnull(document.getElementById("main-content")).appendChild(help_section);
17121683
} else {
17131684
onEachLazy(document.getElementsByClassName("help-menu"), menu => {
17141685
if (menu.offsetWidth !== 0) {
@@ -1854,8 +1825,7 @@ function preLoadCss(cssUrl) {
18541825
sidebarButton.addEventListener("click", e => {
18551826
removeClass(document.documentElement, "hide-sidebar");
18561827
updateLocalStorage("hide-sidebar", "false");
1857-
if (document.querySelector(".rustdoc.src")) {
1858-
// @ts-expect-error
1828+
if (window.rustdocToggleSrcSidebar) {
18591829
window.rustdocToggleSrcSidebar();
18601830
}
18611831
e.preventDefault();

src/librustdoc/html/static/js/rustdoc.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ declare global {
2828
currentTheme: HTMLLinkElement|null;
2929
/** Generated in `render/context.rs` */
3030
SIDEBAR_ITEMS?: { [key: string]: string[] };
31+
/** Notable trait data */
32+
NOTABLE_TRAITS?: { [key: string]: string };
33+
CURRENT_TOOLTIP_ELEMENT?: HTMLElement & { TOOLTIP_BASE: HTMLElement };
3134
/** Used by the popover tooltip code. */
3235
RUSTDOC_TOOLTIP_HOVER_MS: number;
3336
/** Used by the popover tooltip code. */
@@ -93,6 +96,10 @@ declare global {
9396
pending_type_impls?: rustdoc.TypeImpls,
9497
rustdoc_add_line_numbers_to_examples?: function(),
9598
rustdoc_remove_line_numbers_from_examples?: function(),
99+
/** JSON-encoded raw search index */
100+
searchIndex: string,
101+
/** Used in search index shards in order to load data into the in-memory database */
102+
rr_: function(string),
96103
}
97104
interface HTMLElement {
98105
/** Used by the popover tooltip code. */

src/librustdoc/html/static/js/search.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5279,9 +5279,7 @@ if (typeof window !== "undefined") {
52795279
// search.index/root is loaded by main.js, so
52805280
// this script doesn't need to launch it, but
52815281
// must pick it up
5282-
// @ts-ignore
52835282
if (window.searchIndex) {
5284-
// @ts-ignore
52855283
window.rr_(window.searchIndex);
52865284
}
52875285
},

src/librustdoc/html/static/js/storage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function addClass(elem, className) {
117117
* Remove a class from a DOM Element. If `elem` is null,
118118
* does nothing. This function is idempotent.
119119
*
120-
* @param {Element|null} elem
120+
* @param {Element|null|undefined} elem
121121
* @param {string} className
122122
*/
123123
// eslint-disable-next-line no-unused-vars

0 commit comments

Comments
 (0)