From dac617de297a6334771438bc5b95e50d8929d4fa Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Sun, 30 Jun 2024 08:58:00 +0000 Subject: [PATCH] fix(codegen): print some missing typescript attributes (#3980) --- crates/oxc_codegen/src/gen.rs | 13 +++++++++++++ crates/oxc_codegen/tests/mod.rs | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index acaad30d46673..0559aaec1c8b4 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -2095,6 +2095,9 @@ impl<'a, const MINIFY: bool> Gen for Class<'a> { if let Some(id) = &self.id { p.print_hard_space(); id.gen(p, ctx); + if let Some(type_parameters) = self.type_parameters.as_ref() { + type_parameters.gen(p, ctx); + } } if let Some(super_class) = self.super_class.as_ref() { p.print_str(b" extends "); @@ -2444,6 +2447,9 @@ impl<'a, const MINIFY: bool> Gen for PropertyDefinition<'a> { if self.r#static { p.print_str(b"static "); } + if self.readonly { + p.print_str(b"readonly "); + } if self.computed { p.print(b'['); } @@ -2818,6 +2824,10 @@ impl<'a, const MINIFY: bool> Gen for TSMappedType<'a> { p.print_str(b" = "); default.gen(p, ctx); } + if let Some(name_type) = &self.name_type { + p.print_str(b" as "); + name_type.gen(p, ctx); + } p.print_str(b"]"); match self.optional { TSMappedTypeModifierOperator::True => { @@ -3255,6 +3265,9 @@ impl<'a, const MINIFY: bool> Gen for TSTupleElement<'a> { impl<'a, const MINIFY: bool> Gen for TSNamedTupleMember<'a> { fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { self.label.gen(p, ctx); + if self.optional { + p.print_str(b"?"); + } p.print_str(b":"); p.print_soft_space(); self.element_type.gen(p, ctx); diff --git a/crates/oxc_codegen/tests/mod.rs b/crates/oxc_codegen/tests/mod.rs index c7422ac9c3b54..ba8742becf6ea 100644 --- a/crates/oxc_codegen/tests/mod.rs +++ b/crates/oxc_codegen/tests/mod.rs @@ -189,6 +189,16 @@ fn typescript() { "export { Foo, type Bar } from 'foo';\n", false, ); + test_ts( + "type A = { [K in keyof T as K extends string ? B : K ]: T[K] }", + "type A = { [K in keyof T as K extends string ? B : K] : T[K]};\n", + false, + ); + test_ts( + "class A {readonly type = 'frame'}", + "class A {\n\treadonly type = 'frame';\n}\n", + false, + ); } fn test_comment_helper(source_text: &str, expected: &str) {