Skip to content

remove a bunch of @js-expect-error and generally make rustdoc js more typechecked. #136372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 4 additions & 21 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ const ALTERNATIVE_DISPLAY_ID = "alternative-display";
const NOT_DISPLAYED_ID = "not-displayed";
const HELP_BUTTON_ID = "help-button";

/**
* @returns {HTMLElement}
*/
function getSettingsButton() {
// @ts-expect-error
return document.getElementById(SETTINGS_BUTTON_ID);
}

Expand Down Expand Up @@ -236,7 +240,6 @@ function preLoadCss(cssUrl) {
}

if (getSettingsButton()) {
// @ts-expect-error
getSettingsButton().onclick = event => {
if (event.ctrlKey || event.altKey || event.metaKey) {
return;
Expand Down Expand Up @@ -470,7 +473,6 @@ function preLoadCss(cssUrl) {
}
return onEachLazy(implElem.parentElement.parentElement.querySelectorAll(
`[id^="${assocId}"]`),
// @ts-expect-error
item => {
const numbered = /^(.+?)-([0-9]+)$/.exec(item.id);
if (item.id === assocId || (numbered && numbered[1] === assocId)) {
Expand Down Expand Up @@ -687,7 +689,6 @@ function preLoadCss(cssUrl) {
//
// By the way, this is only used by and useful for traits implemented automatically
// (like "Send" and "Sync").
// @ts-expect-error
onEachLazy(synthetic_implementors.getElementsByClassName("impl"), el => {
const aliases = el.getAttribute("data-aliases");
if (!aliases) {
Expand Down Expand Up @@ -740,7 +741,6 @@ function preLoadCss(cssUrl) {
code.innerHTML = struct[TEXT_IDX];
addClass(code, "code-header");

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

Expand Down Expand Up @@ -886,15 +886,13 @@ function preLoadCss(cssUrl) {
const template = document.createElement("template");
template.innerHTML = text;

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

if (href && !href.startsWith("#") && !/^(?:[a-z+]+:)?\/\//.test(href)) {
elem.setAttribute("href", window.rootPath + href);
}
});
// @ts-expect-error
onEachLazy(template.content.querySelectorAll("[id]"), el => {
let i = 0;
if (idMap.has(el.id)) {
Expand All @@ -912,7 +910,6 @@ function preLoadCss(cssUrl) {
const oldHref = `#${el.id}`;
const newHref = `#${el.id}-${i}`;
el.id = `${el.id}-${i}`;
// @ts-expect-error
onEachLazy(template.content.querySelectorAll("a[href]"), link => {
if (link.getAttribute("href") === oldHref) {
link.href = newHref;
Expand All @@ -933,7 +930,6 @@ function preLoadCss(cssUrl) {
// @ts-expect-error
sidebarTraitList.append(li);
} else {
// @ts-expect-error
onEachLazy(templateAssocItems, item => {
let block = hasClass(item, "associatedtype") ? associatedTypes : (
hasClass(item, "associatedconstant") ? associatedConstants : (
Expand Down Expand Up @@ -1040,7 +1036,6 @@ function preLoadCss(cssUrl) {
function expandAllDocs() {
const innerToggle = document.getElementById(toggleAllDocsId);
removeClass(innerToggle, "will-expand");
// @ts-expect-error
onEachLazy(document.getElementsByClassName("toggle"), e => {
if (!hasClass(e, "type-contents-toggle") && !hasClass(e, "more-examples-toggle")) {
e.open = true;
Expand All @@ -1053,7 +1048,6 @@ function preLoadCss(cssUrl) {
function collapseAllDocs() {
const innerToggle = document.getElementById(toggleAllDocsId);
addClass(innerToggle, "will-expand");
// @ts-expect-error
onEachLazy(document.getElementsByClassName("toggle"), e => {
if (e.parentNode.id !== "implementations-list" ||
(!hasClass(e, "implementors-toggle") &&
Expand Down Expand Up @@ -1092,7 +1086,6 @@ function preLoadCss(cssUrl) {
function setImplementorsTogglesOpen(id, open) {
const list = document.getElementById(id);
if (list !== null) {
// @ts-expect-error
onEachLazy(list.getElementsByClassName("implementors-toggle"), e => {
e.open = open;
});
Expand All @@ -1104,7 +1097,6 @@ function preLoadCss(cssUrl) {
setImplementorsTogglesOpen("blanket-implementations-list", false);
}

// @ts-expect-error
onEachLazy(document.getElementsByClassName("toggle"), e => {
if (!hideLargeItemContents && hasClass(e, "type-contents-toggle")) {
e.open = true;
Expand All @@ -1124,7 +1116,6 @@ function preLoadCss(cssUrl) {
}
onEachLazy(document.querySelectorAll(
":not(.scraped-example) > .example-wrap > pre:not(.example-line-numbers)",
// @ts-expect-error
), x => {
const parent = x.parentNode;
const line_numbers = parent.querySelectorAll(".example-line-numbers");
Expand All @@ -1145,7 +1136,6 @@ function preLoadCss(cssUrl) {

// @ts-expect-error
window.rustdoc_remove_line_numbers_from_examples = () => {
// @ts-expect-error
onEachLazy(document.querySelectorAll(".example-wrap > .example-line-numbers"), x => {
x.parentNode.removeChild(x);
});
Expand Down Expand Up @@ -1193,7 +1183,6 @@ function preLoadCss(cssUrl) {
mainElem.addEventListener("click", hideSidebar);
}

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

// @ts-expect-error
onEachLazy(document.querySelectorAll(".toggle > summary:not(.hideme)"), el => {
// @ts-expect-error
el.addEventListener("click", e => {
Expand Down Expand Up @@ -1422,7 +1410,6 @@ function preLoadCss(cssUrl) {
}
}

// @ts-expect-error
onEachLazy(document.getElementsByClassName("tooltip"), e => {
e.onclick = () => {
e.TOOLTIP_FORCE_VISIBLE = e.TOOLTIP_FORCE_VISIBLE ? false : true;
Expand Down Expand Up @@ -1522,9 +1509,7 @@ function preLoadCss(cssUrl) {
if (!getHelpButton().contains(document.activeElement) &&
// @ts-expect-error
!getHelpButton().contains(event.relatedTarget) &&
// @ts-expect-error
!getSettingsButton().contains(document.activeElement) &&
// @ts-expect-error
!getSettingsButton().contains(event.relatedTarget)
) {
// @ts-expect-error
Expand Down Expand Up @@ -1639,7 +1624,6 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
*/
// @ts-expect-error
window.hidePopoverMenus = () => {
// @ts-expect-error
onEachLazy(document.querySelectorAll("rustdoc-toolbar .popover"), elem => {
elem.style.display = "none";
});
Expand Down Expand Up @@ -2159,7 +2143,6 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
buttons.classList.toggle("keep-visible");
}

// @ts-expect-error
onEachLazy(document.querySelectorAll(".docblock .example-wrap"), elem => {
elem.addEventListener("mouseover", addCopyButton);
elem.addEventListener("click", showHideCodeExampleButtons);
Expand Down
9 changes: 7 additions & 2 deletions src/librustdoc/html/static/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
(function() {
const isSettingsPage = window.location.pathname.endsWith("/settings.html");

/**
* @param {string} settingName
* @param {bool} value
*/
function changeSetting(settingName, value) {
if (settingName === "theme") {
const useSystem = value === "system preference" ? "true" : "false";
Expand Down Expand Up @@ -171,7 +175,7 @@
/**
* This function builds the "settings page" and returns the generated HTML element.
*
* @return {HTMLElement}
* @returns {HTMLElement}
*/
function buildSettingsPage() {
const theme_names = getVar("themes").split(",").filter(t => t);
Expand Down Expand Up @@ -293,7 +297,8 @@
if (!isSettingsPage) {
// We replace the existing "onclick" callback.
const settingsButton = getSettingsButton();
const settingsMenu = document.getElementById("settings");
// tell typescript this can never be null
const settingsMenu = /** @type {HTMLElement} */ (document.getElementById("settings"));
settingsButton.onclick = event => {
if (settingsMenu.contains(event.target)) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/static/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function removeClass(elem, className) {
/**
* Run a callback for every element of an Array.
* @param {Array<?>} arr - The array to iterate over
* @param {function(?): boolean|undefined} func - The callback
* @param {function(?): boolean|void} func - The callback
*/
function onEach(arr, func) {
for (const elem of arr) {
Expand All @@ -103,7 +103,7 @@ function onEach(arr, func) {
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection
* https://developer.mozilla.org/en-US/docs/Web/API/NodeList
* @param {NodeList|HTMLCollection} lazyArray - An array to iterate over
* @param {function(?): boolean} func - The callback
* @param {function(?): boolean|void} func - The callback
*/
// eslint-disable-next-line no-unused-vars
function onEachLazy(lazyArray, func) {
Expand Down
Loading