Skip to content

Commit

Permalink
fix(es/codegen): Emit ? for an optional computed property (#8481)
Browse files Browse the repository at this point in the history
**Description:**

Emits optional `?` for a computed property.
  • Loading branch information
dsherret authored Jan 4, 2024
1 parent 8997ed1 commit e0bdc0f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
14 changes: 11 additions & 3 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,10 +1566,12 @@ where
}

emit!(n.key);

if n.is_optional {
punct!("?");
}

if let Some(type_ann) = &n.type_ann {
if n.is_optional {
punct!("?");
}
if n.definite {
punct!("!");
}
Expand Down Expand Up @@ -1635,6 +1637,12 @@ where

emit!(n.key);

// emit for a computed property, but not an identifier already marked as
// optional
if n.is_optional && !n.key.as_ident().map(|i| i.optional).unwrap_or(false) {
punct!("?");
}

if let Some(ty) = &n.type_ann {
if n.definite {
punct!("!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ class MyClass extends Base {
prop2!: string;
#prop3?: string;
#prop4?: string = "test";
#privateOptionalNoType?;
static readonly prop5!: string;
readonly #prop6 = "asdf";
public abstract override readonly prop7 = 5;
override readonly #prop8 = 5;
declare public static readonly prop9: string;
[value]?: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ class MyClass extends Base {
prop2!: string;
#prop3?: string;
#prop4?: string = "test";
#privateOptionalNoType?;
static readonly prop5!: string;
readonly #prop6 = "asdf";
public abstract override readonly prop7 = 5;
override readonly #prop8 = 5;
declare public static readonly prop9: string;
[value]?: string[];
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e0bdc0f

Please sign in to comment.