From 30adc9e3e2e701390666d56c2d07c8520cf1150c Mon Sep 17 00:00:00 2001 From: Dunqing Date: Thu, 20 Jun 2024 16:58:20 +0800 Subject: [PATCH] feat(codegen): print readonly keyword for TSIndexSignature --- crates/oxc_codegen/src/gen.rs | 6 ++++-- .../oxc_isolated_declarations/tests/fixtures/readonly.ts | 4 ++++ .../tests/snapshots/readonly.snap | 8 ++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 crates/oxc_isolated_declarations/tests/fixtures/readonly.ts create mode 100644 crates/oxc_isolated_declarations/tests/snapshots/readonly.snap diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index c8b49eaac9ca76..34802d5fde919d 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -3025,8 +3025,7 @@ impl<'a, const MINIFY: bool> Gen for TSSignature<'a> { Self::TSIndexSignature(signature) => signature.gen(p, ctx), Self::TSPropertySignature(signature) => { if signature.readonly { - p.print_str(b"readonly"); - p.print_hard_space(); + p.print_str(b"readonly "); } if signature.computed { p.print(b'['); @@ -3191,6 +3190,9 @@ impl<'a, const MINIFY: bool> Gen for TSTypeParameterInstantiation<'a> { impl<'a, const MINIFY: bool> Gen for TSIndexSignature<'a> { fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { + if self.readonly { + p.print_str(b"readonly "); + } p.print_str(b"["); for (index, parameter) in self.parameters.iter().enumerate() { if index != 0 { diff --git a/crates/oxc_isolated_declarations/tests/fixtures/readonly.ts b/crates/oxc_isolated_declarations/tests/fixtures/readonly.ts new file mode 100644 index 00000000000000..a2387f95519d86 --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/fixtures/readonly.ts @@ -0,0 +1,4 @@ +export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__ + ? Object.freeze({}) + : {} +export const EMPTY_ARR: readonly never[] = __DEV__ ? Object.freeze([]) : [] diff --git a/crates/oxc_isolated_declarations/tests/snapshots/readonly.snap b/crates/oxc_isolated_declarations/tests/snapshots/readonly.snap new file mode 100644 index 00000000000000..8aabed4ae447ef --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/snapshots/readonly.snap @@ -0,0 +1,8 @@ +--- +source: crates/oxc_isolated_declarations/tests/mod.rs +input_file: crates/oxc_isolated_declarations/tests/fixtures/readonly.ts +--- +==================== .D.TS ==================== + +export declare const EMPTY_OBJ: {readonly [key: string]: any}; +export declare const EMPTY_ARR: readonly (never)[];