Skip to content

Commit 81dfbf5

Browse files
authored
Unrolled build for #140314
Rollup merge of #140314 - lolbinarycat:rustdoc-js-scrape-examples-typecheck, r=notriddle Rustdoc: typecheck scrape-examples.js more typechecking progress, this time we're mostly held back by the fact that `document.querySelectorAll` can't return nice types if its given a compound query (see the issue linked in a code comment). Additionally, it seems like the generated `data-locs` attribute has fields that are never used by anything? r? ```@notriddle```
2 parents f605b57 + c021e7a commit 81dfbf5

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,13 @@ declare namespace rustdoc {
514514
options?: string[],
515515
default: string | boolean,
516516
}
517+
518+
/**
519+
* Single element in the data-locs field of a scraped example.
520+
* First field is the start and end char index,
521+
* other fields seem to be unused.
522+
*
523+
* Generated by `render_call_locations` in `render/mod.rs`.
524+
*/
525+
type ScrapedLoc = [[number, number], string, string]
517526
}

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

Lines changed: 34 additions & 11 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,8 +11,16 @@
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) {
21+
/** @type {HTMLElement[]} */
22+
// blocked on https://github.com/microsoft/TypeScript/issues/29037
23+
// @ts-expect-error
1924
const lines = elt.querySelectorAll("[data-nosnippet]");
2025
let scrollOffset;
2126

@@ -35,10 +40,15 @@
3540
scrollOffset = offsetMid - halfHeight;
3641
}
3742

38-
lines[0].parentElement.scrollTo(0, scrollOffset);
39-
elt.querySelector(".rust").scrollTo(0, scrollOffset);
43+
nonnull(lines[0].parentElement).scrollTo(0, scrollOffset);
44+
nonnull(elt.querySelector(".rust")).scrollTo(0, scrollOffset);
4045
}
4146

47+
/**
48+
* @param {HTMLElement} parent
49+
* @param {string} className
50+
* @param {string} content
51+
*/
4252
function createScrapeButton(parent, className, content) {
4353
const button = document.createElement("button");
4454
button.className = className;
@@ -50,20 +60,24 @@
5060
window.updateScrapedExample = (example, buttonHolder) => {
5161
let locIndex = 0;
5262
const highlights = Array.prototype.slice.call(example.querySelectorAll(".highlight"));
53-
const link = example.querySelector(".scraped-example-title a");
63+
64+
/** @type {HTMLAnchorElement} */
65+
const link = nonnull(example.querySelector(".scraped-example-title a"));
5466
let expandButton = null;
5567

5668
if (!example.classList.contains("expanded")) {
5769
expandButton = createScrapeButton(buttonHolder, "expand", "Show all");
5870
}
59-
const isHidden = example.parentElement.classList.contains("more-scraped-examples");
71+
const isHidden = nonnull(example.parentElement).classList.contains("more-scraped-examples");
6072

73+
// @ts-expect-error
6174
const locs = example.locs;
6275
if (locs.length > 1) {
6376
const next = createScrapeButton(buttonHolder, "next", "Next usage");
6477
const prev = createScrapeButton(buttonHolder, "prev", "Previous usage");
6578

6679
// Toggle through list of examples in a given file
80+
/** @type {function(function(): void): void} */
6781
const onChangeLoc = changeIndex => {
6882
removeClass(highlights[locIndex], "focus");
6983
changeIndex();
@@ -106,10 +120,19 @@
106120
}
107121
};
108122

123+
/**
124+
* Initialize the `locs` field
125+
*
126+
* @param {HTMLElement & {locs?: rustdoc.ScrapedLoc[]}} example
127+
* @param {boolean} isHidden
128+
*/
109129
function setupLoc(example, isHidden) {
110-
example.locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
130+
const locs_str = nonnull(example.attributes.getNamedItem("data-locs")).textContent;
131+
const locs =
132+
JSON.parse(nonnull(nonnull(locs_str)));
133+
example.locs = locs;
111134
// Start with the first example in view
112-
scrollToLoc(example, example.locs[0][0], isHidden);
135+
scrollToLoc(example, locs[0][0], isHidden);
113136
}
114137

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

0 commit comments

Comments
 (0)