Skip to content

Commit

Permalink
feat(semantic): add SymbolTable::symbol_is_mutated method (#7755)
Browse files Browse the repository at this point in the history
Add `SymbolTable::symbol_is_mutated` method. Returns `true` if the symbol is not a `const` and has any references with `ReferenceFlags::Write`.
  • Loading branch information
overlookmotel committed Dec 10, 2024
1 parent b500f55 commit 78dff7d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crates/oxc_semantic/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ impl SymbolTable {
.map(|&reference_id| &self.references[reference_id])
}

/// Get whether a symbol is mutated (i.e. assigned to).
///
/// If symbol is `const`, always returns `false`.
/// Otherwise, returns `true` if the symbol is assigned to somewhere in AST.
pub fn symbol_is_mutated(&self, symbol_id: SymbolId) -> bool {
if self.flags[symbol_id].contains(SymbolFlags::ConstVariable) {
false
} else {
self.get_resolved_references(symbol_id).any(Reference::is_write)
}
}

/// Add a reference to a symbol.
pub fn add_resolved_reference(&mut self, symbol_id: SymbolId, reference_id: ReferenceId) {
let reference_ids = &mut self.resolved_references[symbol_id];
Expand Down

0 comments on commit 78dff7d

Please sign in to comment.