Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/oxlint/src/command/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ pub struct BasicOptions {
/// TypeScript `tsconfig.json` path for reading path alias and project references for import plugin
#[bpaf(argument("./tsconfig.json"), hide_usage)]
pub tsconfig: Option<PathBuf>,

/// Initialize oxlint configuration with default values
#[bpaf(switch, hide_usage)]
pub init: bool,
}

// This is formatted according to
Expand Down
43 changes: 36 additions & 7 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
env,
env, fs,
io::BufWriter,
path::{Path, PathBuf},
time::Instant,
Expand Down Expand Up @@ -118,15 +118,32 @@ impl Runner for LintRunner {

enable_plugins.apply_overrides(&mut oxlintrc.plugins);

let oxlintrc_for_print =
if misc_options.print_config { Some(oxlintrc.clone()) } else { None };
let oxlintrc_for_print = if misc_options.print_config || basic_options.init {
Some(oxlintrc.clone())
} else {
None
};
let config_builder =
ConfigStoreBuilder::from_oxlintrc(false, oxlintrc).with_filters(filter);

if let Some(basic_config_file) = oxlintrc_for_print {
return CliRunResult::PrintConfigResult {
config_file: config_builder.resolve_final_config_file(basic_config_file),
};
let config_file = config_builder.resolve_final_config_file(basic_config_file);
if misc_options.print_config {
return CliRunResult::PrintConfigResult { config_file };
} else if basic_options.init {
match fs::write(Self::DEFAULT_OXLINTRC, config_file) {
Ok(()) => {
return CliRunResult::ConfigFileInitResult {
message: "Configuration file created".to_string(),
}
}
Err(_) => {
return CliRunResult::ConfigFileInitResult {
message: "Failed to create configuration file".to_string(),
}
}
}
}
}

let mut options = LintServiceOptions::new(self.cwd, paths)
Expand Down Expand Up @@ -283,7 +300,7 @@ impl LintRunner {

#[cfg(test)]
mod test {
use std::{env, path::MAIN_SEPARATOR_STR};
use std::{env, fs, path::MAIN_SEPARATOR_STR};

use super::LintRunner;
use crate::cli::{lint_command, CliRunResult, LintResult, Runner};
Expand Down Expand Up @@ -750,6 +767,18 @@ mod test {
assert_eq!(config, expect_json.trim());
}

#[test]
fn test_init_config() {
let args = &["--init"];
let options = lint_command().run_inner(args).unwrap();
let ret = LintRunner::new(options).run();
let CliRunResult::ConfigFileInitResult { message } = ret else {
panic!("Expected configuration file to be created, got {ret:?}")
};
assert_eq!(message, "Configuration file created");
fs::remove_file(LintRunner::DEFAULT_OXLINTRC).unwrap();
}

#[test]
fn test_overrides() {
let result =
Expand Down
5 changes: 5 additions & 0 deletions apps/oxlint/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum CliRunResult {
PathNotFound { paths: Vec<PathBuf> },
LintResult(LintResult),
PrintConfigResult { config_file: String },
ConfigFileInitResult { message: String },
}

/// A summary of a complete linter run.
Expand Down Expand Up @@ -93,6 +94,10 @@ impl Termination for CliRunResult {
println!("{config_file}");
ExitCode::from(0)
}
Self::ConfigFileInitResult { message } => {
println!("{message}");
ExitCode::from(0)
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions tasks/website/src/linter/snapshots/cli.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ snapshot_kind: text
If not provided, Oxlint will look for `.oxlintrc.json` in the current working directory.
- **` --tsconfig`**=_`<./tsconfig.json>`_ &mdash;
TypeScript `tsconfig.json` path for reading path alias and project references for import plugin
- **` --init`** &mdash;
Initialize oxlint configuration with default values



Expand Down
1 change: 1 addition & 0 deletions tasks/website/src/linter/snapshots/cli_terminal.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Basic Configuration
* tries to be compatible with the ESLint v8's format
--tsconfig=<./tsconfig.json> TypeScript `tsconfig.json` path for reading path alias and
project references for import plugin
--init Initialize oxlint configuration with default values

Allowing / Denying Multiple Lints
Accumulate rules and categories from left to right on the command-line.
Expand Down