@@ -7,6 +7,67 @@ mod tester;
77mod walk;
88
99pub mod cli {
10-
1110 pub use crate :: { command:: * , lint:: LintRunner , result:: CliRunResult , runner:: Runner } ;
1211}
12+
13+ #[ cfg( all( feature = "allocator" , not( miri) , not( target_family = "wasm" ) ) ) ]
14+ #[ global_allocator]
15+ static GLOBAL : mimalloc_safe:: MiMalloc = mimalloc_safe:: MiMalloc ;
16+
17+ use cli:: { CliRunResult , LintRunner , Runner } ;
18+ use std:: { ffi:: OsStr , io:: BufWriter } ;
19+
20+ pub fn lint ( ) -> CliRunResult {
21+ init_tracing ( ) ;
22+ init_miette ( ) ;
23+
24+ let mut args = std:: env:: args_os ( ) . peekable ( ) ;
25+
26+ let args = match args. peek ( ) {
27+ Some ( s) if s == OsStr :: new ( "node" ) => args. skip ( 2 ) ,
28+ _ => args. skip ( 1 ) ,
29+ } ;
30+ let args = args. collect :: < Vec < _ > > ( ) ;
31+
32+ // SAFELY skip first two args (node + script.js)
33+ // let cli_args = std::env::args_os().skip(2);
34+ let cmd = crate :: cli:: lint_command ( ) ;
35+ let command = match cmd. run_inner ( & * args) {
36+ Ok ( cmd) => cmd,
37+ Err ( e) => {
38+ e. print_message ( 100 ) ;
39+ return CliRunResult :: InvalidOptionConfig ;
40+ }
41+ } ;
42+
43+ command. handle_threads ( ) ;
44+ // stdio is blocked by LineWriter, use a BufWriter to reduce syscalls.
45+ // See `https://github.com/rust-lang/rust/issues/60673`.
46+ let mut stdout = BufWriter :: new ( std:: io:: stdout ( ) ) ;
47+
48+ LintRunner :: new ( command) . run ( & mut stdout)
49+ }
50+
51+ // Initialize the data which relies on `is_atty` system calls so they don't block subsequent threads.
52+ fn init_miette ( ) {
53+ miette:: set_hook ( Box :: new ( |_| Box :: new ( miette:: MietteHandlerOpts :: new ( ) . build ( ) ) ) ) . unwrap ( ) ;
54+ }
55+
56+ /// To debug `oxc_resolver`:
57+ /// `OXC_LOG=oxc_resolver oxlint --import-plugin`
58+ fn init_tracing ( ) {
59+ use tracing_subscriber:: { filter:: Targets , prelude:: * } ;
60+
61+ // Usage without the `regex` feature.
62+ // <https://github.com/tokio-rs/tracing/issues/1436#issuecomment-918528013>
63+ tracing_subscriber:: registry ( )
64+ . with ( std:: env:: var ( "OXC_LOG" ) . map_or_else (
65+ |_| Targets :: new ( ) ,
66+ |env_var| {
67+ use std:: str:: FromStr ;
68+ Targets :: from_str ( & env_var) . unwrap ( )
69+ } ,
70+ ) )
71+ . with ( tracing_subscriber:: fmt:: layer ( ) )
72+ . init ( ) ;
73+ }
0 commit comments