-
-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(transformer): fix stacks in arrow function transform (#5828)
Push/pop to stacks in matching `enter_*` / `exit_*` visitors. The reason why there was a bug here is a little bit subtle. Between `enter_expression` and `exit_expression`, another transform can replace the `Expression` with something else. Ditto `enter_declaration` + `exit_declaration`. So pushing+popping from stacks in `enter_expression` + `exit_expression` can make the stack get out of sync. e.g.: ```rs impl<'a> Traverse for TransformerWithStack { fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: TraverseCtx<'a>) { if let Expression::FunctionExpression(_) = expr { self.stack.push(true); } } fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: TraverseCtx<'a>) { if let Expression::FunctionExpression(_) = expr { self.stack.pop(); } } } // Transformer that replaces `null` with a function expression (!) impl<'a> Traverse for SomeOtherTransformer { fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: TraverseCtx<'a>) { if let Expression::NullLiteral(_) = expr { *expr = ctx.ast.expression_function( /* ... */ ); } } } ``` `TransformerWithStack` is assuming in `exit_expression` that it previously saw a `FunctionExpression` in `enter_expression`. But `SomeOtherTransformer` has created a *new* `FunctionExpression` after `TransformerWithStack::enter_expression` ran. So in `TransformerWithStack`, `exit_expression` is called 1 more time than `enter_expression`. When `exit_expression` calls `self.stack.pop()`, `self.stack` is empty, and it panics. This example is silly, but real cases of this do exist. e.g. TS transformer creates a new functions when transforming `enum`s. `enter_function` + `exit_function` / `enter_arrow_function_expression` + `exit_arrow_function_expression` not have this problem. As we cannot mutate upwards, there are always the same number of calls to `enter_*` and `exit_*` (same for all `enter_*` / `exit_*` pairs which operate on a struct, *not an enum*).
- Loading branch information
1 parent
4d97184
commit 172fa03
Showing
3 changed files
with
93 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters