Skip to content

Commit

Permalink
fix(es/ast): Add definite and is_override to AutoAccessor (#8436)
Browse files Browse the repository at this point in the history
**Description:**

Adds a `definite` and `is_override` property to `AutoAccessor` as they were missing.
It also fixes codegen for `AutoAccessor` when emitting TypeScript (which is the main reason I need this PR).

**BREAKING CHANGE:**

Adds a `definite` and `is_override` property.

**Related issue:**

 - Closes #8344
  • Loading branch information
dsherret authored and kdy1 committed Jan 21, 2024
1 parent c104f64 commit 572bcae
Show file tree
Hide file tree
Showing 24 changed files with 633 additions and 62 deletions.
8 changes: 8 additions & 0 deletions crates/swc_ecma_ast/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ pub struct AutoAccessor {
/// Typescript extension.
#[cfg_attr(feature = "serde-impl", serde(default))]
pub accessibility: Option<Accessibility>,

#[cfg_attr(feature = "serde-impl", serde(default))]
pub is_override: bool,

#[cfg_attr(feature = "serde-impl", serde(default))]
pub definite: bool,
}

impl Take for AutoAccessor {
Expand All @@ -343,6 +349,8 @@ impl Take for AutoAccessor {
is_static: false,
decorators: Take::dummy(),
accessibility: None,
is_override: false,
definite: false,
}
}
}
16 changes: 16 additions & 0 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,16 +1353,32 @@ where
fn emit_auto_accessor(&mut self, n: &AutoAccessor) -> Result {
self.emit_list(n.span, Some(&n.decorators), ListFormat::Decorators)?;

self.emit_accessibility(n.accessibility)?;

if n.is_static {
keyword!("static");
space!();
}

if n.is_override {
keyword!("override");
space!();
}

keyword!("accessor");
space!();

emit!(n.key);

if let Some(type_ann) = &n.type_ann {
if n.definite {
punct!("!");
}
punct!(":");
space!();
emit!(type_ann);
}

if let Some(init) = &n.value {
formatting_space!();
punct!("=");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ class MyClass extends Base {
public abstract override readonly prop7 = 5;
override readonly #prop8 = 5;
declare public static readonly prop9: string;
accessor prop10!: string = "";
public accessor prop11 = "";
public static accessor prop12 = "";
public prop13: string;
private static prop14: string;
protected override accessor prop15 = 5;
[value]?: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ class MyClass extends Base {
public abstract override readonly prop7 = 5;
override readonly #prop8 = 5;
declare public static readonly prop9: string;
accessor prop10!: string = "";
public accessor prop11 = "";
public static accessor prop12 = "";
public prop13: string;
private static prop14: string;
protected override accessor prop15 = 5;
[value]?: string[];
}

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

2 changes: 2 additions & 0 deletions crates/swc_ecma_parser/src/parser/class_and_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,8 @@ impl<I: Tokens> Parser<I> {
is_static,
decorators,
accessibility,
is_override,
definite,
}));
}

Expand Down
16 changes: 12 additions & 4 deletions crates/swc_ecma_parser/tests/tsc/autoAccessor1.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
},
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "AutoAccessor",
Expand Down Expand Up @@ -95,7 +97,9 @@
"typeAnnotation": null,
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "AutoAccessor",
Expand Down Expand Up @@ -134,7 +138,9 @@
},
"isStatic": true,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "AutoAccessor",
Expand Down Expand Up @@ -166,7 +172,9 @@
"typeAnnotation": null,
"isStatic": true,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
}
],
"superClass": null,
Expand Down
20 changes: 15 additions & 5 deletions crates/swc_ecma_parser/tests/tsc/autoAccessor10.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@
"typeAnnotation": null,
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
}
],
"superClass": null,
Expand Down Expand Up @@ -159,7 +161,9 @@
"typeAnnotation": null,
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
}
],
"superClass": null,
Expand Down Expand Up @@ -297,7 +301,9 @@
"typeAnnotation": null,
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "StaticBlock",
Expand Down Expand Up @@ -429,7 +435,9 @@
"typeAnnotation": null,
"isStatic": true,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
}
],
"superClass": null,
Expand Down Expand Up @@ -488,7 +496,9 @@
"typeAnnotation": null,
"isStatic": true,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
}
],
"superClass": null,
Expand Down
16 changes: 12 additions & 4 deletions crates/swc_ecma_parser/tests/tsc/autoAccessor2.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
},
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "AutoAccessor",
Expand Down Expand Up @@ -111,7 +113,9 @@
"typeAnnotation": null,
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "AutoAccessor",
Expand Down Expand Up @@ -158,7 +162,9 @@
},
"isStatic": true,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "AutoAccessor",
Expand Down Expand Up @@ -198,7 +204,9 @@
"typeAnnotation": null,
"isStatic": true,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "Constructor",
Expand Down
16 changes: 12 additions & 4 deletions crates/swc_ecma_parser/tests/tsc/autoAccessor3.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
},
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "AutoAccessor",
Expand Down Expand Up @@ -95,7 +97,9 @@
"typeAnnotation": null,
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "AutoAccessor",
Expand Down Expand Up @@ -134,7 +138,9 @@
},
"isStatic": true,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "AutoAccessor",
Expand Down Expand Up @@ -166,7 +172,9 @@
"typeAnnotation": null,
"isStatic": true,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
}
],
"superClass": null,
Expand Down
16 changes: 12 additions & 4 deletions crates/swc_ecma_parser/tests/tsc/autoAccessor4.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
},
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "AutoAccessor",
Expand Down Expand Up @@ -95,7 +97,9 @@
"typeAnnotation": null,
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "AutoAccessor",
Expand Down Expand Up @@ -134,7 +138,9 @@
},
"isStatic": true,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
},
{
"type": "AutoAccessor",
Expand Down Expand Up @@ -166,7 +172,9 @@
"typeAnnotation": null,
"isStatic": true,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
}
],
"superClass": null,
Expand Down
4 changes: 3 additions & 1 deletion crates/swc_ecma_parser/tests/tsc/autoAccessor6.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
},
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
}
],
"superClass": null,
Expand Down
8 changes: 6 additions & 2 deletions crates/swc_ecma_parser/tests/tsc/autoAccessor7.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
},
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
}
],
"superClass": null,
Expand Down Expand Up @@ -122,7 +124,9 @@
"typeAnnotation": null,
"isStatic": false,
"decorators": [],
"accessibility": null
"accessibility": null,
"isOverride": false,
"definite": false
}
],
"superClass": {
Expand Down
Loading

0 comments on commit 572bcae

Please sign in to comment.