Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 207777b

Browse files
authoredFeb 5, 2025
Rollup merge of rust-lang#136263 - notriddle:notriddle/typescript2, r=fmease
rustdoc: clean up a bunch of ts-expected-error declarations in main This mostly consists of handling potentially-null input and adding more global functions to the list of globals. Follow-up for rust-lang#136161
2 parents d81701b + 2ea95f8 commit 207777b

File tree

4 files changed

+156
-148
lines changed

4 files changed

+156
-148
lines changed
 

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

Lines changed: 124 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,26 @@ function switchDisplayedElement(elemToDisplay) {
196196
removeClass(el, "hidden");
197197

198198
const mainHeading = elemToDisplay.querySelector(".main-heading");
199-
// @ts-expect-error
200-
if (mainHeading && searchState.rustdocToolbar) {
201-
// @ts-expect-error
202-
if (searchState.rustdocToolbar.parentElement) {
203-
// @ts-expect-error
204-
searchState.rustdocToolbar.parentElement.removeChild(searchState.rustdocToolbar);
199+
if (mainHeading && window.searchState.rustdocToolbar) {
200+
if (window.searchState.rustdocToolbar.parentElement) {
201+
window.searchState.rustdocToolbar.parentElement.removeChild(
202+
window.searchState.rustdocToolbar,
203+
);
205204
}
206-
// @ts-expect-error
207-
mainHeading.appendChild(searchState.rustdocToolbar);
205+
mainHeading.appendChild(window.searchState.rustdocToolbar);
208206
}
209207
}
210208

211209
function browserSupportsHistoryApi() {
212210
return window.history && typeof window.history.pushState === "function";
213211
}
214212

215-
// @ts-expect-error
213+
/**
214+
* Download CSS from the web without making it the active stylesheet.
215+
* We use this in the settings popover so that you don't get FOUC when switching.
216+
*
217+
* @param {string} cssUrl
218+
*/
216219
function preLoadCss(cssUrl) {
217220
// https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload
218221
const link = document.createElement("link");
@@ -225,7 +228,11 @@ function preLoadCss(cssUrl) {
225228
(function() {
226229
const isHelpPage = window.location.pathname.endsWith("/help.html");
227230

228-
// @ts-expect-error
231+
/**
232+
* Run a JavaScript file asynchronously.
233+
* @param {string} url
234+
* @param {function(): any} errorCallback
235+
*/
229236
function loadScript(url, errorCallback) {
230237
const script = document.createElement("script");
231238
script.src = url;
@@ -235,13 +242,12 @@ function preLoadCss(cssUrl) {
235242
document.head.append(script);
236243
}
237244

238-
if (getSettingsButton()) {
239-
// @ts-expect-error
240-
getSettingsButton().onclick = event => {
245+
const settingsButton = getSettingsButton();
246+
if (settingsButton) {
247+
settingsButton.onclick = event => {
241248
if (event.ctrlKey || event.altKey || event.metaKey) {
242249
return;
243250
}
244-
// @ts-expect-error
245251
window.hideAllModals(false);
246252
addClass(getSettingsButton(), "rotate");
247253
event.preventDefault();
@@ -470,7 +476,6 @@ function preLoadCss(cssUrl) {
470476
}
471477
return onEachLazy(implElem.parentElement.parentElement.querySelectorAll(
472478
`[id^="${assocId}"]`),
473-
// @ts-expect-error
474479
item => {
475480
const numbered = /^(.+?)-([0-9]+)$/.exec(item.id);
476481
if (item.id === assocId || (numbered && numbered[1] === assocId)) {
@@ -522,7 +527,6 @@ function preLoadCss(cssUrl) {
522527
ev.preventDefault();
523528
// @ts-expect-error
524529
searchState.defocus();
525-
// @ts-expect-error
526530
window.hideAllModals(true); // true = reset focus for tooltips
527531
}
528532

@@ -687,7 +691,6 @@ function preLoadCss(cssUrl) {
687691
//
688692
// By the way, this is only used by and useful for traits implemented automatically
689693
// (like "Send" and "Sync").
690-
// @ts-expect-error
691694
onEachLazy(synthetic_implementors.getElementsByClassName("impl"), el => {
692695
const aliases = el.getAttribute("data-aliases");
693696
if (!aliases) {
@@ -740,7 +743,6 @@ function preLoadCss(cssUrl) {
740743
code.innerHTML = struct[TEXT_IDX];
741744
addClass(code, "code-header");
742745

743-
// @ts-expect-error
744746
onEachLazy(code.getElementsByTagName("a"), elem => {
745747
const href = elem.getAttribute("href");
746748

@@ -886,15 +888,13 @@ function preLoadCss(cssUrl) {
886888
const template = document.createElement("template");
887889
template.innerHTML = text;
888890

889-
// @ts-expect-error
890891
onEachLazy(template.content.querySelectorAll("a"), elem => {
891892
const href = elem.getAttribute("href");
892893

893894
if (href && !href.startsWith("#") && !/^(?:[a-z+]+:)?\/\//.test(href)) {
894895
elem.setAttribute("href", window.rootPath + href);
895896
}
896897
});
897-
// @ts-expect-error
898898
onEachLazy(template.content.querySelectorAll("[id]"), el => {
899899
let i = 0;
900900
if (idMap.has(el.id)) {
@@ -912,7 +912,6 @@ function preLoadCss(cssUrl) {
912912
const oldHref = `#${el.id}`;
913913
const newHref = `#${el.id}-${i}`;
914914
el.id = `${el.id}-${i}`;
915-
// @ts-expect-error
916915
onEachLazy(template.content.querySelectorAll("a[href]"), link => {
917916
if (link.getAttribute("href") === oldHref) {
918917
link.href = newHref;
@@ -933,7 +932,6 @@ function preLoadCss(cssUrl) {
933932
// @ts-expect-error
934933
sidebarTraitList.append(li);
935934
} else {
936-
// @ts-expect-error
937935
onEachLazy(templateAssocItems, item => {
938936
let block = hasClass(item, "associatedtype") ? associatedTypes : (
939937
hasClass(item, "associatedconstant") ? associatedConstants : (
@@ -1040,7 +1038,6 @@ function preLoadCss(cssUrl) {
10401038
function expandAllDocs() {
10411039
const innerToggle = document.getElementById(toggleAllDocsId);
10421040
removeClass(innerToggle, "will-expand");
1043-
// @ts-expect-error
10441041
onEachLazy(document.getElementsByClassName("toggle"), e => {
10451042
if (!hasClass(e, "type-contents-toggle") && !hasClass(e, "more-examples-toggle")) {
10461043
e.open = true;
@@ -1053,7 +1050,6 @@ function preLoadCss(cssUrl) {
10531050
function collapseAllDocs() {
10541051
const innerToggle = document.getElementById(toggleAllDocsId);
10551052
addClass(innerToggle, "will-expand");
1056-
// @ts-expect-error
10571053
onEachLazy(document.getElementsByClassName("toggle"), e => {
10581054
if (e.parentNode.id !== "implementations-list" ||
10591055
(!hasClass(e, "implementors-toggle") &&
@@ -1092,7 +1088,6 @@ function preLoadCss(cssUrl) {
10921088
function setImplementorsTogglesOpen(id, open) {
10931089
const list = document.getElementById(id);
10941090
if (list !== null) {
1095-
// @ts-expect-error
10961091
onEachLazy(list.getElementsByClassName("implementors-toggle"), e => {
10971092
e.open = open;
10981093
});
@@ -1104,7 +1099,6 @@ function preLoadCss(cssUrl) {
11041099
setImplementorsTogglesOpen("blanket-implementations-list", false);
11051100
}
11061101

1107-
// @ts-expect-error
11081102
onEachLazy(document.getElementsByClassName("toggle"), e => {
11091103
if (!hideLargeItemContents && hasClass(e, "type-contents-toggle")) {
11101104
e.open = true;
@@ -1124,7 +1118,6 @@ function preLoadCss(cssUrl) {
11241118
}
11251119
onEachLazy(document.querySelectorAll(
11261120
":not(.scraped-example) > .example-wrap > pre:not(.example-line-numbers)",
1127-
// @ts-expect-error
11281121
), x => {
11291122
const parent = x.parentNode;
11301123
const line_numbers = parent.querySelectorAll(".example-line-numbers");
@@ -1145,7 +1138,6 @@ function preLoadCss(cssUrl) {
11451138

11461139
// @ts-expect-error
11471140
window.rustdoc_remove_line_numbers_from_examples = () => {
1148-
// @ts-expect-error
11491141
onEachLazy(document.querySelectorAll(".example-wrap > .example-line-numbers"), x => {
11501142
x.parentNode.removeChild(x);
11511143
});
@@ -1157,16 +1149,13 @@ function preLoadCss(cssUrl) {
11571149
}
11581150

11591151
function showSidebar() {
1160-
// @ts-expect-error
11611152
window.hideAllModals(false);
11621153
const sidebar = document.getElementsByClassName("sidebar")[0];
1163-
// @ts-expect-error
11641154
addClass(sidebar, "shown");
11651155
}
11661156

11671157
function hideSidebar() {
11681158
const sidebar = document.getElementsByClassName("sidebar")[0];
1169-
// @ts-expect-error
11701159
removeClass(sidebar, "shown");
11711160
}
11721161

@@ -1193,7 +1182,6 @@ function preLoadCss(cssUrl) {
11931182
mainElem.addEventListener("click", hideSidebar);
11941183
}
11951184

1196-
// @ts-expect-error
11971185
onEachLazy(document.querySelectorAll("a[href^='#']"), el => {
11981186
// For clicks on internal links (<A> tags with a hash property), we expand the section we're
11991187
// jumping to *before* jumping there. We can't do this in onHashChange, because it changes
@@ -1204,7 +1192,6 @@ function preLoadCss(cssUrl) {
12041192
});
12051193
});
12061194

1207-
// @ts-expect-error
12081195
onEachLazy(document.querySelectorAll(".toggle > summary:not(.hideme)"), el => {
12091196
// @ts-expect-error
12101197
el.addEventListener("click", e => {
@@ -1241,7 +1228,6 @@ function preLoadCss(cssUrl) {
12411228
clearTooltipHoverTimeout(window.CURRENT_TOOLTIP_ELEMENT);
12421229
return;
12431230
}
1244-
// @ts-expect-error
12451231
window.hideAllModals(false);
12461232
const wrapper = document.createElement("div");
12471233
if (notable_ty) {
@@ -1422,7 +1408,6 @@ function preLoadCss(cssUrl) {
14221408
}
14231409
}
14241410

1425-
// @ts-expect-error
14261411
onEachLazy(document.getElementsByClassName("tooltip"), e => {
14271412
e.onclick = () => {
14281413
e.TOOLTIP_FORCE_VISIBLE = e.TOOLTIP_FORCE_VISIBLE ? false : true;
@@ -1527,7 +1512,6 @@ function preLoadCss(cssUrl) {
15271512
// @ts-expect-error
15281513
!getSettingsButton().contains(event.relatedTarget)
15291514
) {
1530-
// @ts-expect-error
15311515
window.hidePopoverMenus();
15321516
}
15331517
}
@@ -1626,20 +1610,16 @@ function preLoadCss(cssUrl) {
16261610
*
16271611
* Pass "true" to reset focus for tooltip popovers.
16281612
*/
1629-
// @ts-expect-error
16301613
window.hideAllModals = switchFocus => {
16311614
hideSidebar();
1632-
// @ts-expect-error
16331615
window.hidePopoverMenus();
16341616
hideTooltip(switchFocus);
16351617
};
16361618

16371619
/**
16381620
* Hide all the popover menus.
16391621
*/
1640-
// @ts-expect-error
16411622
window.hidePopoverMenus = () => {
1642-
// @ts-expect-error
16431623
onEachLazy(document.querySelectorAll("rustdoc-toolbar .popover"), elem => {
16441624
elem.style.display = "none";
16451625
});
@@ -1708,7 +1688,6 @@ function preLoadCss(cssUrl) {
17081688
if (shouldShowHelp) {
17091689
showHelp();
17101690
} else {
1711-
// @ts-expect-error
17121691
window.hidePopoverMenus();
17131692
}
17141693
});
@@ -1780,30 +1759,42 @@ function preLoadCss(cssUrl) {
17801759
});
17811760
}
17821761

1783-
// Pointer capture.
1784-
//
1785-
// Resizing is a single-pointer gesture. Any secondary pointer is ignored
1786-
// @ts-expect-error
1762+
/**
1763+
* Pointer capture.
1764+
*
1765+
* Resizing is a single-pointer gesture. Any secondary pointer is ignored
1766+
*
1767+
* @type {null|number}
1768+
*/
17871769
let currentPointerId = null;
17881770

1789-
// "Desired" sidebar size.
1790-
//
1791-
// This is stashed here for window resizing. If the sidebar gets
1792-
// shrunk to maintain BODY_MIN, and then the user grows the window again,
1793-
// it gets the sidebar to restore its size.
1794-
// @ts-expect-error
1771+
/**
1772+
* "Desired" sidebar size.
1773+
*
1774+
* This is stashed here for window resizing. If the sidebar gets
1775+
* shrunk to maintain BODY_MIN, and then the user grows the window again,
1776+
* it gets the sidebar to restore its size.
1777+
*
1778+
* @type {null|number}
1779+
*/
17951780
let desiredSidebarSize = null;
17961781

1797-
// Sidebar resize debouncer.
1798-
//
1799-
// The sidebar itself is resized instantly, but the body HTML can be too
1800-
// big for that, causing reflow jank. To reduce this, we queue up a separate
1801-
// animation frame and throttle it.
1782+
/**
1783+
* Sidebar resize debouncer.
1784+
*
1785+
* The sidebar itself is resized instantly, but the body HTML can be too
1786+
* big for that, causing reflow jank. To reduce this, we queue up a separate
1787+
* animation frame and throttle it.
1788+
*
1789+
* @type {false|ReturnType<typeof setTimeout>}
1790+
*/
18021791
let pendingSidebarResizingFrame = false;
18031792

1804-
// If this page has no sidebar at all, bail out.
1793+
/** @type {HTMLElement|null} */
18051794
const resizer = document.querySelector(".sidebar-resizer");
1795+
/** @type {HTMLElement|null} */
18061796
const sidebar = document.querySelector(".sidebar");
1797+
// If this page has no sidebar at all, bail out.
18071798
if (!resizer || !sidebar) {
18081799
return;
18091800
}
@@ -1820,11 +1811,9 @@ function preLoadCss(cssUrl) {
18201811
// from settings.js, which uses a separate function. It's done here because
18211812
// the minimum sidebar size is rather uncomfortable, and it must pass
18221813
// through that size when using the shrink-to-nothing gesture.
1823-
function hideSidebar() {
1814+
const hideSidebar = function() {
18241815
if (isSrcPage) {
1825-
// @ts-expect-error
18261816
window.rustdocCloseSourceSidebar();
1827-
// @ts-expect-error
18281817
updateLocalStorage("src-sidebar-width", null);
18291818
// [RUSTDOCIMPL] CSS variable fast path
18301819
//
@@ -1837,83 +1826,73 @@ function preLoadCss(cssUrl) {
18371826
//
18381827
// So, to clear it, we need to clear all three.
18391828
document.documentElement.style.removeProperty("--src-sidebar-width");
1840-
// @ts-expect-error
18411829
sidebar.style.removeProperty("--src-sidebar-width");
1842-
// @ts-expect-error
18431830
resizer.style.removeProperty("--src-sidebar-width");
18441831
} else {
18451832
addClass(document.documentElement, "hide-sidebar");
18461833
updateLocalStorage("hide-sidebar", "true");
1847-
// @ts-expect-error
18481834
updateLocalStorage("desktop-sidebar-width", null);
18491835
document.documentElement.style.removeProperty("--desktop-sidebar-width");
1850-
// @ts-expect-error
18511836
sidebar.style.removeProperty("--desktop-sidebar-width");
1852-
// @ts-expect-error
18531837
resizer.style.removeProperty("--desktop-sidebar-width");
18541838
}
1855-
}
1839+
};
18561840

18571841
// Call this function to show the sidebar from the resize handle.
18581842
// On docs pages, this can only happen if the user has grabbed the resize
18591843
// handle, shrunk the sidebar down to nothing, and then pulls back into
18601844
// the visible range without releasing it. You can, however, grab the
18611845
// resize handle on a source page with the sidebar closed, because it
18621846
// remains visible all the time on there.
1863-
function showSidebar() {
1847+
const showSidebar = function() {
18641848
if (isSrcPage) {
1865-
// @ts-expect-error
18661849
window.rustdocShowSourceSidebar();
18671850
} else {
18681851
removeClass(document.documentElement, "hide-sidebar");
18691852
updateLocalStorage("hide-sidebar", "false");
18701853
}
1871-
}
1854+
};
18721855

18731856
/**
18741857
* Call this to set the correct CSS variable and setting.
18751858
* This function doesn't enforce size constraints. Do that before calling it!
18761859
*
18771860
* @param {number} size - CSS px width of the sidebar.
18781861
*/
1879-
function changeSidebarSize(size) {
1862+
const changeSidebarSize = function(size) {
18801863
if (isSrcPage) {
1881-
// @ts-expect-error
1882-
updateLocalStorage("src-sidebar-width", size);
1864+
updateLocalStorage("src-sidebar-width", size.toString());
18831865
// [RUSTDOCIMPL] CSS variable fast path
18841866
//
18851867
// While this property is set on the HTML element at load time,
18861868
// because the sidebar isn't actually loaded yet,
18871869
// we scope this update to the sidebar to avoid hitting a slow
18881870
// path in WebKit.
1889-
// @ts-expect-error
18901871
sidebar.style.setProperty("--src-sidebar-width", size + "px");
1891-
// @ts-expect-error
18921872
resizer.style.setProperty("--src-sidebar-width", size + "px");
18931873
} else {
1894-
// @ts-expect-error
1895-
updateLocalStorage("desktop-sidebar-width", size);
1896-
// @ts-expect-error
1874+
updateLocalStorage("desktop-sidebar-width", size.toString());
18971875
sidebar.style.setProperty("--desktop-sidebar-width", size + "px");
1898-
// @ts-expect-error
18991876
resizer.style.setProperty("--desktop-sidebar-width", size + "px");
19001877
}
1901-
}
1878+
};
19021879

19031880
// Check if the sidebar is hidden. Since src pages and doc pages have
19041881
// different settings, this function has to check that.
1905-
function isSidebarHidden() {
1882+
const isSidebarHidden = function() {
19061883
return isSrcPage ?
19071884
!hasClass(document.documentElement, "src-sidebar-expanded") :
19081885
hasClass(document.documentElement, "hide-sidebar");
1909-
}
1886+
};
19101887

1911-
// Respond to the resize handle event.
1912-
// This function enforces size constraints, and implements the
1913-
// shrink-to-nothing gesture based on thresholds defined above.
1914-
// @ts-expect-error
1915-
function resize(e) {
1916-
// @ts-expect-error
1888+
/**
1889+
* Respond to the resize handle event.
1890+
* This function enforces size constraints, and implements the
1891+
* shrink-to-nothing gesture based on thresholds defined above.
1892+
*
1893+
* @param {PointerEvent} e
1894+
*/
1895+
const resize = function(e) {
19171896
if (currentPointerId === null || currentPointerId !== e.pointerId) {
19181897
return;
19191898
}
@@ -1931,106 +1910,97 @@ function preLoadCss(cssUrl) {
19311910
changeSidebarSize(constrainedPos);
19321911
desiredSidebarSize = constrainedPos;
19331912
if (pendingSidebarResizingFrame !== false) {
1934-
// @ts-expect-error
19351913
clearTimeout(pendingSidebarResizingFrame);
19361914
}
1937-
// @ts-expect-error
19381915
pendingSidebarResizingFrame = setTimeout(() => {
1939-
// @ts-expect-error
19401916
if (currentPointerId === null || pendingSidebarResizingFrame === false) {
19411917
return;
19421918
}
19431919
pendingSidebarResizingFrame = false;
19441920
document.documentElement.style.setProperty(
19451921
"--resizing-sidebar-width",
1946-
// @ts-expect-error
19471922
desiredSidebarSize + "px",
19481923
);
19491924
}, 100);
19501925
}
1951-
}
1926+
};
19521927
// Respond to the window resize event.
19531928
window.addEventListener("resize", () => {
19541929
if (window.innerWidth < RUSTDOC_MOBILE_BREAKPOINT) {
19551930
return;
19561931
}
19571932
stopResize();
1958-
// @ts-expect-error
1959-
if (desiredSidebarSize >= (window.innerWidth - BODY_MIN)) {
1933+
if (desiredSidebarSize !== null && desiredSidebarSize >= (window.innerWidth - BODY_MIN)) {
19601934
changeSidebarSize(window.innerWidth - BODY_MIN);
1961-
// @ts-expect-error
19621935
} else if (desiredSidebarSize !== null && desiredSidebarSize > SIDEBAR_MIN) {
1963-
// @ts-expect-error
19641936
changeSidebarSize(desiredSidebarSize);
19651937
}
19661938
});
1967-
// @ts-expect-error
1968-
function stopResize(e) {
1969-
// @ts-expect-error
1939+
1940+
/**
1941+
* @param {PointerEvent=} e
1942+
*/
1943+
const stopResize = function(e) {
19701944
if (currentPointerId === null) {
19711945
return;
19721946
}
19731947
if (e) {
19741948
e.preventDefault();
19751949
}
1976-
// @ts-expect-error
19771950
desiredSidebarSize = sidebar.getBoundingClientRect().width;
1978-
// @ts-expect-error
19791951
removeClass(resizer, "active");
19801952
window.removeEventListener("pointermove", resize, false);
19811953
window.removeEventListener("pointerup", stopResize, false);
19821954
removeClass(document.documentElement, "sidebar-resizing");
19831955
document.documentElement.style.removeProperty( "--resizing-sidebar-width");
1984-
// @ts-expect-error
19851956
if (resizer.releasePointerCapture) {
1986-
// @ts-expect-error
19871957
resizer.releasePointerCapture(currentPointerId);
19881958
currentPointerId = null;
19891959
}
1990-
}
1991-
// @ts-expect-error
1992-
function initResize(e) {
1993-
// @ts-expect-error
1960+
};
1961+
1962+
/**
1963+
* @param {PointerEvent} e
1964+
*/
1965+
const initResize = function(e) {
19941966
if (currentPointerId !== null || e.altKey || e.ctrlKey || e.metaKey || e.button !== 0) {
19951967
return;
19961968
}
1997-
// @ts-expect-error
19981969
if (resizer.setPointerCapture) {
1999-
// @ts-expect-error
20001970
resizer.setPointerCapture(e.pointerId);
2001-
// @ts-expect-error
20021971
if (!resizer.hasPointerCapture(e.pointerId)) {
20031972
// unable to capture pointer; something else has it
20041973
// on iOS, this usually means you long-clicked a link instead
2005-
// @ts-expect-error
20061974
resizer.releasePointerCapture(e.pointerId);
20071975
return;
20081976
}
20091977
currentPointerId = e.pointerId;
20101978
}
2011-
// @ts-expect-error
20121979
window.hideAllModals(false);
20131980
e.preventDefault();
20141981
window.addEventListener("pointermove", resize, false);
20151982
window.addEventListener("pointercancel", stopResize, false);
20161983
window.addEventListener("pointerup", stopResize, false);
2017-
// @ts-expect-error
20181984
addClass(resizer, "active");
20191985
addClass(document.documentElement, "sidebar-resizing");
2020-
// @ts-expect-error
20211986
const pos = e.clientX - sidebar.offsetLeft - 3;
20221987
document.documentElement.style.setProperty( "--resizing-sidebar-width", pos + "px");
20231988
desiredSidebarSize = null;
2024-
}
1989+
};
20251990
resizer.addEventListener("pointerdown", initResize, false);
20261991
}());
20271992

20281993
// This section handles the copy button that appears next to the path breadcrumbs
20291994
// and the copy buttons on the code examples.
20301995
(function() {
20311996
// Common functions to copy buttons.
2032-
// @ts-expect-error
1997+
/**
1998+
* @param {string|null} content
1999+
*/
20332000
function copyContentToClipboard(content) {
2001+
if (content === null) {
2002+
return;
2003+
}
20342004
const el = document.createElement("textarea");
20352005
el.value = content;
20362006
el.setAttribute("readonly", "");
@@ -2044,15 +2014,17 @@ function preLoadCss(cssUrl) {
20442014
document.body.removeChild(el);
20452015
}
20462016

2047-
// @ts-expect-error
2017+
/**
2018+
* @param {HTMLElement & {reset_button_timeout?: ReturnType<typeof setTimeout>}} button
2019+
*/
20482020
function copyButtonAnimation(button) {
20492021
button.classList.add("clicked");
20502022

20512023
if (button.reset_button_timeout !== undefined) {
2052-
window.clearTimeout(button.reset_button_timeout);
2024+
clearTimeout(button.reset_button_timeout);
20532025
}
20542026

2055-
button.reset_button_timeout = window.setTimeout(() => {
2027+
button.reset_button_timeout = setTimeout(() => {
20562028
button.reset_button_timeout = undefined;
20572029
button.classList.remove("clicked");
20582030
}, 1000);
@@ -2067,9 +2039,7 @@ function preLoadCss(cssUrl) {
20672039
// Most page titles are '<Item> in <path::to::module> - Rust', except
20682040
// modules (which don't have the first part) and keywords/primitives
20692041
// (which don't have a module path)
2070-
// @ts-expect-error
2071-
const title = document.querySelector("title").textContent.replace(" - Rust", "");
2072-
const [item, module] = title.split(" in ");
2042+
const [item, module] = document.title.split(" in ");
20732043
const path = [item];
20742044
if (module !== undefined) {
20752045
path.unshift(module);
@@ -2079,8 +2049,10 @@ function preLoadCss(cssUrl) {
20792049
copyButtonAnimation(but);
20802050
};
20812051

2082-
// Copy buttons on code examples.
2083-
// @ts-expect-error
2052+
/**
2053+
* Copy buttons on code examples.
2054+
* @param {HTMLElement|null} codeElem
2055+
*/
20842056
function copyCode(codeElem) {
20852057
if (!codeElem) {
20862058
// Should never happen, but the world is a dark and dangerous place.
@@ -2089,23 +2061,34 @@ function preLoadCss(cssUrl) {
20892061
copyContentToClipboard(codeElem.textContent);
20902062
}
20912063

2092-
// @ts-expect-error
2064+
/**
2065+
* @param {UIEvent} event
2066+
* @returns {HTMLElement|null}
2067+
*/
20932068
function getExampleWrap(event) {
2094-
let elem = event.target;
2095-
while (!hasClass(elem, "example-wrap")) {
2096-
if (elem === document.body ||
2097-
elem.tagName === "A" ||
2098-
elem.tagName === "BUTTON" ||
2099-
hasClass(elem, "docblock")
2100-
) {
2101-
return null;
2069+
const target = event.target;
2070+
if (target instanceof HTMLElement) {
2071+
/** @type {HTMLElement|null} */
2072+
let elem = target;
2073+
while (elem !== null && !hasClass(elem, "example-wrap")) {
2074+
if (elem === document.body ||
2075+
elem.tagName === "A" ||
2076+
elem.tagName === "BUTTON" ||
2077+
hasClass(elem, "docblock")
2078+
) {
2079+
return null;
2080+
}
2081+
elem = elem.parentElement;
21022082
}
2103-
elem = elem.parentElement;
2083+
return elem;
2084+
} else {
2085+
return null;
21042086
}
2105-
return elem;
21062087
}
21072088

2108-
// @ts-expect-error
2089+
/**
2090+
* @param {UIEvent} event
2091+
*/
21092092
function addCopyButton(event) {
21102093
const elem = getExampleWrap(event);
21112094
if (elem === null) {
@@ -2132,15 +2115,17 @@ function preLoadCss(cssUrl) {
21322115
});
21332116
parent.appendChild(copyButton);
21342117

2135-
if (!elem.parentElement.classList.contains("scraped-example")) {
2118+
if (!elem.parentElement || !elem.parentElement.classList.contains("scraped-example") ||
2119+
!window.updateScrapedExample) {
21362120
return;
21372121
}
21382122
const scrapedWrapped = elem.parentElement;
2139-
// @ts-expect-error
21402123
window.updateScrapedExample(scrapedWrapped, parent);
21412124
}
21422125

2143-
// @ts-expect-error
2126+
/**
2127+
* @param {UIEvent} event
2128+
*/
21442129
function showHideCodeExampleButtons(event) {
21452130
const elem = getExampleWrap(event);
21462131
if (elem === null) {
@@ -2159,7 +2144,6 @@ function preLoadCss(cssUrl) {
21592144
buttons.classList.toggle("keep-visible");
21602145
}
21612146

2162-
// @ts-expect-error
21632147
onEachLazy(document.querySelectorAll(".docblock .example-wrap"), elem => {
21642148
elem.addEventListener("mouseover", addCopyButton);
21652149
elem.addEventListener("click", showHideCodeExampleButtons);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ declare global {
2020
* As a multi-page application, we know this never changes once set.
2121
*/
2222
currentCrate: string|null;
23+
/**
24+
* Hide popovers, tooltips, or the mobile sidebar.
25+
*/
26+
hideAllModals: function(boolean),
27+
/**
28+
* Hide popovers, but leave other modals alone.
29+
*/
30+
hidePopoverMenus: function(),
31+
/**
32+
* Hide the source page sidebar. If it's already closed,
33+
* or if this is a docs page, this function does nothing.
34+
*/
35+
rustdocCloseSourceSidebar: function(),
36+
/**
37+
* Show the source page sidebar. If it's already opened,
38+
* or if this is a docs page, this function does nothing.
39+
*/
40+
rustdocShowSourceSidebar: function(),
41+
/**
42+
* Set up event listeners for a scraped source example.
43+
*/
44+
updateScrapedExample?: function(HTMLElement, HTMLElement),
2345
}
2446
interface HTMLElement {
2547
/** 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
@@ -4732,10 +4732,8 @@ function printTab(nb) {
47324732
// Corrections only kick in on type-based searches.
47334733
const correctionsElem = document.getElementsByClassName("search-corrections");
47344734
if (isTypeSearch) {
4735-
// @ts-expect-error
47364735
removeClass(correctionsElem[0], "hidden");
47374736
} else {
4738-
// @ts-expect-error
47394737
addClass(correctionsElem[0], "hidden");
47404738
}
47414739
} else if (nb !== 0) {

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function hasClass(elem, className) {
5959
* Add a class to a DOM Element. If `elem` is null,
6060
* does nothing. This function is idempotent.
6161
*
62-
* @param {HTMLElement|null} elem
62+
* @param {Element|null} elem
6363
* @param {string} className
6464
*/
6565
function addClass(elem, className) {
@@ -72,7 +72,7 @@ function addClass(elem, className) {
7272
* Remove a class from a DOM Element. If `elem` is null,
7373
* does nothing. This function is idempotent.
7474
*
75-
* @param {HTMLElement|null} elem
75+
* @param {Element|null} elem
7676
* @param {string} className
7777
*/
7878
// eslint-disable-next-line no-unused-vars
@@ -85,7 +85,7 @@ function removeClass(elem, className) {
8585
/**
8686
* Run a callback for every element of an Array.
8787
* @param {Array<?>} arr - The array to iterate over
88-
* @param {function(?): boolean|undefined} func - The callback
88+
* @param {function(?): boolean|void} func - The callback
8989
*/
9090
function onEach(arr, func) {
9191
for (const elem of arr) {
@@ -103,7 +103,7 @@ function onEach(arr, func) {
103103
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection
104104
* https://developer.mozilla.org/en-US/docs/Web/API/NodeList
105105
* @param {NodeList|HTMLCollection} lazyArray - An array to iterate over
106-
* @param {function(?): boolean} func - The callback
106+
* @param {function(?): boolean|void} func - The callback
107107
*/
108108
// eslint-disable-next-line no-unused-vars
109109
function onEachLazy(lazyArray, func) {
@@ -119,11 +119,15 @@ function onEachLazy(lazyArray, func) {
119119
* If localStorage is disabled, this function does nothing.
120120
*
121121
* @param {string} name
122-
* @param {string} value
122+
* @param {string|null} value
123123
*/
124124
function updateLocalStorage(name, value) {
125125
try {
126-
window.localStorage.setItem("rustdoc-" + name, value);
126+
if (value === null) {
127+
window.localStorage.removeItem("rustdoc-" + name);
128+
} else {
129+
window.localStorage.setItem("rustdoc-" + name, value);
130+
}
127131
} catch (e) {
128132
// localStorage is not accessible, do nothing
129133
}

0 commit comments

Comments
 (0)
Please sign in to comment.