From b6fcc6cce5919b298f978592e2861d70575fce64 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Thu, 6 Jun 2024 19:43:41 +0800 Subject: [PATCH] feat(transformer/typescript): remove more typescript ast nodes --- crates/oxc_ast/src/ast/js.rs | 3 ++ crates/oxc_transformer/src/lib.rs | 8 +++++ .../src/typescript/annotations.rs | 35 +++++++++++++----- crates/oxc_transformer/src/typescript/mod.rs | 4 +++ tasks/coverage/transformer_babel.snap | 6 +--- tasks/coverage/transformer_typescript.snap | 36 +------------------ 6 files changed, 44 insertions(+), 48 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 7f824995dbd302..7705ba5bf87d23 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -1136,6 +1136,9 @@ impl<'a> AssignmentTarget<'a> { pub fn get_identifier(&self) -> Option<&str> { self.as_simple_assignment_target().and_then(|it| it.get_identifier()) } + pub fn get_expression(&self) -> Option<&Expression<'a>> { + self.as_simple_assignment_target().and_then(|it| it.get_expression()) + } } /// Macro for matching `AssignmentTarget`'s variants. diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index 2766fbab42326d..19f5f8598c54a2 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -165,6 +165,14 @@ impl<'a> Traverse<'a> for Transformer<'a> { self.x0_typescript.transform_simple_assignment_target(node); } + fn enter_assignment_target( + &mut self, + node: &mut AssignmentTarget<'a>, + _ctx: &mut TraverseCtx<'a>, + ) { + self.x0_typescript.transform_assignment_target(node); + } + fn enter_formal_parameter( &mut self, param: &mut FormalParameter<'a>, diff --git a/crates/oxc_transformer/src/typescript/annotations.rs b/crates/oxc_transformer/src/typescript/annotations.rs index b12365a8a87b0c..eca586396530a7 100644 --- a/crates/oxc_transformer/src/typescript/annotations.rs +++ b/crates/oxc_transformer/src/typescript/annotations.rs @@ -180,7 +180,7 @@ impl<'a> TypeScriptAnnotations<'a> { .as_ref() .is_some_and(|specifiers| specifiers.is_empty())) } - _ => false, + _ => module_decl.is_typescript_syntax(), }; if need_delete { @@ -265,6 +265,23 @@ impl<'a> TypeScriptAnnotations<'a> { } } + 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; + } + } + pub fn transform_formal_parameter(&mut self, param: &mut FormalParameter<'a>) { param.accessibility = None; } @@ -283,15 +300,17 @@ impl<'a> TypeScriptAnnotations<'a> { // Collects parameter properties so that we can add an assignment // for each of them in the constructor body. if def.kind == MethodDefinitionKind::Constructor { - for param in &def.value.params.items { - if !param.is_public() { - continue; + for param in def.value.params.items.as_mut_slice() { + if param.is_public() { + if let Some(id) = param.pattern.get_identifier() { + let assignment = self.create_this_property_assignment(id); + self.assignments.push(assignment); + } } - if let Some(id) = param.pattern.get_identifier() { - let assignment = self.create_this_property_assignment(id); - self.assignments.push(assignment); - } + param.readonly = false; + param.accessibility = None; + param.r#override = false; } } diff --git a/crates/oxc_transformer/src/typescript/mod.rs b/crates/oxc_transformer/src/typescript/mod.rs index 32d62de31c4e8e..876a234585ec95 100644 --- a/crates/oxc_transformer/src/typescript/mod.rs +++ b/crates/oxc_transformer/src/typescript/mod.rs @@ -113,6 +113,10 @@ impl<'a> TypeScript<'a> { self.annotations.transform_simple_assignment_target(target); } + pub fn transform_assignment_target(&mut self, target: &mut AssignmentTarget<'a>) { + self.annotations.transform_assignment_target(target); + } + pub fn transform_formal_parameter(&mut self, param: &mut FormalParameter<'a>) { self.annotations.transform_formal_parameter(param); } diff --git a/tasks/coverage/transformer_babel.snap b/tasks/coverage/transformer_babel.snap index 0016c07ebdba74..edaccc353d7f72 100644 --- a/tasks/coverage/transformer_babel.snap +++ b/tasks/coverage/transformer_babel.snap @@ -2,8 +2,4 @@ commit: 4bd1b2c2 transformer_babel Summary: AST Parsed : 2099/2099 (100.00%) -Positive Passed: 2095/2099 (99.81%) -Mismatch: "typescript/class/parameter-properties/input.ts" -Mismatch: "typescript/export/as-namespace/input.ts" -Mismatch: "typescript/export/equals/input.ts" -Mismatch: "typescript/export/equals-in-unambiguous/input.ts" +Positive Passed: 2099/2099 (100.00%) diff --git a/tasks/coverage/transformer_typescript.snap b/tasks/coverage/transformer_typescript.snap index 3dd226e9d613ea..9f5bf8551c3bfe 100644 --- a/tasks/coverage/transformer_typescript.snap +++ b/tasks/coverage/transformer_typescript.snap @@ -2,39 +2,5 @@ commit: 64d2eeea transformer_typescript Summary: AST Parsed : 5243/5243 (100.00%) -Positive Passed: 5208/5243 (99.33%) -Mismatch: "compiler/amdModuleName1.ts" -Mismatch: "compiler/avoidCycleWithVoidExpressionReturnedFromArrow.ts" -Mismatch: "compiler/clinterfaces.ts" -Mismatch: "compiler/collectionPatternNoError.ts" -Mismatch: "compiler/comparabilityTypeParametersRelatedByUnion.ts" -Mismatch: "compiler/constEnumMergingWithValues1.ts" -Mismatch: "compiler/constEnumMergingWithValues2.ts" -Mismatch: "compiler/constEnumMergingWithValues3.ts" -Mismatch: "compiler/constEnumMergingWithValues4.ts" -Mismatch: "compiler/constEnumMergingWithValues5.ts" +Positive Passed: 5242/5243 (99.98%) Mismatch: "compiler/elidedEmbeddedStatementsReplacedWithSemicolon.ts" -Mismatch: "compiler/es5-umd4.ts" -Mismatch: "compiler/exportAssignValueAndType.ts" -Mismatch: "compiler/exportAssignmentError.ts" -Mismatch: "compiler/exportAssignmentWithImportStatementPrivacyError.ts" -Mismatch: "compiler/exportAssignmentWithPrivacyError.ts" -Mismatch: "compiler/exportAssignmentWithoutIdentifier1.ts" -Mismatch: "compiler/exportEqualNamespaces.ts" -Mismatch: "compiler/exportEqualsAmd.ts" -Mismatch: "compiler/exportEqualsClassNoRedeclarationError.ts" -Mismatch: "compiler/exportEqualsCommonJs.ts" -Mismatch: "compiler/exportEqualsUmd.ts" -Mismatch: "compiler/module_augmentUninstantiatedModule2.ts" -Mismatch: "compiler/noCircularDefinitionOnExportOfPrivateInMergedNamespace.ts" -Mismatch: "compiler/noErrorUsingImportExportModuleAugmentationInDeclarationFile1.ts" -Mismatch: "compiler/recursiveFieldSetting.ts" -Mismatch: "compiler/sourceMapValidationExportAssignment.ts" -Mismatch: "compiler/sourceMapValidationExportAssignmentCommonjs.ts" -Mismatch: "compiler/varianceProblingAndZeroOrderIndexSignatureRelationsAlign.ts" -Mismatch: "compiler/varianceProblingAndZeroOrderIndexSignatureRelationsAlign2.ts" -Mismatch: "conformance/classes/members/privateNames/privateNameFieldParenthesisLeftAssignment.ts" -Mismatch: "conformance/classes/propertyMemberDeclarations/initializationOrdering1.ts" -Mismatch: "conformance/classes/propertyMemberDeclarations/instanceMemberInitialization.ts" -Mismatch: "conformance/decorators/class/constructor/parameter/decoratorOnClassConstructorParameter5.ts" -Mismatch: "conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsTopLevelOfModule.2.ts"