Skip to content

Commit

Permalink
refactor(semantic)!: make SymbolTable fields pub(crate) instead of …
Browse files Browse the repository at this point in the history
…`pub`
  • Loading branch information
Boshen committed Dec 18, 2024
1 parent b3a5f3e commit 3943a2a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 26 deletions.
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/jest/no_confusing_set_timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Rule for NoConfusingSetTimeout {
collect_jest_reference_id(reference_ids, &mut jest_reference_id_list, ctx);
}

for reference_ids in &symbol_table.resolved_references {
for reference_ids in symbol_table.resolved_references() {
collect_jest_reference_id(
reference_ids.iter().copied(),
&mut jest_reference_id_list,
Expand All @@ -110,7 +110,7 @@ impl Rule for NoConfusingSetTimeout {
);
}

for reference_id_list in &symbol_table.resolved_references {
for reference_id_list in symbol_table.resolved_references() {
handle_jest_set_time_out(
ctx,
reference_id_list.iter().copied(),
Expand Down
8 changes: 5 additions & 3 deletions crates/oxc_linter/src/utils/jest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use oxc_ast::{
},
AstKind,
};
use oxc_semantic::{AstNode, ReferenceId, Semantic};
use oxc_index::Idx;
use oxc_semantic::{AstNode, ReferenceId, Semantic, SymbolId};
use oxc_span::CompactStr;
use phf::phf_set;

Expand Down Expand Up @@ -201,9 +202,10 @@ fn collect_ids_referenced_to_import<'a, 'c>(
) -> impl Iterator<Item = (ReferenceId, Option<&'a str>)> + 'c {
semantic
.symbols()
.resolved_references
.iter_enumerated()
.resolved_references()
.enumerate()
.filter_map(|(symbol_id, reference_ids)| {
let symbol_id = SymbolId::from_usize(symbol_id);
if semantic.symbols().get_flags(symbol_id).is_import() {
let id = semantic.symbols().get_declaration(symbol_id);
let Some(AstKind::ImportDeclaration(import_decl)) =
Expand Down
18 changes: 13 additions & 5 deletions crates/oxc_semantic/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export type CompactStr = string;
#[derive(Debug, Default)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify), serde(rename_all = "camelCase"))]
pub struct SymbolTable {
pub spans: IndexVec<SymbolId, Span>,
pub names: IndexVec<SymbolId, CompactStr>,
pub flags: IndexVec<SymbolId, SymbolFlags>,
pub scope_ids: IndexVec<SymbolId, ScopeId>,
pub(crate) spans: IndexVec<SymbolId, Span>,
pub(crate) names: IndexVec<SymbolId, CompactStr>,
pub(crate) flags: IndexVec<SymbolId, SymbolFlags>,
pub(crate) scope_ids: IndexVec<SymbolId, ScopeId>,
/// Pointer to the AST Node where this symbol is declared
pub declarations: IndexVec<SymbolId, NodeId>,
pub(crate) declarations: IndexVec<SymbolId, NodeId>,
pub resolved_references: IndexVec<SymbolId, Vec<ReferenceId>>,
redeclarations: IndexVec<SymbolId, Option<RedeclarationId>>,

Expand All @@ -61,6 +61,14 @@ impl SymbolTable {
self.spans.is_empty()
}

pub fn names(&self) -> impl Iterator<Item = &CompactStr> + '_ {
self.names.iter()
}

pub fn resolved_references(&self) -> impl Iterator<Item = &Vec<ReferenceId>> + '_ {
self.resolved_references.iter()
}

/// Iterate over all symbol IDs in this table.
///
/// Use [`ScopeTree::iter_bindings_in`] to only iterate over symbols declared in a specific
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/tests/integration/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ fn test_module_like_declarations() {

let test = SemanticTester::ts("declare global { interface Window { x: number; } }");
let semantic = test.build();
let global = semantic.symbols().names.iter().find(|name| *name == "global");
let global = semantic.symbols().names().find(|name| *name == "global");
assert!(
global.is_none(),
"A symbol should not be created for global augmentation declarations."
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn get_scope_snapshot(semantic: &Semantic, scopes: impl Iterator<Item = ScopeId>
if index != 0 {
result.push(',');
}
let reference = &semantic.symbols().references[reference_id];
let reference = &semantic.symbols().get_reference(reference_id);
result.push('{');
result
.push_str(format!("\"flags\": \"{:?}\",", reference.flags()).as_str());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ impl<'a> ArrowFunctionConverter<'a> {
let reference = ctx.symbols_mut().get_reference_mut(reference_id);
reference.set_symbol_id(binding.symbol_id);
ctx.scopes_mut().delete_root_unresolved_reference(&ident.name, reference_id);
ctx.symbols_mut().resolved_references[binding.symbol_id].push(reference_id);
ctx.symbols_mut().add_resolved_reference(binding.symbol_id, reference_id);
}

ident.name = binding.name.clone();
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl TraverseScoping {
) -> ReferenceId {
let reference = Reference::new_with_symbol_id(NodeId::DUMMY, symbol_id, flags);
let reference_id = self.symbols.create_reference(reference);
self.symbols.resolved_references[symbol_id].push(reference_id);
self.symbols.add_resolved_reference(symbol_id, reference_id);
reference_id
}

Expand Down Expand Up @@ -422,7 +422,7 @@ impl TraverseScoping {
self.scopes
.root_unresolved_references()
.keys()
.chain(self.symbols.names.iter())
.chain(self.symbols.names())
.filter_map(|name| {
if name.as_bytes().first() == Some(&b'_') {
Some(name.clone())
Expand Down
22 changes: 11 additions & 11 deletions tasks/transform_checker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,31 +394,31 @@ impl PostTransformChecker<'_, '_> {
for symbol_ids in self.symbol_ids_map.pairs() {
// Check names match
let names = self.get_pair(symbol_ids, |scoping, symbol_id| {
scoping.symbols.names[symbol_id].clone()
scoping.symbols.get_name(symbol_id).to_string()
});
if names.is_mismatch() {
self.errors.push_mismatch("Symbol name mismatch", symbol_ids, &names);
}
let symbol_name = names.rebuilt.as_str();
let symbol_name = names.rebuilt;
let mismatch_title = |field| format!("Symbol {field} mismatch for {symbol_name:?}");

// Check flags match
let flags =
self.get_pair(symbol_ids, |scoping, symbol_id| scoping.symbols.flags[symbol_id]);
let flags = self
.get_pair(symbol_ids, |scoping, symbol_id| scoping.symbols.get_flags(symbol_id));
if flags.is_mismatch() {
self.errors.push_mismatch(&mismatch_title("flags"), symbol_ids, flags);
}

// Check spans match
let spans =
self.get_pair(symbol_ids, |scoping, symbol_id| scoping.symbols.spans[symbol_id]);
self.get_pair(symbol_ids, |scoping, symbol_id| scoping.symbols.get_span(symbol_id));
if spans.is_mismatch() {
self.errors.push_mismatch(&mismatch_title("span"), symbol_ids, spans);
}

// Check scope IDs match
let scope_ids = self
.get_pair(symbol_ids, |scoping, symbol_id| scoping.symbols.scope_ids[symbol_id]);
.get_pair(symbol_ids, |scoping, symbol_id| scoping.symbols.get_scope_id(symbol_id));
if self.remap_scope_ids(scope_ids).is_mismatch() {
self.errors.push_mismatch(&mismatch_title("scope ID"), symbol_ids, scope_ids);
}
Expand All @@ -427,7 +427,7 @@ impl PostTransformChecker<'_, '_> {

// Check resolved references match
let reference_ids = self.get_pair(symbol_ids, |scoping, symbol_id| {
scoping.symbols.resolved_references[symbol_id].clone()
scoping.symbols.get_resolved_reference_ids(symbol_id).clone()
});
if self.remap_reference_ids_sets(&reference_ids).is_mismatch() {
self.errors.push_mismatch(
Expand Down Expand Up @@ -457,17 +457,17 @@ impl PostTransformChecker<'_, '_> {
for (reference_ids, name) in self.reference_ids_map.pairs().zip(&self.reference_names) {
// Check symbol IDs match
let symbol_ids = self.get_pair(reference_ids, |scoping, reference_id| {
scoping.symbols.references[reference_id].symbol_id()
scoping.symbols.get_reference(reference_id).symbol_id()
});
let symbol_ids_remapped = Pair::new(
symbol_ids.after_transform.map(|symbol_id| self.symbol_ids_map.get(symbol_id)),
symbol_ids.rebuilt.map(Option::Some),
);
if symbol_ids_remapped.is_mismatch() {
let mismatch_strs = self.get_pair(reference_ids, |scoping, reference_id| {
match scoping.symbols.references[reference_id].symbol_id() {
match scoping.symbols.get_reference(reference_id).symbol_id() {
Some(symbol_id) => {
let symbol_name = &scoping.symbols.names[symbol_id];
let symbol_name = scoping.symbols.get_name(symbol_id);
format!("{symbol_id:?} {symbol_name:?}")
}
None => "<None>".to_string(),
Expand All @@ -481,7 +481,7 @@ impl PostTransformChecker<'_, '_> {

// Check flags match
let flags = self.get_pair(reference_ids, |scoping, reference_id| {
scoping.symbols.references[reference_id].flags()
scoping.symbols.get_reference(reference_id).flags()
});
if flags.is_mismatch() {
self.errors.push_mismatch(
Expand Down

0 comments on commit 3943a2a

Please sign in to comment.