From 026ee6a7e8c13a1fddf66dd77ef2eb22f81d5344 Mon Sep 17 00:00:00 2001 From: dalaoshu Date: Wed, 18 Sep 2024 03:48:03 +0800 Subject: [PATCH] refactor(linter): decouple module resolution from import plugin (#5829) closes #5815 --- apps/oxlint/src/lint/mod.rs | 3 ++- crates/oxc_linter/src/service.rs | 19 ++++++++++++++----- crates/oxc_linter/src/tester.rs | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/apps/oxlint/src/lint/mod.rs b/apps/oxlint/src/lint/mod.rs index 2ffbd34b29ae0..403a39b8964a3 100644 --- a/apps/oxlint/src/lint/mod.rs +++ b/apps/oxlint/src/lint/mod.rs @@ -98,7 +98,8 @@ impl Runner for LintRunner { let number_of_files = paths.len(); let cwd = std::env::current_dir().unwrap(); - let mut options = LintServiceOptions::new(cwd, paths); + let mut options = + LintServiceOptions::new(cwd, paths).with_cross_module(enable_plugins.import_plugin); let lint_options = OxlintOptions::default() .with_filter(filter) .with_config_path(basic_options.config) diff --git a/crates/oxc_linter/src/service.rs b/crates/oxc_linter/src/service.rs index bc6d86afa740a..3cde8d62dbede 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -32,6 +32,8 @@ pub struct LintServiceOptions { /// TypeScript `tsconfig.json` path for reading path alias and project references tsconfig: Option, + + cross_module: bool, } impl LintServiceOptions { @@ -40,7 +42,7 @@ impl LintServiceOptions { where T: Into>, { - Self { cwd: cwd.into(), paths, tsconfig: None } + Self { cwd: cwd.into(), paths, tsconfig: None, cross_module: false } } #[inline] @@ -58,6 +60,13 @@ impl LintServiceOptions { self } + #[inline] + #[must_use] + pub fn with_cross_module(mut self, cross_module: bool) -> Self { + self.cross_module = cross_module; + self + } + #[inline] pub fn cwd(&self) -> &Path { &self.cwd @@ -165,7 +174,7 @@ pub struct Runtime { impl Runtime { fn new(linter: Linter, options: LintServiceOptions) -> Self { - let resolver = linter.options().plugins.has_import().then(|| { + let resolver = options.cross_module.then(|| { Self::get_resolver(options.tsconfig.or_else(|| Some(options.cwd.join("tsconfig.json")))) }); Self { @@ -310,7 +319,7 @@ impl Runtime { .build_module_record(path, program); let module_record = semantic_builder.module_record(); - if self.linter.options().plugins.has_import() { + if self.resolver.is_some() { self.module_map.insert( path.to_path_buf().into_boxed_path(), ModuleState::Resolved(Arc::clone(&module_record)), @@ -392,7 +401,7 @@ impl Runtime { } fn init_cache_state(&self, path: &Path) -> bool { - if !self.linter.options().plugins.has_import() { + if self.resolver.is_none() { return false; } @@ -447,7 +456,7 @@ impl Runtime { } fn ignore_path(&self, path: &Path) { - if self.linter.options().plugins.has_import() { + if self.resolver.is_some() { self.module_map.insert(path.to_path_buf().into_boxed_path(), ModuleState::Ignored); self.update_cache_state(path); } diff --git a/crates/oxc_linter/src/tester.rs b/crates/oxc_linter/src/tester.rs index d0281289c968a..8321ac31e3472 100644 --- a/crates/oxc_linter/src/tester.rs +++ b/crates/oxc_linter/src/tester.rs @@ -380,7 +380,7 @@ impl Tester { let cwd = self.current_working_directory.clone(); let paths = vec![path_to_lint.into_boxed_path()]; - let options = LintServiceOptions::new(cwd, paths); + let options = LintServiceOptions::new(cwd, paths).with_cross_module(self.plugins.import); let lint_service = LintService::from_linter(linter, options); let diagnostic_service = DiagnosticService::default(); let tx_error = diagnostic_service.sender();