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"
}
}
37 changes: 34 additions & 3 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ impl Runner for LintRunner {

let number_of_files = paths.len();

let mut oxlintrc = if let Some(config_path) = basic_options.config.as_ref() {
let mut oxlintrc = Oxlintrc::default();

if let Some(config_path) = basic_options.config.as_ref() {
match Oxlintrc::from_file(config_path) {
Ok(config) => config,
Ok(config) => oxlintrc = config,
Err(diagnostic) => {
let handler = GraphicalReportHandler::new();
let mut err = String::new();
Expand All @@ -115,7 +117,27 @@ impl Runner for LintRunner {
}
}
} else {
Oxlintrc::default()
// 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 = self.cwd.clone();
config_path.push(config_file);

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

enable_plugins.apply_overrides(&mut oxlintrc.plugins);
Expand Down Expand Up @@ -423,6 +445,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
Loading