mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
refactor(transformer): add more debug assertions (#6090)
Test stacks are in expected state at end of traversal.
This commit is contained in:
parent
c90b9bfb6d
commit
6bd29ddc9e
5 changed files with 22 additions and 0 deletions
|
|
@ -169,6 +169,7 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
|
|||
);
|
||||
}
|
||||
debug_assert!(self.this_var_stack.len() == 1);
|
||||
debug_assert!(self.this_var_stack.last().is_none());
|
||||
}
|
||||
|
||||
fn enter_function(&mut self, _func: &mut Function<'a>, _ctx: &mut TraverseCtx<'a>) {
|
||||
|
|
|
|||
|
|
@ -65,6 +65,7 @@ impl<'a> Traverse<'a> for ExponentiationOperator<'a> {
|
|||
#[inline] // Inline because it's no-op in release mode
|
||||
fn exit_program(&mut self, _program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
|
||||
debug_assert!(self.var_declarations.len() == 1);
|
||||
debug_assert!(self.var_declarations.last().is_none());
|
||||
}
|
||||
|
||||
fn enter_statements(
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ impl<'a> Traverse<'a> for NullishCoalescingOperator<'a> {
|
|||
#[inline] // Inline because it's no-op in release mode
|
||||
fn exit_program(&mut self, _program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
|
||||
debug_assert!(self.var_declarations.len() == 1);
|
||||
debug_assert!(self.var_declarations.last().is_none());
|
||||
}
|
||||
|
||||
fn enter_statements(
|
||||
|
|
|
|||
|
|
@ -76,6 +76,7 @@ impl<'a> Traverse<'a> for LogicalAssignmentOperators<'a> {
|
|||
#[inline] // Inline because it's no-op in release mode
|
||||
fn exit_program(&mut self, _program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
|
||||
debug_assert!(self.var_declarations.len() == 1);
|
||||
debug_assert!(self.var_declarations.last().is_none());
|
||||
}
|
||||
|
||||
fn enter_statements(
|
||||
|
|
|
|||
|
|
@ -85,6 +85,24 @@ impl<T> SparseStack<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Get value of last entry on the stack.
|
||||
#[inline]
|
||||
pub fn last(&self) -> Option<&T> {
|
||||
debug_assert!(!self.has_values.is_empty());
|
||||
// SAFETY: `self.has_values` starts with 1 entry. Only `pop` removes entries from it,
|
||||
// and it ensures `self.has_values` always has at least 1 entry.
|
||||
let has_value = unsafe { *self.has_values.last().unwrap_unchecked() };
|
||||
if has_value {
|
||||
debug_assert!(!self.values.is_empty());
|
||||
// SAFETY: Last `self.has_values` is only `true` if there's a corresponding value in `self.values`.
|
||||
// This invariant is maintained in `push`, `pop`, `take_last`, `last_or_init`, and `last_mut_or_init`.
|
||||
let value = unsafe { self.values.last().unwrap_unchecked() };
|
||||
Some(value)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Take value from last entry on the stack, leaving last entry empty.
|
||||
#[inline]
|
||||
pub fn take_last(&mut self) -> Option<T> {
|
||||
|
|
|
|||
Loading…
Reference in a new issue