Skip to content

Commit 0dd96f8

Browse files
authoredDec 24, 2023
fix(es/codegen): Emit override keyword (#8449)
**Description:** Emits the `override` keyword.
1 parent 5dfd0dc commit 0dd96f8

File tree

12 files changed

+176
-13
lines changed

12 files changed

+176
-13
lines changed
 

‎crates/swc_ecma_codegen/src/lib.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,12 @@ where
14661466
formatting_space!();
14671467
}
14681468
}
1469+
1470+
if n.is_override {
1471+
keyword!("override");
1472+
space!()
1473+
}
1474+
14691475
match n.kind {
14701476
MethodKind::Method => {
14711477
if n.function.is_async {
@@ -1544,6 +1550,11 @@ where
15441550
space!();
15451551
}
15461552

1553+
if n.is_override {
1554+
keyword!("override");
1555+
space!()
1556+
}
1557+
15471558
if n.readonly {
15481559
keyword!("readonly");
15491560
space!();
@@ -1590,15 +1601,18 @@ where
15901601
emit!(dec)
15911602
}
15921603

1593-
if n.accessibility != Some(Accessibility::Public) {
1594-
self.emit_accessibility(n.accessibility)?;
1595-
}
1604+
self.emit_accessibility(n.accessibility)?;
15961605

15971606
if n.is_static {
15981607
keyword!("static");
15991608
space!();
16001609
}
16011610

1611+
if n.is_override {
1612+
keyword!("override");
1613+
space!()
1614+
}
1615+
16021616
if n.readonly {
16031617
keyword!("readonly");
16041618
space!()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class MyClass extends Base {
2+
public override method(param: number): string {
3+
}
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class MyClass extends Base {
2+
public override method(param: number): string {}
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class MyClass extends Base{public override method(param:number):string{}}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
class MyClass {
1+
class MyClass extends Base {
22
prop1?: string;
33
prop2!: string;
44
#prop3?: string;
55
#prop4?: string = "test";
66
static readonly prop5!: string;
77
readonly #prop6 = "asdf";
8+
public override readonly prop7 = 5;
9+
override readonly #prop8 = 5;
810
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
class MyClass {
1+
class MyClass extends Base {
22
prop1?: string;
33
prop2!: string;
44
#prop3?: string;
55
#prop4?: string = "test";
66
static readonly prop5!: string;
77
readonly #prop6 = "asdf";
8+
public override readonly prop7 = 5;
9+
override readonly #prop8 = 5;
810
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
class MyClass{prop1?: string;prop2!: string;#prop3?: string;#prop4?: string="test";static readonly prop5!: string;readonly #prop6="asdf"}
1+
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 numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class C extends B {
2+
override prop = 5;
3+
override method() {
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
"type": "Script",
3+
"span": {
4+
"start": 1,
5+
"end": 69,
6+
"ctxt": 0
7+
},
8+
"body": [
9+
{
10+
"type": "ClassDeclaration",
11+
"identifier": {
12+
"type": "Identifier",
13+
"span": {
14+
"start": 7,
15+
"end": 8,
16+
"ctxt": 0
17+
},
18+
"value": "C",
19+
"optional": false
20+
},
21+
"declare": false,
22+
"span": {
23+
"start": 1,
24+
"end": 69,
25+
"ctxt": 0
26+
},
27+
"decorators": [],
28+
"body": [
29+
{
30+
"type": "ClassProperty",
31+
"span": {
32+
"start": 23,
33+
"end": 41,
34+
"ctxt": 0
35+
},
36+
"key": {
37+
"type": "Identifier",
38+
"span": {
39+
"start": 32,
40+
"end": 36,
41+
"ctxt": 0
42+
},
43+
"value": "prop",
44+
"optional": false
45+
},
46+
"value": {
47+
"type": "NumericLiteral",
48+
"span": {
49+
"start": 39,
50+
"end": 40,
51+
"ctxt": 0
52+
},
53+
"value": 5.0,
54+
"raw": "5"
55+
},
56+
"typeAnnotation": null,
57+
"isStatic": false,
58+
"decorators": [],
59+
"accessibility": null,
60+
"isAbstract": false,
61+
"isOptional": false,
62+
"isOverride": true,
63+
"readonly": false,
64+
"declare": false,
65+
"definite": false
66+
},
67+
{
68+
"type": "ClassMethod",
69+
"span": {
70+
"start": 44,
71+
"end": 67,
72+
"ctxt": 0
73+
},
74+
"key": {
75+
"type": "Identifier",
76+
"span": {
77+
"start": 53,
78+
"end": 59,
79+
"ctxt": 0
80+
},
81+
"value": "method",
82+
"optional": false
83+
},
84+
"function": {
85+
"params": [],
86+
"decorators": [],
87+
"span": {
88+
"start": 44,
89+
"end": 67,
90+
"ctxt": 0
91+
},
92+
"body": {
93+
"type": "BlockStatement",
94+
"span": {
95+
"start": 62,
96+
"end": 67,
97+
"ctxt": 0
98+
},
99+
"stmts": []
100+
},
101+
"generator": false,
102+
"async": false,
103+
"typeParameters": null,
104+
"returnType": null
105+
},
106+
"kind": "method",
107+
"isStatic": false,
108+
"accessibility": null,
109+
"isAbstract": false,
110+
"isOptional": false,
111+
"isOverride": true
112+
}
113+
],
114+
"superClass": {
115+
"type": "Identifier",
116+
"span": {
117+
"start": 17,
118+
"end": 18,
119+
"ctxt": 0
120+
},
121+
"value": "B",
122+
"optional": false
123+
},
124+
"isAbstract": false,
125+
"typeParams": null,
126+
"superTypeParams": null,
127+
"implements": []
128+
}
129+
],
130+
"interpreter": null
131+
}

‎crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
export class Product extends TimestampedEntity {
2-
id!: string;
3-
price!: number;
4-
type!: ProductType;
5-
productEntityId!: string;
6-
/* ANCHOR: Relations ------------------------------------------------------ */ orders!: Order[];
7-
discounts!: Discount[];
2+
public id!: string;
3+
public price!: number;
4+
public type!: ProductType;
5+
public productEntityId!: string;
6+
/* ANCHOR: Relations ------------------------------------------------------ */ public orders!: Order[];
7+
public discounts!: Discount[];
88
}
99
_ts_decorate([
1010
PrimaryGeneratedColumn("uuid")

‎crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export class Product extends TimestampedEntity {
2-
id!: string;
2+
public id!: string;
33
}
44
_ts_decorate([
55
PrimaryGeneratedColumn("uuid")

‎crates/swc_ecma_transforms_typescript/src/strip_type.rs

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ impl VisitMut for StripType {
137137

138138
fn visit_mut_class_method(&mut self, n: &mut ClassMethod) {
139139
n.accessibility = None;
140+
n.is_override = false;
140141
n.visit_mut_children_with(self);
141142
}
142143

0 commit comments

Comments
 (0)
Please sign in to comment.