Skip to content

fix(transformer/decorator): do not lose WeakMap when decorator and class properties transforms combined#9952

Merged
graphite-app[bot] merged 1 commit intomainfrom
03-21-fix_transformer_decorator_do_not_lose_weakmap_when_decorator_and_class_properties_transforms_combined
Mar 21, 2025
Merged

fix(transformer/decorator): do not lose WeakMap when decorator and class properties transforms combined#9952
graphite-app[bot] merged 1 commit intomainfrom
03-21-fix_transformer_decorator_do_not_lose_weakmap_when_decorator_and_class_properties_transforms_combined

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Mar 21, 2025

Closes #9171.

Prevent injected statements attached to the Class statement getting lost when class C {} is replaced with let C = class C {}; by moving them to be attached to the new let C statement.


I'm actually unclear why we have to transform class C {} to let C = class C {}; anyway. class C {} already creates a binding C which behaves like a let binding, so it can be reassigned.

Input:

@dec
export class C {}

Current output:

let C = class C {};
C = babelHelpers.decorate([dec], C);
export { C };

Could just be:

export class C {}
C = babelHelpers.decorate([dec], C);

ESBuild does this - it keeps the class declaration.

Copy link
Member Author

overlookmotel commented Mar 21, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 21, 2025

CodSpeed Instrumentation Performance Report

Merging #9952 will create unknown performance changes

Comparing 03-21-fix_transformer_decorator_do_not_lose_weakmap_when_decorator_and_class_properties_transforms_combined (c7fc700) with main (ffd4d13)

Summary

🆕 33 new benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
🆕 codegen[checker.ts] N/A 22.6 ms N/A
🆕 codegen_sourcemap[checker.ts] N/A 65.7 ms N/A
🆕 isolated-declarations[vue-id.ts] N/A 58.3 ms N/A
🆕 lexer[RadixUIAdoptionSection.jsx] N/A 21.4 µs N/A
🆕 lexer[antd.js] N/A 24.8 ms N/A
🆕 lexer[cal.com.tsx] N/A 5.9 ms N/A
🆕 lexer[checker.ts] N/A 14.8 ms N/A
🆕 lexer[pdf.mjs] N/A 3.9 ms N/A
🆕 linter[RadixUIAdoptionSection.jsx] N/A 2.7 ms N/A
🆕 linter[cal.com.tsx] N/A 1.2 s N/A
🆕 linter[checker.ts] N/A 3 s N/A
🆕 mangler[antd.js] N/A 15.8 ms N/A
🆕 mangler[react.development.js] N/A 291.8 µs N/A
🆕 mangler[typescript.js] N/A 39.2 ms N/A
🆕 minifier[antd.js] N/A 165.2 ms N/A
🆕 minifier[react.development.js] N/A 1.8 ms N/A
🆕 minifier[typescript.js] N/A 290.9 ms N/A
🆕 estree[checker.ts] N/A 96.1 ms N/A
🆕 parser[RadixUIAdoptionSection.jsx] N/A 92.5 µs N/A
🆕 parser[antd.js] N/A 113.3 ms N/A
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

@overlookmotel overlookmotel marked this pull request as ready for review March 21, 2025 12:16
@overlookmotel overlookmotel requested a review from Dunqing as a code owner March 21, 2025 12:16
@overlookmotel overlookmotel marked this pull request as draft March 21, 2025 12:17
@Dunqing
Copy link
Member

Dunqing commented Mar 21, 2025

@overlookmotel overlookmotel marked this pull request as ready for review March 21, 2025 12:40
@overlookmotel
Copy link
Member Author

Your esbuild configuration is wrong

Shame on me!

Thanks for pointing that out. Odd that ESBuild produces less optimal output for legacy decorators. But, regardless of what ESBuild does, I think retaining the class C declaration would be perfectly valid (and is shorter).

@Dunqing Dunqing added the 0-merge Merge with Graphite Merge Queue label Mar 21, 2025
Copy link
Member

Dunqing commented Mar 21, 2025

Merge activity

  • Mar 21, 10:07 AM EDT: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Mar 21, 10:07 AM EDT: A user added this pull request to the Graphite merge queue.
  • Mar 21, 10:18 AM EDT: A user merged this pull request with the Graphite merge queue.

…class properties transforms combined (#9952)

Closes #9171.

Prevent injected statements attached to the `Class` statement getting lost when `class C {}` is replaced with `let C = class C {};` by moving them to be attached to the new `let C` statement.

---

I'm actually unclear why we have to transform `class C {}` to `let C = class C {};` anyway. `class C {}` already creates a binding `C` which behaves like a `let` binding, so it can be reassigned.

Input:

```js
@dec
export class C {}
```

Current output:

```js
let C = class C {};
C = babelHelpers.decorate([dec], C);
export { C };
```

Could just be:

```js
export class C {}
C = babelHelpers.decorate([dec], C);
```

[ESBuild does this](https://esbuild.github.io/try/#dAAwLjI0LjAALS10YXJnZXQ9ZXMyMDIwAEBkZWMoKQpjbGFzcyBGb28gewogICNiYXIgPSAiYmFyIjsKICB0ZXN0KCkgeyByZXR1cm4gdGhpcy4jYmFyOyB9Cn0K) - it keeps the class declaration.
graphite-app bot pushed a commit that referenced this pull request Mar 21, 2025
…od (#9951)

Introduce a new method on `StatementInjectorStore` to move any injections attached to one statement to another statement. This is used in #9952.
@graphite-app graphite-app bot force-pushed the 03-21-feat_transformer_add_statementinjectorstore_move_insertions_method branch from 9bdd489 to af78acb Compare March 21, 2025 14:08
@graphite-app graphite-app bot force-pushed the 03-21-fix_transformer_decorator_do_not_lose_weakmap_when_decorator_and_class_properties_transforms_combined branch from 03051ae to c7fc700 Compare March 21, 2025 14:09
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Mar 21, 2025
Base automatically changed from 03-21-feat_transformer_add_statementinjectorstore_move_insertions_method to main March 21, 2025 14:16
@graphite-app graphite-app bot merged commit c7fc700 into main Mar 21, 2025
27 checks passed
@graphite-app graphite-app bot deleted the 03-21-fix_transformer_decorator_do_not_lose_weakmap_when_decorator_and_class_properties_transforms_combined branch March 21, 2025 14:18
graphite-app bot pushed a commit that referenced this pull request Apr 1, 2025
…for class-related tests (#10150)

These tests were added in #9952 for testing `legacy-decorator` plugin interacting with `class-properties`, but these tests didn't enable `class-properties` plugin.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transformer: when using private fields + legacy decorator, the output code does not have the WeakMap to manage private fields

2 participants