refactor(linter): make fields of LintServiceOptions private (#5593)

Re-creation of #5326
This commit is contained in:
DonIsaac 2024-09-08 03:10:02 +00:00
parent aba9194ebc
commit bac03e3b6c
3 changed files with 40 additions and 8 deletions

View file

@ -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);

View file

@ -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)]

View file

@ -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();