Skip to content

Commit c4a2d79

Browse files
committed
fix(transformer/typescript): should remove abstract field early (#12417)
* close rolldown/rolldown#5326 Regression introduced by #12212. The transform execution shift causes the bug because the `class-properties` plugin always assumes that all fields accessed can be transformed. However, the prior change breaks the assumption. This PR reverts some changes to remove the ts-only field early, which respects that `class-properties` assumption
1 parent 299b926 commit c4a2d79

File tree

6 files changed

+24
-21
lines changed

6 files changed

+24
-21
lines changed

crates/oxc_transformer/src/typescript/annotations.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,15 @@ impl<'a> Traverse<'a, TransformState<'a>> for TypeScriptAnnotations<'a, '_> {
221221
class.super_type_arguments = None;
222222
class.implements.clear();
223223
class.r#abstract = false;
224-
}
225224

226-
fn exit_class(&mut self, class: &mut Class<'a>, _: &mut TraverseCtx<'a>) {
227225
// Remove type only members
228226
class.body.body.retain(|elem| match elem {
229227
ClassElement::MethodDefinition(method) => {
230228
matches!(method.r#type, MethodDefinitionType::MethodDefinition)
231229
&& !method.value.is_typescript_syntax()
232230
}
233231
ClassElement::PropertyDefinition(prop) => {
234-
matches!(prop.r#type, PropertyDefinitionType::PropertyDefinition) && !prop.declare
232+
matches!(prop.r#type, PropertyDefinitionType::PropertyDefinition)
235233
}
236234
ClassElement::AccessorProperty(prop) => {
237235
matches!(prop.r#type, AccessorPropertyType::AccessorProperty)
@@ -241,6 +239,16 @@ impl<'a> Traverse<'a, TransformState<'a>> for TypeScriptAnnotations<'a, '_> {
241239
});
242240
}
243241

242+
fn exit_class(&mut self, class: &mut Class<'a>, _: &mut TraverseCtx<'a>) {
243+
// Remove `declare` properties from the class body, other ts-only properties have been removed in `enter_class`.
244+
// The reason that removing `declare` properties here because the legacy-decorator plugin needs to transform
245+
// `declare` field in the `exit_class` phase, so we have to ensure this step is run after the legacy-decorator plugin.
246+
class
247+
.body
248+
.body
249+
.retain(|elem| !matches!(elem, ClassElement::PropertyDefinition(prop) if prop.declare));
250+
}
251+
244252
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
245253
if expr.is_typescript_syntax() {
246254
let inner_expr = expr.get_inner_expression_mut();

tasks/coverage/snapshots/semantic_babel.snap

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,14 @@ after transform: ScopeId(1): [ScopeId(2)]
116116
rebuilt : ScopeId(1): []
117117

118118
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/class-private-method/typescript-invalid-abstract/input.ts
119-
Bindings mismatch:
120-
after transform: ScopeId(0): ["TSAbstractClass", "_TSAbstractClass_brand", "_classPrivateMethodInitSpec", "_foo"]
121-
rebuilt : ScopeId(0): ["TSAbstractClass", "_TSAbstractClass_brand", "_classPrivateMethodInitSpec"]
122119
Scope children mismatch:
123-
after transform: ScopeId(1): [ScopeId(2), ScopeId(3)]
124-
rebuilt : ScopeId(1): [ScopeId(2)]
120+
after transform: ScopeId(1): [ScopeId(2)]
121+
rebuilt : ScopeId(1): []
125122

126123
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/class-private-method/typescript-invalid-abstract-babel-7/input.ts
127-
Bindings mismatch:
128-
after transform: ScopeId(0): ["TSAbstractClass", "_TSAbstractClass_brand", "_classPrivateMethodInitSpec", "_foo"]
129-
rebuilt : ScopeId(0): ["TSAbstractClass", "_TSAbstractClass_brand", "_classPrivateMethodInitSpec"]
130124
Scope children mismatch:
131-
after transform: ScopeId(1): [ScopeId(2), ScopeId(3)]
132-
rebuilt : ScopeId(1): [ScopeId(2)]
125+
after transform: ScopeId(1): [ScopeId(2)]
126+
rebuilt : ScopeId(1): []
133127

134128
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/class-private-property/typescript/input.js
135129
Unresolved references mismatch:
@@ -519,9 +513,6 @@ semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescr
519513
A required parameter cannot follow an optional parameter.
520514
521515
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/private-method-overload/input.ts
522-
Bindings mismatch:
523-
after transform: ScopeId(0): ["Test", "_Test_brand", "_classPrivateMethodInitSpec", "_f", "_f2", "_f3"]
524-
rebuilt : ScopeId(0): ["Test", "_Test_brand", "_classPrivateMethodInitSpec", "_f"]
525516
Scope children mismatch:
526517
after transform: ScopeId(1): [ScopeId(2), ScopeId(3), ScopeId(5)]
527518
rebuilt : ScopeId(1): [ScopeId(2)]

tasks/coverage/snapshots/semantic_typescript.snap

Lines changed: 3 additions & 3 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(3)]
219+
rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2)]
220220
Scope children mismatch:
221-
after transform: ScopeId(3): [ScopeId(4), ScopeId(5), ScopeId(6), ScopeId(11)]
222-
rebuilt : ScopeId(1): [ScopeId(2)]
221+
after transform: ScopeId(3): [ScopeId(4), ScopeId(5), ScopeId(6)]
222+
rebuilt : ScopeId(1): []
223223

224224
semantic Error: tasks/coverage/typescript/tests/cases/compiler/acceptableAlias1.ts
225225
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: 179/297
3+
Passed: 180/298
44

55
# All Passed:
66
* babel-plugin-transform-class-static-block
@@ -20,7 +20,7 @@ Passed: 179/297
2020
* regexp
2121

2222

23-
# babel-plugin-transform-class-properties (21/27)
23+
# babel-plugin-transform-class-properties (22/28)
2424
* private-field-resolve-to-method/input.js
2525
x Output mismatch
2626

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
abstract class A {
2+
public abstract field: string;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A {}

0 commit comments

Comments
 (0)