feat(transformer/decorator): support emitting decorator metadata#9057
Merged
graphite-app[bot] merged 1 commit intomainfrom Feb 19, 2025
Conversation
Member
Author
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
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. |
|
Yay, looking forward to this! |
2625eb3 to
7e84118
Compare
7e84118 to
ba98139
Compare
CodSpeed Performance ReportMerging #9057 will not alter performanceComparing Summary
|
4f84925 to
e58bd9b
Compare
This was referenced Feb 18, 2025
e58bd9b to
a2aad51
Compare
ff8896e to
2143049
Compare
Contributor
Merge activity
|
close: #9186 ## Implementation The Implementation port from [TypeScript]( https://github.com/microsoft/TypeScript/blob/d85767abfd83880cea17cea70f9913e9c4496dcc/src/compiler/transformers/ts.ts#L1119-L1136) ## Example Input: ```ts class Demo { @logmethod public foo(bar: number) {} @prop prop: string = "hello"; } ``` Output: ```js class Demo { foo(bar) {} prop = "hello"; } babelHelpers.decorate([ LogMethod, babelHelpers.decorateParam(0, babelHelpers.decorateMetadata("design:type", Function)), babelHelpers.decorateParam(0, babelHelpers.decorateMetadata("design:paramtypes", [Number])), babelHelpers.decorateParam(0, babelHelpers.decorateMetadata("design:returntype", void 0)) ], Demo.prototype, "foo", null); babelHelpers.decorate([Prop, babelHelpers.decorateMetadata("design:type", String)], Demo.prototype, "prop", void 0); ``` ## Limitations ### Compared to TypeScript We lack a type inference ability that TypeScript has, so we cannot determine the exact type of the TyepReference refers to. See [`LegacyDecoratorMetadata::serialize_type_reference_node`] does. For example: Input: ```ts type Foo = string; class Cls { @dec p: Foo = "" } ``` TypeScript Output: ```js class Cls { constructor() { this.p = ""; } } __decorate([ dec, __metadata("design:type", String) // Infer the type of `Foo` is `String` ], Cls.prototype, "p", void 0); ``` OXC Output: ```js var _ref; class Cls { p = ""; } babelHelpers.decorate([ dec, babelHelpers.decorateMetadata("design:type", typeof (_ref = typeof Foo === "undefined" && Foo) === "function" ? _ref : Object) ], Cls.prototype, "p", void 0); ``` ### Compared to SWC SWC also has the above limitation, considering that SWC has been adopted in [NestJS](https://docs.nestjs.com/recipes/swc#jest--swc), so the limitation may not be a problem. In addition, SWC provides additional support for inferring enum members, which we currently do not have. We haven't dived into how NestJS uses it, so we don't know if it matters, thus we may leave it until we receive feedback.
2143049 to
c21f4b7
Compare
a2aad51 to
90ba283
Compare
Base automatically changed from
02-18-test_transformer_legacy-decorator_update_update-fixtures_for_emitdecoratormetadata
to
main
February 19, 2025 14:09
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

close: #9186
Implementation
The Implementation port from TypeScript
Example
Input:
Output:
Limitations
Compared to TypeScript
We lack a type inference ability that TypeScript has, so we cannot determine the exact type of the TyepReference refers to. See [
LegacyDecoratorMetadata::serialize_type_reference_node] does.For example:
Input:
TypeScript Output:
OXC Output:
Compared to SWC
SWC also has the above limitation, considering that SWC has been adopted in NestJS, so the limitation may not be a problem. In addition, SWC provides additional support for inferring enum members, which we currently do not have. We haven't dived into how NestJS uses it, so we don't know if it matters, thus we may leave it until we receive feedback.