Skip to content

Commit

Permalink
feat(transformer/typescript): remove typescript ast nodes (#3559)
Browse files Browse the repository at this point in the history
According to Babel tests feedback, remove some known ts AST nodes.
There are still many TS AST nodes that need to be deleted
  • Loading branch information
Dunqing committed Jun 7, 2024
1 parent 37cdc13 commit e8a20f8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl<'a> Expression<'a> {
matches!(self, Expression::Identifier(_))
}

pub fn get_identifier_reference(&self) -> Option<&IdentifierReference> {
pub fn get_identifier_reference(&self) -> Option<&IdentifierReference<'a>> {
match self.get_inner_expression() {
Expression::Identifier(ident) => Some(ident),
_ => None,
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ impl<'a> Traverse<'a> for Transformer<'a> {
self.x3_es2015.transform_expression_on_exit(expr);
}

fn enter_simple_assignment_target(
&mut self,
node: &mut SimpleAssignmentTarget<'a>,
_ctx: &mut TraverseCtx<'a>,
) {
self.x0_typescript.transform_simple_assignment_target(node);
}

fn enter_formal_parameter(
&mut self,
param: &mut FormalParameter<'a>,
Expand Down
23 changes: 20 additions & 3 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ impl<'a> TypeScriptAnnotations<'a> {
ModuleDeclaration::ExportAllDeclaration(decl) => {
return !decl.export_kind.is_type()
}
ModuleDeclaration::ExportDefaultDeclaration(decl) => {
return !decl.is_typescript_syntax()
}
ModuleDeclaration::ImportDeclaration(decl) => {
let is_type = decl.import_kind.is_type();

Expand Down Expand Up @@ -228,7 +231,7 @@ impl<'a> TypeScriptAnnotations<'a> {
body.body.retain(|elem| match elem {
ClassElement::MethodDefinition(method) => {
matches!(method.r#type, MethodDefinitionType::MethodDefinition)
|| !method.value.is_typescript_syntax()
&& !method.value.is_typescript_syntax()
}
ClassElement::PropertyDefinition(prop) => {
if prop.value.as_ref().is_some_and(Expression::is_typescript_syntax)
Expand All @@ -239,13 +242,27 @@ impl<'a> TypeScriptAnnotations<'a> {
matches!(prop.r#type, PropertyDefinitionType::PropertyDefinition)
}
}
ClassElement::AccessorProperty(prop) => {
matches!(prop.r#type, AccessorPropertyType::AccessorProperty)
}
ClassElement::TSIndexSignature(_) => false,
_ => true,
ClassElement::StaticBlock(_) => true,
});
}

pub fn transform_expression(&mut self, expr: &mut Expression<'a>) {
*expr = self.ctx.ast.copy(expr.get_inner_expression());
if expr.is_typescript_syntax() {
*expr = self.ctx.ast.copy(expr.get_inner_expression());
}
}

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));
*target = SimpleAssignmentTarget::AssignmentTargetIdentifier(ident);
}
}
}

pub fn transform_formal_parameter(&mut self, param: &mut FormalParameter<'a>) {
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ impl<'a> TypeScript<'a> {
self.annotations.transform_expression(expr);
}

pub fn transform_simple_assignment_target(&mut self, target: &mut SimpleAssignmentTarget<'a>) {
self.annotations.transform_simple_assignment_target(target);
}

pub fn transform_formal_parameter(&mut self, param: &mut FormalParameter<'a>) {
self.annotations.transform_formal_parameter(param);
}
Expand Down
12 changes: 2 additions & 10 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: 4bd1b2c2

Passed: 469/926
Passed: 477/926

# All Passed:
* babel-preset-react
Expand Down Expand Up @@ -445,20 +445,12 @@ Passed: 469/926
* opts/optimizeConstEnums/input.ts
* opts/rewriteImportExtensions/input.ts

# babel-plugin-transform-typescript (125/150)
* cast/multiple-assert-and-assign/input.ts
* class/accessor-allowDeclareFields-false/input.ts
* class/accessor-allowDeclareFields-true/input.ts
* class/methods/input.ts
* class/private-method-override/input.ts
# babel-plugin-transform-typescript (133/150)
* enum/mix-references/input.ts
* enum/ts5.0-const-foldable/input.ts
* exports/declared-types/input.ts
* exports/default-function/input.ts
* exports/interface/input.ts
* imports/type-only-export-specifier-2/input.ts
* lvalues/as-expression/input.ts
* lvalues/type-assertion/input.ts
* optimize-const-enums/custom-values/input.ts
* optimize-const-enums/custom-values-exported/input.ts
* optimize-const-enums/declare/input.ts
Expand Down

0 comments on commit e8a20f8

Please sign in to comment.