-
-
Notifications
You must be signed in to change notification settings - Fork 518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(transformer/decorator): support emitting decorator metadata #9057
feat(transformer/decorator): support emitting decorator metadata #9057
Conversation
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
e58bd9b
to
a2aad51
Compare
ff8896e
to
2143049
Compare
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
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.