From d599b1cb2d1a3ecc0578f5e7fb82e82c283451ab Mon Sep 17 00:00:00 2001 From: Sysix Date: Mon, 18 Nov 2024 20:41:29 +0100 Subject: [PATCH] feat(oxlint): auto detect config file in CLI --- .../auto_config_detection/debugger.js | 1 + .../auto_config_detection/oxlint.json | 5 +++ apps/oxlint/src/lint.rs | 38 +++++++++++++++++-- 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 apps/oxlint/fixtures/auto_config_detection/debugger.js create mode 100644 apps/oxlint/fixtures/auto_config_detection/oxlint.json diff --git a/apps/oxlint/fixtures/auto_config_detection/debugger.js b/apps/oxlint/fixtures/auto_config_detection/debugger.js new file mode 100644 index 0000000000000..eab74692130a6 --- /dev/null +++ b/apps/oxlint/fixtures/auto_config_detection/debugger.js @@ -0,0 +1 @@ +debugger; diff --git a/apps/oxlint/fixtures/auto_config_detection/oxlint.json b/apps/oxlint/fixtures/auto_config_detection/oxlint.json new file mode 100644 index 0000000000000..a63d73a1442b8 --- /dev/null +++ b/apps/oxlint/fixtures/auto_config_detection/oxlint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "no-debugger": "error" + } +} diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 52049828c588b..550ac8106d4bd 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -99,9 +99,11 @@ impl Runner for LintRunner { let cwd = std::env::current_dir().unwrap(); - 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(); @@ -112,7 +114,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 = 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); @@ -387,6 +409,16 @@ mod test { assert_eq!(result.number_of_errors, 0); } + // ToDo: enable test when we can change the `cwd` of the process + // #[test] + // fn oxlint_config_auto_detection() { + // let args = &["fixtures/auto_config_detection/debugger.js"]; + // let result = test(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 = &[