diff --git a/crates/oxc_language_server/src/linter.rs b/crates/oxc_language_server/src/linter.rs index 685ec3a7af57e..b78afd3907498 100644 --- a/crates/oxc_language_server/src/linter.rs +++ b/crates/oxc_language_server/src/linter.rs @@ -285,10 +285,12 @@ impl IsolatedLintHandler { return Some(Self::wrap_diagnostics(path, &original_source_text, reports, start)); }; + let use_cfg = linter.rules().iter().any(|it| it.rule.use_cfg()); + let program = allocator.alloc(ret.program); let semantic_ret = SemanticBuilder::new(javascript_source_text, source_type) .with_trivias(ret.trivias) - .with_cfg(true) + .with_cfg(use_cfg) .with_check_syntax_error(true) .build(program); diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index 686c1938c67d1..a9657dc386812 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -62,6 +62,10 @@ impl Default for Linter { } impl Linter { + pub fn rules(&self) -> &Vec { + &self.rules + } + /// # Errors /// /// Returns `Err` if there are any errors parsing the configuration file. diff --git a/crates/oxc_linter/src/service.rs b/crates/oxc_linter/src/service.rs index 236628f2b591d..4e8025802cee9 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -265,10 +265,12 @@ impl Runtime { let trivias = ret.trivias; + let use_cfg = self.linter.rules.iter().any(|it| it.rule.use_cfg()); + // Build the module record to unblock other threads from waiting for too long. // The semantic model is not built at this stage. let semantic_builder = SemanticBuilder::new(source_text, source_type) - .with_cfg(true) + .with_cfg(use_cfg) .with_trivias(trivias) .with_check_syntax_error(check_syntax_errors) .build_module_record(path.to_path_buf(), program);