diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 42cd321849cf..491be052bca2 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -231,7 +231,6 @@ function preLoadCss(cssUrl) { // When loading settings.html as a standalone page, the equivalent HTML is // generated in context.rs. setTimeout(() => { - // @ts-expect-error const themes = getVar("themes").split(","); for (const theme of themes) { // if there are no themes, do nothing @@ -415,12 +414,10 @@ function preLoadCss(cssUrl) { } window.StringdexOnload.push(() => { loadScript( - // @ts-expect-error getVar("static-root-path") + getVar("search-js"), sendSearchForm, ); }); - // @ts-expect-error loadScript(getVar("static-root-path") + getVar("stringdex-js"), sendSearchForm); loadScript(resourcePath("search.index/root", ".js"), sendSearchForm); } @@ -622,8 +619,7 @@ function preLoadCss(cssUrl) { */ function openParentDetails(elem) { while (elem) { - if (elem.tagName === "DETAILS") { - // @ts-expect-error + if (elem instanceof HTMLDetailsElement) { elem.open = true; } elem = elem.parentElement; @@ -659,10 +655,8 @@ function preLoadCss(cssUrl) { } if (document.activeElement && - document.activeElement.tagName === "INPUT" && - // @ts-expect-error + document.activeElement instanceof HTMLInputElement && document.activeElement.type !== "checkbox" && - // @ts-expect-error document.activeElement.type !== "radio") { switch (getVirtualKey(ev)) { case "Escape": @@ -969,19 +963,19 @@ function preLoadCss(cssUrl) { const selfPath = script ? script.getAttribute("data-self-path") : null; // These sidebar blocks need filled in, too. - const mainContent = document.querySelector("#main-content"); - const sidebarSection = document.querySelector(".sidebar section"); + const mainContent = nonnull(document.querySelector("#main-content")); + const sidebarSection = nonnull(document.querySelector(".sidebar section")); let methods = document.querySelector(".sidebar .block.method"); let associatedTypes = document.querySelector(".sidebar .block.associatedtype"); let associatedConstants = document.querySelector(".sidebar .block.associatedconstant"); let sidebarTraitList = document.querySelector(".sidebar .block.trait-implementation"); - // @ts-expect-error - for (const impList of imp[window.currentCrate]) { + for (const impList of imp[nonnull(window.currentCrate)]) { const types = impList.slice(2); const text = impList[0]; - const isTrait = impList[1] !== 0; const traitName = impList[1]; + const isTrait = typeof traitName === "string"; + // @ts-expect-error if (types.indexOf(selfPath) === -1) { continue; } @@ -1005,28 +999,19 @@ function preLoadCss(cssUrl) { h.appendChild(link); trait_implementations = outputList; trait_implementations_header = outputListHeader; - // @ts-expect-error sidebarSection.appendChild(h); sidebarTraitList = document.createElement("ul"); sidebarTraitList.className = "block trait-implementation"; - // @ts-expect-error sidebarSection.appendChild(sidebarTraitList); - // @ts-expect-error mainContent.appendChild(outputListHeader); - // @ts-expect-error mainContent.appendChild(outputList); } else { implementations = outputList; if (trait_implementations) { - // @ts-expect-error mainContent.insertBefore(outputListHeader, trait_implementations_header); - // @ts-expect-error mainContent.insertBefore(outputList, trait_implementations_header); } else { - const mainContent = document.querySelector("#main-content"); - // @ts-expect-error mainContent.appendChild(outputListHeader); - // @ts-expect-error mainContent.appendChild(outputList); } } @@ -1071,8 +1056,7 @@ function preLoadCss(cssUrl) { if (isTrait) { const li = document.createElement("li"); const a = document.createElement("a"); - // @ts-expect-error - a.href = `#${template.content.querySelector(".impl").id}`; + a.href = `#${nonnull(template.content.querySelector(".impl")).id}`; a.textContent = traitName; li.appendChild(a); // @ts-expect-error @@ -1099,14 +1083,10 @@ function preLoadCss(cssUrl) { const insertionReference = methods || sidebarTraitList; if (insertionReference) { const insertionReferenceH = insertionReference.previousElementSibling; - // @ts-expect-error sidebarSection.insertBefore(blockHeader, insertionReferenceH); - // @ts-expect-error sidebarSection.insertBefore(block, insertionReferenceH); } else { - // @ts-expect-error sidebarSection.appendChild(blockHeader); - // @ts-expect-error sidebarSection.appendChild(block); } if (hasClass(item, "associatedtype")) { diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index 555324c16f71..3857e6e9fd87 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -526,7 +526,8 @@ declare namespace rustdoc { } type TypeImpls = { - [cratename: string]: Array> + /* [text, traitName (0 if not a trait), ...types] */ + [cratename: string]: Array<[string, string|0, ...string[]]> } /** @@ -578,4 +579,16 @@ declare namespace rustdoc { "typeNameIdOfHof": number, "typeNameIdOfNever": number, }; + + type VarName = "name" + | "root-path" + | "static-root-path" + | "current-crate" + | "themes" + | "resource-suffix" + | "rustdoc-version" + | "channel" + | "search-js" + | "stringdex-js" + | "settings-js"; } diff --git a/src/librustdoc/html/static/js/storage.js b/src/librustdoc/html/static/js/storage.js index 1ec7cb4ced06..29b94ea424e2 100644 --- a/src/librustdoc/html/static/js/storage.js +++ b/src/librustdoc/html/static/js/storage.js @@ -199,12 +199,16 @@ function getCurrentValue(name) { * Get a value from the rustdoc-vars div, which is used to convey data from * Rust to the JS. If there is no such element, return null. * - * @param {string} name - * @returns {string|null} + * @param {rustdoc.VarName} name + * @returns {string} */ function getVar(name) { const el = document.querySelector("head > meta[name='rustdoc-vars']"); - return el ? el.getAttribute("data-" + name) : null; + const v = el ? el.getAttribute("data-" + name) : null; + if (v !== null) { + return v; + } + throw `rustdoc var "${name}" is missing`; } /** @@ -294,6 +298,8 @@ const updateTheme = (function() { return updateTheme; })(); +// typescript thinks we're forgetting to call window.matchMedia, +// but we're checking browser support of media queries. // @ts-ignore if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) { // update the preferred dark theme if the user is already using a dark theme