Skip to content

Comments

fix(minifier): disallow merging assignments to let declarations when TDZ error would be introduced#13635

Merged
graphite-app[bot] merged 1 commit intomainfrom
09-10-fix_minifier_disallow_merging_assignments_to_let_declarations_when_tdz_error_would_be_introduced
Sep 10, 2025
Merged

fix(minifier): disallow merging assignments to let declarations when TDZ error would be introduced#13635
graphite-app[bot] merged 1 commit intomainfrom
09-10-fix_minifier_disallow_merging_assignments_to_let_declarations_when_tdz_error_would_be_introduced

Conversation

@sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Sep 10, 2025

let x;
x = (() => x?.a)();

should not be transformed to

let x = (() => x?.a)();

, as it would introduce a TDZ error.

To fix that, I limited the right hand side of the assignment to be a literal value for let declarations.

The precise check would be to check whether the right hand side includes a reference to the variable declared on the left hand side, but I didn't do that for now as it is complicated. If we implement that, we can improve this condition too.

// should not move assignment above other variables for let
// this could cause TDZ errors (e.g. `let a, b; b = a;`)
if decl.kind == VariableDeclarationKind::Let {
break;
}

fixes #13615
refs #13270

@github-actions github-actions bot added A-minifier Area - Minifier C-bug Category - Bug labels Sep 10, 2025
Copy link
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 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.

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

@sapphi-red sapphi-red force-pushed the 09-10-fix_minifier_disallow_merging_assignments_to_let_declarations_when_tdz_error_would_be_introduced branch from 5d597f7 to 2c8e941 Compare September 10, 2025 03:34
@codspeed-hq
Copy link

codspeed-hq bot commented Sep 10, 2025

CodSpeed Instrumentation Performance Report

Merging #13635 will not alter performance

Comparing 09-10-fix_minifier_disallow_merging_assignments_to_let_declarations_when_tdz_error_would_be_introduced (f9fd65b) with main (476729b)1

Summary

✅ 37 untouched benchmarks

Footnotes

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

@sapphi-red sapphi-red marked this pull request as ready for review September 10, 2025 03:35
Copilot AI review requested due to automatic review settings September 10, 2025 03:35
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 bug in the minifier where merging assignments to let declarations could introduce Temporal Dead Zone (TDZ) errors. The fix prevents merging when the assignment's right-hand side is not a literal value for let declarations, ensuring that expressions like let x; x = (() => x?.a)() are not incorrectly transformed to let x = (() => x?.a)().

  • Added safety check to prevent TDZ errors when merging assignments to let declarations
  • Restricted merging to literal values only for let declarations while keeping existing behavior for var
  • Added comprehensive test cases covering various TDZ scenarios

Reviewed Changes

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

File Description
crates/oxc_minifier/src/peephole/minimize_statements.rs Core logic fix adding TDZ safety check for let declarations
crates/oxc_minifier/tests/peephole/merge_assignments_to_declarations.rs Test cases covering TDZ error scenarios
tasks/track_memory_allocations/allocs_minifier.snap Memory allocation tracking update reflecting the code changes

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

@sapphi-red sapphi-red requested a review from Boshen September 10, 2025 03:48
@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

…TDZ error would be introduced (#13635)

```js
let x;
x = (() => x?.a)();
```
should not be transformed to
```js
let x = (() => x?.a)();
```
, as it would introduce a TDZ error.

To fix that, I limited the right hand side of the assignment to be a literal value for let declarations.

The precise check would be to check whether the right hand side includes a reference to the variable declared on the left hand side, but I didn't do that for now as it is complicated. If we implement that, we can improve this condition too.
https://github.com/oxc-project/oxc/blob/5d597f71c06a9c56fe404a712c05b61e2da3697a/crates/oxc_minifier/src/peephole/minimize_statements.rs#L544-L548

fixes #13615
refs #13270
@graphite-app graphite-app bot force-pushed the 09-10-fix_minifier_disallow_merging_assignments_to_let_declarations_when_tdz_error_would_be_introduced branch from 2c8e941 to f9fd65b Compare September 10, 2025 04:32
@graphite-app graphite-app bot merged commit f9fd65b into main Sep 10, 2025
27 checks passed
@graphite-app graphite-app bot deleted the 09-10-fix_minifier_disallow_merging_assignments_to_let_declarations_when_tdz_error_would_be_introduced branch September 10, 2025 04:38
@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-minifier Area - Minifier C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

minifier: immediate access and assign after declaration causes TDZ exception

2 participants