From ecfb3a0a35b41dd09bd91703957879b975acef85 Mon Sep 17 00:00:00 2001 From: Don Isaac Date: Sat, 7 Sep 2024 21:05:13 -0400 Subject: [PATCH] refactor(linter): make fields of `LintServiceOptions` private --- apps/oxlint/src/lint/mod.rs | 10 +++++---- crates/oxc_linter/src/service.rs | 36 +++++++++++++++++++++++++++++--- crates/oxc_linter/src/tester.rs | 2 +- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/apps/oxlint/src/lint/mod.rs b/apps/oxlint/src/lint/mod.rs index 8e7055824cea9b..cbc9c99caf66ee 100644 --- a/apps/oxlint/src/lint/mod.rs +++ b/apps/oxlint/src/lint/mod.rs @@ -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) @@ -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.", @@ -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); diff --git a/crates/oxc_linter/src/service.rs b/crates/oxc_linter/src/service.rs index 4060acf1946621..152f4841e7eb80 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -25,13 +25,43 @@ use crate::{ pub struct LintServiceOptions { /// Current working directory - pub cwd: Box, + cwd: Box, /// All paths to lint - pub paths: Vec>, + paths: Vec>, /// TypeScript `tsconfig.json` path for reading path alias and project references - pub tsconfig: Option, + tsconfig: Option, +} + +impl LintServiceOptions { + #[must_use] + pub fn new(cwd: T, paths: Vec>) -> Self + where + T: Into>, + { + Self { cwd: cwd.into(), paths, tsconfig: None } + } + + #[inline] + #[must_use] + pub fn with_tsconfig(mut self, tsconfig: T) -> Self + where + T: Into, + { + 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)] diff --git a/crates/oxc_linter/src/tester.rs b/crates/oxc_linter/src/tester.rs index 0b683b4ccdafe7..21c63c886951bc 100644 --- a/crates/oxc_linter/src/tester.rs +++ b/crates/oxc_linter/src/tester.rs @@ -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();