Skip to content

Commit

Permalink
rustdoc: Fix doc aliases with crate filtering
Browse files Browse the repository at this point in the history
Fix a crash when searching for an alias contained in the currently selected filter crate.

Also remove alias search results for crates that should be filtered out.

The test suite needed to be fixed to actually take into account the crate filtering and check that there are no results when none are expected.
  • Loading branch information
ollie27 committed Jun 23, 2020
1 parent 033013c commit 478750c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1015,12 +1015,13 @@ function defocusSearchBar() {
var aliases = [];
var crateAliases = [];
var i;
if (filterCrates !== undefined &&
ALIASES[filterCrates] &&
ALIASES[filterCrates][query.search]) {
for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
aliases.push(
createAliasFromItem(searchIndex[ALIASES[filterCrates][query.search]]));
if (filterCrates !== undefined) {
if (ALIASES[filterCrates] && ALIASES[filterCrates][query.search]) {
for (i = 0; i < ALIASES[filterCrates][query.search].length; ++i) {
aliases.push(
createAliasFromItem(
searchIndex[ALIASES[filterCrates][query.search][i]]));
}
}
} else {
Object.keys(ALIASES).forEach(function(crate) {
Expand Down
9 changes: 9 additions & 0 deletions src/test/rustdoc-js/doc-alias-filter-out.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// exact-check

const QUERY = 'true';

const FILTER_CRATE = 'some_other_crate';

const EXPECTED = {
'others': [],
};
4 changes: 4 additions & 0 deletions src/test/rustdoc-js/doc-alias-filter-out.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![feature(doc_alias)]

#[doc(alias = "true")]
pub struct Foo;
17 changes: 17 additions & 0 deletions src/test/rustdoc-js/doc-alias-filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// exact-check

const QUERY = 'true';

const FILTER_CRATE = 'doc_alias_filter';

const EXPECTED = {
'others': [
{
'path': 'doc_alias_filter',
'name': 'Foo',
'alias': 'true',
'href': '../doc_alias_filter/struct.Foo.html',
'is_alias': true
},
],
};
7 changes: 7 additions & 0 deletions src/test/rustdoc-js/doc-alias-filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(doc_alias)]

#[doc(alias = "true")]
pub struct Foo;

#[doc(alias = "false")]
pub struct Bar;
13 changes: 11 additions & 2 deletions src/tools/rustdoc-js/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ function runSearch(query, expected, index, loaded, loadedFile, queryName) {
break;
}
var entry = expected[key];

if (exact_check == true && entry.length !== results[key].length) {
error_text.push(queryName + "==> Expected exactly " + entry.length +
" results but found " + results[key].length + " in '" + key + "'");
}

var prev_pos = -1;
for (var i = 0; i < entry.length; ++i) {
var entry_pos = lookForEntry(entry[i], results[key]);
Expand Down Expand Up @@ -307,8 +313,11 @@ function checkResult(error_text, loadedFile, displaySuccess) {
}

function runChecks(testFile, loaded, index) {
var loadedFile = loadContent(
readFile(testFile) + 'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;');
var testFileContent = readFile(testFile) + 'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;';
if (testFileContent.indexOf("FILTER_CRATE") !== -1) {
testFileContent += "exports.FILTER_CRATE = FILTER_CRATE;";
}
var loadedFile = loadContent(testFileContent);

const expected = loadedFile.EXPECTED;
const query = loadedFile.QUERY;
Expand Down

0 comments on commit 478750c

Please sign in to comment.