diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index 0cde216bc749..2810bf824f14 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -1466,6 +1466,12 @@ where formatting_space!(); } } + + if n.is_override { + keyword!("override"); + space!() + } + match n.kind { MethodKind::Method => { if n.function.is_async { @@ -1544,6 +1550,11 @@ where space!(); } + if n.is_override { + keyword!("override"); + space!() + } + if n.readonly { keyword!("readonly"); space!(); @@ -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!() diff --git a/crates/swc_ecma_codegen/tests/fixture/typescript/class_method/input.js b/crates/swc_ecma_codegen/tests/fixture/typescript/class_method/input.js new file mode 100644 index 000000000000..ab6c0a8e967f --- /dev/null +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/class_method/input.js @@ -0,0 +1,4 @@ +class MyClass extends Base { + public override method(param: number): string { + } +} diff --git a/crates/swc_ecma_codegen/tests/fixture/typescript/class_method/output.js b/crates/swc_ecma_codegen/tests/fixture/typescript/class_method/output.js new file mode 100644 index 000000000000..424647169da9 --- /dev/null +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/class_method/output.js @@ -0,0 +1,3 @@ +class MyClass extends Base { + public override method(param: number): string {} +} diff --git a/crates/swc_ecma_codegen/tests/fixture/typescript/class_method/output.min.js b/crates/swc_ecma_codegen/tests/fixture/typescript/class_method/output.min.js new file mode 100644 index 000000000000..6c4f4b3e3e11 --- /dev/null +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/class_method/output.min.js @@ -0,0 +1 @@ +class MyClass extends Base{public override method(param:number):string{}} diff --git a/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/input.js b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/input.js index b5325b3ca63e..f94757605eba 100644 --- a/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/input.js +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/input.js @@ -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; } diff --git a/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.js b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.js index b5325b3ca63e..f94757605eba 100644 --- a/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.js +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.js @@ -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; } diff --git a/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.min.js b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.min.js index 4d6c0686d641..60f19b05552f 100644 --- a/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.min.js +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.min.js @@ -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} diff --git a/crates/swc_ecma_parser/tests/typescript/class/override/input.ts b/crates/swc_ecma_parser/tests/typescript/class/override/input.ts new file mode 100644 index 000000000000..554c39ed3e41 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/class/override/input.ts @@ -0,0 +1,5 @@ +class C extends B { + override prop = 5; + override method() { + } +} \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/typescript/class/override/input.ts.json b/crates/swc_ecma_parser/tests/typescript/class/override/input.ts.json new file mode 100644 index 000000000000..14b5ea575a4b --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/class/override/input.ts.json @@ -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 +} diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts index 224206c1f376..0f94a1b1115a 100644 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts @@ -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") diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts index 1642c73ce823..0fa6a46a6066 100644 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts @@ -1,5 +1,5 @@ export class Product extends TimestampedEntity { - id!: string; + public id!: string; } _ts_decorate([ PrimaryGeneratedColumn("uuid") diff --git a/crates/swc_ecma_transforms_typescript/src/strip_type.rs b/crates/swc_ecma_transforms_typescript/src/strip_type.rs index b62a9b145774..3407ca4cbc68 100644 --- a/crates/swc_ecma_transforms_typescript/src/strip_type.rs +++ b/crates/swc_ecma_transforms_typescript/src/strip_type.rs @@ -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); }