Skip to content

Commit

Permalink
fix(es/codegen): Emit override keyword (#8449)
Browse files Browse the repository at this point in the history
**Description:**

Emits the `override` keyword.
  • Loading branch information
dsherret authored Dec 24, 2023
1 parent 5dfd0dc commit 0dd96f8
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 13 deletions.
20 changes: 17 additions & 3 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,12 @@ where
formatting_space!();
}
}

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

match n.kind {
MethodKind::Method => {
if n.function.is_async {
Expand Down Expand Up @@ -1544,6 +1550,11 @@ where
space!();
}

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

if n.readonly {
keyword!("readonly");
space!();
Expand Down Expand Up @@ -1590,15 +1601,18 @@ where
emit!(dec)
}

if n.accessibility != Some(Accessibility::Public) {
self.emit_accessibility(n.accessibility)?;
}
self.emit_accessibility(n.accessibility)?;

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

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

if n.readonly {
keyword!("readonly");
space!()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class MyClass extends Base {
public override method(param: number): string {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class MyClass extends Base {
public override method(param: number): string {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class MyClass extends Base{public override method(param:number):string{}}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class MyClass {
class MyClass extends Base {
prop1?: string;
prop2!: string;
#prop3?: string;
#prop4?: string = "test";
static readonly prop5!: string;
readonly #prop6 = "asdf";
public override readonly prop7 = 5;
override readonly #prop8 = 5;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class MyClass {
class MyClass extends Base {
prop1?: string;
prop2!: string;
#prop3?: string;
#prop4?: string = "test";
static readonly prop5!: string;
readonly #prop6 = "asdf";
public override readonly prop7 = 5;
override readonly #prop8 = 5;
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class MyClass{prop1?: string;prop2!: string;#prop3?: string;#prop4?: string="test";static readonly prop5!: string;readonly #prop6="asdf"}
class MyClass extends Base{prop1?: string;prop2!: string;#prop3?: string;#prop4?: string="test";static readonly prop5!: string;readonly #prop6="asdf";public override readonly prop7=5;override readonly #prop8=5}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class C extends B {
override prop = 5;
override method() {
}
}
131 changes: 131 additions & 0 deletions crates/swc_ecma_parser/tests/typescript/class/override/input.ts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"type": "Script",
"span": {
"start": 1,
"end": 69,
"ctxt": 0
},
"body": [
{
"type": "ClassDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 7,
"end": 8,
"ctxt": 0
},
"value": "C",
"optional": false
},
"declare": false,
"span": {
"start": 1,
"end": 69,
"ctxt": 0
},
"decorators": [],
"body": [
{
"type": "ClassProperty",
"span": {
"start": 23,
"end": 41,
"ctxt": 0
},
"key": {
"type": "Identifier",
"span": {
"start": 32,
"end": 36,
"ctxt": 0
},
"value": "prop",
"optional": false
},
"value": {
"type": "NumericLiteral",
"span": {
"start": 39,
"end": 40,
"ctxt": 0
},
"value": 5.0,
"raw": "5"
},
"typeAnnotation": null,
"isStatic": false,
"decorators": [],
"accessibility": null,
"isAbstract": false,
"isOptional": false,
"isOverride": true,
"readonly": false,
"declare": false,
"definite": false
},
{
"type": "ClassMethod",
"span": {
"start": 44,
"end": 67,
"ctxt": 0
},
"key": {
"type": "Identifier",
"span": {
"start": 53,
"end": 59,
"ctxt": 0
},
"value": "method",
"optional": false
},
"function": {
"params": [],
"decorators": [],
"span": {
"start": 44,
"end": 67,
"ctxt": 0
},
"body": {
"type": "BlockStatement",
"span": {
"start": 62,
"end": 67,
"ctxt": 0
},
"stmts": []
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": null
},
"kind": "method",
"isStatic": false,
"accessibility": null,
"isAbstract": false,
"isOptional": false,
"isOverride": true
}
],
"superClass": {
"type": "Identifier",
"span": {
"start": 17,
"end": 18,
"ctxt": 0
},
"value": "B",
"optional": false
},
"isAbstract": false,
"typeParams": null,
"superTypeParams": null,
"implements": []
}
],
"interpreter": null
}
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[];
public id!: string;
public price!: number;
public type!: ProductType;
public productEntityId!: string;
/* ANCHOR: Relations ------------------------------------------------------ */ public orders!: Order[];
public 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;
public id!: string;
}
_ts_decorate([
PrimaryGeneratedColumn("uuid")
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_typescript/src/strip_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ impl VisitMut for StripType {

fn visit_mut_class_method(&mut self, n: &mut ClassMethod) {
n.accessibility = None;
n.is_override = false;
n.visit_mut_children_with(self);
}

Expand Down

0 comments on commit 0dd96f8

Please sign in to comment.