Skip to content

fix(minifier): normalize { x: x } shorthand so adjacent-if merge is idempotent#22401

Merged
graphite-app[bot] merged 1 commit into
mainfrom
fix/compressor-idempotent-if-merge
May 18, 2026
Merged

fix(minifier): normalize { x: x } shorthand so adjacent-if merge is idempotent#22401
graphite-app[bot] merged 1 commit into
mainfrom
fix/compressor-idempotent-if-merge

Conversation

@Dunqing

@Dunqing Dunqing commented May 14, 2026

Copy link
Copy Markdown
Member

Why

The merge pass for consecutive if (..) return X; statements decides whether two jump bodies are equal via content_eq, and ObjectProperty::content_eq includes the shorthand flag. Codegen, in contrast, always prints { x: x } as { x } whenever the value is an identifier matching the key. The flag disagreement made the merge non-idempotent. Given:

function _(body) {
  if (a) return { body };
  if (b) return { body };
  if (c) return { body };
  if (d) return { body: body };
}

pass 1 merged only the first three (their shorthand matched) and left the trailing if alone:

function _(body) {
  if (a || b || c) return { body };
  if (d) return { body };
}

pass 2 then merged all four, because the printed { body } reparses with shorthand = true. The fixed-point loop converges, but a second call to Compressor::build would still mutate the AST — the contract monitor-oxc checks.

Fix

Extend substitute_object_property (already called once per ObjectProperty exit in the peephole loop, so no new traversal dispatch) to set shorthand = true whenever the value is an identifier with the same name as a non-computed, non-method, non-__proto__ init key. The AST now matches what codegen would emit, so subsequent content_eq-based merges converge in a single pass. Checks are ordered to bail on the common case first (already shorthand, or non-identifier value); output text and the minsize / allocs snapshots are unchanged.

Originally surfaced by monitor-oxc CI on @typespec/ts-http-runtime's sendRequest.js: https://github.com/oxc-project/monitor-oxc/actions/runs/25841541741/job/75927903765

@github-actions github-actions Bot added the A-minifier Area - Minifier label May 14, 2026
@codspeed-hq

codspeed-hq Bot commented May 14, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 48 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing fix/compressor-idempotent-if-merge (0d40222) with main (e9ec7c6)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Dunqing Dunqing marked this pull request as draft May 14, 2026 06:59
@Dunqing Dunqing force-pushed the fix/compressor-idempotent-if-merge branch 4 times, most recently from 3ea3352 to 0d40222 Compare May 18, 2026 03:23

Dunqing commented May 18, 2026

Copy link
Copy Markdown
Member Author

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 changes, fast-track this PR to the front of the merge queue

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.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@Dunqing Dunqing marked this pull request as ready for review May 18, 2026 08:43
@Dunqing Dunqing added the 0-merge Merge with Graphite Merge Queue label May 18, 2026

Dunqing commented May 18, 2026

Copy link
Copy Markdown
Member Author

Merge activity

… idempotent (#22401)

## Why

The merge pass for consecutive `if (..) return X;` statements decides whether two jump bodies are equal via `content_eq`, and `ObjectProperty::content_eq` includes the `shorthand` flag. Codegen, in contrast, always prints `{ x: x }` as `{ x }` whenever the value is an identifier matching the key. The flag disagreement made the merge non-idempotent. Given:

```js
function _(body) {
  if (a) return { body };
  if (b) return { body };
  if (c) return { body };
  if (d) return { body: body };
}
```

pass 1 merged only the first three (their `shorthand` matched) and left the trailing if alone:

```js
function _(body) {
  if (a || b || c) return { body };
  if (d) return { body };
}
```

pass 2 then merged all four, because the printed `{ body }` reparses with `shorthand = true`. The fixed-point loop converges, but a second call to `Compressor::build` would still mutate the AST — the contract `monitor-oxc` checks.

## Fix

Extend `substitute_object_property` (already called once per `ObjectProperty` exit in the peephole loop, so no new traversal dispatch) to set `shorthand = true` whenever the value is an identifier with the same name as a non-computed, non-method, non-`__proto__` init key. The AST now matches what codegen would emit, so subsequent `content_eq`-based merges converge in a single pass. Checks are ordered to bail on the common case first (already shorthand, or non-identifier value); output text and the `minsize` / `allocs` snapshots are unchanged.

Originally surfaced by `monitor-oxc` CI on `@typespec/ts-http-runtime`'s `sendRequest.js`: https://github.com/oxc-project/monitor-oxc/actions/runs/25841541741/job/75927903765
@graphite-app graphite-app Bot force-pushed the fix/compressor-idempotent-if-merge branch from 0d40222 to c27a8cf Compare May 18, 2026 08:45
@graphite-app graphite-app Bot merged commit c27a8cf into main May 18, 2026
29 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label May 18, 2026
@graphite-app graphite-app Bot deleted the fix/compressor-idempotent-if-merge branch May 18, 2026 08:50
camc314 pushed a commit that referenced this pull request May 18, 2026
### 🐛 Bug Fixes

- 0f26de6 ecmascript: Resolve identifier value type via tracked
constants (#22234) (Alexander Lichter)
- c27a8cf minifier: Normalize `{ x: x }` shorthand so adjacent-if merge
is idempotent (#22401) (Dunqing)
- e431a0e parser: Break extends clause loop on fatal error (#22517)
(Boshen)
- e9ec7c6 minifier: Fold optional chains by base nullishness (#22236)
(Alexander Lichter)
- e6090e7 transformer: Keep enum IIFE when a non-inlinable value
reference remains (#22501) (Dunqing)
- 931b7d6 transformer: Inline const enum members through type-cast
wrappers (#22500) (Dunqing)
- b9615b2 codegen: Preserve string quotes in require() calls during
minification (#22475) (zennnnnnn11)
- c73c159 transformer/async-to-generator: Reparent parameter initializer
scopes (#22507) (camc314)
- ecfd3ca transformer/async-to-generator: Move only parameter bindings
(#22503) (camc314)
- 3ce3431 transformer/explicit-resource-managment: Preserve shadowed
for-head block (#22451) (camc314)

### ⚡ Performance

- ce92c6c semantic: `#[inline]` `Scoping::get_binding` (#22414)
(Dunqing)
- 98be95c regular_expression: Track regex flags via bitflags (#22427)
(Boshen)
- dbbc059 jsdoc: Skip should_attach_jsdoc when no remaining comments
(#22409) (Boshen)
- 217d7d8 minifier: Index `SymbolValues` by `SymbolId` (#22441)
(Dunqing)
- d782b78 minifier: Use BitSet for LiveUsageCollector live references
(#22425) (Boshen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-minifier Area - Minifier

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant