Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c123adf

Browse files
committed
rustdoc js: add nonundef and use it to remove a ts-expect-error
1 parent 90f5eab commit c123adf

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
declare global {
77
/** Map from crate name to directory structure, for source view */
88
declare var srcIndex: Map<string, rustdoc.Dir>;
9-
/** Defined and documented in `main.js` */
9+
/** Defined and documented in `storage.js` */
1010
declare function nonnull(x: T|null, msg: string|undefined);
11+
/** Defined and documented in `storage.js` */
12+
declare function nonundef(x: T|undefined, msg: string|undefined);
1113
interface Window {
1214
/** Make the current theme easy to find */
1315
currentTheme: HTMLLinkElement|null;

src/librustdoc/html/static/js/search.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ignore-tidy-filelength
2-
/* global addClass, getNakedUrl, getSettingValue, getVar */
2+
/* global addClass, getNakedUrl, getSettingValue, getVar, nonnull, nonundef */
33
/* global onEachLazy, removeClass, searchState, browserSupportsHistoryApi, exports */
44

55
"use strict";
@@ -338,9 +338,8 @@ function getFilteredNextElem(query, parserState, elems, isInGenerics) {
338338
// The type filter doesn't count as an element since it's a modifier.
339339
const typeFilterElem = elems.pop();
340340
checkExtraTypeFilterCharacters(start, parserState);
341-
// typeFilterElem is not null. If it was, the elems.length check would have fired.
342-
// @ts-expect-error
343-
parserState.typeFilter = typeFilterElem.normalizedPathLast;
341+
// typeFilterElem is not undefined. If it was, the elems.length check would have fired.
342+
parserState.typeFilter = nonundef(typeFilterElem).normalizedPathLast;
344343
parserState.pos += 1;
345344
parserState.totalElems -= 1;
346345
query.literalSearch = false;

src/librustdoc/html/static/js/storage.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ function nonnull(x, msg) {
4343
}
4444
}
4545

46+
/**
47+
* Assert that the passed value is not undefined, then return it.
48+
*
49+
* Takes an optional error message argument.
50+
*
51+
* Must be defined in this file, as it is loaded before all others.
52+
*
53+
* @template T
54+
* @param {T|undefined} x
55+
* @param {string=} msg
56+
* @returns T
57+
*/
58+
// used in other files, not yet used in this one.
59+
// eslint-disable-next-line no-unused-vars
60+
function nonundef(x, msg) {
61+
if (x === undefined) {
62+
throw (msg || "unexpected null value!");
63+
} else {
64+
return x;
65+
}
66+
}
67+
4668
/**
4769
* Get a configuration value. If it's not set, get the default.
4870
*

0 commit comments

Comments
 (0)