Skip to content
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
1 change: 0 additions & 1 deletion crates/oxc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
1 change: 0 additions & 1 deletion crates/oxc_linter/src/service/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
29 changes: 15 additions & 14 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<Stats>,
excess_capacity: f64,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

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

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/jsdoc/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod checker;
mod class;
mod diagnostics;
mod is_global_reference;
#[cfg(feature = "linter")]
mod jsdoc;
mod label;
mod node;
Expand All @@ -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;
Expand Down Expand Up @@ -80,6 +82,7 @@ pub struct Semantic<'a> {
irregular_whitespaces: Box<[Span]>,

/// Parsed JSDoc comments.
#[cfg(feature = "linter")]
jsdoc: JSDocFinder<'a>,

unused_labels: Vec<NodeId>,
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion tasks/benchmark/benches/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions tasks/benchmark/benches/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down
Loading