Skip to content

Commit

Permalink
refactor(transformer): TS annotations transform use move_expression (
Browse files Browse the repository at this point in the history
…#4982)

Follow on after #4920. #4920 removed a usage of `AstBuilder::copy` and replaced with 2 new methods `AstBuilder::move_identifier_reference` and `AstBuilder:: move_member_expression`.

We can instead use `AstBuilder::move_expression` earlier and then unpack the enum again. Hopefully the compiler can see that the 2 `unreachable!()` branches are indeed unreachable and elide them.

`move_expression` is preferable to `move_member_expression` as it only creates 1 temporary `Box<Expression::NullLiteral>` instead of 2.
  • Loading branch information
overlookmotel committed Aug 19, 2024
1 parent 1bd5853 commit 4796ece
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
22 changes: 0 additions & 22 deletions crates/oxc_ast/src/ast_builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,13 @@ impl<'a> AstBuilder<'a> {
unsafe { std::mem::transmute_copy(src) }
}

/// Moves the identifier reference out by replacing it with a dummy identifier reference.
#[inline]
pub fn move_identifier_reference(
self,
expr: &mut IdentifierReference<'a>,
) -> IdentifierReference<'a> {
let dummy = self.identifier_reference(expr.span(), Atom::empty());
mem::replace(expr, dummy)
}

/// Moves the expression out by replacing it with a null expression.
#[inline]
pub fn move_expression(self, expr: &mut Expression<'a>) -> Expression<'a> {
let null_expr = self.expression_null_literal(expr.span());
mem::replace(expr, null_expr)
}

/// Moves the member expression out by replacing it with a dummy expression.
#[inline]
pub fn move_member_expression(self, expr: &mut MemberExpression<'a>) -> MemberExpression<'a> {
let dummy = self.member_expression_computed(
expr.span(),
self.expression_null_literal(expr.span()),
self.expression_null_literal(expr.span()),
false,
);
mem::replace(expr, dummy)
}

#[inline]
pub fn move_statement(self, stmt: &mut Statement<'a>) -> Statement<'a> {
let empty_stmt = self.empty_statement(stmt.span());
Expand Down
18 changes: 11 additions & 7 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,20 @@ impl<'a> TypeScriptAnnotations<'a> {
if let Some(expr) = target.get_expression_mut() {
match expr.get_inner_expression_mut() {
// `foo!++` to `foo++`
Expression::Identifier(ident) => {
*target = self.ctx.ast.simple_assignment_target_from_identifier_reference(
self.ctx.ast.move_identifier_reference(ident),
);
inner_expr @ Expression::Identifier(_) => {
let inner_expr = self.ctx.ast.move_expression(inner_expr);
let Expression::Identifier(ident) = inner_expr else {
unreachable!();
};
*target = SimpleAssignmentTarget::AssignmentTargetIdentifier(ident);
}
// `foo.bar!++` to `foo.bar++`
inner_expr @ match_member_expression!(Expression) => {
*target = SimpleAssignmentTarget::from(
self.ctx.ast.move_member_expression(inner_expr.to_member_expression_mut()),
);
let inner_expr = self.ctx.ast.move_expression(inner_expr);
let Ok(member_expr) = MemberExpression::try_from(inner_expr) else {
unreachable!();
};
*target = SimpleAssignmentTarget::from(member_expr);
}
_ => {
// This should be never hit until more syntax is added to the JavaScript/TypeScrips
Expand Down

0 comments on commit 4796ece

Please sign in to comment.