From 5af54d41405c005bf47f701d25cd9c8f3001e130 Mon Sep 17 00:00:00 2001 From: Sysix Date: Tue, 19 Nov 2024 01:30:28 +0100 Subject: [PATCH] feat(oxlint): add `--working-dir` argument --- apps/oxlint/src/command/lint.rs | 5 +++++ apps/oxlint/src/lint.rs | 34 ++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/apps/oxlint/src/command/lint.rs b/apps/oxlint/src/command/lint.rs index f404714428505..c5c130c70ce39 100644 --- a/apps/oxlint/src/command/lint.rs +++ b/apps/oxlint/src/command/lint.rs @@ -68,6 +68,11 @@ pub struct BasicOptions { /// TypeScript `tsconfig.json` path for reading path alias and project references for import plugin #[bpaf(argument("./tsconfig.json"), hide_usage)] pub tsconfig: Option, + + /// The working directory where oxlint should be executed + /// defaults to `env::current_dir()` + #[bpaf(long("working-dir"), hide_usage)] + pub working_dir: Option, } // This is formatted according to diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 52049828c588b..274f038bddc85 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -51,6 +51,19 @@ impl Runner for LintRunner { let provided_path_count = paths.len(); let now = Instant::now(); + let mut cwd = env::current_dir().expect("Failed to get current working directory"); + + // append the working directory paths + if let Some(working_dir) = basic_options.working_dir { + cwd.push(working_dir); + + paths = paths.into_iter().map(|x| { + let mut new = cwd.clone(); + new.push(x); + new + }).collect() + } + // The ignore crate whitelists explicit paths, but priority // should be given to the ignore file. Many users lint // automatically and pass a list of changed files explicitly. @@ -72,13 +85,7 @@ impl Runner for LintRunner { }); } - if let Ok(cwd) = env::current_dir() { - paths.push(cwd); - } else { - return CliRunResult::InvalidOptions { - message: "Failed to get current working directory.".to_string(), - }; - } + paths.push(cwd.clone()); } let filter = match Self::get_filters(filter) { @@ -97,8 +104,6 @@ impl Runner for LintRunner { let number_of_files = paths.len(); - let cwd = std::env::current_dir().unwrap(); - let mut oxlintrc = if let Some(config_path) = basic_options.config.as_ref() { match Oxlintrc::from_file(config_path) { Ok(config) => config, @@ -131,6 +136,7 @@ impl Runner for LintRunner { let mut options = LintServiceOptions::new(cwd, paths).with_cross_module(builder.plugins().has_import()); + let linter = builder.build(); let tsconfig = basic_options.tsconfig; @@ -530,6 +536,16 @@ mod test { assert_eq!(result.number_of_errors, 0); } + #[test] + fn working_dir_option() { + let args = &["--working-dir", "fixtures/linter", "debugger.js"]; + let result = test(args); + assert!(result.number_of_rules > 0); + assert_eq!(result.number_of_files, 1); + assert_eq!(result.number_of_warnings, 1); + assert_eq!(result.number_of_errors, 0); + } + #[test] fn test_tsconfig_option() { // passed