Skip to content

Commit 9558087

Browse files
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>
1 parent 63f4d6c commit 9558087

5 files changed

Lines changed: 62 additions & 16 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"no-debugger": "error"
4+
}
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
debugger;

apps/oxlint/src/command/lint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub struct BasicOptions {
6262
/// Oxlint configuration file (experimental)
6363
/// * only `.json` extension is supported
6464
/// * tries to be compatible with the ESLint v8's format
65+
///
66+
/// If not provided, Oxlint will look for `.oxlintrc.json` in the current working directory.
6567
#[bpaf(long, short, argument("./oxlintrc.json"))]
6668
pub config: Option<PathBuf>,
6769

apps/oxlint/src/lint.rs

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use std::{env, io::BufWriter, path::PathBuf, time::Instant};
1+
use std::{
2+
env,
3+
io::BufWriter,
4+
path::{Path, PathBuf},
5+
time::Instant,
6+
};
27

38
use ignore::gitignore::Gitignore;
49
use oxc_diagnostics::{DiagnosticService, GraphicalReportHandler};
@@ -102,21 +107,13 @@ impl Runner for LintRunner {
102107

103108
let number_of_files = paths.len();
104109

105-
let mut oxlintrc = if let Some(config_path) = basic_options.config.as_ref() {
106-
match Oxlintrc::from_file(config_path) {
107-
Ok(config) => config,
108-
Err(diagnostic) => {
109-
let handler = GraphicalReportHandler::new();
110-
let mut err = String::new();
111-
handler.render_report(&mut err, &diagnostic).unwrap();
112-
return CliRunResult::InvalidOptions {
113-
message: format!("Failed to parse configuration file.\n{err}"),
114-
};
115-
}
116-
}
117-
} else {
118-
Oxlintrc::default()
119-
};
110+
let config_search_result = Self::find_oxlint_config(&self.cwd, &basic_options.config);
111+
112+
if let Err(err) = config_search_result {
113+
return err;
114+
}
115+
116+
let mut oxlintrc = config_search_result.unwrap();
120117

121118
enable_plugins.apply_overrides(&mut oxlintrc.plugins);
122119

@@ -179,6 +176,8 @@ impl Runner for LintRunner {
179176
}
180177

181178
impl LintRunner {
179+
const DEFAULT_OXLINTRC: &'static str = ".oxlintrc.json";
180+
182181
#[must_use]
183182
pub fn with_cwd(mut self, cwd: PathBuf) -> Self {
184183
self.cwd = cwd;
@@ -241,6 +240,34 @@ impl LintRunner {
241240

242241
Ok(filters)
243242
}
243+
244+
// finds the oxlint config
245+
// when config is provided, but not found, an CliRunResult is returned, else the oxlintrc config file is returned
246+
// when no config is provided, it will search for the default file names in the current working directory
247+
// when no file is found, the default configuration is returned
248+
fn find_oxlint_config(cwd: &Path, config: &Option<PathBuf>) -> Result<Oxlintrc, CliRunResult> {
249+
if let Some(config_path) = config {
250+
return match Oxlintrc::from_file(config_path) {
251+
Ok(config) => Ok(config),
252+
Err(diagnostic) => {
253+
let handler = GraphicalReportHandler::new();
254+
let mut err = String::new();
255+
handler.render_report(&mut err, &diagnostic).unwrap();
256+
return Err(CliRunResult::InvalidOptions {
257+
message: format!("Failed to parse configuration file.\n{err}"),
258+
});
259+
}
260+
};
261+
}
262+
263+
// no config argument is provided,
264+
// auto detect default config file from current work directory
265+
// or return the default configuration, when no valid file is found
266+
let mut config_path = cwd.to_path_buf();
267+
config_path.push(Self::DEFAULT_OXLINTRC);
268+
269+
Oxlintrc::from_file(&config_path).or_else(|_| Ok(Oxlintrc::default()))
270+
}
244271
}
245272

246273
#[cfg(all(test, not(target_os = "windows")))]
@@ -432,6 +459,15 @@ mod test {
432459
assert_eq!(result.number_of_errors, 0);
433460
}
434461

462+
#[test]
463+
fn oxlint_config_auto_detection() {
464+
let args = &["debugger.js"];
465+
let result = test_with_cwd("fixtures/auto_config_detection", args);
466+
assert_eq!(result.number_of_files, 1);
467+
assert_eq!(result.number_of_warnings, 0);
468+
assert_eq!(result.number_of_errors, 1);
469+
}
470+
435471
#[test]
436472
fn eslintrc_no_undef() {
437473
let args = &[

tasks/website/src/linter/snapshots/cli.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ snapshot_kind: text
1111
Oxlint configuration file (experimental)
1212
* only `.json` extension is supported
1313
* tries to be compatible with the ESLint v8's format
14+
15+
If not provided, Oxlint will look for `.oxlintrc.json` in the current working directory.
1416
- **` --tsconfig`**=_`<./tsconfig.json>`_ &mdash;
1517
TypeScript `tsconfig.json` path for reading path alias and project references for import plugin
1618

0 commit comments

Comments
 (0)