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

Commit 49bf6ca

Browse files
committed
search.js: add undef2null and eliminate more @ts-expect-error
1 parent e9a5470 commit 49bf6ca

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ function onEachBtwn(arr, func, funcBtwn) {
3333
}
3434
}
3535

36+
/**
37+
* Convert any `undefined` to `null`.
38+
*
39+
* @template T
40+
* @param {T|undefined} x
41+
* @returns {T|null}
42+
*/
43+
function undef2null(x) {
44+
if (x !== undefined) {
45+
return x;
46+
}
47+
return null;
48+
}
49+
3650
// ==================== Core search logic begin ====================
3751
// This mapping table should match the discriminants of
3852
// `rustdoc::formats::item_type::ItemType` type in Rust.
@@ -2133,8 +2147,7 @@ class DocSearch {
21332147
// convert `rawPaths` entries into object form
21342148
// generate normalizedPaths for function search mode
21352149
let len = rawPaths.length;
2136-
const lastPathU = itemPaths.get(0);
2137-
let lastPath = lastPathU === undefined ? null : lastPathU;
2150+
let lastPath = undef2null(itemPaths.get(0));
21382151
for (let i = 0; i < len; ++i) {
21392152
const elem = rawPaths[i];
21402153
const ty = elem[0];
@@ -2192,12 +2205,11 @@ class DocSearch {
21922205
}
21932206
const name = itemNames[i] === "" ? lastName : itemNames[i];
21942207
const word = itemNames[i] === "" ? lastWord : itemNames[i].toLowerCase();
2195-
/** @type {string} */
2196-
// @ts-expect-error
2197-
const path = itemPaths.has(i) ? itemPaths.get(i) : lastPath;
2198-
const paramNames = itemParamNames.has(i) ?
2199-
// @ts-expect-error
2200-
itemParamNames.get(i).split(",") :
2208+
const pathU = itemPaths.get(i);
2209+
const path = pathU !== undefined ? pathU : lastPath;
2210+
const paramNameString = itemParamNames.get(i);
2211+
const paramNames = paramNameString !== undefined ?
2212+
paramNameString.split(",") :
22012213
lastParamNames;
22022214
const type = itemFunctionDecoder.next();
22032215
if (type !== null) {
@@ -2239,8 +2251,7 @@ class DocSearch {
22392251
word,
22402252
normalizedName,
22412253
bitIndex,
2242-
implDisambiguator: implDisambiguator.has(i) ?
2243-
implDisambiguator.get(i) : null,
2254+
implDisambiguator: undef2null(implDisambiguator.get(i)),
22442255
};
22452256
this.nameTrie.insert(normalizedName, id, this.tailTable);
22462257
id += 1;

0 commit comments

Comments
 (0)