Skip to content
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Serialize;
#[allow(clippy::wildcard_imports)]
use crate::ast::*;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, PartialOrd, Ord)]
pub struct Span {
pub start: u32,
pub end: u32,
Expand Down
19 changes: 19 additions & 0 deletions crates/oxc_cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ impl Command {
.alias("check")
.about("Lint this repository.")
.arg_required_else_help(true)
.arg(
Arg::new("fix")
.long("fix")
.required(false)
.action(ArgAction::SetTrue)
.help("This option allows you to enable oxc to fix as many issues as possible. If enabled, only unfixed issues are reported in the output")
)
.arg(
Arg::new("quiet")
.long("quiet")
Expand Down Expand Up @@ -134,6 +141,18 @@ mod test {
assert!(!matches.get_flag("quiet"));
}

#[test]
fn test_fix_true() {
let matches = get_lint_matches("oxc lint foo.js --fix");
assert!(matches.get_flag("fix"));
}

#[test]
fn test_fix_false() {
let matches = get_lint_matches("oxc lint foo.js");
assert!(!matches.get_flag("fix"));
}

#[test]
fn test_max_warnings_none() {
let arg = "oxc lint foo.js";
Expand Down
8 changes: 5 additions & 3 deletions crates/oxc_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ impl Cli {
})
.collect::<Vec<_>>();

let fix = self.cli_options.fix;

let (_, (warnings, diagnostics)): (_, (usize, usize)) = rayon::join(
move || {
paths.par_iter().for_each(|path| {
let diagnostics = Self::lint_path(path);
let diagnostics = Self::lint_path(path, fix);

for d in diagnostics {
sender.send(d).unwrap();
Expand Down Expand Up @@ -118,7 +120,7 @@ impl Cli {
}
}

fn lint_path(path: &Path) -> Vec<Error> {
fn lint_path(path: &Path, fix: bool) -> Vec<Error> {
let source_text = fs::read_to_string(path).expect("{name} not found");
let allocator = Allocator::default();
let source_type = SourceType::from_path(path).expect("incorrect {path:?}");
Expand All @@ -127,7 +129,7 @@ impl Cli {
let result = if ret.errors.is_empty() {
let program = allocator.alloc(ret.program);
let semantic = SemanticBuilder::new().build(program, ret.trivias);
Linter::new().run(&Rc::new(semantic), &source_text)
Linter::new().run(&Rc::new(semantic), &source_text, fix)
} else {
LintRunResult { fixed_source: source_text.clone().into(), diagnostics: ret.errors }
};
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use glob::Pattern;

pub struct CliOptions {
pub quiet: bool,
pub fix: bool,
pub max_warnings: Option<usize>,
pub paths: Vec<PathBuf>,
pub ignore_path: String,
Expand Down Expand Up @@ -38,6 +39,7 @@ impl<'a> TryFrom<&'a ArgMatches> for CliOptions {

Ok(Self {
quiet: matches.get_flag("quiet"),
fix: matches.get_flag("fix"),
max_warnings: matches.get_one("max-warnings").copied(),
paths,
ignore_path,
Expand Down
Loading