From c2f27a05fa0caf87bc52217eeb7818a588e93b2c Mon Sep 17 00:00:00 2001 From: Don Isaac Date: Fri, 23 Aug 2024 15:12:20 -0400 Subject: [PATCH] refactor(linter): make fields of `LintServiceOptions` private --- apps/oxlint/src/lint/mod.rs | 8 ++++---- crates/oxc_linter/src/service.rs | 24 +++++++++++++++++++++--- crates/oxc_linter/src/tester.rs | 2 +- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/apps/oxlint/src/lint/mod.rs b/apps/oxlint/src/lint/mod.rs index 3668e0a24da7ae..4becfd54f0e0f1 100644 --- a/apps/oxlint/src/lint/mod.rs +++ b/apps/oxlint/src/lint/mod.rs @@ -121,19 +121,19 @@ impl Runner for LintRunner { } }; - let tsconfig = basic_options.tsconfig; - if let Some(path) = tsconfig.as_ref() { + let mut options = LintServiceOptions::new(cwd, paths); + if let Some(path) = basic_options.tsconfig { if !path.is_file() { - let path = if path.is_relative() { cwd.join(path) } else { path.clone() }; + 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.", ), }; } + options = options.with_tsconfig(path); } - 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 44fbd8dace0fec..76e326a4b9dfa3 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -23,15 +23,33 @@ use crate::{ Fixer, Linter, Message, }; +#[derive(Debug, Clone)] 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>, U: Into>>(cwd: T, paths: Vec) -> Self { + Self { cwd: cwd.into(), paths: paths.into_iter().map(Into::into).collect(), tsconfig: None } + } + + #[must_use] + pub fn with_tsconfig>(mut self, tsconfig: P) -> Self { + self.tsconfig = Some(tsconfig.into()); + self + } + + 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 5381ace2eacba3..7979e26e885292 100644 --- a/crates/oxc_linter/src/tester.rs +++ b/crates/oxc_linter/src/tester.rs @@ -372,7 +372,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();