mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
refactor(transformer): TS annotations transform use move_expression (#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.
This commit is contained in:
parent
1bd5853310
commit
4796ece77d
2 changed files with 11 additions and 29 deletions
|
|
@ -69,16 +69,6 @@ impl<'a> AstBuilder<'a> {
|
||||||
unsafe { std::mem::transmute_copy(src) }
|
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.
|
/// Moves the expression out by replacing it with a null expression.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn move_expression(self, expr: &mut Expression<'a>) -> Expression<'a> {
|
pub fn move_expression(self, expr: &mut Expression<'a>) -> Expression<'a> {
|
||||||
|
|
@ -86,18 +76,6 @@ impl<'a> AstBuilder<'a> {
|
||||||
mem::replace(expr, null_expr)
|
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]
|
#[inline]
|
||||||
pub fn move_statement(self, stmt: &mut Statement<'a>) -> Statement<'a> {
|
pub fn move_statement(self, stmt: &mut Statement<'a>) -> Statement<'a> {
|
||||||
let empty_stmt = self.empty_statement(stmt.span());
|
let empty_stmt = self.empty_statement(stmt.span());
|
||||||
|
|
|
||||||
|
|
@ -226,16 +226,20 @@ impl<'a> TypeScriptAnnotations<'a> {
|
||||||
if let Some(expr) = target.get_expression_mut() {
|
if let Some(expr) = target.get_expression_mut() {
|
||||||
match expr.get_inner_expression_mut() {
|
match expr.get_inner_expression_mut() {
|
||||||
// `foo!++` to `foo++`
|
// `foo!++` to `foo++`
|
||||||
Expression::Identifier(ident) => {
|
inner_expr @ Expression::Identifier(_) => {
|
||||||
*target = self.ctx.ast.simple_assignment_target_from_identifier_reference(
|
let inner_expr = self.ctx.ast.move_expression(inner_expr);
|
||||||
self.ctx.ast.move_identifier_reference(ident),
|
let Expression::Identifier(ident) = inner_expr else {
|
||||||
);
|
unreachable!();
|
||||||
|
};
|
||||||
|
*target = SimpleAssignmentTarget::AssignmentTargetIdentifier(ident);
|
||||||
}
|
}
|
||||||
// `foo.bar!++` to `foo.bar++`
|
// `foo.bar!++` to `foo.bar++`
|
||||||
inner_expr @ match_member_expression!(Expression) => {
|
inner_expr @ match_member_expression!(Expression) => {
|
||||||
*target = SimpleAssignmentTarget::from(
|
let inner_expr = self.ctx.ast.move_expression(inner_expr);
|
||||||
self.ctx.ast.move_member_expression(inner_expr.to_member_expression_mut()),
|
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
|
// This should be never hit until more syntax is added to the JavaScript/TypeScrips
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue