Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(linter): decouple module resolution from import plugin #5829

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/oxlint/src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 14 additions & 5 deletions crates/oxc_linter/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct LintServiceOptions {

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

cross_module: bool,
}

impl LintServiceOptions {
Expand All @@ -40,7 +42,7 @@ impl LintServiceOptions {
where
T: Into<Box<Path>>,
{
Self { cwd: cwd.into(), paths, tsconfig: None }
Self { cwd: cwd.into(), paths, tsconfig: None, cross_module: false }
}

#[inline]
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
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 @@ -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();
Expand Down