fix(es/decorators): Fix metadata for accessors (#9444)
Some checks are pending
CI / Cargo fmt (push) Waiting to run
CI / Cargo clippy (push) Waiting to run
CI / Check license of dependencies (push) Waiting to run
CI / Check (macos-latest) (push) Waiting to run
CI / Check (ubuntu-latest) (push) Waiting to run
CI / Check (windows-latest) (push) Waiting to run
CI / Test wasm (binding_core_wasm) (push) Waiting to run
CI / Test wasm (binding_minifier_wasm) (push) Waiting to run
CI / Test wasm (binding_typescript_wasm) (push) Waiting to run
CI / List crates (push) Waiting to run
CI / Test - ${{ matrix.settings.crate }} - ${{ matrix.settings.os }} (push) Blocked by required conditions
CI / Test node bindings - ${{ matrix.os }} (macos-latest) (push) Waiting to run
CI / Test node bindings - ${{ matrix.os }} (windows-latest) (push) Waiting to run
CI / Test with @swc/cli (push) Waiting to run
CI / Miri (better_scoped_tls) (push) Waiting to run
CI / Miri (string_enum) (push) Waiting to run
CI / Miri (swc) (push) Waiting to run
CI / Miri (swc_bundler) (push) Waiting to run
CI / Miri (swc_ecma_codegen) (push) Waiting to run
CI / Miri (swc_ecma_minifier) (push) Waiting to run
CI / Done (push) Blocked by required conditions
Benchmark / Bench everything (push) Waiting to run

**Related issue:**
 - Closes https://github.com/swc-project/swc/issues/9435
This commit is contained in:
Austaras 2024-08-18 21:27:20 +08:00 committed by GitHub
parent 9d10fa4e7c
commit 99738ef412
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 141 additions and 3 deletions

View File

@ -0,0 +1,6 @@
---
swc_ecma_transforms_proposal: patch
swc_core: patch
---
fix(es/proposal): Metadata for accessor

View File

@ -0,0 +1,44 @@
import { Entity, Column } from "typeorm";
import { Field, ObjectType, registerEnumType } from "type-graphql";
import { Enum1, Enum2 } from "./enum.js";
export enum Enum3 {
A = "A",
B = "B",
C = "C",
D = "D",
}
registerEnumType(Enum3, { name: "Enum3" });
@Entity("user", { schema: "public" })
@ObjectType("User")
export class User {
@Column({ name: "first_name" })
@Field({ nullable: true })
firstName?: string;
@Column({ name: "last_name" })
@Field({ nullable: true })
lastName?: string;
@Field()
get fullName(): string {
if (!this.firstName && !this.lastName) {
return "";
}
return `${this.firstName} ${this.lastName}`.trim();
}
@Column()
@Field(() => Enum1)
enum1: Enum1;
@Column()
@Field(() => Enum2)
enum2: Enum2;
@Column()
@Field(() => Enum3)
enum3: Enum3;
}

View File

@ -0,0 +1,69 @@
import { Entity, Column } from "typeorm";
import { Field, ObjectType, registerEnumType } from "type-graphql";
import { Enum1, Enum2 } from "./enum.js";
export enum Enum3 {
A = "A",
B = "B",
C = "C",
D = "D"
}
registerEnumType(Enum3, {
name: "Enum3"
});
export class User {
firstName?: string;
lastName?: string;
get fullName(): string {
if (!this.firstName && !this.lastName) {
return "";
}
return `${this.firstName} ${this.lastName}`.trim();
}
enum1: Enum1;
enum2: Enum2;
enum3: Enum3;
}
_ts_decorate([
Column({
name: "first_name"
}),
Field({
nullable: true
}),
_ts_metadata("design:type", String)
], User.prototype, "firstName", void 0);
_ts_decorate([
Column({
name: "last_name"
}),
Field({
nullable: true
}),
_ts_metadata("design:type", String)
], User.prototype, "lastName", void 0);
_ts_decorate([
Field(),
_ts_metadata("design:type", String),
_ts_metadata("design:paramtypes", [])
], User.prototype, "fullName", null);
_ts_decorate([
Column(),
Field(()=>Enum1),
_ts_metadata("design:type", typeof Enum1 === "undefined" ? Object : Enum1)
], User.prototype, "enum1", void 0);
_ts_decorate([
Column(),
Field(()=>Enum2),
_ts_metadata("design:type", typeof Enum2 === "undefined" ? Object : Enum2)
], User.prototype, "enum2", void 0);
_ts_decorate([
Column(),
Field(()=>Enum3),
_ts_metadata("design:type", String)
], User.prototype, "enum3", void 0);
User = _ts_decorate([
Entity("user", {
schema: "public"
}),
ObjectType("User")
], User);

View File

@ -192,8 +192,25 @@ impl VisitMut for Metadata<'_> {
}
{
let dec = self
.create_metadata_design_decorator("design:type", quote_ident!("Function").as_arg());
let type_arg = match m.kind {
MethodKind::Method => quote_ident!("Function").as_arg(),
MethodKind::Getter => {
let return_type = m.function.return_type.as_deref();
if let Some(kind) = self.enums.get_kind_as_str(return_type) {
quote_ident!(kind).as_arg()
} else {
serialize_type(self.class_name, return_type).as_arg()
}
}
MethodKind::Setter => serialize_type(
self.class_name,
get_type_ann_of_pat(&m.function.params[0].pat),
)
.as_arg(),
};
let dec = self.create_metadata_design_decorator("design:type", type_arg);
m.function.decorators.push(dec);
}
{
@ -217,7 +234,9 @@ impl VisitMut for Metadata<'_> {
);
m.function.decorators.push(dec);
}
{
// https://github.com/microsoft/TypeScript/blob/2a8865e6ba95c9bdcdb9e2c9c08f10c5f5c75391/src/compiler/transformers/ts.ts#L1180
if m.kind == MethodKind::Method {
// Copy tsc behaviour
// https://github.com/microsoft/TypeScript/blob/5e8c261b6ab746213f19ee3501eb8c48a6215dd7/src/compiler/transformers/typeSerializer.ts#L242
let dec = self.create_metadata_design_decorator(