diff --git a/crates/oxc/README.md b/crates/oxc/README.md index 39a894cbb41cd..fe2ae2e6b70d4 100644 --- a/crates/oxc/README.md +++ b/crates/oxc/README.md @@ -101,7 +101,6 @@ let SemanticBuilderReturn { errors: semantic_errors, } = SemanticBuilder::new() .with_check_syntax_error(true) // Enable extra syntax error checking - .with_build_jsdoc(true) // Enable JSDoc parsing .with_cfg(true) // Build a Control Flow Graph .build(&program); // Produce the `Semantic` diff --git a/crates/oxc_linter/src/service/runtime.rs b/crates/oxc_linter/src/service/runtime.rs index 8ce4896bb4633..aa65fdab3237b 100644 --- a/crates/oxc_linter/src/service/runtime.rs +++ b/crates/oxc_linter/src/service/runtime.rs @@ -993,7 +993,6 @@ impl Runtime { let semantic_ret = SemanticBuilder::new() .with_cfg(true) .with_scope_tree_child_ids(true) - .with_build_jsdoc(true) .with_check_syntax_error(check_syntax_errors) .build(allocator.alloc(ret.program)); diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 10122fce59077..c786718561422 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -24,13 +24,14 @@ use oxc_syntax::{ symbol::{SymbolFlags, SymbolId}, }; +#[cfg(feature = "linter")] +use crate::jsdoc::JSDocBuilder; use crate::{ - JSDocFinder, Semantic, + Semantic, binder::{Binder, ModuleInstanceState}, checker, class::ClassTableBuilder, diagnostics::redeclaration, - jsdoc::JSDocBuilder, label::UnusedLabels, node::AstNodes, scoping::{Bindings, Scoping}, @@ -89,7 +90,7 @@ pub struct SemanticBuilder<'a> { pub(crate) unresolved_references: UnresolvedReferencesStack<'a>, unused_labels: UnusedLabels<'a>, - build_jsdoc: bool, + #[cfg(feature = "linter")] jsdoc: JSDocBuilder<'a>, stats: Option, excess_capacity: f64, @@ -143,7 +144,7 @@ impl<'a> SemanticBuilder<'a> { scoping, unresolved_references: UnresolvedReferencesStack::new(), unused_labels: UnusedLabels::default(), - build_jsdoc: false, + #[cfg(feature = "linter")] jsdoc: JSDocBuilder::default(), stats: None, excess_capacity: 0.0, @@ -171,13 +172,6 @@ impl<'a> SemanticBuilder<'a> { self } - /// Enable/disable JSDoc parsing. - #[must_use] - pub fn with_build_jsdoc(mut self, yes: bool) -> Self { - self.build_jsdoc = yes; - self - } - /// Enable or disable building a [`ControlFlowGraph`]. /// /// [`ControlFlowGraph`]: oxc_cfg::ControlFlowGraph @@ -236,7 +230,8 @@ impl<'a> SemanticBuilder<'a> { pub fn build(mut self, program: &'a Program<'a>) -> SemanticBuilderReturn<'a> { self.source_text = program.source_text; self.source_type = program.source_type; - if self.build_jsdoc { + #[cfg(feature = "linter")] + { self.jsdoc = JSDocBuilder::new(self.source_text, &program.comments); } @@ -290,7 +285,8 @@ impl<'a> SemanticBuilder<'a> { self.unresolved_references.into_root().into_iter().map(|(k, v)| (k.as_str(), v)), ); - let jsdoc = if self.build_jsdoc { self.jsdoc.build() } else { JSDocFinder::default() }; + #[cfg(feature = "linter")] + let jsdoc = self.jsdoc.build(); #[cfg(debug_assertions)] self.unused_labels.assert_empty(); @@ -303,6 +299,7 @@ impl<'a> SemanticBuilder<'a> { nodes: self.nodes, scoping: self.scoping, classes: self.class_table_builder.build(), + #[cfg(feature = "linter")] jsdoc, unused_labels: self.unused_labels.labels, #[cfg(feature = "cfg")] @@ -327,8 +324,12 @@ impl<'a> SemanticBuilder<'a> { } fn create_ast_node(&mut self, kind: AstKind<'a>) { + #[cfg(not(feature = "linter"))] + let flags = self.current_node_flags; + #[cfg(feature = "linter")] let mut flags = self.current_node_flags; - if self.build_jsdoc && self.jsdoc.retrieve_attached_jsdoc(&kind) { + #[cfg(feature = "linter")] + if self.jsdoc.retrieve_attached_jsdoc(&kind) { flags |= NodeFlags::JSDoc; } diff --git a/crates/oxc_semantic/src/jsdoc/builder.rs b/crates/oxc_semantic/src/jsdoc/builder.rs index 87d4612551e64..e0c1b3aa4c225 100644 --- a/crates/oxc_semantic/src/jsdoc/builder.rs +++ b/crates/oxc_semantic/src/jsdoc/builder.rs @@ -207,7 +207,7 @@ mod test { ) -> Semantic<'a> { let source_type = source_type.unwrap_or_default(); let ret = Parser::new(allocator, source_text, source_type).parse(); - SemanticBuilder::new().with_build_jsdoc(true).build(allocator.alloc(ret.program)).semantic + SemanticBuilder::new().build(allocator.alloc(ret.program)).semantic } fn get_jsdocs<'a>( diff --git a/crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs b/crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs index 73e5c8aac8d03..5a25d58db67e2 100644 --- a/crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs +++ b/crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs @@ -45,7 +45,7 @@ mod test { fn build_semantic<'a>(allocator: &'a Allocator, source_text: &'a str) -> Semantic<'a> { let source_type = SourceType::default(); let ret = Parser::new(allocator, source_text, source_type).parse(); - SemanticBuilder::new().with_build_jsdoc(true).build(allocator.alloc(ret.program)).semantic + SemanticBuilder::new().build(allocator.alloc(ret.program)).semantic } #[test] diff --git a/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs b/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs index 1e982ec812736..b25425cf2af72 100644 --- a/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs +++ b/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs @@ -194,7 +194,7 @@ mod test { fn build_semantic<'a>(allocator: &'a Allocator, source_text: &'a str) -> Semantic<'a> { let source_type = SourceType::default(); let ret = Parser::new(allocator, source_text, source_type).parse(); - SemanticBuilder::new().with_build_jsdoc(true).build(allocator.alloc(ret.program)).semantic + SemanticBuilder::new().build(allocator.alloc(ret.program)).semantic } #[test] diff --git a/crates/oxc_semantic/src/lib.rs b/crates/oxc_semantic/src/lib.rs index 96b473c381d58..8a396f763d05d 100644 --- a/crates/oxc_semantic/src/lib.rs +++ b/crates/oxc_semantic/src/lib.rs @@ -32,6 +32,7 @@ mod checker; mod class; mod diagnostics; mod is_global_reference; +#[cfg(feature = "linter")] mod jsdoc; mod label; mod node; @@ -43,6 +44,7 @@ mod unresolved_stack; pub use ast_types_bitset::AstTypesBitset; pub use builder::{SemanticBuilder, SemanticBuilderReturn}; pub use is_global_reference::IsGlobalReference; +#[cfg(feature = "linter")] pub use jsdoc::{JSDoc, JSDocFinder, JSDocTag}; pub use node::{AstNode, AstNodes}; pub use scoping::Scoping; @@ -80,6 +82,7 @@ pub struct Semantic<'a> { irregular_whitespaces: Box<[Span]>, /// Parsed JSDoc comments. + #[cfg(feature = "linter")] jsdoc: JSDocFinder<'a>, unused_labels: Vec, @@ -162,6 +165,7 @@ impl<'a> Semantic<'a> { /// Parsed [`JSDoc`] comments. /// /// Will be empty if JSDoc parsing is disabled. + #[cfg(feature = "linter")] pub fn jsdoc(&self) -> &JSDocFinder<'a> { &self.jsdoc } diff --git a/tasks/benchmark/benches/linter.rs b/tasks/benchmark/benches/linter.rs index b6e499d2d06ba..bcea7bdde3af1 100644 --- a/tasks/benchmark/benches/linter.rs +++ b/tasks/benchmark/benches/linter.rs @@ -32,7 +32,6 @@ fn bench_linter(criterion: &mut Criterion) { let parser_ret = Parser::new(&allocator, source_text, source_type).parse(); let path = Path::new(""); let semantic_ret = SemanticBuilder::new() - .with_build_jsdoc(true) .with_scope_tree_child_ids(true) .with_cfg(true) .build(&parser_ret.program); diff --git a/tasks/benchmark/benches/semantic.rs b/tasks/benchmark/benches/semantic.rs index c4eeb0542fba0..28c5e79fafbcc 100644 --- a/tasks/benchmark/benches/semantic.rs +++ b/tasks/benchmark/benches/semantic.rs @@ -29,10 +29,7 @@ fn bench_semantic(criterion: &mut Criterion) { // We return `errors` to be dropped outside of the measured section, as usually // code would have no errors. One of our benchmarks `cal.com.tsx` has a lot of errors, // but that's atypical, so don't want to include it in benchmark time. - let ret = SemanticBuilder::new() - .with_build_jsdoc(true) - .with_check_syntax_error(true) - .build(&program); + let ret = SemanticBuilder::new().with_check_syntax_error(true).build(&program); let ret = black_box(ret); ret.errors });