Skip to content

Commit

Permalink
refactor(semantic): make SemanticBuilder opaque (#4851)
Browse files Browse the repository at this point in the history
change visibility of building-related methods and properties of SemanticBuilder
from `pub` to `pub(crate)`.
  • Loading branch information
DonIsaac committed Aug 14, 2024
1 parent 2afccd7 commit ea1e64a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use oxc_span::{GetSpan, SourceType};

use crate::{scope::ScopeFlags, symbol::SymbolFlags, SemanticBuilder};

pub trait Binder<'a> {
pub(crate) trait Binder<'a> {
#[allow(unused_variables)]
fn bind(&self, builder: &mut SemanticBuilder<'a>) {}
}
Expand Down
54 changes: 29 additions & 25 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,41 @@ macro_rules! control_flow {
/// [`build`]: SemanticBuilder::build
pub struct SemanticBuilder<'a> {
/// source code of the parsed program
pub source_text: &'a str,
pub(crate) source_text: &'a str,

/// source type of the parsed program
pub source_type: SourceType,
pub(crate) source_type: SourceType,

trivias: Trivias,

/// Semantic early errors such as redeclaration errors.
errors: RefCell<Vec<OxcDiagnostic>>,

// states
pub current_node_id: AstNodeId,
pub current_node_flags: NodeFlags,
pub current_symbol_flags: SymbolFlags,
pub current_scope_id: ScopeId,
pub(crate) current_node_id: AstNodeId,
pub(crate) current_node_flags: NodeFlags,
pub(crate) current_symbol_flags: SymbolFlags,
pub(crate) current_scope_id: ScopeId,
/// Stores current `AstKind::Function` and `AstKind::ArrowFunctionExpression` during AST visit
pub function_stack: Vec<AstNodeId>,
pub(crate) function_stack: Vec<AstNodeId>,
// To make a namespace/module value like
// we need the to know the modules we are inside
// and when we reach a value declaration we set it
// to value like
pub namespace_stack: Vec<SymbolId>,
pub(crate) namespace_stack: Vec<SymbolId>,
current_reference_flag: ReferenceFlag,
pub hoisting_variables: FxHashMap<ScopeId, FxHashMap<Atom<'a>, SymbolId>>,
pub(crate) hoisting_variables: FxHashMap<ScopeId, FxHashMap<Atom<'a>, SymbolId>>,

// builders
pub nodes: AstNodes<'a>,
pub scope: ScopeTree,
pub symbols: SymbolTable,
pub(crate) nodes: AstNodes<'a>,
pub(crate) scope: ScopeTree,
pub(crate) symbols: SymbolTable,

unresolved_references: UnresolvedReferencesStack<'a>,

pub(crate) module_record: Arc<ModuleRecord>,

pub label_builder: LabelBuilder<'a>,
pub(crate) label_builder: LabelBuilder<'a>,

build_jsdoc: bool,
jsdoc: JSDocBuilder<'a>,
Expand All @@ -104,9 +104,9 @@ pub struct SemanticBuilder<'a> {
/// See: [`crate::checker::check`]
check_syntax_error: bool,

pub cfg: Option<ControlFlowGraphBuilder<'a>>,
pub(crate) cfg: Option<ControlFlowGraphBuilder<'a>>,

pub class_table_builder: ClassTableBuilder,
pub(crate) class_table_builder: ClassTableBuilder,

ast_node_records: Vec<AstNodeId>,
}
Expand Down Expand Up @@ -278,7 +278,7 @@ impl<'a> SemanticBuilder<'a> {
}

/// Push a Syntax Error
pub fn error(&self, error: OxcDiagnostic) {
pub(crate) fn error(&self, error: OxcDiagnostic) {
self.errors.borrow_mut().push(error);
}

Expand Down Expand Up @@ -336,16 +336,16 @@ impl<'a> SemanticBuilder<'a> {
}

#[inline]
pub fn current_scope_flags(&self) -> ScopeFlags {
pub(crate) fn current_scope_flags(&self) -> ScopeFlags {
self.scope.get_flags(self.current_scope_id)
}

/// Is the current scope in strict mode?
pub fn strict_mode(&self) -> bool {
pub(crate) fn strict_mode(&self) -> bool {
self.current_scope_flags().is_strict_mode()
}

pub fn set_function_node_flag(&mut self, flag: NodeFlags) {
pub(crate) fn set_function_node_flag(&mut self, flag: NodeFlags) {
if let Some(current_function) = self.function_stack.last() {
*self.nodes.get_node_mut(*current_function).flags_mut() |= flag;
}
Expand All @@ -357,7 +357,7 @@ impl<'a> SemanticBuilder<'a> {
/// excludes: the flags which node cannot be declared alongside in a symbol table. Used to report forbidden declarations.
///
/// Reports errors for conflicting identifier names.
pub fn declare_symbol_on_scope(
pub(crate) fn declare_symbol_on_scope(
&mut self,
span: Span,
name: &str,
Expand Down Expand Up @@ -385,7 +385,7 @@ impl<'a> SemanticBuilder<'a> {
}

/// Declare a new symbol on the current scope.
pub fn declare_symbol(
pub(crate) fn declare_symbol(
&mut self,
span: Span,
name: &str,
Expand All @@ -399,7 +399,7 @@ impl<'a> SemanticBuilder<'a> {
/// current scope. Returns the symbol id if it exists and is not excluded by `excludes`.
///
/// Only records a redeclaration error if `report_error` is `true`.
pub fn check_redeclaration(
pub(crate) fn check_redeclaration(
&self,
scope_id: ScopeId,
span: Span,
Expand All @@ -420,7 +420,11 @@ impl<'a> SemanticBuilder<'a> {
/// Declare an unresolved reference in the current scope.
///
/// # Panics
pub fn declare_reference(&mut self, name: Atom<'a>, reference: Reference) -> ReferenceId {
pub(crate) fn declare_reference(
&mut self,
name: Atom<'a>,
reference: Reference,
) -> ReferenceId {
let reference_flag = *reference.flag();
let reference_id = self.symbols.create_reference(reference);

Expand All @@ -433,7 +437,7 @@ impl<'a> SemanticBuilder<'a> {
}

/// Declares a `Symbol` for the node, shadowing previous declarations in the same scope.
pub fn declare_shadow_symbol(
pub(crate) fn declare_shadow_symbol(
&mut self,
name: &str,
span: Span,
Expand Down Expand Up @@ -516,7 +520,7 @@ impl<'a> SemanticBuilder<'a> {
}
}

pub fn add_redeclare_variable(&mut self, symbol_id: SymbolId, span: Span) {
pub(crate) fn add_redeclare_variable(&mut self, symbol_id: SymbolId, span: Span) {
self.symbols.add_redeclaration(symbol_id, span);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use oxc_syntax::scope::{ScopeFlags, ScopeId};

#[allow(clippy::struct_field_names)]
#[derive(Default, Debug)]
pub struct Counter {
pub(crate) struct Counter {
pub nodes_count: usize,
pub scopes_count: usize,
pub symbols_count: usize,
Expand Down

0 comments on commit ea1e64a

Please sign in to comment.