Skip to content

Commit

Permalink
feat(oxlint): add cwd property to LintRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Nov 19, 2024
1 parent e3223c5 commit e162095
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 39 deletions.
5 changes: 0 additions & 5 deletions apps/oxlint/src/command/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ 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
/// appends the path to the current work directory
#[bpaf(long("working-dir"), argument("./package/sub-package"), hide_usage)]
pub working_dir: Option<PathBuf>,
}

// This is formatted according to
Expand Down
76 changes: 46 additions & 30 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, io::BufWriter, time::Instant};
use std::{env, io::BufWriter, path::PathBuf, time::Instant};

use ignore::gitignore::Gitignore;
use oxc_diagnostics::{DiagnosticService, GraphicalReportHandler};
Expand All @@ -18,13 +18,14 @@ use crate::{

pub struct LintRunner {
options: LintCommand,
cwd: PathBuf,
}

impl Runner for LintRunner {
type Options = LintCommand;

fn new(options: Self::Options) -> Self {
Self { options }
Self { options, cwd: env::current_dir().expect("Failed to get current working directory") }
}

fn run(self) -> CliRunResult {
Expand All @@ -51,21 +52,15 @@ 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 path_with_cwd = cwd.clone();
path_with_cwd.push(x);
path_with_cwd
})
.collect();
}
// append cwd to all paths
paths = paths
.into_iter()
.map(|x| {
let mut path_with_cwd = self.cwd.clone();
path_with_cwd.push(x);
path_with_cwd
})
.collect();

// The ignore crate whitelists explicit paths, but priority
// should be given to the ignore file. Many users lint
Expand All @@ -88,7 +83,7 @@ impl Runner for LintRunner {
});
}

paths.push(cwd.clone());
paths.push(self.cwd.clone());
}

let filter = match Self::get_filters(filter) {
Expand Down Expand Up @@ -137,8 +132,8 @@ impl Runner for LintRunner {
};
}

let mut options =
LintServiceOptions::new(cwd, paths).with_cross_module(builder.plugins().has_import());
let mut options = LintServiceOptions::new(self.cwd, paths)
.with_cross_module(builder.plugins().has_import());

let linter = builder.build();

Expand Down Expand Up @@ -184,6 +179,11 @@ impl Runner for LintRunner {
}

impl LintRunner {
pub fn with_cwd(mut self, cwd: PathBuf) -> Self {
self.cwd = cwd;
self
}

fn get_diagnostic_service(
warning_options: &WarningOptions,
output_options: &OutputOptions,
Expand Down Expand Up @@ -244,6 +244,8 @@ impl LintRunner {

#[cfg(all(test, not(target_os = "windows")))]
mod test {
use std::{env, path::PathBuf};

use super::LintRunner;
use crate::cli::{lint_command, CliRunResult, LintResult, Runner};

Expand All @@ -257,6 +259,20 @@ mod test {
}
}

fn test_with_cwd(cwd: &str, args: &[&str]) -> LintResult {
let mut new_args = vec!["--silent"];
new_args.extend(args);
let options = lint_command().run_inner(new_args.as_slice()).unwrap();

let mut current_cwd = env::current_dir().unwrap();
current_cwd.push(cwd);

match LintRunner::new(options).with_cwd(PathBuf::from(current_cwd)).run() {
CliRunResult::LintResult(lint_result) => lint_result,
other => panic!("{other:?}"),
}
}

fn test_invalid_options(args: &[&str]) -> String {
let mut new_args = vec!["--quiet"];
new_args.extend(args);
Expand Down Expand Up @@ -288,6 +304,16 @@ mod test {
assert_eq!(result.number_of_errors, 0);
}

#[test]
fn cwd() {
let args = &["debugger.js"];
let result = test_with_cwd("fixtures/linter", 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 file() {
let args = &["fixtures/linter/debugger.js"];
Expand Down Expand Up @@ -539,16 +565,6 @@ 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
2 changes: 0 additions & 2 deletions tasks/website/src/linter/snapshots/cli.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ snapshot_kind: text
* tries to be compatible with the ESLint v8's format
- **` --tsconfig`**=_`<./tsconfig.json>`_ &mdash;
TypeScript `tsconfig.json` path for reading path alias and project references for import plugin
- **` --working-dir`**=_`<./package/sub-package>`_ &mdash;
The working directory where oxlint should be executed appends the path to the current work directory



Expand Down
2 changes: 0 additions & 2 deletions tasks/website/src/linter/snapshots/cli_terminal.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Basic Configuration
* tries to be compatible with the ESLint v8's format
--tsconfig=<./tsconfig.json> TypeScript `tsconfig.json` path for reading path alias and
project references for import plugin
--working-dir=<./package/sub-package> The working directory where oxlint should be executed
appends the path to the current work directory

Allowing / Denying Multiple Lints
Accumulate rules and categories from left to right on the command-line.
Expand Down

0 comments on commit e162095

Please sign in to comment.