Skip to content

Commit 0527f02

Browse files
Add eslint as part of tidy run
1 parent cb678b9 commit 0527f02

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/tools/tidy/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub mod mir_opt_tests;
8282
pub mod pal;
8383
pub mod rustdoc_css_themes;
8484
pub mod rustdoc_gui_tests;
85+
pub mod rustdoc_js;
8586
pub mod rustdoc_templates;
8687
pub mod style;
8788
pub mod target_policy;

src/tools/tidy/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fn main() {
3535
let library_path = root_path.join("library");
3636
let compiler_path = root_path.join("compiler");
3737
let librustdoc_path = src_path.join("librustdoc");
38+
let tools_path = src_path.join("tools");
3839
let crashes_path = tests_path.join("crashes");
3940

4041
let args: Vec<String> = env::args().skip(1).collect();
@@ -108,6 +109,7 @@ fn main() {
108109
check!(rustdoc_gui_tests, &tests_path);
109110
check!(rustdoc_css_themes, &librustdoc_path);
110111
check!(rustdoc_templates, &librustdoc_path);
112+
check!(rustdoc_js, &librustdoc_path, &tools_path);
111113
check!(known_bug, &crashes_path);
112114
check!(unknown_revision, &tests_path);
113115

src/tools/tidy/src/rustdoc_js.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//! Tidy check to ensure that rustdoc templates didn't forget a `{# #}` to strip extra whitespace
2+
//! characters.
3+
4+
use std::ffi::OsStr;
5+
use std::path::{Path, PathBuf};
6+
use std::process::Command;
7+
8+
use ignore::DirEntry;
9+
10+
use crate::walk::walk_no_read;
11+
12+
fn run_eslint(args: &[PathBuf], config_folder: PathBuf, bad: &mut bool) {
13+
let mut child = match Command::new("npx")
14+
.arg("eslint")
15+
.arg("-c")
16+
.arg(config_folder.join(".eslintrc.js"))
17+
.args(args)
18+
.spawn()
19+
{
20+
Ok(child) => child,
21+
Err(error) => {
22+
*bad = true;
23+
eprintln!("failed to run eslint: {error:?}");
24+
return;
25+
}
26+
};
27+
match child.wait() {
28+
Ok(exit_status) => {
29+
if exit_status.success() {
30+
return;
31+
}
32+
eprintln!("eslint command failed");
33+
}
34+
Err(error) => eprintln!("eslint command failed: {error:?}"),
35+
}
36+
*bad = true;
37+
}
38+
39+
fn get_eslint_version_inner(global: bool) -> Option<String> {
40+
let mut command = Command::new("npm");
41+
command.arg("list").arg("--parseable").arg("--long").arg("--depth=0");
42+
if global {
43+
command.arg("--global");
44+
}
45+
let output = command.output().ok()?;
46+
let lines = String::from_utf8_lossy(&output.stdout);
47+
lines.lines().find_map(|l| l.split(':').nth(1)?.strip_prefix("eslint@")).map(|v| v.to_owned())
48+
}
49+
50+
fn get_eslint_version() -> Option<String> {
51+
get_eslint_version_inner(false).or_else(|| get_eslint_version_inner(true))
52+
}
53+
54+
const ESLINT_VERSION: &str = "8.6.0";
55+
56+
pub fn check(librustdoc_path: &Path, tools_path: &Path, bad: &mut bool) {
57+
match get_eslint_version() {
58+
Some(version) => {
59+
if version != ESLINT_VERSION {
60+
*bad = true;
61+
eprintln!(
62+
"⚠️ Installed version of eslint (`{version}`) is different than the \
63+
one used in the CI (`{ESLINT_VERSION}`)",
64+
);
65+
eprintln!(
66+
"You can install this version using `npm update eslint` or by using \
67+
`npm install eslint@{ESLINT_VERSION}`",
68+
);
69+
return;
70+
}
71+
}
72+
None => {
73+
eprintln!("`eslint` doesn't seem to be installed. Skipping tidy check for JS files.");
74+
eprintln!("You can install it using `npm install eslint@{ESLINT_VERSION}`");
75+
return;
76+
}
77+
}
78+
let mut files_to_check = Vec::new();
79+
walk_no_read(
80+
&[&librustdoc_path.join("html/static/js")],
81+
|path, is_dir| is_dir || !path.extension().is_some_and(|ext| ext == OsStr::new("js")),
82+
&mut |path: &DirEntry| {
83+
files_to_check.push(path.path().into());
84+
},
85+
);
86+
println!("Running eslint on rustdoc JS files");
87+
run_eslint(&files_to_check, librustdoc_path.join("html/static"), bad);
88+
89+
run_eslint(&[tools_path.join("rustdoc-js/tester.js")], tools_path.join("rustdoc-js"), bad);
90+
run_eslint(&[tools_path.join("rustdoc-gui/tester.js")], tools_path.join("rustdoc-gui"), bad);
91+
}

0 commit comments

Comments
 (0)