Skip to content

Commit

Permalink
refactor(semantic): remove serialize (#8015)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 19, 2024
1 parent 0a38eea commit e7476a1
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 225 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions crates/oxc_semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ itertools = { workspace = true }
phf = { workspace = true, features = ["macros"] }
rustc-hash = { workspace = true }

serde = { workspace = true, features = ["derive"], optional = true }
tsify = { workspace = true, optional = true }
wasm-bindgen = { workspace = true, optional = true }

[dev-dependencies]
oxc_parser = { workspace = true }

Expand All @@ -50,4 +46,4 @@ serde_json = { workspace = true }

[features]
default = []
serialize = ["dep:serde", "dep:tsify", "dep:wasm-bindgen", "oxc_span/serialize", "oxc_syntax/serialize"]
serialize = ["oxc_span/serialize", "oxc_syntax/serialize"]
3 changes: 1 addition & 2 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{Atom, CompactStr, SourceType, Span};
use oxc_syntax::{
node::{NodeFlags, NodeId},
reference::{ReferenceFlags, ReferenceId},
reference::{Reference, ReferenceFlags, ReferenceId},
scope::{ScopeFlags, ScopeId},
symbol::{SymbolFlags, SymbolId},
};
Expand All @@ -30,7 +30,6 @@ use crate::{
jsdoc::JSDocBuilder,
label::UnusedLabels,
node::AstNodes,
reference::Reference,
scope::{Bindings, ScopeTree},
stats::Stats,
symbol::SymbolTable,
Expand Down
4 changes: 1 addition & 3 deletions crates/oxc_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use oxc_span::{GetSpan, SourceType, Span};
// Re-export flags and ID types
pub use oxc_syntax::{
node::{NodeFlags, NodeId},
reference::{ReferenceFlags, ReferenceId},
reference::{Reference, ReferenceFlags, ReferenceId},
scope::{ScopeFlags, ScopeId},
symbol::{SymbolFlags, SymbolId},
};
Expand All @@ -30,7 +30,6 @@ mod diagnostics;
mod jsdoc;
mod label;
mod node;
mod reference;
mod scope;
mod stats;
mod symbol;
Expand All @@ -39,7 +38,6 @@ mod unresolved_stack;
pub use builder::{SemanticBuilder, SemanticBuilderReturn};
pub use jsdoc::{JSDoc, JSDocFinder, JSDocTag};
pub use node::{AstNode, AstNodes};
pub use reference::Reference;
pub use scope::ScopeTree;
pub use stats::Stats;
pub use symbol::{IsGlobalReference, SymbolTable};
Expand Down
119 changes: 0 additions & 119 deletions crates/oxc_semantic/src/reference.rs

This file was deleted.

17 changes: 1 addition & 16 deletions crates/oxc_semantic/src/symbol.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
use std::mem;

#[cfg(feature = "serialize")]
use serde::Serialize;
#[cfg(feature = "serialize")]
use tsify::Tsify;

use oxc_ast::ast::{Expression, IdentifierReference};
use oxc_index::IndexVec;
use oxc_span::{CompactStr, Span};
use oxc_syntax::{
node::NodeId,
reference::ReferenceId,
reference::{Reference, ReferenceId},
scope::ScopeId,
symbol::{RedeclarationId, SymbolFlags, SymbolId},
};

use crate::reference::Reference;

#[cfg(feature = "serialize")]
#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = r#"
export type IndexVec<I, T> = Array<T>;
export type CompactStr = string;
"#;

/// Symbol Table
///
/// `SoA` (Struct of Arrays) for memory efficiency.
Expand All @@ -32,7 +18,6 @@ export type CompactStr = string;
/// `redeclare_variables` (32 bytes per symbol), store `Option<RedeclarationId>` (4 bytes).
/// That ID indexes into `redeclarations` where the actual `Vec<Span>` is stored.
#[derive(Debug, Default)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify), serde(rename_all = "camelCase"))]
pub struct SymbolTable {
pub(crate) spans: IndexVec<SymbolId, Span>,
pub(crate) names: IndexVec<SymbolId, CompactStr>,
Expand Down
106 changes: 106 additions & 0 deletions crates/oxc_syntax/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use oxc_index::Idx;
#[cfg(feature = "serialize")]
use serde::{Serialize, Serializer};

use crate::{node::NodeId, symbol::SymbolId};

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct ReferenceId(NonMaxU32);

Expand Down Expand Up @@ -208,3 +210,107 @@ impl<'alloc> CloneIn<'alloc> for ReferenceFlags {
*self
}
}

/// Describes where and how a Symbol is used in the AST.
///
/// References indicate how they are being used using [`ReferenceFlags`]. Refer
/// to the documentation for [`ReferenceFlags`] for more information.
///
/// ## Resolution
/// References to symbols that could be resolved have their `symbol_id` field
/// populated. [`None`] indicates that either a global variable or a
/// non-existent symbol is being referenced.
///
/// The node identified by `node_id` will be an `IdentifierReference`.
/// Note that declarations do not count as references, even if the declaration
/// is being used in an expression.
///
/// ```ts
/// const arr = [1, 2, 3].map(function mapper(x) { return x + 1; });
/// // Not considered a reference ^^^^^^
/// ```
#[cfg_attr(feature = "serialize", derive(Serialize), serde(rename_all = "camelCase"))]
#[derive(Debug, Clone)]
pub struct Reference {
/// The AST node making the reference.
node_id: NodeId,
/// The symbol being referenced.
///
/// This will be [`None`] if no symbol could be found within
/// the reference's scope tree. Usually this indicates a global variable or
/// a reference to a non-existent symbol.
symbol_id: Option<SymbolId>,
/// Describes how this referenced is used by other AST nodes. References can
/// be reads, writes, or both.
flags: ReferenceFlags,
}

impl Reference {
/// Create a new unresolved reference.
#[inline]
pub fn new(node_id: NodeId, flags: ReferenceFlags) -> Self {
Self { node_id, symbol_id: None, flags }
}

/// Create a new resolved reference on a symbol.
#[inline]
pub fn new_with_symbol_id(node_id: NodeId, symbol_id: SymbolId, flags: ReferenceFlags) -> Self {
Self { node_id, symbol_id: Some(symbol_id), flags }
}

/// Get the id of the node that is referencing the symbol.
#[inline]
pub fn node_id(&self) -> NodeId {
self.node_id
}

/// Get the id of the symbol being referenced.
///
/// Will return [`None`] if the symbol could not be resolved.
#[inline]
pub fn symbol_id(&self) -> Option<SymbolId> {
self.symbol_id
}

#[inline]
pub fn set_symbol_id(&mut self, symbol_id: SymbolId) {
self.symbol_id = Some(symbol_id);
}

#[inline]
pub fn flags(&self) -> ReferenceFlags {
self.flags
}

#[inline]
pub fn flags_mut(&mut self) -> &mut ReferenceFlags {
&mut self.flags
}

/// Returns `true` if the identifier value was read.
///
/// This is not mutually exclusive with [`Reference::is_write`].
#[inline]
pub fn is_read(&self) -> bool {
self.flags.is_read()
}

/// Returns `true` if the identifier was written to.
///
/// This is not mutually exclusive with [`Reference::is_read`].
#[inline]
pub fn is_write(&self) -> bool {
self.flags.is_write()
}

/// Returns `true` if this reference is used in a value context.
pub fn is_value(&self) -> bool {
self.flags.is_value()
}

/// Returns `true` if this reference is used in a type context.
#[inline]
pub fn is_type(&self) -> bool {
self.flags.is_type()
}
}
Loading

0 comments on commit e7476a1

Please sign in to comment.