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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(oxlint): add cwd property to LintRunner
Sysix committed Nov 19, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit e162095a301d3dad236233dfd8a75859a485061d
5 changes: 0 additions & 5 deletions apps/oxlint/src/command/lint.rs
Original file line number Diff line number Diff line change
@@ -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
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};
@@ -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,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
@@ -88,7 +83,7 @@ impl Runner for LintRunner {
});
}

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

let filter = match Self::get_filters(filter) {
@@ -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();

@@ -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,
@@ -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};

@@ -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);
@@ -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"];
@@ -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
2 changes: 0 additions & 2 deletions tasks/website/src/linter/snapshots/cli.snap
Original file line number Diff line number Diff line change
@@ -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



2 changes: 0 additions & 2 deletions tasks/website/src/linter/snapshots/cli_terminal.snap
Original file line number Diff line number Diff line change
@@ -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.