Skip to content

Commit

Permalink
feat(oxlint): add --working-dir argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Nov 19, 2024
1 parent bf839c1 commit 5af54d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
5 changes: 5 additions & 0 deletions apps/oxlint/src/command/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,

/// The working directory where oxlint should be executed
/// defaults to `env::current_dir()`
#[bpaf(long("working-dir"), hide_usage)]
pub working_dir: Option<PathBuf>,
}

// This is formatted according to
Expand Down
34 changes: 25 additions & 9 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5af54d4

Please sign in to comment.