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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't debugger on by default since it's in correctness ?
so if it failed to pick up the config, it would still return 1 here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will output it as an warning not an error.
Deleting the oxlint.json the assertion should be:

assert_eq!(result.number_of_warnings, 1);
assert_eq!(result.number_of_errors, 0);

}

#[test]
fn eslintrc_no_undef() {
let args = &[
Expand Down
Loading