rustdoc js: add nonundef and use it to remove a ts-expect-error

This commit is contained in:
binarycat 2025-03-24 15:57:07 -05:00
parent 90f5eab952
commit c123adf860
3 changed files with 28 additions and 5 deletions

View file

@ -6,8 +6,10 @@
declare global {
/** Map from crate name to directory structure, for source view */
declare var srcIndex: Map<string, rustdoc.Dir>;
/** Defined and documented in `main.js` */
/** Defined and documented in `storage.js` */
declare function nonnull(x: T|null, msg: string|undefined);
/** Defined and documented in `storage.js` */
declare function nonundef(x: T|undefined, msg: string|undefined);
interface Window {
/** Make the current theme easy to find */
currentTheme: HTMLLinkElement|null;

View file

@ -1,5 +1,5 @@
// ignore-tidy-filelength
/* global addClass, getNakedUrl, getSettingValue, getVar */
/* global addClass, getNakedUrl, getSettingValue, getVar, nonnull, nonundef */
/* global onEachLazy, removeClass, searchState, browserSupportsHistoryApi, exports */
"use strict";
@ -338,9 +338,8 @@ function getFilteredNextElem(query, parserState, elems, isInGenerics) {
// The type filter doesn't count as an element since it's a modifier.
const typeFilterElem = elems.pop();
checkExtraTypeFilterCharacters(start, parserState);
// typeFilterElem is not null. If it was, the elems.length check would have fired.
// @ts-expect-error
parserState.typeFilter = typeFilterElem.normalizedPathLast;
// typeFilterElem is not undefined. If it was, the elems.length check would have fired.
parserState.typeFilter = nonundef(typeFilterElem).normalizedPathLast;
parserState.pos += 1;
parserState.totalElems -= 1;
query.literalSearch = false;

View file

@ -43,6 +43,28 @@ function nonnull(x, msg) {
}
}
/**
* Assert that the passed value is not undefined, then return it.
*
* Takes an optional error message argument.
*
* Must be defined in this file, as it is loaded before all others.
*
* @template T
* @param {T|undefined} x
* @param {string=} msg
* @returns T
*/
// used in other files, not yet used in this one.
// eslint-disable-next-line no-unused-vars
function nonundef(x, msg) {
if (x === undefined) {
throw (msg || "unexpected null value!");
} else {
return x;
}
}
/**
* Get a configuration value. If it's not set, get the default.
*