Rollup merge of #151386 - lolbinarycat:rustdoc-ts-cleanup, r=GuillaumeGomez
rustdoc: more js cleanup Continuing the effort of removing `@ts-expect-error` from the codebase wherever possible, this time focusing on `main.js`. Found some oddities with `register_type_impls`, fixed most of them, but the one that I couldn't figure out is what's going on with `sidebarTraitList`. It's queried, then if there are any trait imps, unconditionally overwritten, then latter code assumes that one of these two things has initialized it, but it's not obvious why this would be the case, or if there's a reason this wasn't done in a more straightforwards way. r? @GuillaumeGomez
This commit is contained in:
commit
9f3f83bfee
3 changed files with 31 additions and 32 deletions
|
|
@ -231,7 +231,6 @@ function preLoadCss(cssUrl) {
|
||||||
// When loading settings.html as a standalone page, the equivalent HTML is
|
// When loading settings.html as a standalone page, the equivalent HTML is
|
||||||
// generated in context.rs.
|
// generated in context.rs.
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// @ts-expect-error
|
|
||||||
const themes = getVar("themes").split(",");
|
const themes = getVar("themes").split(",");
|
||||||
for (const theme of themes) {
|
for (const theme of themes) {
|
||||||
// if there are no themes, do nothing
|
// if there are no themes, do nothing
|
||||||
|
|
@ -415,12 +414,10 @@ function preLoadCss(cssUrl) {
|
||||||
}
|
}
|
||||||
window.StringdexOnload.push(() => {
|
window.StringdexOnload.push(() => {
|
||||||
loadScript(
|
loadScript(
|
||||||
// @ts-expect-error
|
|
||||||
getVar("static-root-path") + getVar("search-js"),
|
getVar("static-root-path") + getVar("search-js"),
|
||||||
sendSearchForm,
|
sendSearchForm,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
// @ts-expect-error
|
|
||||||
loadScript(getVar("static-root-path") + getVar("stringdex-js"), sendSearchForm);
|
loadScript(getVar("static-root-path") + getVar("stringdex-js"), sendSearchForm);
|
||||||
loadScript(resourcePath("search.index/root", ".js"), sendSearchForm);
|
loadScript(resourcePath("search.index/root", ".js"), sendSearchForm);
|
||||||
}
|
}
|
||||||
|
|
@ -622,8 +619,7 @@ function preLoadCss(cssUrl) {
|
||||||
*/
|
*/
|
||||||
function openParentDetails(elem) {
|
function openParentDetails(elem) {
|
||||||
while (elem) {
|
while (elem) {
|
||||||
if (elem.tagName === "DETAILS") {
|
if (elem instanceof HTMLDetailsElement) {
|
||||||
// @ts-expect-error
|
|
||||||
elem.open = true;
|
elem.open = true;
|
||||||
}
|
}
|
||||||
elem = elem.parentElement;
|
elem = elem.parentElement;
|
||||||
|
|
@ -659,10 +655,8 @@ function preLoadCss(cssUrl) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (document.activeElement &&
|
if (document.activeElement &&
|
||||||
document.activeElement.tagName === "INPUT" &&
|
document.activeElement instanceof HTMLInputElement &&
|
||||||
// @ts-expect-error
|
|
||||||
document.activeElement.type !== "checkbox" &&
|
document.activeElement.type !== "checkbox" &&
|
||||||
// @ts-expect-error
|
|
||||||
document.activeElement.type !== "radio") {
|
document.activeElement.type !== "radio") {
|
||||||
switch (getVirtualKey(ev)) {
|
switch (getVirtualKey(ev)) {
|
||||||
case "Escape":
|
case "Escape":
|
||||||
|
|
@ -969,19 +963,19 @@ function preLoadCss(cssUrl) {
|
||||||
const selfPath = script ? script.getAttribute("data-self-path") : null;
|
const selfPath = script ? script.getAttribute("data-self-path") : null;
|
||||||
|
|
||||||
// These sidebar blocks need filled in, too.
|
// These sidebar blocks need filled in, too.
|
||||||
const mainContent = document.querySelector("#main-content");
|
const mainContent = nonnull(document.querySelector("#main-content"));
|
||||||
const sidebarSection = document.querySelector(".sidebar section");
|
const sidebarSection = nonnull(document.querySelector(".sidebar section"));
|
||||||
let methods = document.querySelector(".sidebar .block.method");
|
let methods = document.querySelector(".sidebar .block.method");
|
||||||
let associatedTypes = document.querySelector(".sidebar .block.associatedtype");
|
let associatedTypes = document.querySelector(".sidebar .block.associatedtype");
|
||||||
let associatedConstants = document.querySelector(".sidebar .block.associatedconstant");
|
let associatedConstants = document.querySelector(".sidebar .block.associatedconstant");
|
||||||
let sidebarTraitList = document.querySelector(".sidebar .block.trait-implementation");
|
let sidebarTraitList = document.querySelector(".sidebar .block.trait-implementation");
|
||||||
|
|
||||||
// @ts-expect-error
|
for (const impList of imp[nonnull(window.currentCrate)]) {
|
||||||
for (const impList of imp[window.currentCrate]) {
|
|
||||||
const types = impList.slice(2);
|
const types = impList.slice(2);
|
||||||
const text = impList[0];
|
const text = impList[0];
|
||||||
const isTrait = impList[1] !== 0;
|
|
||||||
const traitName = impList[1];
|
const traitName = impList[1];
|
||||||
|
const isTrait = typeof traitName === "string";
|
||||||
|
// @ts-expect-error
|
||||||
if (types.indexOf(selfPath) === -1) {
|
if (types.indexOf(selfPath) === -1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -1005,28 +999,19 @@ function preLoadCss(cssUrl) {
|
||||||
h.appendChild(link);
|
h.appendChild(link);
|
||||||
trait_implementations = outputList;
|
trait_implementations = outputList;
|
||||||
trait_implementations_header = outputListHeader;
|
trait_implementations_header = outputListHeader;
|
||||||
// @ts-expect-error
|
|
||||||
sidebarSection.appendChild(h);
|
sidebarSection.appendChild(h);
|
||||||
sidebarTraitList = document.createElement("ul");
|
sidebarTraitList = document.createElement("ul");
|
||||||
sidebarTraitList.className = "block trait-implementation";
|
sidebarTraitList.className = "block trait-implementation";
|
||||||
// @ts-expect-error
|
|
||||||
sidebarSection.appendChild(sidebarTraitList);
|
sidebarSection.appendChild(sidebarTraitList);
|
||||||
// @ts-expect-error
|
|
||||||
mainContent.appendChild(outputListHeader);
|
mainContent.appendChild(outputListHeader);
|
||||||
// @ts-expect-error
|
|
||||||
mainContent.appendChild(outputList);
|
mainContent.appendChild(outputList);
|
||||||
} else {
|
} else {
|
||||||
implementations = outputList;
|
implementations = outputList;
|
||||||
if (trait_implementations) {
|
if (trait_implementations) {
|
||||||
// @ts-expect-error
|
|
||||||
mainContent.insertBefore(outputListHeader, trait_implementations_header);
|
mainContent.insertBefore(outputListHeader, trait_implementations_header);
|
||||||
// @ts-expect-error
|
|
||||||
mainContent.insertBefore(outputList, trait_implementations_header);
|
mainContent.insertBefore(outputList, trait_implementations_header);
|
||||||
} else {
|
} else {
|
||||||
const mainContent = document.querySelector("#main-content");
|
|
||||||
// @ts-expect-error
|
|
||||||
mainContent.appendChild(outputListHeader);
|
mainContent.appendChild(outputListHeader);
|
||||||
// @ts-expect-error
|
|
||||||
mainContent.appendChild(outputList);
|
mainContent.appendChild(outputList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1071,8 +1056,7 @@ function preLoadCss(cssUrl) {
|
||||||
if (isTrait) {
|
if (isTrait) {
|
||||||
const li = document.createElement("li");
|
const li = document.createElement("li");
|
||||||
const a = document.createElement("a");
|
const a = document.createElement("a");
|
||||||
// @ts-expect-error
|
a.href = `#${nonnull(template.content.querySelector(".impl")).id}`;
|
||||||
a.href = `#${template.content.querySelector(".impl").id}`;
|
|
||||||
a.textContent = traitName;
|
a.textContent = traitName;
|
||||||
li.appendChild(a);
|
li.appendChild(a);
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
|
|
@ -1099,14 +1083,10 @@ function preLoadCss(cssUrl) {
|
||||||
const insertionReference = methods || sidebarTraitList;
|
const insertionReference = methods || sidebarTraitList;
|
||||||
if (insertionReference) {
|
if (insertionReference) {
|
||||||
const insertionReferenceH = insertionReference.previousElementSibling;
|
const insertionReferenceH = insertionReference.previousElementSibling;
|
||||||
// @ts-expect-error
|
|
||||||
sidebarSection.insertBefore(blockHeader, insertionReferenceH);
|
sidebarSection.insertBefore(blockHeader, insertionReferenceH);
|
||||||
// @ts-expect-error
|
|
||||||
sidebarSection.insertBefore(block, insertionReferenceH);
|
sidebarSection.insertBefore(block, insertionReferenceH);
|
||||||
} else {
|
} else {
|
||||||
// @ts-expect-error
|
|
||||||
sidebarSection.appendChild(blockHeader);
|
sidebarSection.appendChild(blockHeader);
|
||||||
// @ts-expect-error
|
|
||||||
sidebarSection.appendChild(block);
|
sidebarSection.appendChild(block);
|
||||||
}
|
}
|
||||||
if (hasClass(item, "associatedtype")) {
|
if (hasClass(item, "associatedtype")) {
|
||||||
|
|
|
||||||
15
src/librustdoc/html/static/js/rustdoc.d.ts
vendored
15
src/librustdoc/html/static/js/rustdoc.d.ts
vendored
|
|
@ -526,7 +526,8 @@ declare namespace rustdoc {
|
||||||
}
|
}
|
||||||
|
|
||||||
type TypeImpls = {
|
type TypeImpls = {
|
||||||
[cratename: string]: Array<Array<string|0>>
|
/* [text, traitName (0 if not a trait), ...types] */
|
||||||
|
[cratename: string]: Array<[string, string|0, ...string[]]>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -578,4 +579,16 @@ declare namespace rustdoc {
|
||||||
"typeNameIdOfHof": number,
|
"typeNameIdOfHof": number,
|
||||||
"typeNameIdOfNever": 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";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -199,12 +199,16 @@ function getCurrentValue(name) {
|
||||||
* Get a value from the rustdoc-vars div, which is used to convey data from
|
* 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.
|
* Rust to the JS. If there is no such element, return null.
|
||||||
*
|
*
|
||||||
* @param {string} name
|
* @param {rustdoc.VarName} name
|
||||||
* @returns {string|null}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
function getVar(name) {
|
function getVar(name) {
|
||||||
const el = document.querySelector("head > meta[name='rustdoc-vars']");
|
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;
|
return updateTheme;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
// typescript thinks we're forgetting to call window.matchMedia,
|
||||||
|
// but we're checking browser support of media queries.
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
|
if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
|
||||||
// update the preferred dark theme if the user is already using a dark theme
|
// update the preferred dark theme if the user is already using a dark theme
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue