Skip to content

Commit 0b4f8df

Browse files
authored
Rollup merge of rust-lang#138318 - lolbinarycat:rustdoc-js-less-expect-error-part2, r=notriddle
Rustdoc: remove a bunch of @ts-expect-error from main.js r? ``@notriddle`` Most remaining instances of ```@ts-expect-error``` in `search.js` and `main.js` are some sort of unchecked assertion, most of them involving nullibility, and we have yet to decide on how to handle these.
2 parents 7742eda + 20bac26 commit 0b4f8df

File tree

2 files changed

+52
-37
lines changed

2 files changed

+52
-37
lines changed

src/librustdoc/html/static/js/main.js

+27-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Local js definitions:
2-
/* global addClass, getSettingValue, hasClass, searchState, updateLocalStorage */
2+
/* global addClass, getSettingValue, hasClass, updateLocalStorage */
33
/* global onEachLazy, removeClass, getVar */
44

55
"use strict";
@@ -121,12 +121,9 @@ function getNakedUrl() {
121121
* doesn't have a parent node.
122122
*
123123
* @param {HTMLElement} newNode
124-
* @param {HTMLElement} referenceNode
124+
* @param {HTMLElement & { parentNode: HTMLElement }} referenceNode
125125
*/
126126
function insertAfter(newNode, referenceNode) {
127-
// You're not allowed to pass an element with no parent.
128-
// I dunno how to make TS's typechecker see that.
129-
// @ts-expect-error
130127
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
131128
}
132129

@@ -305,11 +302,10 @@ function preLoadCss(cssUrl) {
305302
window.searchState.timeout = null;
306303
}
307304
},
308-
// @ts-expect-error
309305
isDisplayed: () => {
310306
const outputElement = window.searchState.outputElement();
311-
return outputElement &&
312-
outputElement.parentElement &&
307+
return !!outputElement &&
308+
!!outputElement.parentElement &&
313309
outputElement.parentElement.id === ALTERNATIVE_DISPLAY_ID;
314310
},
315311
// Sets the focus on the search bar at the top of the page
@@ -325,8 +321,6 @@ function preLoadCss(cssUrl) {
325321
search = window.searchState.outputElement();
326322
}
327323
switchDisplayedElement(search);
328-
// @ts-expect-error
329-
window.searchState.mouseMovedAfterSearch = false;
330324
document.title = window.searchState.title;
331325
},
332326
removeQueryParameters: () => {
@@ -503,43 +497,49 @@ function preLoadCss(cssUrl) {
503497
handleHashes(ev);
504498
}
505499

506-
// @ts-expect-error
500+
/**
501+
* @param {HTMLElement|null} elem
502+
*/
507503
function openParentDetails(elem) {
508504
while (elem) {
509505
if (elem.tagName === "DETAILS") {
506+
// @ts-expect-error
510507
elem.open = true;
511508
}
512-
elem = elem.parentNode;
509+
elem = elem.parentElement;
513510
}
514511
}
515512

516-
// @ts-expect-error
513+
/**
514+
* @param {string} id
515+
*/
517516
function expandSection(id) {
518517
openParentDetails(document.getElementById(id));
519518
}
520519

521-
// @ts-expect-error
520+
/**
521+
* @param {KeyboardEvent} ev
522+
*/
522523
function handleEscape(ev) {
523-
// @ts-expect-error
524-
searchState.clearInputTimeout();
525-
// @ts-expect-error
526-
searchState.hideResults();
524+
window.searchState.clearInputTimeout();
525+
window.searchState.hideResults();
527526
ev.preventDefault();
528-
// @ts-expect-error
529-
searchState.defocus();
527+
window.searchState.defocus();
530528
window.hideAllModals(true); // true = reset focus for tooltips
531529
}
532530

533-
// @ts-expect-error
531+
/**
532+
* @param {KeyboardEvent} ev
533+
*/
534534
function handleShortcut(ev) {
535535
// Don't interfere with browser shortcuts
536536
const disableShortcuts = getSettingValue("disable-shortcuts") === "true";
537537
if (ev.ctrlKey || ev.altKey || ev.metaKey || disableShortcuts) {
538538
return;
539539
}
540540

541-
// @ts-expect-error
542-
if (document.activeElement.tagName === "INPUT" &&
541+
if (document.activeElement &&
542+
document.activeElement.tagName === "INPUT" &&
543543
// @ts-expect-error
544544
document.activeElement.type !== "checkbox" &&
545545
// @ts-expect-error
@@ -559,8 +559,7 @@ function preLoadCss(cssUrl) {
559559
case "S":
560560
case "/":
561561
ev.preventDefault();
562-
// @ts-expect-error
563-
searchState.focus();
562+
window.searchState.focus();
564563
break;
565564

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

588587
function addSidebarItems() {
589-
// @ts-expect-error
590588
if (!window.SIDEBAR_ITEMS) {
591589
return;
592590
}
@@ -675,7 +673,6 @@ function preLoadCss(cssUrl) {
675673
}
676674

677675
// <https://github.com/search?q=repo%3Arust-lang%2Frust+[RUSTDOCIMPL]+trait.impl&type=code>
678-
// @ts-expect-error
679676
window.register_implementors = imp => {
680677
const implementors = document.getElementById("implementors-list");
681678
const synthetic_implementors = document.getElementById("synthetic-implementors-list");
@@ -767,9 +764,7 @@ function preLoadCss(cssUrl) {
767764
}
768765
}
769766
};
770-
// @ts-expect-error
771767
if (window.pending_implementors) {
772-
// @ts-expect-error
773768
window.register_implementors(window.pending_implementors);
774769
}
775770

@@ -802,16 +797,14 @@ function preLoadCss(cssUrl) {
802797
*
803798
* - After processing all of the impls, it sorts the sidebar items by name.
804799
*
805-
* @param {{[cratename: string]: Array<Array<string|0>>}} imp
800+
* @param {rustdoc.TypeImpls} imp
806801
*/
807-
// @ts-expect-error
808802
window.register_type_impls = imp => {
809803
// @ts-expect-error
810804
if (!imp || !imp[window.currentCrate]) {
811805
return;
812806
}
813-
// @ts-expect-error
814-
window.pending_type_impls = null;
807+
window.pending_type_impls = undefined;
815808
const idMap = new Map();
816809

817810
let implementations = document.getElementById("implementations-list");
@@ -997,9 +990,7 @@ function preLoadCss(cssUrl) {
997990
list.replaceChildren(...newChildren);
998991
}
999992
};
1000-
// @ts-expect-error
1001993
if (window.pending_type_impls) {
1002-
// @ts-expect-error
1003994
window.register_type_impls(window.pending_type_impls);
1004995
}
1005996

@@ -1695,8 +1686,7 @@ function preLoadCss(cssUrl) {
16951686
addSidebarCrates();
16961687
onHashChange(null);
16971688
window.addEventListener("hashchange", onHashChange);
1698-
// @ts-expect-error
1699-
searchState.setup();
1689+
window.searchState.setup();
17001690
}());
17011691

17021692
// Hide, show, and resize the sidebar

src/librustdoc/html/static/js/rustdoc.d.ts

+25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ declare global {
77
interface Window {
88
/** Make the current theme easy to find */
99
currentTheme: HTMLLinkElement|null;
10+
/** Generated in `render/context.rs` */
11+
SIDEBAR_ITEMS?: { [key: string]: string[] };
1012
/** Used by the popover tooltip code. */
1113
RUSTDOC_TOOLTIP_HOVER_MS: number;
1214
/** Used by the popover tooltip code. */
@@ -42,6 +44,17 @@ declare global {
4244
* Set up event listeners for a scraped source example.
4345
*/
4446
updateScrapedExample?: function(HTMLElement, HTMLElement),
47+
/**
48+
* register trait implementors, called by code generated in
49+
* `write_shared.rs`
50+
*/
51+
register_implementors?: function(rustdoc.Implementors): void,
52+
/**
53+
* fallback in case `register_implementors` isn't defined yet.
54+
*/
55+
pending_implementors?: rustdoc.Implementors,
56+
register_type_impls?: function(rustdoc.TypeImpls): void,
57+
pending_type_impls?: rustdoc.TypeImpls,
4558
}
4659
interface HTMLElement {
4760
/** Used by the popover tooltip code. */
@@ -413,4 +426,16 @@ declare namespace rustdoc {
413426
};
414427

415428
type VlqData = VlqData[] | number;
429+
430+
/**
431+
* Maps from crate names to trait implementation data.
432+
* Provied by generated `trait.impl` files.
433+
*/
434+
type Implementors = {
435+
[key: string]: Array<[string, number, Array<string>]>
436+
}
437+
438+
type TypeImpls = {
439+
[cratename: string]: Array<Array<string|0>>
440+
}
416441
}

0 commit comments

Comments
 (0)