Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/codegen): Emit override keyword #8449

Merged
merged 4 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got a bit confused because the override keyword didn't show up expect when doing extends. This seems ok, but probably the AST should just represent the code as-is.

}
}
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
}
Loading