Skip to content
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

Rustdoc: remove a bunch of @ts-expect-error from main.js #138318

Merged
merged 11 commits into from
Mar 13, 2025
Merged
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
64 changes: 27 additions & 37 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Local js definitions:
/* global addClass, getSettingValue, hasClass, searchState, updateLocalStorage */
/* global addClass, getSettingValue, hasClass, updateLocalStorage */
/* global onEachLazy, removeClass, getVar */

"use strict";
Expand Down Expand Up @@ -121,12 +121,9 @@ function getNakedUrl() {
* doesn't have a parent node.
*
* @param {HTMLElement} newNode
* @param {HTMLElement} referenceNode
* @param {HTMLElement & { parentNode: HTMLElement }} referenceNode
*/
function insertAfter(newNode, referenceNode) {
// You're not allowed to pass an element with no parent.
// I dunno how to make TS's typechecker see that.
// @ts-expect-error
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

Expand Down Expand Up @@ -305,11 +302,10 @@ function preLoadCss(cssUrl) {
window.searchState.timeout = null;
}
},
// @ts-expect-error
isDisplayed: () => {
const outputElement = window.searchState.outputElement();
return outputElement &&
outputElement.parentElement &&
return !!outputElement &&
!!outputElement.parentElement &&
outputElement.parentElement.id === ALTERNATIVE_DISPLAY_ID;
},
// Sets the focus on the search bar at the top of the page
Expand All @@ -325,8 +321,6 @@ function preLoadCss(cssUrl) {
search = window.searchState.outputElement();
}
switchDisplayedElement(search);
// @ts-expect-error
window.searchState.mouseMovedAfterSearch = false;
document.title = window.searchState.title;
},
removeQueryParameters: () => {
Expand Down Expand Up @@ -503,43 +497,49 @@ function preLoadCss(cssUrl) {
handleHashes(ev);
}

// @ts-expect-error
/**
* @param {HTMLElement|null} elem
*/
function openParentDetails(elem) {
while (elem) {
if (elem.tagName === "DETAILS") {
// @ts-expect-error
elem.open = true;
}
elem = elem.parentNode;
elem = elem.parentElement;
}
}

// @ts-expect-error
/**
* @param {string} id
*/
function expandSection(id) {
openParentDetails(document.getElementById(id));
}

// @ts-expect-error
/**
* @param {KeyboardEvent} ev
*/
function handleEscape(ev) {
// @ts-expect-error
searchState.clearInputTimeout();
// @ts-expect-error
searchState.hideResults();
window.searchState.clearInputTimeout();
window.searchState.hideResults();
ev.preventDefault();
// @ts-expect-error
searchState.defocus();
window.searchState.defocus();
window.hideAllModals(true); // true = reset focus for tooltips
}

// @ts-expect-error
/**
* @param {KeyboardEvent} ev
*/
function handleShortcut(ev) {
// Don't interfere with browser shortcuts
const disableShortcuts = getSettingValue("disable-shortcuts") === "true";
if (ev.ctrlKey || ev.altKey || ev.metaKey || disableShortcuts) {
return;
}

// @ts-expect-error
if (document.activeElement.tagName === "INPUT" &&
if (document.activeElement &&
document.activeElement.tagName === "INPUT" &&
// @ts-expect-error
document.activeElement.type !== "checkbox" &&
// @ts-expect-error
Expand All @@ -559,8 +559,7 @@ function preLoadCss(cssUrl) {
case "S":
case "/":
ev.preventDefault();
// @ts-expect-error
searchState.focus();
window.searchState.focus();
break;

case "+":
Expand All @@ -586,7 +585,6 @@ function preLoadCss(cssUrl) {
document.addEventListener("keydown", handleShortcut);

function addSidebarItems() {
// @ts-expect-error
if (!window.SIDEBAR_ITEMS) {
return;
}
Expand Down Expand Up @@ -675,7 +673,6 @@ function preLoadCss(cssUrl) {
}

// <https://github.com/search?q=repo%3Arust-lang%2Frust+[RUSTDOCIMPL]+trait.impl&type=code>
// @ts-expect-error
window.register_implementors = imp => {
const implementors = document.getElementById("implementors-list");
const synthetic_implementors = document.getElementById("synthetic-implementors-list");
Expand Down Expand Up @@ -767,9 +764,7 @@ function preLoadCss(cssUrl) {
}
}
};
// @ts-expect-error
if (window.pending_implementors) {
// @ts-expect-error
window.register_implementors(window.pending_implementors);
}

Expand Down Expand Up @@ -802,16 +797,14 @@ function preLoadCss(cssUrl) {
*
* - After processing all of the impls, it sorts the sidebar items by name.
*
* @param {{[cratename: string]: Array<Array<string|0>>}} imp
* @param {rustdoc.TypeImpls} imp
*/
// @ts-expect-error
window.register_type_impls = imp => {
// @ts-expect-error
if (!imp || !imp[window.currentCrate]) {
return;
}
// @ts-expect-error
window.pending_type_impls = null;
window.pending_type_impls = undefined;
const idMap = new Map();

let implementations = document.getElementById("implementations-list");
Expand Down Expand Up @@ -997,9 +990,7 @@ function preLoadCss(cssUrl) {
list.replaceChildren(...newChildren);
}
};
// @ts-expect-error
if (window.pending_type_impls) {
// @ts-expect-error
window.register_type_impls(window.pending_type_impls);
}

Expand Down Expand Up @@ -1695,8 +1686,7 @@ function preLoadCss(cssUrl) {
addSidebarCrates();
onHashChange(null);
window.addEventListener("hashchange", onHashChange);
// @ts-expect-error
searchState.setup();
window.searchState.setup();
}());

// Hide, show, and resize the sidebar
Expand Down
25 changes: 25 additions & 0 deletions src/librustdoc/html/static/js/rustdoc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ declare global {
interface Window {
/** Make the current theme easy to find */
currentTheme: HTMLLinkElement|null;
/** Generated in `render/context.rs` */
SIDEBAR_ITEMS?: { [key: string]: string[] };
/** Used by the popover tooltip code. */
RUSTDOC_TOOLTIP_HOVER_MS: number;
/** Used by the popover tooltip code. */
Expand Down Expand Up @@ -42,6 +44,17 @@ declare global {
* Set up event listeners for a scraped source example.
*/
updateScrapedExample?: function(HTMLElement, HTMLElement),
/**
* register trait implementors, called by code generated in
* `write_shared.rs`
*/
register_implementors?: function(rustdoc.Implementors): void,
/**
* fallback in case `register_implementors` isn't defined yet.
*/
pending_implementors?: rustdoc.Implementors,
register_type_impls?: function(rustdoc.TypeImpls): void,
pending_type_impls?: rustdoc.TypeImpls,
}
interface HTMLElement {
/** Used by the popover tooltip code. */
Expand Down Expand Up @@ -413,4 +426,16 @@ declare namespace rustdoc {
};

type VlqData = VlqData[] | number;

/**
* Maps from crate names to trait implementation data.
* Provied by generated `trait.impl` files.
*/
type Implementors = {
[key: string]: Array<[string, number, Array<string>]>
}

type TypeImpls = {
[cratename: string]: Array<Array<string|0>>
}
}
Loading