}
@@ -2695,6 +2729,10 @@ class DocSearch {
const buildHrefAndPath = item => {
let displayPath;
let href;
+ if (item.is_alias) {
+ this.FOUND_ALIASES.add(item.word);
+ item = item.original;
+ }
const type = itemTypes[item.ty];
const name = item.name;
let path = item.path;
@@ -3198,8 +3236,7 @@ class DocSearch {
result.item = this.searchIndex[result.id];
result.word = this.searchIndex[result.id].word;
if (isReturnTypeQuery) {
- // we are doing a return-type based search,
- // deprioritize "clone-like" results,
+ // We are doing a return-type based search, deprioritize "clone-like" results,
// ie. functions that also take the queried type as an argument.
const resultItemType = result.item && result.item.type;
if (!resultItemType) {
@@ -4259,28 +4296,13 @@ class DocSearch {
return false;
}
- // this does not yet have a type in `rustdoc.d.ts`.
- // @ts-expect-error
- function createAliasFromItem(item) {
- return {
- crate: item.crate,
- name: item.name,
- path: item.path,
- descShard: item.descShard,
- descIndex: item.descIndex,
- exactPath: item.exactPath,
- ty: item.ty,
- parent: item.parent,
- type: item.type,
- is_alias: true,
- bitIndex: item.bitIndex,
- implDisambiguator: item.implDisambiguator,
- };
- }
-
// @ts-expect-error
const handleAliases = async(ret, query, filterCrates, currentCrate) => {
const lowerQuery = query.toLowerCase();
+ if (this.FOUND_ALIASES.has(lowerQuery)) {
+ return;
+ }
+ this.FOUND_ALIASES.add(lowerQuery);
// We separate aliases and crate aliases because we want to have current crate
// aliases to be before the others in the displayed results.
// @ts-expect-error
@@ -4292,7 +4314,7 @@ class DocSearch {
&& this.ALIASES.get(filterCrates).has(lowerQuery)) {
const query_aliases = this.ALIASES.get(filterCrates).get(lowerQuery);
for (const alias of query_aliases) {
- aliases.push(createAliasFromItem(this.searchIndex[alias]));
+ aliases.push(alias);
}
}
} else {
@@ -4302,7 +4324,7 @@ class DocSearch {
const pushTo = crate === currentCrate ? crateAliases : aliases;
const query_aliases = crateAliasesIndex.get(lowerQuery);
for (const alias of query_aliases) {
- pushTo.push(createAliasFromItem(this.searchIndex[alias]));
+ pushTo.push(alias);
}
}
}
@@ -4310,9 +4332,9 @@ class DocSearch {
// @ts-expect-error
const sortFunc = (aaa, bbb) => {
- if (aaa.path < bbb.path) {
+ if (aaa.original.path < bbb.original.path) {
return 1;
- } else if (aaa.path === bbb.path) {
+ } else if (aaa.original.path === bbb.original.path) {
return 0;
}
return -1;
@@ -4321,21 +4343,10 @@ class DocSearch {
crateAliases.sort(sortFunc);
aliases.sort(sortFunc);
- // @ts-expect-error
- const fetchDesc = alias => {
- // @ts-expect-error
- return this.searchIndexEmptyDesc.get(alias.crate).contains(alias.bitIndex) ?
- "" : this.searchState.loadDesc(alias);
- };
- const [crateDescs, descs] = await Promise.all([
- // @ts-expect-error
- Promise.all(crateAliases.map(fetchDesc)),
- Promise.all(aliases.map(fetchDesc)),
- ]);
-
// @ts-expect-error
const pushFunc = alias => {
- alias.alias = query;
+ // Cloning `alias` to prevent its fields to be updated.
+ alias = {...alias};
const res = buildHrefAndPath(alias);
alias.displayPath = pathSplitter(res[0]);
alias.fullPath = alias.displayPath + alias.name;
@@ -4347,16 +4358,8 @@ class DocSearch {
}
};
- aliases.forEach((alias, i) => {
- // @ts-expect-error
- alias.desc = descs[i];
- });
aliases.forEach(pushFunc);
// @ts-expect-error
- crateAliases.forEach((alias, i) => {
- alias.desc = crateDescs[i];
- });
- // @ts-expect-error
crateAliases.forEach(pushFunc);
};
@@ -4802,7 +4805,7 @@ async function addTab(array, query, display) {
output.className = "search-results " + extraClass;
const lis = Promise.all(array.map(async item => {
- const name = item.name;
+ const name = item.is_alias ? item.original.name : item.name;
const type = itemTypes[item.ty];
const longType = longItemTypes[item.ty];
const typeName = longType.length !== 0 ? `${longType}` : "?";
@@ -4822,7 +4825,7 @@ async function addTab(array, query, display) {
let alias = " ";
if (item.is_alias) {
alias = ` \
-${item.alias} - see \
+${item.name} - see \
`;
}
resultName.insertAdjacentHTML(
@@ -5201,6 +5204,7 @@ function registerSearchEvents() {
if (searchState.input.value.length === 0) {
searchState.hideResults();
} else {
+ // @ts-ignore
searchState.timeout = setTimeout(search, 500);
}
};
@@ -5842,8 +5846,8 @@ Lev1TParametricDescription.prototype.offsetIncrs3 = /*2 bits per value */ new In
// be called ONLY when the whole file has been parsed and loaded.
// @ts-expect-error
-function initSearch(searchIndx) {
- rawSearchIndex = searchIndx;
+function initSearch(searchIndex) {
+ rawSearchIndex = searchIndex;
if (typeof window !== "undefined") {
// @ts-expect-error
docSearch = new DocSearch(rawSearchIndex, ROOT_PATH, searchState);
diff --git a/src/tools/rustdoc-js/tester.js b/src/tools/rustdoc-js/tester.js
index f70fc917770c6..0baa179e16b2d 100644
--- a/src/tools/rustdoc-js/tester.js
+++ b/src/tools/rustdoc-js/tester.js
@@ -28,7 +28,14 @@ function readFile(filePath) {
}
function contentToDiffLine(key, value) {
- return `"${key}": "${value}",`;
+ if (typeof value === "object" && !Array.isArray(value) && value !== null) {
+ const out = Object.entries(value)
+ .filter(([subKey, _]) => ["path", "name"].includes(subKey))
+ .map(([subKey, subValue]) => `"${subKey}": ${JSON.stringify(subValue)}`)
+ .join(", ");
+ return `"${key}": ${out},`;
+ }
+ return `"${key}": ${JSON.stringify(value)},`;
}
function shouldIgnoreField(fieldName) {
@@ -37,47 +44,61 @@ function shouldIgnoreField(fieldName) {
fieldName === "proposeCorrectionTo";
}
+function valueMapper(key, testOutput) {
+ const isAlias = testOutput["is_alias"];
+ let value = testOutput[key];
+ // To make our life easier, if there is a "parent" type, we add it to the path.
+ if (key === "path") {
+ if (testOutput["parent"] !== undefined) {
+ if (value.length > 0) {
+ value += "::" + testOutput["parent"]["name"];
+ } else {
+ value = testOutput["parent"]["name"];
+ }
+ } else if (testOutput["is_alias"]) {
+ value = valueMapper(key, testOutput["original"]);
+ }
+ } else if (isAlias && key === "alias") {
+ value = testOutput["name"];
+ } else if (isAlias && ["name"].includes(key)) {
+ value = testOutput["original"][key];
+ }
+ return value;
+}
+
// This function is only called when no matching result was found and therefore will only display
// the diff between the two items.
-function betterLookingDiff(entry, data) {
+function betterLookingDiff(expected, testOutput) {
let output = " {\n";
- const spaces = " ";
- for (const key in entry) {
- if (!Object.prototype.hasOwnProperty.call(entry, key)) {
+ const spaces = " ";
+ for (const key in expected) {
+ if (!Object.prototype.hasOwnProperty.call(expected, key)) {
continue;
}
- if (!data || !Object.prototype.hasOwnProperty.call(data, key)) {
- output += "-" + spaces + contentToDiffLine(key, entry[key]) + "\n";
+ if (!testOutput || !Object.prototype.hasOwnProperty.call(testOutput, key)) {
+ output += "-" + spaces + contentToDiffLine(key, expected[key]) + "\n";
continue;
}
- const value = data[key];
- if (value !== entry[key]) {
- output += "-" + spaces + contentToDiffLine(key, entry[key]) + "\n";
+ const value = valueMapper(key, testOutput);
+ if (value !== expected[key]) {
+ output += "-" + spaces + contentToDiffLine(key, expected[key]) + "\n";
output += "+" + spaces + contentToDiffLine(key, value) + "\n";
} else {
- output += spaces + contentToDiffLine(key, value) + "\n";
+ output += spaces + " " + contentToDiffLine(key, value) + "\n";
}
}
return output + " }";
}
-function lookForEntry(entry, data) {
- return data.findIndex(data_entry => {
+function lookForEntry(expected, testOutput) {
+ return testOutput.findIndex(testOutputEntry => {
let allGood = true;
- for (const key in entry) {
- if (!Object.prototype.hasOwnProperty.call(entry, key)) {
+ for (const key in expected) {
+ if (!Object.prototype.hasOwnProperty.call(expected, key)) {
continue;
}
- let value = data_entry[key];
- // To make our life easier, if there is a "parent" type, we add it to the path.
- if (key === "path" && data_entry["parent"] !== undefined) {
- if (value.length > 0) {
- value += "::" + data_entry["parent"]["name"];
- } else {
- value = data_entry["parent"]["name"];
- }
- }
- if (value !== entry[key]) {
+ const value = valueMapper(key, testOutputEntry);
+ if (value !== expected[key]) {
allGood = false;
break;
}
diff --git a/tests/rustdoc-js-std/alias-lev.js b/tests/rustdoc-js-std/alias-lev.js
new file mode 100644
index 0000000000000..17f3dc25d7633
--- /dev/null
+++ b/tests/rustdoc-js-std/alias-lev.js
@@ -0,0 +1,11 @@
+// This test ensures that aliases are also allowed to be partially matched.
+
+// ignore-order
+
+const EXPECTED = {
+ // The full alias name is `getcwd`.
+ 'query': 'getcw',
+ 'others': [
+ { 'path': 'std::env', 'name': 'current_dir', 'alias': 'getcwd' },
+ ],
+};
diff --git a/tests/rustdoc-js/non-english-identifier.js b/tests/rustdoc-js/non-english-identifier.js
index f2180b4c75530..3d50bd3ee9057 100644
--- a/tests/rustdoc-js/non-english-identifier.js
+++ b/tests/rustdoc-js/non-english-identifier.js
@@ -115,11 +115,10 @@ const EXPECTED = [
query: '加法',
others: [
{
- name: "add",
+ name: "加法",
path: "non_english_identifier",
- is_alias: true,
- alias: "加法",
- href: "../non_english_identifier/macro.add.html"
+ href: "../non_english_identifier/trait.加法.html",
+ desc: "Add"
},
{
name: "add",
@@ -129,11 +128,13 @@ const EXPECTED = [
href: "../non_english_identifier/fn.add.html"
},
{
- name: "加法",
+ name: "add",
path: "non_english_identifier",
- href: "../non_english_identifier/trait.加法.html",
- desc: "Add"
- }],
+ is_alias: true,
+ alias: "加法",
+ href: "../non_english_identifier/macro.add.html"
+ },
+ ],
in_args: [{
name: "加上",
path: "non_english_identifier::加法",