Skip to content

Commit 0d17c57

Browse files
committed
fix(transformer/legacy-decorator): decorated fields with the declare modifier are not transformed
1 parent facd3cd commit 0d17c57

File tree

11 files changed

+44
-20
lines changed

11 files changed

+44
-20
lines changed

crates/oxc_transformer/src/decorator/legacy/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'a> Traverse<'a, TransformState<'a>> for LegacyDecoratorMetadata<'a, '_> {
174174
prop: &mut PropertyDefinition<'a>,
175175
ctx: &mut TraverseCtx<'a>,
176176
) {
177-
if prop.declare || prop.decorators.is_empty() {
177+
if prop.decorators.is_empty() {
178178
return;
179179
}
180180
prop.decorators.push(self.create_design_type_metadata(prop.type_annotation.as_ref(), ctx));

crates/oxc_transformer/src/decorator/legacy/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,9 @@ impl<'a> LegacyDecorator<'a, '_> {
603603

604604
for element in &mut class.body.body {
605605
let (is_static, key, descriptor, decorations) = match element {
606-
ClassElement::MethodDefinition(method) if !method.value.is_typescript_syntax() => {
606+
ClassElement::MethodDefinition(method)
607+
if !method.value.is_typescript_syntax() && method.value.body.is_some() =>
608+
{
607609
let Some(decorations) = self.get_all_decorators_of_class_method(method, ctx)
608610
else {
609611
continue;
@@ -876,7 +878,10 @@ impl<'a> LegacyDecorator<'a, '_> {
876878
PrivateInExpressionDetector::has_private_in_expression_in_method_decorator(method);
877879
}
878880
}
879-
ClassElement::MethodDefinition(method) if !method.value.is_typescript_syntax() => {
881+
ClassElement::MethodDefinition(method)
882+
if !method.value.r#type.is_typescript_syntax()
883+
&& method.value.body.is_some() =>
884+
{
880885
class_element_is_decorated |= !method.decorators.is_empty()
881886
|| Self::class_method_parameter_is_decorated(&method.value);
882887

@@ -885,8 +890,8 @@ impl<'a> LegacyDecorator<'a, '_> {
885890
PrivateInExpressionDetector::has_private_in_expression_in_method_decorator(method);
886891
}
887892
}
888-
ClassElement::PropertyDefinition(prop) if !prop.declare => {
889-
class_element_is_decorated |= !prop.decorators.is_empty();
893+
ClassElement::PropertyDefinition(prop) if !prop.decorators.is_empty() => {
894+
class_element_is_decorated = true;
890895

891896
if class_element_is_decorated && !has_private_in_expression_in_decorator {
892897
has_private_in_expression_in_decorator =
@@ -896,7 +901,7 @@ impl<'a> LegacyDecorator<'a, '_> {
896901
}
897902
}
898903
ClassElement::AccessorProperty(accessor) => {
899-
class_element_is_decorated |= !accessor.decorators.is_empty();
904+
class_element_is_decorated = true;
900905

901906
if class_element_is_decorated && !has_private_in_expression_in_decorator {
902907
has_private_in_expression_in_decorator =

crates/oxc_transformer/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,6 @@ impl<'a> Traverse<'a, TransformState<'a>> for TransformerImpl<'a, '_> {
303303
}
304304

305305
fn enter_class_body(&mut self, body: &mut ClassBody<'a>, ctx: &mut TraverseCtx<'a>) {
306-
if let Some(typescript) = self.x0_typescript.as_mut() {
307-
typescript.enter_class_body(body, ctx);
308-
}
309306
self.x2_es2022.enter_class_body(body, ctx);
310307
}
311308

crates/oxc_transformer/src/typescript/annotations.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ impl<'a> Traverse<'a, TransformState<'a>> for TypeScriptAnnotations<'a, '_> {
223223
class.r#abstract = false;
224224
}
225225

226-
fn enter_class_body(&mut self, body: &mut ClassBody<'a>, _ctx: &mut TraverseCtx<'a>) {
226+
fn exit_class(&mut self, class: &mut Class<'a>, _: &mut TraverseCtx<'a>) {
227227
// Remove type only members
228-
body.body.retain(|elem| match elem {
228+
class.body.body.retain(|elem| match elem {
229229
ClassElement::MethodDefinition(method) => {
230230
matches!(method.r#type, MethodDefinitionType::MethodDefinition)
231231
&& !method.value.is_typescript_syntax()
@@ -346,7 +346,6 @@ impl<'a> Traverse<'a, TransformState<'a>> for TypeScriptAnnotations<'a, '_> {
346346
);
347347

348348
def.accessibility = None;
349-
def.declare = false;
350349
def.definite = false;
351350
def.r#override = false;
352351
def.optional = false;

crates/oxc_transformer/src/typescript/class.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,13 @@ impl<'a> TypeScript<'a, '_> {
246246
constructor: &mut MethodDefinition<'a>,
247247
ctx: &mut TraverseCtx<'a>,
248248
) {
249-
if !constructor.kind.is_constructor() {
249+
if !constructor.kind.is_constructor() || constructor.value.body.is_none() {
250250
return;
251251
}
252252

253253
let params = &constructor.value.params.items;
254254
let assignments = Self::convert_constructor_params(params, ctx).collect::<Vec<_>>();
255255

256-
// `constructor {}` is guaranteed that it is `Some`.
257256
let constructor_body_statements = &mut constructor.value.body.as_mut().unwrap().statements;
258257
let super_call_position = Self::get_super_call_position(constructor_body_statements);
259258

crates/oxc_transformer/src/typescript/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,10 @@ impl<'a> Traverse<'a, TransformState<'a>> for TypeScript<'a, '_> {
131131
}
132132

133133
fn exit_class(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) {
134+
self.annotations.exit_class(class, ctx);
134135
self.transform_class_on_exit(class, ctx);
135136
}
136137

137-
fn enter_class_body(&mut self, body: &mut ClassBody<'a>, ctx: &mut TraverseCtx<'a>) {
138-
self.annotations.enter_class_body(body, ctx);
139-
}
140-
141138
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
142139
self.annotations.enter_expression(expr, ctx);
143140
}

tasks/transform_conformance/snapshots/oxc.snap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
commit: 1d4546bc
22

3-
Passed: 176/293
3+
Passed: 178/295
44

55
# All Passed:
66
* babel-plugin-transform-class-static-block
@@ -494,7 +494,7 @@ after transform: SymbolId(4): ScopeId(1)
494494
rebuilt : SymbolId(5): ScopeId(4)
495495

496496

497-
# legacy-decorators (4/76)
497+
# legacy-decorators (6/78)
498498
* oxc/metadata/abstract-class/input.ts
499499
Symbol reference IDs mismatch for "Dependency":
500500
after transform: SymbolId(1): [ReferenceId(1), ReferenceId(2), ReferenceId(3)]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class DeclareFields {
2+
@dec declare name: number;
3+
@dec declare output: string;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class DeclareFields {}
2+
3+
babelHelpers.decorate([dec], DeclareFields.prototype, "name", void 0);
4+
babelHelpers.decorate([dec], DeclareFields.prototype, "output", void 0);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class DeclareFields {
2+
@dec declare name: number;
3+
@dec declare output: string;
4+
}

0 commit comments

Comments
 (0)