Skip to content

Commit

Permalink
Improve code and add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 8, 2021
1 parent af66036 commit 802a293
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,8 @@ fn init_id_map() -> FxHashMap<String, usize> {
}}
}

// IMPORTANT: Do NOT change the formatting or name of this macro
// without updating the tidy check.
html_id_map!(
// This is the list of IDs used in Javascript.
"help",
Expand Down
2 changes: 2 additions & 0 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
// Trait documentation
document(w, cx, it, None, HeadingOffset::H2);

// This function is checked in tidy for rustdoc IDs. If you rename/update it, don't forget
// to update the `src/tools/tidy/rustdoc_html_ids.rs` file.
fn write_small_section_header(w: &mut Buffer, id: &str, title: &str, extra_content: &str) {
write!(
w,
Expand Down
21 changes: 14 additions & 7 deletions src/tools/tidy/src/rustdoc_html_ids.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Checks that the rustdoc ID map is up-to-date. The goal here is to check a few things:
//!
//! * All IDs created by rustdoc (through JS or files generation) are declared in the ID map.
//! * All IDs created by rustdoc (through JS or `.html` files generation) are declared in the
//! ID map.
//! * There are no unused IDs.

use std::collections::HashMap;
Expand Down Expand Up @@ -31,7 +32,8 @@ fn extract_ids(path: &Path, bad: &mut bool) -> HashMap<String, usize> {
}
}
// We're now in the function body, time to retrieve the IDs!
while let Some(Ok(line)) = iter.next() {
while let Some(line) = iter.next() {
let line = line.unwrap();
let line = line.trim_start();
if line.starts_with("// ") {
// It's a comment, ignoring this line...
Expand All @@ -44,13 +46,14 @@ fn extract_ids(path: &Path, bad: &mut bool) -> HashMap<String, usize> {
if ids.insert(id.to_owned(), 0).is_some() {
eprintln!(
"=> ID `{}` is defined more than once in the ID map in file `{}`",
id, ID_MAP_PATH
id,
path.display(),
);
*bad = true;
}
}
if ids.is_empty() {
eprintln!("=> No IDs were found in rustdoc in file `{}`...", ID_MAP_PATH);
eprintln!("=> No IDs were found in rustdoc in file `{}`...", path.display());
*bad = true;
}
ids
Expand Down Expand Up @@ -102,7 +105,11 @@ fn check_ids(
check_id(path, trimmed.split('"').skip(1).next().unwrap(), ids, line_nb, bad);
is_checking_small_section_header = None;
}
} else if trimmed.starts_with("write_small_section_header(") {
} else if trimmed.contains("write_small_section_header(")
&& !trimmed.contains("fn write_small_section_header(")
{
// First we extract the arguments.
let trimmed = trimmed.split("write_small_section_header(").skip(1).next().unwrap_or("");
// This function is used to create section: the second argument of the function is an
// ID and we need to check it as well, hence this specific check...
if trimmed.contains(',') {
Expand Down Expand Up @@ -145,12 +152,12 @@ pub fn check(path: &Path, bad: &mut bool) {
);
if small_section_header_checked == 0 {
eprintln!(
"No call to the `write_small_section_header` function was found. Was it renamed?",
"=> No call to the `write_small_section_header` function was found. Was it renamed?",
);
*bad = true;
}
for (id, nb) in ids {
if IDS_USED_IN_JS.iter().any(|i| i == &id) {
if IDS_USED_IN_JS.contains(&id.as_str()) {
if nb != 0 {
eprintln!("=> ID `{}` is not supposed to be used in Rust code but in the JS!", id);
*bad = true;
Expand Down

0 comments on commit 802a293

Please sign in to comment.