Skip to content

Commit 7346099

Browse files
committed
refactor(linter): move oxlint application code into separate module (#13745)
Pure refactor. Move code for running the linter in `oxlint` into a separate `run.rs` module. This is preparation for making `oxlint` into a NAPI package (#13723).
1 parent 3f3ee10 commit 7346099

File tree

2 files changed

+89
-84
lines changed

2 files changed

+89
-84
lines changed

apps/oxlint/src/lib.rs

Lines changed: 5 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::io::BufWriter;
2-
1+
/// Re-exported external linter types for use in `napi/oxlint`.
32
pub use oxc_linter::{
43
ExternalLinter, ExternalLinterLintFileCb, ExternalLinterLoadPluginCb, LintFileResult,
54
PluginLoadResult,
@@ -10,98 +9,20 @@ mod js_plugins;
109
mod lint;
1110
mod output_formatter;
1211
mod result;
12+
mod run;
1313
mod walk;
1414

1515
#[cfg(test)]
1616
mod tester;
1717

18-
use lint::LintRunner;
19-
use result::CliRunResult;
20-
2118
/// Re-exported CLI-related items for use in `tasks/website`.
2219
pub mod cli {
2320
pub use super::{command::*, lint::LintRunner, result::CliRunResult};
2421
}
2522

23+
/// Main export for binary
24+
pub use run::lint;
25+
2626
#[cfg(all(feature = "allocator", not(miri), not(target_family = "wasm")))]
2727
#[global_allocator]
2828
static GLOBAL: mimalloc_safe::MiMalloc = mimalloc_safe::MiMalloc;
29-
30-
/// Run the linter.
31-
pub fn lint(mut external_linter: Option<ExternalLinter>) -> CliRunResult {
32-
init_tracing();
33-
init_miette();
34-
35-
let mut args = std::env::args_os();
36-
// If first arg is `node`, also skip script path (`node script.js ...`).
37-
// Otherwise, just skip first arg (`oxlint ...`).
38-
if args.next().is_some_and(|arg| arg == "node") {
39-
args.next();
40-
}
41-
let args = args.collect::<Vec<_>>();
42-
43-
let cmd = crate::cli::lint_command();
44-
let command = match cmd.run_inner(&*args) {
45-
Ok(cmd) => cmd,
46-
Err(e) => {
47-
e.print_message(100);
48-
return if e.exit_code() == 0 {
49-
CliRunResult::LintSucceeded
50-
} else {
51-
CliRunResult::InvalidOptionConfig
52-
};
53-
}
54-
};
55-
56-
command.handle_threads();
57-
58-
#[expect(clippy::print_stderr)]
59-
if command.experimental_js_plugins {
60-
// If no `ExternalLinter`, this function was not called by `napi/oxlint`
61-
if external_linter.is_none() {
62-
eprintln!("ERROR: JS plugins are not supported at present");
63-
return CliRunResult::InvalidOptionConfig;
64-
}
65-
66-
// Exit early if not 64-bit little-endian, to avoid a panic later on when trying to create
67-
// a fixed-size allocator for raw transfer
68-
if cfg!(not(all(target_pointer_width = "64", target_endian = "little"))) {
69-
eprintln!(
70-
"ERROR: JS plugins are only supported on 64-bit little-endian platforms at present"
71-
);
72-
return CliRunResult::InvalidOptionConfig;
73-
}
74-
} else {
75-
external_linter = None;
76-
}
77-
78-
// stdio is blocked by LineWriter, use a BufWriter to reduce syscalls.
79-
// See `https://github.com/rust-lang/rust/issues/60673`.
80-
let mut stdout = BufWriter::new(std::io::stdout());
81-
82-
LintRunner::new(command, external_linter).run(&mut stdout)
83-
}
84-
85-
/// Initialize the data which relies on `is_atty` system calls so they don't block subsequent threads.
86-
fn init_miette() {
87-
miette::set_hook(Box::new(|_| Box::new(miette::MietteHandlerOpts::new().build()))).unwrap();
88-
}
89-
90-
/// To debug `oxc_resolver`:
91-
/// `OXC_LOG=oxc_resolver oxlint --import-plugin`
92-
fn init_tracing() {
93-
use tracing_subscriber::{filter::Targets, prelude::*};
94-
95-
// Usage without the `regex` feature.
96-
// <https://github.com/tokio-rs/tracing/issues/1436#issuecomment-918528013>
97-
tracing_subscriber::registry()
98-
.with(std::env::var("OXC_LOG").map_or_else(
99-
|_| Targets::new(),
100-
|env_var| {
101-
use std::str::FromStr;
102-
Targets::from_str(&env_var).unwrap()
103-
},
104-
))
105-
.with(tracing_subscriber::fmt::layer())
106-
.init();
107-
}

apps/oxlint/src/run.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use std::io::BufWriter;
2+
3+
use oxc_linter::ExternalLinter;
4+
5+
use crate::{lint::LintRunner, result::CliRunResult};
6+
7+
/// Run the linter.
8+
pub fn lint(mut external_linter: Option<ExternalLinter>) -> CliRunResult {
9+
init_tracing();
10+
init_miette();
11+
12+
let mut args = std::env::args_os();
13+
// If first arg is `node`, also skip script path (`node script.js ...`).
14+
// Otherwise, just skip first arg (`oxlint ...`).
15+
if args.next().is_some_and(|arg| arg == "node") {
16+
args.next();
17+
}
18+
let args = args.collect::<Vec<_>>();
19+
20+
let cmd = crate::cli::lint_command();
21+
let command = match cmd.run_inner(&*args) {
22+
Ok(cmd) => cmd,
23+
Err(e) => {
24+
e.print_message(100);
25+
return if e.exit_code() == 0 {
26+
CliRunResult::LintSucceeded
27+
} else {
28+
CliRunResult::InvalidOptionConfig
29+
};
30+
}
31+
};
32+
33+
command.handle_threads();
34+
35+
#[expect(clippy::print_stderr)]
36+
if command.experimental_js_plugins {
37+
// If no `ExternalLinter`, this function was not called by `napi/oxlint`
38+
if external_linter.is_none() {
39+
eprintln!("ERROR: JS plugins are not supported at present");
40+
return CliRunResult::InvalidOptionConfig;
41+
}
42+
43+
// Exit early if not 64-bit little-endian, to avoid a panic later on when trying to create
44+
// a fixed-size allocator for raw transfer
45+
if cfg!(not(all(target_pointer_width = "64", target_endian = "little"))) {
46+
eprintln!(
47+
"ERROR: JS plugins are only supported on 64-bit little-endian platforms at present"
48+
);
49+
return CliRunResult::InvalidOptionConfig;
50+
}
51+
} else {
52+
external_linter = None;
53+
}
54+
55+
// stdio is blocked by LineWriter, use a BufWriter to reduce syscalls.
56+
// See `https://github.com/rust-lang/rust/issues/60673`.
57+
let mut stdout = BufWriter::new(std::io::stdout());
58+
59+
LintRunner::new(command, external_linter).run(&mut stdout)
60+
}
61+
62+
/// Initialize the data which relies on `is_atty` system calls so they don't block subsequent threads.
63+
fn init_miette() {
64+
miette::set_hook(Box::new(|_| Box::new(miette::MietteHandlerOpts::new().build()))).unwrap();
65+
}
66+
67+
/// To debug `oxc_resolver`:
68+
/// `OXC_LOG=oxc_resolver oxlint --import-plugin`
69+
fn init_tracing() {
70+
use tracing_subscriber::{filter::Targets, prelude::*};
71+
72+
// Usage without the `regex` feature.
73+
// <https://github.com/tokio-rs/tracing/issues/1436#issuecomment-918528013>
74+
tracing_subscriber::registry()
75+
.with(std::env::var("OXC_LOG").map_or_else(
76+
|_| Targets::new(),
77+
|env_var| {
78+
use std::str::FromStr;
79+
Targets::from_str(&env_var).unwrap()
80+
},
81+
))
82+
.with(tracing_subscriber::fmt::layer())
83+
.init();
84+
}

0 commit comments

Comments
 (0)