Skip to content

Commit d8d9b75

Browse files
committed
rustdoc js: add nonnull helper and typecheck src-script.js
1 parent 93257e2 commit d8d9b75

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@
1111
window.RUSTDOC_TOOLTIP_HOVER_MS = 300;
1212
window.RUSTDOC_TOOLTIP_HOVER_EXIT_MS = 450;
1313

14+
/**
15+
* Assert that the passed value is nonnull, then return it.
16+
*
17+
* Takes an optional error message argument.
18+
*
19+
* @template T
20+
* @param {T|null} x
21+
* @param {string=} msg
22+
* @returns T
23+
*/
24+
// used in other files, not yet used in this one.
25+
// eslint-disable-next-line no-unused-vars
26+
function nonnull(x, msg) {
27+
if (x === null) {
28+
throw (msg || "unexpected null value!");
29+
} else {
30+
return x;
31+
}
32+
}
33+
1434
/**
1535
* Given a basename (e.g. "storage") and an extension (e.g. ".js"), return a URL
1636
* for a resource under the root-path, with the resource-suffix.

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

+29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
/* eslint-disable */
66
declare global {
7+
/** Map from crate name to directory structure, for source view */
8+
declare var srcIndex: Map<string, rustdoc.Dir>;
9+
/** Defined and documented in `main.js` */
10+
declare function nonnull(x: T|null, msg: string|undefined);
711
interface Window {
812
/** Make the current theme easy to find */
913
currentTheme: HTMLLinkElement|null;
@@ -40,6 +44,23 @@ declare global {
4044
* or if this is a docs page, this function does nothing.
4145
*/
4246
rustdocShowSourceSidebar: function(),
47+
/**
48+
* Close the sidebar in source code view
49+
*/
50+
rustdocCloseSourceSidebar?: function(),
51+
/**
52+
* Shows the sidebar in source code view
53+
*/
54+
rustdocShowSourceSidebar?: function(),
55+
/**
56+
* Toggles the sidebar in source code view
57+
*/
58+
rustdocToggleSrcSidebar?: function(),
59+
/**
60+
* create's the sidebar in source code view.
61+
* called in generated `src-files.js`.
62+
*/
63+
createSrcSidebar?: function(),
4364
/**
4465
* Set up event listeners for a scraped source example.
4566
*/
@@ -438,4 +459,12 @@ declare namespace rustdoc {
438459
type TypeImpls = {
439460
[cratename: string]: Array<Array<string|0>>
440461
}
462+
463+
/**
464+
* Directory structure for source code view,
465+
* defined in generated `src-files.js`.
466+
*
467+
* is a tuple of (filename, subdirs, filenames).
468+
*/
469+
type Dir = [string, rustdoc.Dir[], string[]]
441470
}

src/librustdoc/html/static/js/src-script.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33

44
// Local js definitions:
55
/* global addClass, onEachLazy, removeClass, browserSupportsHistoryApi */
6-
/* global updateLocalStorage, getVar */
6+
/* global updateLocalStorage, getVar, nonnull */
77

8-
// Eventually fix this.
9-
// @ts-nocheck
108

119
"use strict";
1210

@@ -29,6 +27,14 @@ function closeSidebarIfMobile() {
2927
}
3028
}
3129

30+
/**
31+
* @param {rustdoc.Dir} elem
32+
* @param {HTMLElement} parent
33+
* @param {string} fullPath
34+
* @param {boolean} hasFoundFile
35+
*
36+
* @returns {boolean} - new value for hasFoundFile
37+
*/
3238
function createDirEntry(elem, parent, fullPath, hasFoundFile) {
3339
const dirEntry = document.createElement("details");
3440
const summary = document.createElement("summary");
@@ -95,7 +101,7 @@ window.rustdocToggleSrcSidebar = () => {
95101
// This function is called from "src-files.js", generated in `html/render/write_shared.rs`.
96102
// eslint-disable-next-line no-unused-vars
97103
function createSrcSidebar() {
98-
const container = document.querySelector("nav.sidebar");
104+
const container = nonnull(document.querySelector("nav.sidebar"));
99105

100106
const sidebar = document.createElement("div");
101107
sidebar.id = "src-sidebar";
@@ -111,6 +117,7 @@ function createSrcSidebar() {
111117
// Focus on the current file in the source files sidebar.
112118
const selected_elem = sidebar.getElementsByClassName("selected")[0];
113119
if (typeof selected_elem !== "undefined") {
120+
// @ts-expect-error
114121
selected_elem.focus();
115122
}
116123
}
@@ -130,19 +137,20 @@ function highlightSrcLines() {
130137
to = from;
131138
from = tmp;
132139
}
133-
let elem = document.getElementById(from);
140+
const from_s = "" + from;
141+
let elem = document.getElementById(from_s);
134142
if (!elem) {
135143
return;
136144
}
137-
const x = document.getElementById(from);
145+
const x = document.getElementById(from_s);
138146
if (x) {
139147
x.scrollIntoView();
140148
}
141149
onEachLazy(document.querySelectorAll("a[data-nosnippet]"), e => {
142150
removeClass(e, "line-highlighted");
143151
});
144152
for (let i = from; i <= to; ++i) {
145-
elem = document.getElementById(i);
153+
elem = document.getElementById("" + i);
146154
if (!elem) {
147155
break;
148156
}
@@ -153,11 +161,12 @@ function highlightSrcLines() {
153161
const handleSrcHighlight = (function() {
154162
let prev_line_id = 0;
155163

164+
/** @type {function(string): void} */
156165
const set_fragment = name => {
157166
const x = window.scrollX,
158167
y = window.scrollY;
159168
if (browserSupportsHistoryApi()) {
160-
history.replaceState(null, null, "#" + name);
169+
history.replaceState(null, "", "#" + name);
161170
highlightSrcLines();
162171
} else {
163172
location.replace("#" + name);
@@ -166,6 +175,7 @@ const handleSrcHighlight = (function() {
166175
window.scrollTo(x, y);
167176
};
168177

178+
// @ts-expect-error
169179
return ev => {
170180
let cur_line_id = parseInt(ev.target.id, 10);
171181
// This event handler is attached to the entire line number column, but it should only
@@ -191,7 +201,7 @@ const handleSrcHighlight = (function() {
191201
} else {
192202
prev_line_id = cur_line_id;
193203

194-
set_fragment(cur_line_id);
204+
set_fragment("" + cur_line_id);
195205
}
196206
};
197207
}());

0 commit comments

Comments
 (0)