From 44a10c4b91b073a0e69813c74057c4a03bc16dd5 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Sun, 21 Jul 2024 14:04:17 +0000 Subject: [PATCH] fix(codegen): object shorthand with parens `({x: (x)})` -> `({ x })` (#4391) --- crates/oxc_codegen/examples/codegen.rs | 2 +- crates/oxc_codegen/src/gen.rs | 2 +- crates/oxc_codegen/tests/integration/unit.rs | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/oxc_codegen/examples/codegen.rs b/crates/oxc_codegen/examples/codegen.rs index 7baffcb9255b2..ccd10b491884c 100644 --- a/crates/oxc_codegen/examples/codegen.rs +++ b/crates/oxc_codegen/examples/codegen.rs @@ -57,7 +57,7 @@ fn main() -> std::io::Result<()> { } let printed = CodeGenerator::new() .enable_comment( - &source_text, + &printed, ret.trivias.clone(), CommentOptions { preserve_annotate_comments: true }, ) diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index ec6869a68d6d4..97cb7f9dc90af 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -1554,7 +1554,7 @@ impl<'a, const MINIFY: bool> Gen for ObjectProperty<'a> { let mut shorthand = false; if let PropertyKey::StaticIdentifier(key) = &self.key { - if let Expression::Identifier(ident) = &self.value { + if let Expression::Identifier(ident) = self.value.without_parenthesized() { if key.name == p.get_identifier_reference_name(ident) { shorthand = true; } diff --git a/crates/oxc_codegen/tests/integration/unit.rs b/crates/oxc_codegen/tests/integration/unit.rs index 670ac475d434c..88e527b597abe 100644 --- a/crates/oxc_codegen/tests/integration/unit.rs +++ b/crates/oxc_codegen/tests/integration/unit.rs @@ -130,6 +130,7 @@ fn for_stmt() { fn shorthand() { test("let _ = { x }", "let _ = {x};\n"); test("let { x } = y", "let { x } = y;\n"); + test("({ x: (x) })", "({x});\n"); test("({ x } = y)", "({x} = y);\n"); }