From 4c9973010a5c2b865e6ed676a4e0364234cf5d88 Mon Sep 17 00:00:00 2001 From: Dion <39664950+Dionysusnu@users.noreply.github.com> Date: Fri, 15 Apr 2022 10:00:04 +0200 Subject: [PATCH] fix(es/codegen): Fix codegen of arrow expressions (#4306) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Donny/강동윤 --- crates/swc_ecma_codegen/src/lib.rs | 7 +++++++ .../output.ts | 4 ++-- .../output.ts | 4 ++-- .../output.ts | 4 ++-- .../output.ts | 2 +- .../output.ts | 2 +- crates/swc_ecma_transforms_typescript/tests/strip.rs | 8 ++++---- 7 files changed, 19 insertions(+), 12 deletions(-) diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index a471a27bd1f..fa2f72297ad 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -940,6 +940,13 @@ where punct!(")"); } + if let Some(ty) = &node.return_type { + punct!(":"); + formatting_space!(); + emit!(ty); + formatting_space!(); + } + punct!("=>"); emit!(node.body); diff --git a/crates/swc_ecma_transforms_base/tests/ts-resolver/ts_type_parameter_used_as_type_parameter_contraint_01/output.ts b/crates/swc_ecma_transforms_base/tests/ts-resolver/ts_type_parameter_used_as_type_parameter_contraint_01/output.ts index c8c644b02ab..04771f6f2c2 100644 --- a/crates/swc_ecma_transforms_base/tests/ts-resolver/ts_type_parameter_used_as_type_parameter_contraint_01/output.ts +++ b/crates/swc_ecma_transforms_base/tests/ts-resolver/ts_type_parameter_used_as_type_parameter_contraint_01/output.ts @@ -1,6 +1,6 @@ var f3 = (x__2: T__2, y__2: U__2)=>{ function bar__2() { - var g__3 = (a__4: X__4, b__4: Y__4)=>{ + var g__3 = (a__4: X__4, b__4: Y__4): T__2 =>{ x__2 = y__2; return y__2; }; @@ -8,7 +8,7 @@ var f3 = (x__2: T__2, y__2: U__2)=>{ }; var f4 = (x__5: T__5, y__5: U__5)=>{ function bar__5() { - var g__6 = (a__7: X__7, b__7: Y__7)=>{ + var g__6 = (a__7: X__7, b__7: Y__7): T__5 =>{ x__5 = y__5; return y__5; }; diff --git a/crates/swc_ecma_transforms_base/tests/ts-resolver/tsc/typeParameterUsedAsTypeParameterConstraint4.ts/output.ts b/crates/swc_ecma_transforms_base/tests/ts-resolver/tsc/typeParameterUsedAsTypeParameterConstraint4.ts/output.ts index 461ced8a32a..feccb925b8c 100644 --- a/crates/swc_ecma_transforms_base/tests/ts-resolver/tsc/typeParameterUsedAsTypeParameterConstraint4.ts/output.ts +++ b/crates/swc_ecma_transforms_base/tests/ts-resolver/tsc/typeParameterUsedAsTypeParameterConstraint4.ts/output.ts @@ -29,7 +29,7 @@ function foo2(x__9: T__9, y__9: U__9): W { } var f3 = (x__12: T__12, y__12: U__12)=>{ function bar__12(r__13: X, s__13: Y) { - var g__13 = (a__14: X__14, b__14: Y__14)=>{ + var g__13 = (a__14: X__14, b__14: Y__14): T__12 =>{ x__12 = y__12; return y__12; }; @@ -37,7 +37,7 @@ var f3 = (x__12: T__12, y__12: U__12)=>{ }; var f4 = (x__15: V, y__15: X)=>{ function bar__15() { - var g__16 = (a__17: X__17, b__17: Y__17)=>{ + var g__16 = (a__17: X__17, b__17: Y__17): T__15 =>{ x__15 = y__15; return y__15; }; diff --git a/crates/swc_ecma_transforms_base/tests/ts-resolver/type_parameter_used_as_type_parameter_contrain/output.ts b/crates/swc_ecma_transforms_base/tests/ts-resolver/type_parameter_used_as_type_parameter_contrain/output.ts index 3f34fdab471..83c0587d660 100644 --- a/crates/swc_ecma_transforms_base/tests/ts-resolver/type_parameter_used_as_type_parameter_contrain/output.ts +++ b/crates/swc_ecma_transforms_base/tests/ts-resolver/type_parameter_used_as_type_parameter_contrain/output.ts @@ -14,11 +14,11 @@ var f2 = function(x__5: T__5, y__5: U__5): T__5 { x__5 = y__5; return y__5; }; -var f3 = (x__6: T__6, y__6: U__6)=>{ +var f3 = (x__6: T__6, y__6: U__6): T__6 =>{ x__6 = y__6; return y__6; }; -var f4 = (x__7: T__7, y__7: U__7)=>{ +var f4 = (x__7: T__7, y__7: U__7): T__7 =>{ x__7 = y__7; return y__7; }; diff --git a/crates/swc_ecma_transforms_base/tests/ts-resolver/type_parameter_used_as_type_parameter_contrain_2/output.ts b/crates/swc_ecma_transforms_base/tests/ts-resolver/type_parameter_used_as_type_parameter_contrain_2/output.ts index 7e8fdb6cbcb..c9dbdc5b2de 100644 --- a/crates/swc_ecma_transforms_base/tests/ts-resolver/type_parameter_used_as_type_parameter_contrain_2/output.ts +++ b/crates/swc_ecma_transforms_base/tests/ts-resolver/type_parameter_used_as_type_parameter_contrain_2/output.ts @@ -1,4 +1,4 @@ -var f3 = (x__2: T__2, y__2: U__2)=>{ +var f3 = (x__2: T__2, y__2: U__2): T__2 =>{ x__2 = y__2; return y__2; }; diff --git a/crates/swc_ecma_transforms_base/tests/ts-resolver/type_parameter_used_as_type_parameter_contrain_3/output.ts b/crates/swc_ecma_transforms_base/tests/ts-resolver/type_parameter_used_as_type_parameter_contrain_3/output.ts index 1d1b24631f1..f81b1befea3 100644 --- a/crates/swc_ecma_transforms_base/tests/ts-resolver/type_parameter_used_as_type_parameter_contrain_3/output.ts +++ b/crates/swc_ecma_transforms_base/tests/ts-resolver/type_parameter_used_as_type_parameter_contrain_3/output.ts @@ -1,4 +1,4 @@ -var f4 = (x__2: T__2, y__2: U__2)=>{ +var f4 = (x__2: T__2, y__2: U__2): T__2 =>{ x__2 = y__2; return y__2; }; diff --git a/crates/swc_ecma_transforms_typescript/tests/strip.rs b/crates/swc_ecma_transforms_typescript/tests/strip.rs index 990dd08dbed..d7945e44534 100644 --- a/crates/swc_ecma_transforms_typescript/tests/strip.rs +++ b/crates/swc_ecma_transforms_typescript/tests/strip.rs @@ -2114,7 +2114,7 @@ export default (identifier: string, level = 0, b = "", m = false) => { const proseTypes = new Map(); // deno-lint-ignore ban-types - const prose = (l, i, nl, bp): string => { + const prose = (l, i, nl, bp) => { return i(l) + bp + "prose {" + nl + i(l + 1) + "color: #374151;" + nl + i(l + 1) + "max-width: 65ch;" + nl + @@ -2407,7 +2407,7 @@ export default (identifier: string, level = 0, b = "", m = false) => { proseTypes.set("prose", prose); // deno-lint-ignore ban-types - const proseSm = (l, i, nl, bp): string => { + const proseSm = (l, i, nl, bp) => { return i(l) + bp + "prose-sm {" + nl + i(l + 1) + "font-size: 0.875rem;" + nl + i(l + 1) + "line-height: 1.7142857;" + nl + @@ -2595,7 +2595,7 @@ export default (identifier: string, level = 0, b = "", m = false) => { proseTypes.set("prose-sm", proseSm); // deno-lint-ignore ban-types - const proseLg = (l, i, nl, bp): string => { + const proseLg = (l, i, nl, bp) => { return i(l) + bp + "prose-lg {" + nl + i(l + 1) + "font-size: 1.125rem;" + nl + i(l + 1) + "line-height: 1.7777778;" + nl + @@ -2783,7 +2783,7 @@ export default (identifier: string, level = 0, b = "", m = false) => { proseTypes.set("prose-lg", proseLg); // deno-lint-ignore ban-types - const proseXl = (l, i, nl, bp): string => { + const proseXl = (l, i, nl, bp) => { return i(l) + bp + "prose-xl {" + nl + i(l + 1) + "font-size: 1.25rem;" + nl + i(l + 1) + "line-height: 1.8;" + nl +