mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 20:28:58 +00:00
feat(oxlint): implement --init cli option (#8453)
Implemented --init cli option to create oxlint configuration files with default values. close https://github.com/oxc-project/oxc/issues/7453 --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
407187894e
commit
4ac2e990a2
5 changed files with 48 additions and 7 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use std::{
|
||||
env,
|
||||
env, fs,
|
||||
io::BufWriter,
|
||||
path::{Path, PathBuf},
|
||||
time::Instant,
|
||||
|
|
@ -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)
|
||||
|
|
@ -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};
|
||||
|
|
@ -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 =
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -93,6 +94,10 @@ impl Termination for CliRunResult {
|
|||
println!("{config_file}");
|
||||
ExitCode::from(0)
|
||||
}
|
||||
Self::ConfigFileInitResult { message } => {
|
||||
println!("{message}");
|
||||
ExitCode::from(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@ snapshot_kind: text
|
|||
If not provided, Oxlint will look for `.oxlintrc.json` in the current working directory.
|
||||
- **` --tsconfig`**=_`<./tsconfig.json>`_ —
|
||||
TypeScript `tsconfig.json` path for reading path alias and project references for import plugin
|
||||
- **` --init`** —
|
||||
Initialize oxlint configuration with default values
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue