Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 10, 2025

Fixes #13558

Problem

When minifying JavaScript code with destructuring assignments that contain string literal property keys (especially those with hyphens), the codegen was incorrectly using backticks instead of double quotes:

// Input
({"aria-label": ariaLabel} = input);

// Incorrect output (before fix)
({`aria-label`:ariaLabel}=input);

// Correct output (after fix)  
({"aria-label":ariaLabel}=input);

Root Cause

The issue was in the AssignmentTargetPropertyProperty implementation in crates/oxc_codegen/src/gen.rs. Unlike the regular PropertyKey implementation and other similar constructs (TSMethodSignature, TSPropertySignature, etc.), it was missing a specific case for PropertyKey::StringLiteral and was falling through to the default case that calls key.to_expression().print_expr(), which allows backticks by default.

Solution

Added explicit handling for PropertyKey::StringLiteral in the AssignmentTargetPropertyProperty::r#gen method to call p.print_string_literal(s, /* allow_backtick */ false), ensuring consistency with how property keys are handled elsewhere in the codebase.

The fix:

  • Only affects destructuring assignments with string literal property keys
  • Maintains existing behavior for all other cases (identifiers, computed properties, etc.)
  • Follows the same pattern used in other PropertyKey implementations throughout the codebase

Testing

Added comprehensive test cases covering:

  • Basic hyphenated property keys: {"aria-label": ariaLabel}
  • Properties with newlines: {"key-with-newline\n": val}
  • Mixed computed and literal properties: {["computed"]: a, "literal": b}
  • Destructuring declarations: let {"test-key": testKey} = obj
  • Object literals with string keys: ({ "test-key": key })
  • Class properties with string keys: (class { "test-key" = key })

All existing tests continue to pass, confirming the fix doesn't break any existing functionality.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@graphite-app
Copy link
Contributor

graphite-app bot commented Sep 10, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

Copilot AI changed the title [WIP] Fix @oxc-project/oxc/issues/13558 . Check @oxc-project/oxc/pull/8839 for how to print double/single quotes. Make sure to add a test in @oxc-project/oxc/files/crates/oxc_codegen/tests/integration/js.rs Fix object property keys using backticks in destructuring assignments during minification Sep 10, 2025
Copilot AI requested a review from sapphi-red September 10, 2025 02:07
Copilot finished work on behalf of sapphi-red September 10, 2025 02:07
@github-actions github-actions bot added the A-codegen Area - Code Generation label Sep 10, 2025
@sapphi-red
Copy link
Member

@copilot add tests for the following cases

({ "test-key": key });
(class { "test-key" = key });

@codspeed-hq
Copy link

codspeed-hq bot commented Sep 10, 2025

CodSpeed Instrumentation Performance Report

Merging #13631 will not alter performance

Comparing copilot/fix-bb2a078b-eb5d-4baf-89f4-7e6d00125ae9 (bf50a02) with main (e66f93b)1

Summary

✅ 37 untouched benchmarks

Footnotes

  1. No successful run was found on main (bf50a02) during the generation of this report, so e66f93b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Copy link
Contributor Author

Copilot AI commented Sep 10, 2025

@copilot add tests for the following cases

({ "test-key": key });

...

Added tests for both requested cases:

  • Object literal with string key: ({ "test-key": key })
  • Class property with string key: (class { "test-key" = key })

Both test cases now verify that string literal property keys are properly handled with double quotes during minification. (c9bccc7)

Copilot AI changed the title Fix object property keys using backticks in destructuring assignments during minification Fix object property keys using backticks in destructuring assignments, object literals, and class properties during minification Sep 10, 2025
Copilot AI requested a review from sapphi-red September 10, 2025 02:19
Copilot finished work on behalf of sapphi-red September 10, 2025 02:19
@sapphi-red sapphi-red changed the title Fix object property keys using backticks in destructuring assignments, object literals, and class properties during minification fix(codegen): avoid backticks for object property keys in destructuring assignments Sep 10, 2025
@github-actions github-actions bot added the C-bug Category - Bug label Sep 10, 2025
@sapphi-red sapphi-red marked this pull request as ready for review September 10, 2025 02:29
Copilot AI review requested due to automatic review settings September 10, 2025 02:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a codegen issue where destructuring assignments with string literal property keys (especially hyphenated ones) were incorrectly using backticks instead of double quotes during minification.

  • Added explicit handling for PropertyKey::StringLiteral in AssignmentTargetPropertyProperty
  • Ensured string literals use double quotes by setting allow_backtick to false
  • Added comprehensive test coverage for various destructuring scenarios

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
crates/oxc_codegen/src/gen.rs Added explicit PropertyKey::StringLiteral case to prevent backtick usage in destructuring assignments
crates/oxc_codegen/tests/integration/js.rs Added test cases covering destructuring with string literal keys and mixed property types

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@sapphi-red sapphi-red assigned Boshen and unassigned sapphi-red Sep 10, 2025
@sapphi-red sapphi-red requested a review from Boshen September 10, 2025 02:30
@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Sep 10, 2025
Copy link
Member

Boshen commented Sep 10, 2025

Merge activity

…ng assignments (#13631)

Fixes #13558

## Problem

When minifying JavaScript code with destructuring assignments that contain string literal property keys (especially those with hyphens), the codegen was incorrectly using backticks instead of double quotes:

```javascript
// Input
({"aria-label": ariaLabel} = input);

// Incorrect output (before fix)
({`aria-label`:ariaLabel}=input);

// Correct output (after fix)
({"aria-label":ariaLabel}=input);
```

## Root Cause

The issue was in the `AssignmentTargetPropertyProperty` implementation in `crates/oxc_codegen/src/gen.rs`. Unlike the regular `PropertyKey` implementation and other similar constructs (`TSMethodSignature`, `TSPropertySignature`, etc.), it was missing a specific case for `PropertyKey::StringLiteral` and was falling through to the default case that calls `key.to_expression().print_expr()`, which allows backticks by default.

## Solution

Added explicit handling for `PropertyKey::StringLiteral` in the `AssignmentTargetPropertyProperty::r#gen` method to call `p.print_string_literal(s, /* allow_backtick */ false)`, ensuring consistency with how property keys are handled elsewhere in the codebase.

The fix:
- Only affects destructuring assignments with string literal property keys
- Maintains existing behavior for all other cases (identifiers, computed properties, etc.)
- Follows the same pattern used in other `PropertyKey` implementations throughout the codebase

## Testing

Added comprehensive test cases covering:
- Basic hyphenated property keys: `{"aria-label": ariaLabel}`
- Properties with newlines: `{"key-with-newline\n": val}`
- Mixed computed and literal properties: `{["computed"]: a, "literal": b}`
- Destructuring declarations: `let {"test-key": testKey} = obj`
- Object literals with string keys: `({ "test-key": key })`
- Class properties with string keys: `(class { "test-key" = key })`

All existing tests continue to pass, confirming the fix doesn't break any existing functionality.

<!-- START COPILOT CODING AGENT SUFFIX -->

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey3.medallia.com/?EAHeSx-AP01bZqG0Ld9QLQ) to start the survey.
@graphite-app graphite-app bot force-pushed the copilot/fix-bb2a078b-eb5d-4baf-89f4-7e6d00125ae9 branch from b285299 to bf50a02 Compare September 10, 2025 02:59
@graphite-app graphite-app bot merged commit bf50a02 into main Sep 10, 2025
27 checks passed
@graphite-app graphite-app bot deleted the copilot/fix-bb2a078b-eb5d-4baf-89f4-7e6d00125ae9 branch September 10, 2025 03:04
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Sep 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-codegen Area - Code Generation C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Destructuring hypenated keys leads to invalid template strings with minify whitespace

3 participants