Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(transformer): shorten TS transform code #3836

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 13 additions & 23 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ impl<'a> TypeScriptAnnotations<'a> {

program.body.retain_mut(|stmt| {
let need_retain = match stmt {
// fix namespace/export-type-only/input.ts
// The namespace is type only. So if its name appear in the ExportNamedDeclaration, we should remove it.
Statement::TSModuleDeclaration(_) => return false,
Statement::ExportNamedDeclaration(decl) => {
if decl.export_kind.is_type() {
false
Expand Down Expand Up @@ -203,27 +200,18 @@ impl<'a> TypeScriptAnnotations<'a> {

pub fn transform_simple_assignment_target(&mut self, target: &mut SimpleAssignmentTarget<'a>) {
if let Some(expr) = target.get_expression() {
if let Some(ident) = expr.get_inner_expression().get_identifier_reference() {
let ident = self.ctx.ast.alloc(self.ctx.ast.copy(ident));
if let Expression::Identifier(ident) = expr.get_inner_expression() {
let ident = self.ctx.ast.copy(ident);
*target = SimpleAssignmentTarget::AssignmentTargetIdentifier(ident);
}
}
}

pub fn transform_assignment_target(&mut self, target: &mut AssignmentTarget<'a>) {
if let Some(new_target) = target
.get_expression()
.map(Expression::get_inner_expression)
.and_then(|expr| match expr {
match_member_expression!(Expression) => {
Some(self.ctx.ast.simple_assignment_target_member_expression(
self.ctx.ast.copy(expr.as_member_expression().unwrap()),
))
}
_ => None,
})
{
*target = new_target;
if let Some(expr) = target.get_expression() {
if let Some(member_expr) = expr.get_inner_expression().as_member_expression() {
*target = AssignmentTarget::from(self.ctx.ast.copy(member_expr));
}
}
}

Expand Down Expand Up @@ -323,11 +311,12 @@ impl<'a> TypeScriptAnnotations<'a> {

pub fn transform_statements(&mut self, stmts: &mut ArenaVec<'a, Statement<'a>>) {
// Remove declare declaration
stmts.retain(|stmt| match stmt {
match_declaration!(Statement) => {
stmt.to_declaration().modifiers().map_or(true, |m| !m.is_contains_declare())
stmts.retain(|stmt| {
if let Some(decl) = stmt.as_declaration() {
decl.modifiers().map_or(true, |m| !m.is_contains_declare())
} else {
true
}
_ => true,
});
}

Expand All @@ -339,7 +328,8 @@ impl<'a> TypeScriptAnnotations<'a> {
// Remove TS specific statements
stmts.retain(|stmt| match stmt {
Statement::ExpressionStatement(s) => !s.expression.is_typescript_syntax(),
Statement::TSModuleDeclaration(_) => true,
// Any namespaces left after namespace transform are type only, so remove them
Statement::TSModuleDeclaration(_) => false,
match_declaration!(Statement) => !stmt.to_declaration().is_typescript_syntax(),
// Ignore ModuleDeclaration as it's handled in the program
_ => true,
Expand Down
Loading