mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
feat(oxlint): add cwd property to LintRunner (#7352)
allows to use multiple `LintRunner` in one file targeting multiple directory. The current problem is for the test in #7348 we can not change `env::current_dir`, so as a workaround I introduce a new property :) See more info here: https://discord.com/channels/1079625926024900739/1117322804291964931/1308179827576016897
This commit is contained in:
parent
234c7b9fef
commit
d3a0119a42
3 changed files with 51 additions and 13 deletions
|
|
@ -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};
|
||||
|
|
@ -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 {
|
||||
|
|
@ -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.
|
||||
|
|
@ -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) {
|
||||
|
|
@ -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,
|
||||
|
|
@ -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;
|
||||
|
|
@ -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,
|
||||
|
|
@ -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};
|
||||
|
||||
|
|
@ -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);
|
||||
|
|
@ -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"];
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
---
|
||||
source: tasks/website/src/linter/cli.rs
|
||||
expression: snapshot
|
||||
snapshot_kind: text
|
||||
---
|
||||
## Usage
|
||||
**`oxlint`** \[**`-c`**=_`<./oxlintrc.json>`_\] \[_`PATH`_\]...
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
---
|
||||
source: tasks/website/src/linter/cli.rs
|
||||
expression: snapshot
|
||||
snapshot_kind: text
|
||||
---
|
||||
Usage: [-c=<./oxlintrc.json>] [PATH]...
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue