From d599b1cb2d1a3ecc0578f5e7fb82e82c283451ab Mon Sep 17 00:00:00 2001 From: Sysix Date: Mon, 18 Nov 2024 20:41:29 +0100 Subject: [PATCH 1/9] 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 = &[ From 01add6d81e449429ca6484fb07fd49543ad1f7b0 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:00:48 +0000 Subject: [PATCH 2/9] [autofix.ci] apply automated fixes --- apps/oxlint/src/lint.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 86752ebf30af1..eb48d38a44f42 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -103,7 +103,7 @@ impl Runner for LintRunner { let number_of_files = paths.len(); let mut oxlintrc = Oxlintrc::default(); - + if let Some(config_path) = basic_options.config.as_ref() { match Oxlintrc::from_file(config_path) { Ok(config) => oxlintrc = config, From 6f4219130052a2d17304f196d1af548861ac6180 Mon Sep 17 00:00:00 2001 From: Sysix Date: Wed, 20 Nov 2024 15:13:02 +0100 Subject: [PATCH 3/9] feat(oxlint): auto detect config file in CLI --- apps/oxlint/src/command/lint.rs | 9 +++ apps/oxlint/src/lint.rs | 83 ++++++++++++--------- tasks/website/src/linter/snapshots/cli.snap | 9 +++ 3 files changed, 66 insertions(+), 35 deletions(-) diff --git a/apps/oxlint/src/command/lint.rs b/apps/oxlint/src/command/lint.rs index f404714428505..7da12475f7428 100644 --- a/apps/oxlint/src/command/lint.rs +++ b/apps/oxlint/src/command/lint.rs @@ -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` + /// * `.oxlintrc` + /// * `.eslintrc` + /// * `.eslintrc.json` #[bpaf(long, short, argument("./oxlintrc.json"))] pub config: Option, diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index eb48d38a44f42..f8f71e103c3ab 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -102,43 +102,13 @@ impl Runner for LintRunner { let number_of_files = paths.len(); - let mut oxlintrc = Oxlintrc::default(); + let config_search_result = Self::find_oxlint_config(&self.cwd, &basic_options.config); - if let Some(config_path) = basic_options.config.as_ref() { - match Oxlintrc::from_file(config_path) { - Ok(config) => oxlintrc = 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 { - // 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", - ]; + if let Err(err) = config_search_result { + return err; + } - 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; - }; - } - }; + let mut oxlintrc = config_search_result.unwrap(); enable_plugins.apply_overrides(&mut oxlintrc.plugins); @@ -263,6 +233,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: &PathBuf, config: &Option) -> Result { + 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}"), + }); + } + }; + } else { + // 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) { + return Ok(result); + }; + } + }; + + Ok(Oxlintrc::default()) + } } #[cfg(all(test, not(target_os = "windows")))] diff --git a/tasks/website/src/linter/snapshots/cli.snap b/tasks/website/src/linter/snapshots/cli.snap index d4184ce620767..2e2048903f4c7 100644 --- a/tasks/website/src/linter/snapshots/cli.snap +++ b/tasks/website/src/linter/snapshots/cli.snap @@ -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>`_ — TypeScript `tsconfig.json` path for reading path alias and project references for import plugin From 4009017406034ef1865611422841fecc9d9a5d64 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:14:15 +0000 Subject: [PATCH 4/9] [autofix.ci] apply automated fixes --- apps/oxlint/src/command/lint.rs | 2 +- apps/oxlint/src/lint.rs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/apps/oxlint/src/command/lint.rs b/apps/oxlint/src/command/lint.rs index 7da12475f7428..8cdbda5bb9878 100644 --- a/apps/oxlint/src/command/lint.rs +++ b/apps/oxlint/src/command/lint.rs @@ -62,7 +62,7 @@ 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` diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index f8f71e103c3ab..86e01c0b8f6f8 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -234,11 +234,14 @@ impl LintRunner { Ok(filters) } - // finds the oxlint config + // 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: &PathBuf, config: &Option) -> Result { + fn find_oxlint_config( + cwd: &PathBuf, + config: &Option, + ) -> Result { if let Some(config_path) = config { return match Oxlintrc::from_file(config_path) { Ok(config) => Ok(config), From a64594c563a194c1d65cc4f4ca884c4956648929 Mon Sep 17 00:00:00 2001 From: Sysix Date: Wed, 20 Nov 2024 15:30:43 +0100 Subject: [PATCH 5/9] feat(oxlint): auto detect config file in CLI --- apps/oxlint/src/lint.rs | 48 ++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 86e01c0b8f6f8..6ff727a4c4fa1 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -1,4 +1,4 @@ -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}; @@ -239,7 +239,7 @@ impl LintRunner { // 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: &PathBuf, + cwd: &Path, config: &Option, ) -> Result { if let Some(config_path) = config { @@ -254,28 +254,28 @@ impl LintRunner { }); } }; - } else { - // 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) { - return Ok(result); - }; - } - }; + } + + // 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()) } From 1b236d566b423762c3e26488b69ebf67efc77406 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:32:09 +0000 Subject: [PATCH 6/9] [autofix.ci] apply automated fixes --- apps/oxlint/src/lint.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 6ff727a4c4fa1..cb61f1ba5ff58 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -1,4 +1,9 @@ -use std::{env, io::BufWriter, path::{Path, PathBuf}, time::Instant}; +use std::{ + env, + io::BufWriter, + path::{Path, PathBuf}, + time::Instant, +}; use ignore::gitignore::Gitignore; use oxc_diagnostics::{DiagnosticService, GraphicalReportHandler}; @@ -238,10 +243,7 @@ impl LintRunner { // 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, - ) -> Result { + fn find_oxlint_config(cwd: &Path, config: &Option) -> Result { if let Some(config_path) = config { return match Oxlintrc::from_file(config_path) { Ok(config) => Ok(config), @@ -275,7 +277,7 @@ impl LintRunner { if let Ok(result) = Oxlintrc::from_file(&config_path) { return Ok(result); }; - } + } Ok(Oxlintrc::default()) } From 5cdff9b04df6f272753006209fd813c54efe5229 Mon Sep 17 00:00:00 2001 From: Sysix Date: Thu, 21 Nov 2024 15:08:00 +0100 Subject: [PATCH 7/9] feat(oxlint): auto detect config file in CLI --- apps/oxlint/src/command/lint.rs | 3 --- apps/oxlint/src/lint.rs | 5 +---- tasks/website/src/linter/snapshots/cli.snap | 3 --- 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/apps/oxlint/src/command/lint.rs b/apps/oxlint/src/command/lint.rs index 8cdbda5bb9878..844f66e896058 100644 --- a/apps/oxlint/src/command/lint.rs +++ b/apps/oxlint/src/command/lint.rs @@ -68,9 +68,6 @@ pub struct BasicOptions { /// * `oxlint.json` /// * `.oxlintrc.json` /// * `.oxlint.json` - /// * `.oxlintrc` - /// * `.eslintrc` - /// * `.eslintrc.json` #[bpaf(long, short, argument("./oxlintrc.json"))] pub config: Option, diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index cb61f1ba5ff58..f4ef7fce319ea 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -264,10 +264,7 @@ impl LintRunner { "oxlintrc.json", "oxlint.json", ".oxlintrc.json", - ".oxlint.json", - ".oxlintrc", - ".eslintrc", - ".eslintrc.json", + ".oxlint.json" ]; for config_file in search_configs { diff --git a/tasks/website/src/linter/snapshots/cli.snap b/tasks/website/src/linter/snapshots/cli.snap index 2e2048903f4c7..0edb318fe178b 100644 --- a/tasks/website/src/linter/snapshots/cli.snap +++ b/tasks/website/src/linter/snapshots/cli.snap @@ -17,9 +17,6 @@ snapshot_kind: text * `oxlint.json` * `.oxlintrc.json` * `.oxlint.json` -* `.oxlintrc` -* `.eslintrc` -* `.eslintrc.json` - **` --tsconfig`**=_`<./tsconfig.json>`_ — TypeScript `tsconfig.json` path for reading path alias and project references for import plugin From 5fe8522d4a8cd94eb5d890a0b1caac72abd7f5b4 Mon Sep 17 00:00:00 2001 From: Sysix Date: Sat, 23 Nov 2024 15:29:07 +0100 Subject: [PATCH 8/9] feat(oxlint): auto detect config file in CLI --- .../{oxlint.json => .oxlintrc.json} | 0 apps/oxlint/src/command/lint.rs | 6 +---- apps/oxlint/src/lint.rs | 23 +++++++------------ tasks/website/src/linter/snapshots/cli.snap | 6 +---- 4 files changed, 10 insertions(+), 25 deletions(-) rename apps/oxlint/fixtures/auto_config_detection/{oxlint.json => .oxlintrc.json} (100%) diff --git a/apps/oxlint/fixtures/auto_config_detection/oxlint.json b/apps/oxlint/fixtures/auto_config_detection/.oxlintrc.json similarity index 100% rename from apps/oxlint/fixtures/auto_config_detection/oxlint.json rename to apps/oxlint/fixtures/auto_config_detection/.oxlintrc.json diff --git a/apps/oxlint/src/command/lint.rs b/apps/oxlint/src/command/lint.rs index 844f66e896058..e444cb9f623e3 100644 --- a/apps/oxlint/src/command/lint.rs +++ b/apps/oxlint/src/command/lint.rs @@ -63,11 +63,7 @@ pub struct BasicOptions { /// * 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` + /// If not provided, Oxlint will look for `.oxlintrc.json` in the current working directory. #[bpaf(long, short, argument("./oxlintrc.json"))] pub config: Option, diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index f4ef7fce319ea..644537e7d2779 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -176,6 +176,8 @@ impl Runner for LintRunner { } impl LintRunner { + const DEFAULT_OXLINTRC: &'static str = ".oxlintrc.json"; + #[must_use] pub fn with_cwd(mut self, cwd: PathBuf) -> Self { self.cwd = cwd; @@ -259,22 +261,13 @@ impl LintRunner { } // no config argument is provided, - // auto detect possible files from current work directory - let search_configs = &[ - "oxlintrc.json", - "oxlint.json", - ".oxlintrc.json", - ".oxlint.json" - ]; - - for config_file in search_configs { - let mut config_path = cwd.to_path_buf(); - config_path.push(config_file); + // auto detect default config file from current work directory + let mut config_path = cwd.to_path_buf(); + config_path.push(Self::DEFAULT_OXLINTRC); - if let Ok(result) = Oxlintrc::from_file(&config_path) { - return Ok(result); - }; - } + if let Ok(result) = Oxlintrc::from_file(&config_path) { + return Ok(result); + }; Ok(Oxlintrc::default()) } diff --git a/tasks/website/src/linter/snapshots/cli.snap b/tasks/website/src/linter/snapshots/cli.snap index 0edb318fe178b..e7653edd16d2a 100644 --- a/tasks/website/src/linter/snapshots/cli.snap +++ b/tasks/website/src/linter/snapshots/cli.snap @@ -12,11 +12,7 @@ snapshot_kind: text * 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` + If not provided, Oxlint will look for `.oxlintrc.json` in the current working directory. - **` --tsconfig`**=_`<./tsconfig.json>`_ — TypeScript `tsconfig.json` path for reading path alias and project references for import plugin From b226cb24c513a4013c4e8e5875e5e7ab346c313e Mon Sep 17 00:00:00 2001 From: Sysix Date: Sat, 23 Nov 2024 15:37:44 +0100 Subject: [PATCH 9/9] feat(oxlint): auto detect config file in CLI --- apps/oxlint/src/lint.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 644537e7d2779..7ced35a4aefdb 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -262,14 +262,11 @@ impl LintRunner { // no config argument is provided, // auto detect default config file from current work directory + // or return the default configuration, when no valid file is found let mut config_path = cwd.to_path_buf(); config_path.push(Self::DEFAULT_OXLINTRC); - if let Ok(result) = Oxlintrc::from_file(&config_path) { - return Ok(result); - }; - - Ok(Oxlintrc::default()) + Oxlintrc::from_file(&config_path).or_else(|_| Ok(Oxlintrc::default())) } }