Skip to content
Open
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
36 changes: 8 additions & 28 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -622,8 +619,7 @@ function preLoadCss(cssUrl) {
*/
function openParentDetails(elem) {
while (elem) {
if (elem.tagName === "DETAILS") {
// @ts-expect-error
if (elem instanceof HTMLDetailsElement) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's more correct, I find it so much less readable. ^^' (nothing to change here, just me sad that better code doesn't always look better)

elem.open = true;
}
elem = elem.parentElement;
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not the same check at all. Is there a reason why we go from integer comparison to checking it's a string? (lack of context on my side here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impList[1] has type string | 0, so this is actually an equivalent check, though I could see how it could really not look like it without that context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. JS is weird and we really abuse it sometimes. XD

// @ts-expect-error
if (types.indexOf(selfPath) === -1) {
continue;
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this one has expect-error now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't, I think the intermediate commit got messed up by some rebasing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, wait, I misread the diff, this is also Type 'string | 0' is not assignable to type 'string | null'., but this time the error is somewhat legitimate (well, it's a limitation of typescript not realizing that the value of isTrait is linked to the type of traitName)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add this as comment so future us will not have to search again for this explanation please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah will do. i originally didn't because i hoped i could find a way to get rid of it easily.

li.appendChild(a);
// @ts-expect-error
Expand All @@ -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")) {
Expand Down
15 changes: 14 additions & 1 deletion src/librustdoc/html/static/js/rustdoc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,8 @@ declare namespace rustdoc {
}

type TypeImpls = {
[cratename: string]: Array<Array<string|0>>
/* [text, traitName (0 if not a trait), ...types] */
[cratename: string]: Array<[string, string|0, ...string[]]>
}

/**
Expand Down Expand Up @@ -576,4 +577,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";
}
12 changes: 9 additions & 3 deletions src/librustdoc/html/static/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
}

/**
Expand Down Expand Up @@ -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
Expand Down
Loading