Skip to content

Commit

Permalink
refactor(linter): make fields of LintServiceOptions private
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Sep 8, 2024
1 parent aba9194 commit ecfb3a0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
10 changes: 6 additions & 4 deletions apps/oxlint/src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ impl Runner for LintRunner {

let number_of_files = paths.len();

let cwd = std::env::current_dir().unwrap().into_boxed_path();
let cwd = std::env::current_dir().unwrap();
let mut options = LintServiceOptions::new(cwd, paths);
let lint_options = LintOptions::default()
.with_filter(filter)
.with_config_path(basic_options.config)
Expand Down Expand Up @@ -122,8 +123,10 @@ impl Runner for LintRunner {

let tsconfig = basic_options.tsconfig;
if let Some(path) = tsconfig.as_ref() {
if !path.is_file() {
let path = if path.is_relative() { cwd.join(path) } else { path.clone() };
if path.is_file() {
options = options.with_tsconfig(path);
} else {
let path = if path.is_relative() { options.cwd().join(path) } else { path.clone() };
return CliRunResult::InvalidOptions {
message: format!(
"The tsconfig file {path:?} does not exist, Please provide a valid tsconfig file.",
Expand All @@ -132,7 +135,6 @@ impl Runner for LintRunner {
}
}

let options = LintServiceOptions { cwd, paths, tsconfig };
let lint_service = LintService::new(linter, options);
let mut diagnostic_service =
Self::get_diagnostic_service(&warning_options, &output_options, &misc_options);
Expand Down
36 changes: 33 additions & 3 deletions crates/oxc_linter/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,43 @@ use crate::{

pub struct LintServiceOptions {
/// Current working directory
pub cwd: Box<Path>,
cwd: Box<Path>,

/// All paths to lint
pub paths: Vec<Box<Path>>,
paths: Vec<Box<Path>>,

/// TypeScript `tsconfig.json` path for reading path alias and project references
pub tsconfig: Option<PathBuf>,
tsconfig: Option<PathBuf>,
}

impl LintServiceOptions {
#[must_use]
pub fn new<T>(cwd: T, paths: Vec<Box<Path>>) -> Self
where
T: Into<Box<Path>>,
{
Self { cwd: cwd.into(), paths, tsconfig: None }
}

#[inline]
#[must_use]
pub fn with_tsconfig<T>(mut self, tsconfig: T) -> Self
where
T: Into<PathBuf>,
{
let tsconfig = tsconfig.into();
// Should this be canonicalized?
let tsconfig = if tsconfig.is_relative() { self.cwd.join(tsconfig) } else { tsconfig };
debug_assert!(tsconfig.is_file());

self.tsconfig = Some(tsconfig);
self
}

#[inline]
pub fn cwd(&self) -> &Path {
&self.cwd
}
}

#[derive(Clone)]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl Tester {

let cwd = self.current_working_directory.clone();
let paths = vec![path_to_lint.into_boxed_path()];
let options = LintServiceOptions { cwd, paths, tsconfig: None };
let options = LintServiceOptions::new(cwd, paths);
let lint_service = LintService::from_linter(linter, options);
let diagnostic_service = DiagnosticService::default();
let tx_error = diagnostic_service.sender();
Expand Down

0 comments on commit ecfb3a0

Please sign in to comment.