@@ -22,6 +22,8 @@ use std::path::{Path, PathBuf};
22
22
use std:: process:: Command ;
23
23
use std:: { fmt, fs, io} ;
24
24
25
+ use crate :: walk:: walk_no_read;
26
+
25
27
const MIN_PY_REV : ( u32 , u32 ) = ( 3 , 9 ) ;
26
28
const MIN_PY_REV_STR : & str = "≥3.9" ;
27
29
@@ -56,8 +58,8 @@ fn check_impl(
56
58
extra_checks : Option < & str > ,
57
59
pos_args : & [ String ] ,
58
60
) -> 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" ) ;
61
63
62
64
// Split comma-separated args up
63
65
let lint_args = match extra_checks {
@@ -72,6 +74,8 @@ fn check_impl(
72
74
let shell_lint = lint_args. contains ( & "shell:lint" ) || shell_all;
73
75
let cpp_all = lint_args. contains ( & "cpp" ) ;
74
76
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;
75
79
76
80
let mut py_path = None ;
77
81
@@ -224,6 +228,44 @@ fn check_impl(
224
228
shellcheck_runner ( & merge_args ( & cfg_args, & file_args_shc) ) ?;
225
229
}
226
230
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
+
227
269
Ok ( ( ) )
228
270
}
229
271
@@ -532,6 +574,24 @@ fn find_with_extension(
532
574
Ok ( output)
533
575
}
534
576
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
+
535
595
#[ derive( Debug ) ]
536
596
enum Error {
537
597
Io ( io:: Error ) ,
0 commit comments