Skip to content

Commit 9e1d15a

Browse files
committed
rustdoc js: add ScrapedLoc type
1 parent 5b6533e commit 9e1d15a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -491,4 +491,11 @@ declare namespace rustdoc {
491491
options?: string[],
492492
default: string | boolean,
493493
}
494+
495+
/**
496+
* Single element in the data-locs field of a scraped example.
497+
* First field is the start and end char index,
498+
* other fields seem to be unused.
499+
*/
500+
type ScrapedLoc = [[number, number], string, string]
494501
}

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* @param {boolean} isHidden
1919
*/
2020
function scrollToLoc(elt, loc, isHidden) {
21+
/** @type {HTMLElement[]} */
22+
// blocked on https://github.com/microsoft/TypeScript/issues/29037
23+
// @ts-expect-error
2124
const lines = elt.querySelectorAll("[data-nosnippet]");
2225
let scrollOffset;
2326

@@ -57,6 +60,8 @@
5760
window.updateScrapedExample = (example, buttonHolder) => {
5861
let locIndex = 0;
5962
const highlights = Array.prototype.slice.call(example.querySelectorAll(".highlight"));
63+
64+
/** @type {HTMLAnchorElement} */
6065
const link = nonnull(example.querySelector(".scraped-example-title a"));
6166
let expandButton = null;
6267

@@ -72,6 +77,7 @@
7277
const prev = createScrapeButton(buttonHolder, "prev", "Previous usage");
7378

7479
// Toggle through list of examples in a given file
80+
/** @type {function(function(): void): void} */
7581
const onChangeLoc = changeIndex => {
7682
removeClass(highlights[locIndex], "focus");
7783
changeIndex();
@@ -117,14 +123,13 @@
117123
/**
118124
* Intitialize the `locs` field
119125
*
120-
* @param {HTMLElement} example
126+
* @param {HTMLElement & {locs?: rustdoc.ScrapedLoc[]}} example
121127
* @param {boolean} isHidden
122128
*/
123129
function setupLoc(example, isHidden) {
124-
// @ts-expect-error
125-
example.locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
130+
const locs = example.locs = JSON.parse(nonnull(nonnull(example.attributes.getNamedItem("data-locs")).textContent));
126131
// Start with the first example in view
127-
scrollToLoc(example, example.locs[0][0], isHidden);
132+
scrollToLoc(example, locs[0][0], isHidden);
128133
}
129134

130135
const firstExamples = document.querySelectorAll(".scraped-example-list > .scraped-example");

0 commit comments

Comments
 (0)