Skip to content

fix(minifier): preserve raw CR in template literals#21645

Merged
graphite-app[bot] merged 1 commit intomainfrom
fix/minifier-compressor-idempotency
Apr 27, 2026
Merged

fix(minifier): preserve raw CR in template literals#21645
graphite-app[bot] merged 1 commit intomainfrom
fix/minifier-compressor-idempotency

Conversation

@Dunqing
Copy link
Copy Markdown
Member

@Dunqing Dunqing commented Apr 22, 2026

Summary

Fixes raw carriage return escaping when the minifier folds a RegExp constructor argument into a template literal.

Problem

replace_known_methods can combine a string literal and template literal into a new template literal raw segment, for example:

const re = new RegExp('\r' + `${x.source}`)

The old escaping handled backslashes, backticks, ${, and the \r\n sequence, but not a standalone carriage return. That allowed a raw CR to be emitted directly into the generated template literal source.

This is not only a second-run idempotency issue. The first minifier pass was already producing unsafe generated source for a raw template element. Re-parsing that output can normalize it differently, which then shows up as a second-pass output change.

Fix

  • Escape every \r in generated template literal raw text as \\r.
  • Keep the existing escaping for \, `, and $.
  • Preserve CRLF behavior: \r\n still becomes \\r followed by the LF line break, matching the existing intended output shape while also covering lone CR.

Test

Adds minimal regressions to the existing test_add_template_literal test in replace_known_methods.rs, using the existing test_value helper:

const re = new RegExp('\r' + `${x.source}`)

The cases cover a lone CR, CRLF, and multiple CR characters. The shared helper verifies the first minifier pass emits the expected raw template text and that running the minifier again on that output is idempotent.

Testing

  • cargo test -p oxc_minifier test_add_template_literal

Copy link
Copy Markdown
Member Author

Dunqing commented Apr 22, 2026


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 draft April 22, 2026 15:58
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 22, 2026

Merging this PR will not alter performance

✅ 44 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing fix/minifier-compressor-idempotency (3f40dde) with main (ea4b358)

Open in CodSpeed

Footnotes

  1. 7 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 force-pushed the fix/minifier-compressor-idempotency branch from 1e94e24 to fc0be6a Compare April 26, 2026 14:59
@Dunqing Dunqing changed the title fix(minifier): rebuild scoping between compressor passes fix(minifier): preserve raw CR in template literals Apr 26, 2026
@Dunqing Dunqing force-pushed the fix/minifier-compressor-idempotency branch 6 times, most recently from e2d329d to f9554bb Compare April 26, 2026 15:50
@Dunqing
Copy link
Copy Markdown
Member Author

Dunqing commented Apr 26, 2026

@claude review

Comment thread crates/oxc_minifier/tests/peephole/replace_known_methods.rs Outdated
@Dunqing Dunqing requested a review from sapphi-red April 27, 2026 05:36
@Dunqing Dunqing added the 0-merge Merge with Graphite Merge Queue label Apr 27, 2026
Copy link
Copy Markdown
Member Author

Dunqing commented Apr 27, 2026

Merge activity

## Summary

Fixes raw carriage return escaping when the minifier folds a `RegExp` constructor argument into a template literal.

## Problem

`replace_known_methods` can combine a string literal and template literal into a new template literal raw segment, for example:

```js
const re = new RegExp('\r' + `${x.source}`)
```

The old escaping handled backslashes, backticks, `${`, and the `\r\n` sequence, but not a standalone carriage return. That allowed a raw CR to be emitted directly into the generated template literal source.

This is not only a second-run idempotency issue. The first minifier pass was already producing unsafe generated source for a raw template element. Re-parsing that output can normalize it differently, which then shows up as a second-pass output change.

## Fix

- Escape every `\r` in generated template literal raw text as `\\r`.
- Keep the existing escaping for `\`, `` ` ``, and `$`.
- Preserve CRLF behavior: `\r\n` still becomes `\\r` followed by the LF line break, matching the existing intended output shape while also covering lone CR.

## Test

Adds minimal regressions to the existing `test_add_template_literal` test in `replace_known_methods.rs`, using the existing `test_value` helper:

```js
const re = new RegExp('\r' + `${x.source}`)
```

The cases cover a lone CR, CRLF, and multiple CR characters. The shared helper verifies the first minifier pass emits the expected raw template text and that running the minifier again on that output is idempotent.

## Testing

- `cargo test -p oxc_minifier test_add_template_literal`
@graphite-app graphite-app Bot force-pushed the fix/minifier-compressor-idempotency branch from 3f40dde to 0d608c2 Compare April 27, 2026 05:54
@graphite-app graphite-app Bot merged commit 0d608c2 into main Apr 27, 2026
27 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label Apr 27, 2026
@graphite-app graphite-app Bot deleted the fix/minifier-compressor-idempotency branch April 27, 2026 05:58
camc314 pushed a commit that referenced this pull request Apr 27, 2026
### 💥 BREAKING CHANGES

- 502e804 ast: [**BREAKING**] Reduce size of `TSTypePredicateName`
(#21711) (overlookmotel)
- 5651539 ast: [**BREAKING**] Reduce size of `JSXExpression` (#21710)
(overlookmotel)
- c44e280 ast: [**BREAKING**] Reduce size of `ArrayExpressionElement`
(#21709) (overlookmotel)
- c5b3deb syntax: [**BREAKING**] Remove `CommentNodeId` (#21679)
(overlookmotel)

### 🚀 Features

- b738a39 allocator: Add `Allocator::cursor_ptr` method (#21773)
(overlookmotel)
- 678767e ast: Generate node_id accessors for AST enum wrappers (#21653)
(camc314)
- f091d77 minifier: Inline constant spread elements into arrays (#21095)
(Armano)

### 🐛 Bug Fixes

- 0d608c2 minifier: Preserve raw CR in template literals (#21645)
(Dunqing)
- a889ea9 minifier: Track pure functions in DCE mode (#21722) (Dunqing)
- 674dfac allocator: `Arena` retry allocation when chunk size approaches
maximum (#21777) (overlookmotel)
- f130cc0 allocator: Fix arithmetic overflow in
`Arena::new_chunk_memory_details` (#21745) (overlookmotel)
- b9bf239 allocator: Fix UB in `Arena::grow_zeroed` (#21739)
(overlookmotel)
- d2b9389 allocator: Clippy warning when building without `testing`
feature (#21681) (camc314)
- 503dc86 codegen: Map sourcemaps from visible output starts (#21662)
(Dunqing)
- c92bd3b transformer: Use SPAN for synthesized helper calls to prevent
comment misattribution (#21578) (Dunqing)
- 0d80441 codegen: Add mapping before printing `#` for private ident
(#21619) (camc314)

### ⚡ Performance

- 9fa362e napi/parser: Do not generate tokens except in tests (#21811)
(overlookmotel)
- 0044392 allocator: Reduce branches when allocating new chunk (#21776)
(overlookmotel)
- 7896bd0 allocator: `Allocator::used_bytes` do not use chunk iterator
(#21771) (overlookmotel)
- a5c562f allocator: Remove check in `Arena::new_chunk_memory_details`
(#21750) (overlookmotel)
- 35bbe1f allocator: `Arena` use unchecked size round up where
guaranteed no overflow (#21743) (overlookmotel)
- ffe229b allocator: Remove unnecessary check from
`Arena::try_alloc_layout_slow_impl` (#21732) (overlookmotel)
- 72fece5 allocator: Use `NonNull::offset_from_unsigned` in
`Arena::chunk_capacity` (#21731) (overlookmotel)
- cab32ae ast: Add `#[inline(always)]` to `node_id` methods on enums
with all variants unboxed (#21707) (overlookmotel)
- b179688 parser: Allocate `TriviaBuilder` comments in the arena
(#21512) (Boshen)
- 2290f31 lexer: Fix perf of `Token::set_*` methods on Rust 1.95.0
(#21659) (overlookmotel)
- 1b58029 allocator: Move code into cold path in `Arena::alloc_layout`
(#21622) (overlookmotel)
- 3cf7cef allocator: Reduce instructions on allocation hot path (#21510)
(overlookmotel)

### 📚 Documentation

- ce65070 data_structures: Document why `as_ref` and `as_mut` on
`NonNullConst` and `NonNullMut` take `self` (#21800) (overlookmotel)
- 93b7dbd allocator: Improve doc comments for `ChunkFooter` (#21733)
(overlookmotel)
- 295db8d transformer: Fix comment (#21717) (overlookmotel)
- 5c93af8 ast: Add comments explaining `#[inline(always)]` to `node_id`
methods on enums (#21706) (overlookmotel)
- e4cea25 transform: Use the `node:` namespace in the example (#19998)
(루밀LuMir)

### 🛡️ Security

- d8076c9 deps: Update rolldown (#21639) (renovate)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
graphite-app Bot pushed a commit that referenced this pull request May 8, 2026
Stacked on #21645.

## Summary

Fixes stale `DirectEval` scope flags after peephole passes remove a dead direct eval.

## Problem

Semantic analysis marks scopes that contain direct eval. The minifier uses that flag conservatively: if a scope may contain direct eval, declarations in that scope cannot always be removed because eval may reference them dynamically.

However, peephole compression can remove the direct eval itself:

```js
function f(){if(false)eval('x');var x}f()
```

Before this fix, the AST no longer contained the `eval`, but the semantic scope still kept the stale `DirectEval` flag. That stale flag made declaration removal too conservative during the same minifier run. A fresh reparse did not have the stale flag, so a second minifier run could produce smaller output, which exposed the `monitor-oxc` idempotency failure.

## Fix

- After a changed peephole pass, collect direct eval scope IDs from the live AST.
- Refresh `DirectEval` flags in semantic scoping from that live set.
- Keep `DirectEval` protection for scopes that still contain a live direct eval.
- Remove stale `DirectEval` protection only for scopes whose direct eval was eliminated by earlier compression.

## Test

Adds a minimal regression in `remove_unused_declaration.rs` using the existing `test_options` helper:

```js
function f(){if(false)eval('x');var x}f()
```

The test verifies the dead eval no longer keeps `var x` alive in the same minifier run.

## Testing

- `cargo test -p oxc_minifier remove_unused_declaration_after_dead_direct_eval`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants