feat(oxlint): auto detect config file in CLI (#7348)

waiting fior #7352

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Alexander S. 2024-11-23 16:17:05 +01:00 committed by GitHub
parent 63f4d6c24a
commit 9558087db9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 62 additions and 16 deletions

View file

@ -0,0 +1,5 @@
{
"rules": {
"no-debugger": "error"
}
}

View file

@ -0,0 +1 @@
debugger;

View file

@ -62,6 +62,8 @@ pub struct BasicOptions {
/// Oxlint configuration file (experimental)
/// * only `.json` extension is supported
/// * tries to be compatible with the ESLint v8's format
///
/// If not provided, Oxlint will look for `.oxlintrc.json` in the current working directory.
#[bpaf(long, short, argument("./oxlintrc.json"))]
pub config: Option<PathBuf>,

View file

@ -1,4 +1,9 @@
use std::{env, io::BufWriter, path::PathBuf, time::Instant};
use std::{
env,
io::BufWriter,
path::{Path, PathBuf},
time::Instant,
};
use ignore::gitignore::Gitignore;
use oxc_diagnostics::{DiagnosticService, GraphicalReportHandler};
@ -102,21 +107,13 @@ impl Runner for LintRunner {
let number_of_files = paths.len();
let mut oxlintrc = if let Some(config_path) = basic_options.config.as_ref() {
match Oxlintrc::from_file(config_path) {
Ok(config) => config,
Err(diagnostic) => {
let handler = GraphicalReportHandler::new();
let mut err = String::new();
handler.render_report(&mut err, &diagnostic).unwrap();
return CliRunResult::InvalidOptions {
message: format!("Failed to parse configuration file.\n{err}"),
};
}
}
} else {
Oxlintrc::default()
};
let config_search_result = Self::find_oxlint_config(&self.cwd, &basic_options.config);
if let Err(err) = config_search_result {
return err;
}
let mut oxlintrc = config_search_result.unwrap();
enable_plugins.apply_overrides(&mut oxlintrc.plugins);
@ -179,6 +176,8 @@ impl Runner for LintRunner {
}
impl LintRunner {
const DEFAULT_OXLINTRC: &'static str = ".oxlintrc.json";
#[must_use]
pub fn with_cwd(mut self, cwd: PathBuf) -> Self {
self.cwd = cwd;
@ -241,6 +240,34 @@ impl LintRunner {
Ok(filters)
}
// finds the oxlint config
// when config is provided, but not found, an CliRunResult is returned, else the oxlintrc config file is returned
// when no config is provided, it will search for the default file names in the current working directory
// when no file is found, the default configuration is returned
fn find_oxlint_config(cwd: &Path, config: &Option<PathBuf>) -> Result<Oxlintrc, CliRunResult> {
if let Some(config_path) = config {
return match Oxlintrc::from_file(config_path) {
Ok(config) => Ok(config),
Err(diagnostic) => {
let handler = GraphicalReportHandler::new();
let mut err = String::new();
handler.render_report(&mut err, &diagnostic).unwrap();
return Err(CliRunResult::InvalidOptions {
message: format!("Failed to parse configuration file.\n{err}"),
});
}
};
}
// no config argument is provided,
// auto detect default config file from current work directory
// or return the default configuration, when no valid file is found
let mut config_path = cwd.to_path_buf();
config_path.push(Self::DEFAULT_OXLINTRC);
Oxlintrc::from_file(&config_path).or_else(|_| Ok(Oxlintrc::default()))
}
}
#[cfg(all(test, not(target_os = "windows")))]
@ -432,6 +459,15 @@ mod test {
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn oxlint_config_auto_detection() {
let args = &["debugger.js"];
let result = test_with_cwd("fixtures/auto_config_detection", args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 1);
}
#[test]
fn eslintrc_no_undef() {
let args = &[

View file

@ -11,6 +11,8 @@ snapshot_kind: text
Oxlint configuration file (experimental)
* only `.json` extension is supported
* tries to be compatible with the ESLint v8's format
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