@@ -22,6 +22,8 @@ use std::path::{Path, PathBuf};
2222use std:: process:: Command ;
2323use std:: { fmt, fs, io} ;
2424
25+ use crate :: walk:: walk_no_read;
26+
2527const MIN_PY_REV : ( u32 , u32 ) = ( 3 , 9 ) ;
2628const MIN_PY_REV_STR : & str = "≥3.9" ;
2729
@@ -56,8 +58,8 @@ fn check_impl(
5658 extra_checks : Option < & str > ,
5759 pos_args : & [ String ] ,
5860) -> Result < ( ) , Error > {
59- let show_diff = std :: env :: var ( "TIDY_PRINT_DIFF" )
60- . map_or ( false , |v| v. eq_ignore_ascii_case ( "true" ) || v == "1" ) ;
61+ let show_diff =
62+ std :: env :: var ( "TIDY_PRINT_DIFF" ) . is_ok_and ( |v| v. eq_ignore_ascii_case ( "true" ) || v == "1" ) ;
6163
6264 // Split comma-separated args up
6365 let lint_args = match extra_checks {
@@ -72,6 +74,8 @@ fn check_impl(
7274 let shell_lint = lint_args. contains ( & "shell:lint" ) || shell_all;
7375 let cpp_all = lint_args. contains ( & "cpp" ) ;
7476 let cpp_fmt = lint_args. contains ( & "cpp:fmt" ) || cpp_all;
77+ let js_all = lint_args. contains ( & "js" ) ;
78+ let js_lint = lint_args. contains ( & "js:lint" ) || js_all;
7579
7680 let mut py_path = None ;
7781
@@ -224,6 +228,44 @@ fn check_impl(
224228 shellcheck_runner ( & merge_args ( & cfg_args, & file_args_shc) ) ?;
225229 }
226230
231+ if js_lint {
232+ eprintln ! ( "running `eslint` on JS files" ) ;
233+ let src_path = root_path. join ( "src" ) ;
234+ let eslint_version_path =
235+ src_path. join ( "ci/docker/host-x86_64/mingw-check-tidy/eslint.version" ) ;
236+ let eslint_version = fs:: read_to_string ( & eslint_version_path)
237+ . map_err ( |error| {
238+ Error :: Generic ( format ! (
239+ "failed to read `eslint` version file `{}`: {error:?}" ,
240+ eslint_version_path. display( )
241+ ) )
242+ } ) ?
243+ . trim ( )
244+ . to_string ( ) ;
245+ let mut files_to_check = Vec :: new ( ) ;
246+ let librustdoc_path = src_path. join ( "librustdoc" ) ;
247+ let tools_path = src_path. join ( "tools" ) ;
248+ walk_no_read (
249+ & [ & librustdoc_path. join ( "html/static/js" ) ] ,
250+ |path, is_dir| is_dir || path. extension ( ) != Some ( OsStr :: new ( "js" ) ) ,
251+ & mut |path| {
252+ files_to_check. push ( path. path ( ) . into ( ) ) ;
253+ } ,
254+ ) ;
255+ run_eslint ( & eslint_version, & files_to_check, librustdoc_path. join ( "html/static" ) ) ?;
256+
257+ run_eslint (
258+ & eslint_version,
259+ & [ tools_path. join ( "rustdoc-js/tester.js" ) ] ,
260+ tools_path. join ( "rustdoc-js" ) ,
261+ ) ?;
262+ run_eslint (
263+ & eslint_version,
264+ & [ tools_path. join ( "rustdoc-gui/tester.js" ) ] ,
265+ tools_path. join ( "rustdoc-gui" ) ,
266+ ) ?;
267+ }
268+
227269 Ok ( ( ) )
228270}
229271
@@ -532,6 +574,24 @@ fn find_with_extension(
532574 Ok ( output)
533575}
534576
577+ fn run_eslint ( eslint_version : & str , args : & [ PathBuf ] , config_folder : PathBuf ) -> Result < ( ) , Error > {
578+ match Command :: new ( "npx" )
579+ . arg ( format ! ( "eslint@{eslint_version}" ) )
580+ . arg ( "-c" )
581+ . arg ( config_folder. join ( ".eslintrc.js" ) )
582+ . args ( args)
583+ . output ( )
584+ {
585+ Ok ( output) if output. status . success ( ) => Ok ( ( ) ) ,
586+ Ok ( _) => Err ( Error :: FailedCheck ( "eslint" ) ) ,
587+ Err ( _) => Err ( Error :: MissingReq (
588+ "`npx`" ,
589+ "`eslint` JS linter" ,
590+ Some ( "`npx` comes bundled with `node` and `npm`" . to_string ( ) ) ,
591+ ) ) ,
592+ }
593+ }
594+
535595#[ derive( Debug ) ]
536596enum Error {
537597 Io ( io:: Error ) ,
0 commit comments