Skip to content

Commit e0bdc0f

Browse files
authoredJan 4, 2024
fix(es/codegen): Emit ? for an optional computed property (#8481)
**Description:** Emits optional `?` for a computed property.
1 parent 8997ed1 commit e0bdc0f

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed
 

‎crates/swc_ecma_codegen/src/lib.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1566,10 +1566,12 @@ where
15661566
}
15671567

15681568
emit!(n.key);
1569+
1570+
if n.is_optional {
1571+
punct!("?");
1572+
}
1573+
15691574
if let Some(type_ann) = &n.type_ann {
1570-
if n.is_optional {
1571-
punct!("?");
1572-
}
15731575
if n.definite {
15741576
punct!("!");
15751577
}
@@ -1635,6 +1637,12 @@ where
16351637

16361638
emit!(n.key);
16371639

1640+
// emit for a computed property, but not an identifier already marked as
1641+
// optional
1642+
if n.is_optional && !n.key.as_ident().map(|i| i.optional).unwrap_or(false) {
1643+
punct!("?");
1644+
}
1645+
16381646
if let Some(ty) = &n.type_ann {
16391647
if n.definite {
16401648
punct!("!");

‎crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/input.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ class MyClass extends Base {
33
prop2!: string;
44
#prop3?: string;
55
#prop4?: string = "test";
6+
#privateOptionalNoType?;
67
static readonly prop5!: string;
78
readonly #prop6 = "asdf";
89
public abstract override readonly prop7 = 5;
910
override readonly #prop8 = 5;
1011
declare public static readonly prop9: string;
12+
[value]?: string[];
1113
}

‎crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ class MyClass extends Base {
33
prop2!: string;
44
#prop3?: string;
55
#prop4?: string = "test";
6+
#privateOptionalNoType?;
67
static readonly prop5!: string;
78
readonly #prop6 = "asdf";
89
public abstract override readonly prop7 = 5;
910
override readonly #prop8 = 5;
1011
declare public static readonly prop9: string;
12+
[value]?: string[];
1113
}

‎crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.