Skip to content

Commit 22b37b3

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

File tree

13 files changed

+90
-55
lines changed

13 files changed

+90
-55
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -885,8 +885,8 @@ impl<'a> LegacyDecorator<'a, '_> {
885885
PrivateInExpressionDetector::has_private_in_expression_in_method_decorator(method);
886886
}
887887
}
888-
ClassElement::PropertyDefinition(prop) if !prop.declare => {
889-
class_element_is_decorated |= !prop.decorators.is_empty();
888+
ClassElement::PropertyDefinition(prop) if !prop.decorators.is_empty() => {
889+
class_element_is_decorated = true;
890890

891891
if class_element_is_decorated && !has_private_in_expression_in_decorator {
892892
has_private_in_expression_in_decorator =
@@ -895,8 +895,8 @@ impl<'a> LegacyDecorator<'a, '_> {
895895
);
896896
}
897897
}
898-
ClassElement::AccessorProperty(accessor) => {
899-
class_element_is_decorated |= !accessor.decorators.is_empty();
898+
ClassElement::AccessorProperty(accessor) if !accessor.decorators.is_empty() => {
899+
class_element_is_decorated = true;
900900

901901
if class_element_is_decorated && !has_private_in_expression_in_decorator {
902902
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/coverage/snapshots/semantic_babel.snap

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

33
semantic_babel Summary:
44
AST Parsed : 2362/2362 (100.00%)
5-
Positive Passed: 1945/2362 (82.35%)
5+
Positive Passed: 1946/2362 (82.39%)
66
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/annex-b/enabled/3.3-function-in-if-body/input.js
77
Symbol scope ID mismatch for "f":
88
after transform: SymbolId(0): ScopeId(4294967294)
@@ -134,14 +134,20 @@ after transform: ScopeId(1): [ScopeId(2)]
134134
rebuilt : ScopeId(1): []
135135

136136
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/class-private-method/typescript-invalid-abstract/input.ts
137+
Bindings mismatch:
138+
after transform: ScopeId(0): ["TSAbstractClass", "_TSAbstractClass_brand", "_classPrivateMethodInitSpec", "_foo"]
139+
rebuilt : ScopeId(0): ["TSAbstractClass", "_TSAbstractClass_brand", "_classPrivateMethodInitSpec"]
137140
Scope children mismatch:
138-
after transform: ScopeId(1): [ScopeId(2)]
139-
rebuilt : ScopeId(1): []
141+
after transform: ScopeId(1): [ScopeId(2), ScopeId(3)]
142+
rebuilt : ScopeId(1): [ScopeId(2)]
140143

141144
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/class-private-method/typescript-invalid-abstract-babel-7/input.ts
145+
Bindings mismatch:
146+
after transform: ScopeId(0): ["TSAbstractClass", "_TSAbstractClass_brand", "_classPrivateMethodInitSpec", "_foo"]
147+
rebuilt : ScopeId(0): ["TSAbstractClass", "_TSAbstractClass_brand", "_classPrivateMethodInitSpec"]
142148
Scope children mismatch:
143-
after transform: ScopeId(1): [ScopeId(2)]
144-
rebuilt : ScopeId(1): []
149+
after transform: ScopeId(1): [ScopeId(2), ScopeId(3)]
150+
rebuilt : ScopeId(1): [ScopeId(2)]
145151

146152
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/class-private-property/typescript/input.js
147153
Unresolved references mismatch:
@@ -419,11 +425,6 @@ Multiple constructor implementations are not allowed.
419425
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/declare/input.ts
420426
Identifier `x` has already been declared
421427
422-
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/declare-readonly-field-initializer/input.ts
423-
Unresolved references mismatch:
424-
after transform: ["a"]
425-
rebuilt : []
426-
427428
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/expression-extends/input.ts
428429
Unresolved references mismatch:
429430
after transform: ["T", "f"]
@@ -546,6 +547,9 @@ semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescr
546547
A required parameter cannot follow an optional parameter.
547548
548549
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/private-method-overload/input.ts
550+
Bindings mismatch:
551+
after transform: ScopeId(0): ["Test", "_Test_brand", "_classPrivateMethodInitSpec", "_f", "_f2", "_f3"]
552+
rebuilt : ScopeId(0): ["Test", "_Test_brand", "_classPrivateMethodInitSpec", "_f"]
549553
Scope children mismatch:
550554
after transform: ScopeId(1): [ScopeId(2), ScopeId(3), ScopeId(5)]
551555
rebuilt : ScopeId(1): [ScopeId(2)]

tasks/coverage/snapshots/semantic_typescript.snap

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ rebuilt : ScopeId(0): []
216216
semantic Error: tasks/coverage/typescript/tests/cases/compiler/abstractPropertyBasics.ts
217217
Scope children mismatch:
218218
after transform: ScopeId(0): [ScopeId(1), ScopeId(3), ScopeId(7)]
219-
rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2)]
219+
rebuilt : ScopeId(0): [ScopeId(1), ScopeId(3)]
220220
Scope children mismatch:
221-
after transform: ScopeId(3): [ScopeId(4), ScopeId(5), ScopeId(6)]
222-
rebuilt : ScopeId(1): []
221+
after transform: ScopeId(3): [ScopeId(4), ScopeId(5), ScopeId(6), ScopeId(11)]
222+
rebuilt : ScopeId(1): [ScopeId(2)]
223223

224224
semantic Error: tasks/coverage/typescript/tests/cases/compiler/acceptableAlias1.ts
225225
Bindings mismatch:
@@ -8625,12 +8625,9 @@ after transform: SymbolId(0): [ReferenceId(2), ReferenceId(8), ReferenceId(9)]
86258625
rebuilt : SymbolId(0): [ReferenceId(8), ReferenceId(9)]
86268626

86278627
semantic Error: tasks/coverage/typescript/tests/cases/compiler/decoratorMetadataElidedImportOnDeclare.ts
8628-
Bindings mismatch:
8629-
after transform: ScopeId(0): ["Observable", "Test", "whatever"]
8630-
rebuilt : ScopeId(0): ["Test", "whatever"]
8631-
Symbol reference IDs mismatch for "whatever":
8632-
after transform: SymbolId(1): [ReferenceId(0)]
8633-
rebuilt : SymbolId(0): []
8628+
Symbol reference IDs mismatch for "Observable":
8629+
after transform: SymbolId(0): [ReferenceId(1), ReferenceId(3), ReferenceId(4)]
8630+
rebuilt : SymbolId(0): [ReferenceId(5), ReferenceId(6)]
86348631

86358632
semantic Error: tasks/coverage/typescript/tests/cases/compiler/decoratorMetadataForMethodWithNoReturnTypeAnnotation01.ts
86368633
Bindings mismatch:
@@ -12966,21 +12963,27 @@ after transform: ["DecoratorContext", "require"]
1296612963
rebuilt : ["require"]
1296712964

1296812965
semantic Error: tasks/coverage/typescript/tests/cases/compiler/esNextWeakRefs_IterableWeakMap.ts
12966+
Symbol reference IDs mismatch for "_Symbol$toStringTag":
12967+
after transform: SymbolId(26): [ReferenceId(73), ReferenceId(74)]
12968+
rebuilt : SymbolId(3): [ReferenceId(6)]
12969+
Symbol reference IDs mismatch for "_Symbol$iterator":
12970+
after transform: SymbolId(29): [ReferenceId(82), ReferenceId(83)]
12971+
rebuilt : SymbolId(4): [ReferenceId(18)]
1296912972
Unresolved references mismatch:
1297012973
after transform: ["FinalizationRegistry", "Generator", "Iterable", "Object", "Set", "Symbol", "WeakMap", "WeakRef", "undefined"]
1297112974
rebuilt : ["FinalizationRegistry", "Object", "Set", "Symbol", "WeakMap", "WeakRef", "undefined"]
1297212975
Unresolved reference IDs mismatch for "WeakRef":
1297312976
after transform: [ReferenceId(0), ReferenceId(2), ReferenceId(11), ReferenceId(15), ReferenceId(33)]
12974-
rebuilt : [ReferenceId(26)]
12977+
rebuilt : [ReferenceId(30)]
1297512978
Unresolved reference IDs mismatch for "Set":
1297612979
after transform: [ReferenceId(1), ReferenceId(14)]
12977-
rebuilt : [ReferenceId(10)]
12980+
rebuilt : [ReferenceId(12)]
1297812981
Unresolved reference IDs mismatch for "Symbol":
1297912982
after transform: [ReferenceId(8), ReferenceId(55), ReferenceId(69), ReferenceId(72)]
12980-
rebuilt : [ReferenceId(81), ReferenceId(84)]
12983+
rebuilt : [ReferenceId(85), ReferenceId(88)]
1298112984
Unresolved reference IDs mismatch for "WeakMap":
12982-
after transform: [ReferenceId(5), ReferenceId(9), ReferenceId(108), ReferenceId(109), ReferenceId(110)]
12983-
rebuilt : [ReferenceId(2), ReferenceId(3), ReferenceId(4), ReferenceId(7)]
12985+
after transform: [ReferenceId(5), ReferenceId(9), ReferenceId(114), ReferenceId(115), ReferenceId(116)]
12986+
rebuilt : [ReferenceId(2), ReferenceId(3), ReferenceId(4), ReferenceId(9)]
1298412987

1298512988
semantic Error: tasks/coverage/typescript/tests/cases/compiler/escapedIdentifiers.ts
1298612989
Namespaces exporting non-const are not supported by Babel. Change to const or see: https://babeljs.io/docs/en/babel-plugin-transform-typescript
@@ -18816,14 +18819,17 @@ rebuilt : ScopeId(0): []
1881618819

1881718820
semantic Error: tasks/coverage/typescript/tests/cases/compiler/iterableTReturnTNext.ts
1881818821
Bindings mismatch:
18819-
after transform: ScopeId(0): ["MyMap", "_source", "_wrapAsyncGenerator", "doubles", "map", "r1", "r2", "r3", "set", "source"]
18820-
rebuilt : ScopeId(0): ["MyMap", "_source", "_wrapAsyncGenerator", "doubles", "r1", "r2", "r3", "source"]
18822+
after transform: ScopeId(0): ["MyMap", "_Symbol$iterator", "_Symbol$toStringTag", "_defineProperty", "_source", "_wrapAsyncGenerator", "doubles", "map", "r1", "r2", "r3", "set", "source"]
18823+
rebuilt : ScopeId(0): ["MyMap", "_Symbol$iterator", "_Symbol$toStringTag", "_defineProperty", "_source", "_wrapAsyncGenerator", "doubles", "r1", "r2", "r3", "source"]
1882118824
Scope children mismatch:
1882218825
after transform: ScopeId(0): [ScopeId(1), ScopeId(3), ScopeId(15), ScopeId(16)]
1882318826
rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(4)]
1882418827
Scope children mismatch:
1882518828
after transform: ScopeId(6): [ScopeId(7)]
18826-
rebuilt : ScopeId(7): []
18829+
rebuilt : ScopeId(8): []
18830+
Symbol reference IDs mismatch for "_Symbol$toStringTag":
18831+
after transform: SymbolId(20): [ReferenceId(29), ReferenceId(30)]
18832+
rebuilt : SymbolId(2): [ReferenceId(19)]
1882718833
Reference symbol mismatch for "map":
1882818834
after transform: SymbolId(0) "map"
1882918835
rebuilt : <None>
@@ -18838,7 +18844,7 @@ after transform: ["Error", "Map", "MapIterator", "Set", "Symbol", "arguments", "
1883818844
rebuilt : ["Error", "Symbol", "arguments", "map", "require", "set", "undefined"]
1883918845
Unresolved reference IDs mismatch for "Symbol":
1884018846
after transform: [ReferenceId(10), ReferenceId(17)]
18841-
rebuilt : [ReferenceId(15)]
18847+
rebuilt : [ReferenceId(14)]
1884218848

1884318849
semantic Error: tasks/coverage/typescript/tests/cases/compiler/jqueryInference.ts
1884418850
Bindings mismatch:
@@ -19837,13 +19843,13 @@ rebuilt : []
1983719843
semantic Error: tasks/coverage/typescript/tests/cases/compiler/mergedInstantiationAssignment.ts
1983819844
Symbol reference IDs mismatch for "GenericObject":
1983919845
after transform: SymbolId(0): [ReferenceId(1), ReferenceId(2)]
19840-
rebuilt : SymbolId(0): [ReferenceId(0)]
19846+
rebuilt : SymbolId(1): [ReferenceId(1)]
1984119847
Symbol reference IDs mismatch for "GenericObjectWithoutSetter":
1984219848
after transform: SymbolId(4): [ReferenceId(5), ReferenceId(6)]
19843-
rebuilt : SymbolId(3): [ReferenceId(2)]
19849+
rebuilt : SymbolId(4): [ReferenceId(4)]
1984419850
Symbol reference IDs mismatch for "NormalObject":
1984519851
after transform: SymbolId(7): [ReferenceId(8), ReferenceId(9)]
19846-
rebuilt : SymbolId(5): [ReferenceId(4)]
19852+
rebuilt : SymbolId(6): [ReferenceId(6)]
1984719853

1984819854
semantic Error: tasks/coverage/typescript/tests/cases/compiler/mergedInterfaceFromMultipleFiles1.ts
1984919855
Scope children mismatch:
@@ -36363,14 +36369,20 @@ rebuilt : ["Object", "dec", "require"]
3636336369

3636436370
semantic Error: tasks/coverage/typescript/tests/cases/conformance/decorators/decoratorInAmbientContext.ts
3636536371
Bindings mismatch:
36366-
after transform: ScopeId(0): ["Foo", "b", "decorator"]
36367-
rebuilt : ScopeId(0): ["Foo", "b"]
36372+
after transform: ScopeId(0): ["Foo", "_decorate", "_decorateMetadata", "_defineProperty", "b", "decorator"]
36373+
rebuilt : ScopeId(0): ["Foo", "_decorate", "_decorateMetadata", "_defineProperty", "b"]
3636836374
Scope children mismatch:
3636936375
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
3637036376
rebuilt : ScopeId(0): [ScopeId(1)]
36371-
Symbol reference IDs mismatch for "b":
36372-
after transform: SymbolId(3): [ReferenceId(3)]
36373-
rebuilt : SymbolId(0): []
36377+
Reference symbol mismatch for "decorator":
36378+
after transform: SymbolId(0) "decorator"
36379+
rebuilt : <None>
36380+
Reference symbol mismatch for "decorator":
36381+
after transform: SymbolId(0) "decorator"
36382+
rebuilt : <None>
36383+
Unresolved references mismatch:
36384+
after transform: ["Number", "Symbol", "require"]
36385+
rebuilt : ["Number", "Symbol", "decorator", "require"]
3637436386

3637536387
semantic Error: tasks/coverage/typescript/tests/cases/conformance/decorators/decoratorMetadata.ts
3637636388
Bindings mismatch:

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+
}

0 commit comments

Comments
 (0)