Skip to content

Commit

Permalink
fix(es/codegen): Fix codegen of ts specific syntax in class props (#8426
Browse files Browse the repository at this point in the history
)

**Description:**

This fixes the codegen of private properties with `is_optional` or `definite` and class properties with `definite`.
  • Loading branch information
dsherret authored Dec 17, 2023
1 parent 4bd1590 commit 7566ddf
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 9 deletions.
9 changes: 9 additions & 0 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,12 @@ where

emit!(n.key);
if let Some(type_ann) = &n.type_ann {
if n.is_optional {
punct!("?");
}
if n.definite {
punct!("!");
}
punct!(":");
space!();
emit!(type_ann);
Expand Down Expand Up @@ -1583,6 +1589,9 @@ where
emit!(n.key);

if let Some(ty) = &n.type_ann {
if n.definite {
punct!("!");
}
punct!(":");
space!();
emit!(ty);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class MyClass {
prop1?: string;
prop2!: string;
#prop3?: string;
#prop4?: string = "test";
static readonly prop5!: string;
readonly #prop6 = "asdf";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class MyClass {
prop1?: string;
prop2!: string;
#prop3?: string;
#prop4?: string = "test";
static readonly prop5!: string;
readonly #prop6 = "asdf";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class MyClass{prop1?: string;prop2!: string;#prop3?: string;#prop4?: string="test";static readonly prop5!: string;readonly #prop6="asdf"}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ enum MyEnum {
y = "yyy"
}
class Xpto {
value: MyEnum;
value!: MyEnum;
}
_ts_decorate([
Decorator(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class User {
currency: "usd" | "eur" | "yen";
currency!: "usd" | "eur" | "yen";
}
_ts_decorate([
column(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export class Product extends TimestampedEntity {
id: string;
price: number;
type: ProductType;
productEntityId: string;
/* ANCHOR: Relations ------------------------------------------------------ */ orders: Order[];
discounts: Discount[];
id!: string;
price!: number;
type!: ProductType;
productEntityId!: string;
/* ANCHOR: Relations ------------------------------------------------------ */ orders!: Order[];
discounts!: Discount[];
}
_ts_decorate([
PrimaryGeneratedColumn("uuid")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class Product extends TimestampedEntity {
id: string;
id!: string;
}
_ts_decorate([
PrimaryGeneratedColumn("uuid")
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_transforms_typescript/src/strip_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ impl VisitMut for StripType {
prop.readonly = false;
prop.is_override = false;
prop.is_optional = false;
prop.definite = false;
prop.accessibility = None;
prop.visit_mut_children_with(self);
}
Expand All @@ -152,6 +153,7 @@ impl VisitMut for StripType {
prop.readonly = false;
prop.is_override = false;
prop.is_optional = false;
prop.definite = false;
prop.accessibility = None;
prop.visit_mut_children_with(self);
}
Expand Down

0 comments on commit 7566ddf

Please sign in to comment.