From 539957130d095589bdec370f407e1637b156d537 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 31 Aug 2023 18:51:18 +0200 Subject: [PATCH] Improve `search.js` code --- src/librustdoc/html/static/js/search.js | 30 +++++++++++-------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 42088e73554..00b6b3a4420 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -263,7 +263,6 @@ function initSearch(rawSearchIndex) { * @returns {integer} */ function buildTypeMapIndex(name) { - if (name === "" || name === null) { return -1; } @@ -1380,7 +1379,7 @@ function initSearch(rawSearchIndex) { * @type Map */ const queryElemSet = new Map(); - const addQueryElemToQueryElemSet = function addQueryElemToQueryElemSet(queryElem) { + const addQueryElemToQueryElemSet = queryElem => { let currentQueryElemList; if (queryElemSet.has(queryElem.id)) { currentQueryElemList = queryElemSet.get(queryElem.id); @@ -1397,7 +1396,7 @@ function initSearch(rawSearchIndex) { * @type Map */ const fnTypeSet = new Map(); - const addFnTypeToFnTypeSet = function addFnTypeToFnTypeSet(fnType) { + const addFnTypeToFnTypeSet = fnType => { // Pure generic, or an item that's not matched by any query elems. // Try [unboxing] it. // @@ -2385,12 +2384,11 @@ ${item.displayPath}${name}\ lowercasePaths ); } + // `0` is used as a sentinel because it's fewer bytes than `null` + const item = pathIndex === 0 ? null : lowercasePaths[pathIndex - 1]; return { - // `0` is used as a sentinel because it's fewer bytes than `null` - id: pathIndex === 0 - ? -1 - : buildTypeMapIndex(lowercasePaths[pathIndex - 1].name), - ty: pathIndex === 0 ? null : lowercasePaths[pathIndex - 1].ty, + id: item === null ? -1 : buildTypeMapIndex(item.name), + ty: item === null ? null : item.ty, generics: generics, }; }); @@ -2419,14 +2417,13 @@ ${item.displayPath}${name}\ if (functionSearchType === 0) { return null; } - let inputs, output; + let inputs, output, item; if (typeof functionSearchType[INPUTS_DATA] === "number") { const pathIndex = functionSearchType[INPUTS_DATA]; + item = pathIndex === 0 ? null : lowercasePaths[pathIndex - 1]; inputs = [{ - id: pathIndex === 0 - ? -1 - : buildTypeMapIndex(lowercasePaths[pathIndex - 1].name), - ty: pathIndex === 0 ? null : lowercasePaths[pathIndex - 1].ty, + id: item === null ? -1 : buildTypeMapIndex(item.name), + ty: item === null ? null : item.ty, generics: [], }]; } else { @@ -2438,11 +2435,10 @@ ${item.displayPath}${name}\ if (functionSearchType.length > 1) { if (typeof functionSearchType[OUTPUT_DATA] === "number") { const pathIndex = functionSearchType[OUTPUT_DATA]; + item = pathIndex === 0 ? null : lowercasePaths[pathIndex - 1]; output = [{ - id: pathIndex === 0 - ? -1 - : buildTypeMapIndex(lowercasePaths[pathIndex - 1].name), - ty: pathIndex === 0 ? null : lowercasePaths[pathIndex - 1].ty, + id: item === null ? -1 : buildTypeMapIndex(item.name), + ty: item === null ? null : item.ty, generics: [], }]; } else {