Skip to content

Commit d78e762

Browse files
committed
scrape-examples.js: give each function a signature
1 parent c79bbfa commit d78e762

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/librustdoc/html/static/js/scrape-examples.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
/* global addClass, hasClass, removeClass, onEachLazy */
2-
3-
// Eventually fix this.
4-
// @ts-nocheck
1+
/* global addClass, hasClass, removeClass, onEachLazy, nonnull */
52

63
"use strict";
74

@@ -14,7 +11,12 @@
1411
const DEFAULT_MAX_LINES = 5;
1512
const HIDDEN_MAX_LINES = 10;
1613

17-
// Scroll code block to the given code location
14+
/**
15+
* Scroll code block to the given code location
16+
* @param {HTMLElement} elt
17+
* @param {[number, number]} loc
18+
* @param {boolean} isHidden
19+
*/
1820
function scrollToLoc(elt, loc, isHidden) {
1921
const lines = elt.querySelectorAll("[data-nosnippet]");
2022
let scrollOffset;
@@ -35,10 +37,15 @@
3537
scrollOffset = offsetMid - halfHeight;
3638
}
3739

38-
lines[0].parentElement.scrollTo(0, scrollOffset);
39-
elt.querySelector(".rust").scrollTo(0, scrollOffset);
40+
nonnull(lines[0].parentElement).scrollTo(0, scrollOffset);
41+
nonnull(elt.querySelector(".rust")).scrollTo(0, scrollOffset);
4042
}
4143

44+
/**
45+
* @param {HTMLElement} parent
46+
* @param {string} className
47+
* @param {string} content
48+
*/
4249
function createScrapeButton(parent, className, content) {
4350
const button = document.createElement("button");
4451
button.className = className;
@@ -50,14 +57,15 @@
5057
window.updateScrapedExample = (example, buttonHolder) => {
5158
let locIndex = 0;
5259
const highlights = Array.prototype.slice.call(example.querySelectorAll(".highlight"));
53-
const link = example.querySelector(".scraped-example-title a");
60+
const link = nonnull(example.querySelector(".scraped-example-title a"));
5461
let expandButton = null;
5562

5663
if (!example.classList.contains("expanded")) {
5764
expandButton = createScrapeButton(buttonHolder, "expand", "Show all");
5865
}
59-
const isHidden = example.parentElement.classList.contains("more-scraped-examples");
66+
const isHidden = nonnull(example.parentElement).classList.contains("more-scraped-examples");
6067

68+
// @ts-expect-error
6169
const locs = example.locs;
6270
if (locs.length > 1) {
6371
const next = createScrapeButton(buttonHolder, "next", "Next usage");
@@ -106,7 +114,14 @@
106114
}
107115
};
108116

117+
/**
118+
* Intitialize the `locs` field
119+
*
120+
* @param {HTMLElement} example
121+
* @param {boolean} isHidden
122+
*/
109123
function setupLoc(example, isHidden) {
124+
// @ts-expect-error
110125
example.locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
111126
// Start with the first example in view
112127
scrollToLoc(example, example.locs[0][0], isHidden);

0 commit comments

Comments
 (0)