Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(oxlint): add cwd property to LintRunner #7352

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 49 additions & 13 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,6 +52,16 @@ impl Runner for LintRunner {
let provided_path_count = paths.len();
let now = Instant::now();

// 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
// automatically and pass a list of changed files explicitly.
Expand All @@ -72,13 +83,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(self.cwd.clone());
}

let filter = match Self::get_filters(filter) {
Expand All @@ -97,8 +102,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 @@ -129,8 +132,9 @@ 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();

let tsconfig = basic_options.tsconfig;
Expand Down Expand Up @@ -175,6 +179,12 @@ impl Runner for LintRunner {
}

impl LintRunner {
#[must_use]
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 @@ -235,6 +245,8 @@ impl LintRunner {

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

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

Expand All @@ -248,6 +260,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(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 @@ -279,6 +305,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
1 change: 1 addition & 0 deletions tasks/website/src/linter/snapshots/cli.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: tasks/website/src/linter/cli.rs
expression: snapshot
snapshot_kind: text
---
## Usage
**`oxlint`** \[**`-c`**=_`<./oxlintrc.json>`_\] \[_`PATH`_\]...
Expand Down
1 change: 1 addition & 0 deletions tasks/website/src/linter/snapshots/cli_terminal.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: tasks/website/src/linter/cli.rs
expression: snapshot
snapshot_kind: text
---
Usage: [-c=<./oxlintrc.json>] [PATH]...

Expand Down
Loading