Skip to content

Commit f0affc6

Browse files
committed
fix(transformer/typescript): preserve computed property key with side effects when removing class fields
1 parent 24a72d7 commit f0affc6

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

crates/oxc_transformer/src/typescript/class.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<'a> TypeScript<'a> {
209209
pub(super) fn transform_class_on_exit(
210210
&self,
211211
class: &mut Class<'a>,
212-
_ctx: &mut TraverseCtx<'a>,
212+
ctx: &mut TraverseCtx<'a>,
213213
) {
214214
if !self.remove_class_fields_without_initializer {
215215
return;
@@ -220,6 +220,13 @@ impl<'a> TypeScript<'a> {
220220
&& prop.value.is_none()
221221
&& !prop.key.is_private_identifier()
222222
{
223+
// Keep properties whose computed keys have side effects.
224+
// e.g. legacy decorator transforms `[FIELD_NAME]` to `[_a = FIELD_NAME]`,
225+
// and removing the property would discard the assignment.
226+
// This matches TypeScript's behavior of keeping such properties.
227+
if let Some(key) = prop.key.as_expression() {
228+
return key_needs_temp_var(key, ctx);
229+
}
223230
return false;
224231
}
225232
true

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: 87a048db
22

3-
Passed: 204/335
3+
Passed: 206/337
44

55
# All Passed:
66
* babel-plugin-transform-class-static-block
@@ -555,7 +555,7 @@ x Output mismatch
555555
x Output mismatch
556556

557557

558-
# legacy-decorators (6/86)
558+
# legacy-decorators (8/88)
559559
* oxc/class-without-name-with-decorated_class/input.ts
560560
Bindings mismatch:
561561
after transform: ScopeId(0): ["dec"]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const FIELD_NAME = "myField";
2+
3+
function dec(target: any, key: string) {}
4+
5+
class MyModel {
6+
@dec
7+
[FIELD_NAME]: string;
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"plugins": [
3+
"transform-legacy-decorator",
4+
[
5+
"transform-typescript",
6+
{
7+
"removeClassFieldsWithoutInitializer": true
8+
}
9+
]
10+
]
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var _FIELD_NAME;
2+
const FIELD_NAME = "myField";
3+
function dec(target, key) {}
4+
class MyModel {
5+
[_FIELD_NAME = FIELD_NAME];
6+
}
7+
babelHelpers.decorate([dec], MyModel.prototype, _FIELD_NAME, void 0);

0 commit comments

Comments
 (0)