From 5d28ce45cacbe3825bc5c2b1abb2f701bfa9ec05 Mon Sep 17 00:00:00 2001 From: binarycat Date: Thu, 21 Aug 2025 22:08:11 -0500 Subject: [PATCH 1/7] typecheck window.searchIndex --- src/librustdoc/html/static/js/main.js | 1 - src/librustdoc/html/static/js/rustdoc.d.ts | 2 ++ src/librustdoc/html/static/js/search.js | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 20fc6b75d3773..3dbddb8176b6f 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -409,7 +409,6 @@ function preLoadCss(cssUrl) { searchLoaded = true; // @ts-expect-error window.rr_ = data => { - // @ts-expect-error window.searchIndex = data; }; if (!window.StringdexOnload) { diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index 28852125fe17a..c9253e2cdc598 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -93,6 +93,8 @@ declare global { pending_type_impls?: rustdoc.TypeImpls, rustdoc_add_line_numbers_to_examples?: function(), rustdoc_remove_line_numbers_from_examples?: function(), + /** JSON-encoded raw search index */ + searchIndex: string, } interface HTMLElement { /** Used by the popover tooltip code. */ diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 42b87d562529b..0cc29f03b1db3 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -5244,7 +5244,6 @@ if (typeof window !== "undefined") { // search.index/root is loaded by main.js, so // this script doesn't need to launch it, but // must pick it up - // @ts-ignore if (window.searchIndex) { // @ts-ignore window.rr_(window.searchIndex); From 4b73a7ec7887f3ca5116d15cb4a277040708337c Mon Sep 17 00:00:00 2001 From: binarycat Date: Thu, 21 Aug 2025 22:19:46 -0500 Subject: [PATCH 2/7] typecheck window.rr_ --- src/librustdoc/html/static/js/main.js | 1 - src/librustdoc/html/static/js/rustdoc.d.ts | 2 ++ src/librustdoc/html/static/js/search.js | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 3dbddb8176b6f..9dbcc9cd138ba 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -407,7 +407,6 @@ function preLoadCss(cssUrl) { function loadSearch() { if (!searchLoaded) { searchLoaded = true; - // @ts-expect-error window.rr_ = data => { window.searchIndex = data; }; diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index c9253e2cdc598..8dfbb2bb9ed29 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -95,6 +95,8 @@ declare global { rustdoc_remove_line_numbers_from_examples?: function(), /** JSON-encoded raw search index */ searchIndex: string, + /** Used in search index shards in order to load data into the in-memory database */ + rr_: function(string), } interface HTMLElement { /** Used by the popover tooltip code. */ diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 0cc29f03b1db3..bd9dfe4588935 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -5245,7 +5245,6 @@ if (typeof window !== "undefined") { // this script doesn't need to launch it, but // must pick it up if (window.searchIndex) { - // @ts-ignore window.rr_(window.searchIndex); } }, From 6c22ef501e6df233bb71e7bdac683f547571ba62 Mon Sep 17 00:00:00 2001 From: binarycat Date: Thu, 21 Aug 2025 22:42:37 -0500 Subject: [PATCH 3/7] typecheck window.NOTABLE_TRAITS --- src/librustdoc/html/static/js/main.js | 2 -- src/librustdoc/html/static/js/rustdoc.d.ts | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 9dbcc9cd138ba..a7a3da0a4bc20 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -1327,11 +1327,9 @@ function preLoadCss(cssUrl) { */ function showTooltip(e) { const notable_ty = e.getAttribute("data-notable-ty"); - // @ts-expect-error if (!window.NOTABLE_TRAITS && notable_ty) { const data = document.getElementById("notable-traits-data"); if (data) { - // @ts-expect-error window.NOTABLE_TRAITS = JSON.parse(data.innerText); } else { throw new Error("showTooltip() called with notable without any notable traits!"); diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index 8dfbb2bb9ed29..3f78ad535af78 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -28,6 +28,8 @@ declare global { currentTheme: HTMLLinkElement|null; /** Generated in `render/context.rs` */ SIDEBAR_ITEMS?: { [key: string]: string[] }; + /** Notable trait data */ + NOTABLE_TRAITS?: { [key: string]: string }; /** Used by the popover tooltip code. */ RUSTDOC_TOOLTIP_HOVER_MS: number; /** Used by the popover tooltip code. */ From dfec2bbdca6e7b83b108e87753d4c6dde5d758fd Mon Sep 17 00:00:00 2001 From: binarycat Date: Thu, 21 Aug 2025 22:54:43 -0500 Subject: [PATCH 4/7] typecheck window.CURRENT_TOOLTIP_ELEMENT --- src/librustdoc/html/static/js/main.js | 36 +++------------------- src/librustdoc/html/static/js/rustdoc.d.ts | 1 + src/librustdoc/html/static/js/storage.js | 2 +- 3 files changed, 7 insertions(+), 32 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index a7a3da0a4bc20..e55265f68b461 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -1275,13 +1275,11 @@ function preLoadCss(cssUrl) { } window.addEventListener("resize", () => { - // @ts-expect-error if (window.CURRENT_TOOLTIP_ELEMENT) { // As a workaround to the behavior of `contains: layout` used in doc togglers, // tooltip popovers are positioned using javascript. // // This means when the window is resized, we need to redo the layout. - // @ts-expect-error const base = window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE; const force_visible = base.TOOLTIP_FORCE_VISIBLE; hideTooltip(false); @@ -1337,14 +1335,15 @@ function preLoadCss(cssUrl) { } // Make this function idempotent. If the tooltip is already shown, avoid doing extra work // and leave it alone. - // @ts-expect-error if (window.CURRENT_TOOLTIP_ELEMENT && window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE === e) { - // @ts-expect-error clearTooltipHoverTimeout(window.CURRENT_TOOLTIP_ELEMENT); return; } window.hideAllModals(false); - const wrapper = document.createElement("div"); + // use Object.assign to make sure the object has the correct type + // with all of the correct fields before it is assigned to a variable, + // as typescript has no way to change the type of a variable once it is initialized. + const wrapper = Object.assign(document.createElement("div"), {TOOLTIP_BASE: e}); if (notable_ty) { wrapper.innerHTML = "
" + // @ts-expect-error @@ -1390,11 +1389,7 @@ function preLoadCss(cssUrl) { ); } wrapper.style.visibility = ""; - // @ts-expect-error window.CURRENT_TOOLTIP_ELEMENT = wrapper; - // @ts-expect-error - window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE = e; - // @ts-expect-error clearTooltipHoverTimeout(window.CURRENT_TOOLTIP_ELEMENT); wrapper.onpointerenter = ev => { // If this is a synthetic touch event, ignore it. A click event will be along shortly. @@ -1429,19 +1424,15 @@ function preLoadCss(cssUrl) { */ function setTooltipHoverTimeout(element, show) { clearTooltipHoverTimeout(element); - // @ts-expect-error if (!show && !window.CURRENT_TOOLTIP_ELEMENT) { // To "hide" an already hidden element, just cancel its timeout. return; } - // @ts-expect-error if (show && window.CURRENT_TOOLTIP_ELEMENT) { // To "show" an already visible element, just cancel its timeout. return; } - // @ts-expect-error if (window.CURRENT_TOOLTIP_ELEMENT && - // @ts-expect-error window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE !== element) { // Don't do anything if another tooltip is already visible. return; @@ -1464,7 +1455,6 @@ function preLoadCss(cssUrl) { */ function clearTooltipHoverTimeout(element) { if (element.TOOLTIP_HOVER_TIMEOUT !== undefined) { - // @ts-expect-error removeClass(window.CURRENT_TOOLTIP_ELEMENT, "fade-out"); clearTimeout(element.TOOLTIP_HOVER_TIMEOUT); delete element.TOOLTIP_HOVER_TIMEOUT; @@ -1473,15 +1463,10 @@ function preLoadCss(cssUrl) { // @ts-expect-error function tooltipBlurHandler(event) { - // @ts-expect-error if (window.CURRENT_TOOLTIP_ELEMENT && - // @ts-expect-error !window.CURRENT_TOOLTIP_ELEMENT.contains(document.activeElement) && - // @ts-expect-error !window.CURRENT_TOOLTIP_ELEMENT.contains(event.relatedTarget) && - // @ts-expect-error !window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.contains(document.activeElement) && - // @ts-expect-error !window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.contains(event.relatedTarget) ) { // Work around a difference in the focus behaviour between Firefox, Chrome, and Safari. @@ -1503,30 +1488,22 @@ function preLoadCss(cssUrl) { * If set to `false`, leave keyboard focus alone. */ function hideTooltip(focus) { - // @ts-expect-error if (window.CURRENT_TOOLTIP_ELEMENT) { - // @ts-expect-error if (window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.TOOLTIP_FORCE_VISIBLE) { if (focus) { - // @ts-expect-error window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.focus(); } - // @ts-expect-error window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.TOOLTIP_FORCE_VISIBLE = false; } - // @ts-expect-error document.body.removeChild(window.CURRENT_TOOLTIP_ELEMENT); - // @ts-expect-error clearTooltipHoverTimeout(window.CURRENT_TOOLTIP_ELEMENT); - // @ts-expect-error - window.CURRENT_TOOLTIP_ELEMENT = null; + window.CURRENT_TOOLTIP_ELEMENT = undefined; } } onEachLazy(document.getElementsByClassName("tooltip"), e => { e.onclick = () => { e.TOOLTIP_FORCE_VISIBLE = e.TOOLTIP_FORCE_VISIBLE ? false : true; - // @ts-expect-error if (window.CURRENT_TOOLTIP_ELEMENT && !e.TOOLTIP_FORCE_VISIBLE) { hideTooltip(true); } else { @@ -1562,9 +1539,7 @@ function preLoadCss(cssUrl) { if (ev.pointerType !== "mouse") { return; } - // @ts-expect-error if (!e.TOOLTIP_FORCE_VISIBLE && window.CURRENT_TOOLTIP_ELEMENT && - // @ts-expect-error !window.CURRENT_TOOLTIP_ELEMENT.contains(ev.relatedTarget)) { // Tooltip pointer leave gesture: // @@ -1597,7 +1572,6 @@ function preLoadCss(cssUrl) { // * https://www.nngroup.com/articles/tooltip-guidelines/ // * https://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown setTooltipHoverTimeout(e, false); - // @ts-expect-error addClass(window.CURRENT_TOOLTIP_ELEMENT, "fade-out"); } }; diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index 3f78ad535af78..3ac10742e4152 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -30,6 +30,7 @@ declare global { SIDEBAR_ITEMS?: { [key: string]: string[] }; /** Notable trait data */ NOTABLE_TRAITS?: { [key: string]: string }; + CURRENT_TOOLTIP_ELEMENT?: HTMLElement & { TOOLTIP_BASE: HTMLElement }; /** Used by the popover tooltip code. */ RUSTDOC_TOOLTIP_HOVER_MS: number; /** Used by the popover tooltip code. */ diff --git a/src/librustdoc/html/static/js/storage.js b/src/librustdoc/html/static/js/storage.js index c055eb0f8081d..40ab8be03c93d 100644 --- a/src/librustdoc/html/static/js/storage.js +++ b/src/librustdoc/html/static/js/storage.js @@ -117,7 +117,7 @@ function addClass(elem, className) { * Remove a class from a DOM Element. If `elem` is null, * does nothing. This function is idempotent. * - * @param {Element|null} elem + * @param {Element|null|undefined} elem * @param {string} className */ // eslint-disable-next-line no-unused-vars From cb572cb876cb901f279fbe64d6e7a6462ac8097c Mon Sep 17 00:00:00 2001 From: binarycat Date: Thu, 21 Aug 2025 22:59:37 -0500 Subject: [PATCH 5/7] typecheck tooltipBlurHandler --- src/librustdoc/html/static/js/main.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index e55265f68b461..bfa9e78994428 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -1461,7 +1461,9 @@ function preLoadCss(cssUrl) { } } - // @ts-expect-error + /** + * @param {Event & { relatedTarget: Node }} event + */ function tooltipBlurHandler(event) { if (window.CURRENT_TOOLTIP_ELEMENT && !window.CURRENT_TOOLTIP_ELEMENT.contains(document.activeElement) && From 4a2d6dfe26706b058b045dd2d4cc73832ede7728 Mon Sep 17 00:00:00 2001 From: binarycat Date: Thu, 21 Aug 2025 23:04:26 -0500 Subject: [PATCH 6/7] typecheck: add nonnull around element known to exist --- src/librustdoc/html/static/js/main.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index bfa9e78994428..c09cc5da35e33 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -1679,8 +1679,7 @@ function preLoadCss(cssUrl) { if (isHelpPage) { const help_section = document.createElement("section"); help_section.appendChild(container); - // @ts-expect-error - document.getElementById("main-content").appendChild(help_section); + nonnull(document.getElementById("main-content")).appendChild(help_section); } else { onEachLazy(document.getElementsByClassName("help-menu"), menu => { if (menu.offsetWidth !== 0) { From 771c6415b295407fb380c859b243354d61d41cf7 Mon Sep 17 00:00:00 2001 From: binarycat Date: Thu, 21 Aug 2025 23:05:50 -0500 Subject: [PATCH 7/7] main.js: only call window.rustdocToggleSrcSidebar if it exists --- src/librustdoc/html/static/js/main.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index c09cc5da35e33..4fcba5f120ba2 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -1825,8 +1825,7 @@ function preLoadCss(cssUrl) { sidebarButton.addEventListener("click", e => { removeClass(document.documentElement, "hide-sidebar"); updateLocalStorage("hide-sidebar", "false"); - if (document.querySelector(".rustdoc.src")) { - // @ts-expect-error + if (window.rustdocToggleSrcSidebar) { window.rustdocToggleSrcSidebar(); } e.preventDefault();