Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 10 commits into from
Nov 23, 2024
1 change: 1 addition & 0 deletions apps/oxlint/fixtures/auto_config_detection/debugger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
debugger;
5 changes: 5 additions & 0 deletions apps/oxlint/fixtures/auto_config_detection/oxlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-debugger": "error"
}
}
9 changes: 9 additions & 0 deletions apps/oxlint/src/command/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ 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 the following files in the current working directory:
/// * `oxlintrc.json`
/// * `oxlint.json`
/// * `.oxlintrc.json`
/// * `.oxlint.json`
Boshen marked this conversation as resolved.
Show resolved Hide resolved
/// * `.oxlintrc`
/// * `.eslintrc`
/// * `.eslintrc.json`
Sysix marked this conversation as resolved.
Show resolved Hide resolved
#[bpaf(long, short, argument("./oxlintrc.json"))]
pub config: Option<PathBuf>,

Expand Down
81 changes: 65 additions & 16 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -241,6 +238,49 @@ 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 possible files from current work directory
let search_configs = &[
"oxlintrc.json",
"oxlint.json",
".oxlintrc.json",
".oxlint.json",
".oxlintrc",
".eslintrc",
".eslintrc.json",
];

for config_file in search_configs {
let mut config_path = cwd.to_path_buf();
config_path.push(config_file);

if let Ok(result) = Oxlintrc::from_file(&config_path) {
return Ok(result);
};
}

Ok(Oxlintrc::default())
}
}

#[cfg(all(test, not(target_os = "windows")))]
Expand Down Expand Up @@ -423,6 +463,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);
}
camc314 marked this conversation as resolved.
Show resolved Hide resolved

#[test]
fn eslintrc_no_undef() {
let args = &[
Expand Down
9 changes: 9 additions & 0 deletions tasks/website/src/linter/snapshots/cli.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ 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 the following files in the current working directory:
* `oxlintrc.json`
* `oxlint.json`
* `.oxlintrc.json`
* `.oxlint.json`
* `.oxlintrc`
* `.eslintrc`
* `.eslintrc.json`
- **` --tsconfig`**=_`<./tsconfig.json>`_ &mdash;
TypeScript `tsconfig.json` path for reading path alias and project references for import plugin

Expand Down
Loading