Skip to content

fix(javascript): keep a sloppy block's function in its block where Annex B does not hoist it in the minifier - #22598

Merged
alexander-akait merged 3 commits into
webpack:mainfrom
aryanraj45:fix/minify-annex-b-skipped-block-function
Oct 11, 2026
Merged

alexander-akait merged 3 commits into
webpack:mainfrom
aryanraj45:fix/minify-annex-b-skipped-block-function

Conversation

@aryanraj45

@aryanraj45 aryanraj45 commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Summary

Summary:
Annex B hoists a sloppy block's function only where a var of its name is no early error, and sets that var only when its block runs. Every one was hoisted and the name read as fixed, so a let, const, class, for head, catch pattern, parameter or other function of that name made the minifier throw or misprint.

function f() { { function g() {} } let g = 1; return g; } // before: minifier throws
{ let g = 1; { function g() { return 7; } g(); } } // mangle only, before: TypeError
{ function h() { return 1; } } h(); function h() { return 2; } // before: 2, after: 1

Shapes swept: 60/60 right (uglify-js 39, oxc 10, terser 8); test262 annexB: 777/784 from 673, no regression; 21 bundles: 0 bytes. V8's reading of an outer same-name block function stays.

What kind of change does this PR introduce?

fix

Did you add tests for your changes?

Yes, test/unitCases/syntax-printer.unittest.js: each declaration kind and option set, the code main threw on, and where Annex B still hoists.

Does this PR introduce a breaking change?

No

If relevant, what needs to be documented once your changes are merged or what have you already documented?

n/a

Use of AI

I used Claude Code to find the failing test262 cases, compare minifiers, write the fix and tests. I reviewed every change myself.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of JavaScript block-level function declarations when their names overlap with lexical bindings in surrounding scopes, including parameters, destructured patterns, loop heads, and catch clauses.
    • Fixed cases where compression or name mangling could alter runtime behavior or fail to preserve valid scope behavior.
    • Preserved expected hoisting behavior when no conflicting lexical declaration exists, and improved handling of later declarations with the same name.
    • Corrected minification for certain valid redeclaration cases.

…nex B does not hoist it in the minifier

Annex B hoists a block's function only where a `var` of its name is no early error, but every one was hoisted, so the minifier threw `"f" is redeclared` on a later `let f`, dropped a read that throws and mangled a block's own call apart. A `let`, `const`, class, `for` head, `catch` pattern or parameter of its name now keeps it in its block.
@changeset-bot

changeset-bot Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 03032f7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: webpack/webpack/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 78c1b564-1f94-413d-a448-aa8561b34c12


📥 Commits

Reviewing files that changed from the base of the PR and between 993a2b8 and 03032f7.



📒 Files selected for processing (2)
  • lib/javascript/syntax-printer.js
  • test/unitCases/syntax-printer.unittest.js


Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.




📝 Walkthrough
📝 Walkthrough

Walkthrough

The scope analyzer now checks enclosing lexical declarations when assigning sloppy block function declarations to scopes. The syntax printer adjusts block-function detection and candidate scanning. Regression tests cover lexical-binding contexts and minification settings, including redeclaration errors when corrections are disabled.

Changes

Sloppy block function scope

Layer / File(s) Summary
Detect lexical bindings and select function scope
lib/javascript/ScopeAnalyzer.js
The analyzer detects matching names in binding patterns and enclosing lexical declarations. With parameterScopes enabled, it uses these checks when selecting a scope for function declarations.
Adjust block-function detection and candidate scanning
lib/javascript/syntax-printer.js
Block-function detection checks all identifiers. Candidate scanning stops at matching function declarations outside the definition scope body.
Verify minification behavior
test/unitCases/syntax-printer.unittest.js
Regression cases check runtime equivalence across lexical-binding contexts and compression and mangling settings. Additional cases check redeclaration errors when corrections are disabled.

Suggested labels: area: parser



Priority: ➖ Normal

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 03032

No actionable merge-blocking risk was identified in the changed sloppy block-function handling.

Pre-merge checks | Passed 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title uses the required Conventional Commit form, fix(javascript): subject, and accurately describes the sloppy block-function minifier fix.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot added the area: parser JavaScript parsing and dependency analysis (lib/javascript, lib/dependencies) label Oct 10, 2026
…`let` of its name in a `catch` body

A `catch` body's statements sit in the catch scope with no block of their own, so a `let` there was missed and the function still hoisted. Also covers labeled, `while`, `do` and `with` bodies, arrow, generator and async functions, renamed, defaulted, nested and `for-of` patterns, an inlined function, `toplevel`, and a `let` in an outer function, where the function still hoists.
@codecov

codecov Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.23%. Comparing base (a23b99e) to head (03032f7).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #22598      +/-   ##
==========================================
+ Coverage   95.76%   96.23%   +0.46%     
==========================================
  Files         774      773       -1     
  Lines      128803   128616     -187     
  Branches    42745    42589     -156     
==========================================
+ Hits       123351   123770     +419     
+ Misses       5452     4846     -606     
Flag Coverage Δ
css-parsing 25.09% <2.94%> (-0.03%) ⬇️
html5lib 27.00% <2.94%> (-0.02%) ⬇️
integration 82.38% <2.94%> (-0.06%) ⬇️
minify-corpora 79.76% <100.00%> (?)
syntax-equivalence 79.38% <ø> (ø)
test262 57.10% <67.04%> (+2.46%) ⬆️
test262-parser 84.55% <ø> (ø)
unit 75.40% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Reported by Types Coverage #1180 for 03032f7, computed by this pull request's own code.

Types Coverage

Coverage after merging fix/minify-annex-b-skipped-block-function into main will be
97.29%
Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
bin
   webpack.js98.82%100%100%98.82%103
declarations
   WebpackOptions.ts99.94%100%100%99.94%4188
examples
   build-common.js100%100%100%100%
   buildAll.js100%100%100%100%
   examples.js100%100%100%100%
   template-common.js98.21%100%100%98.21%72
examples/custom-javascript-parser
   test.filter.js100%100%100%100%
examples/custom-javascript-parser/internals
   acorn-parse.js100%100%100%100%
   meriyah-parse.js100%100%100%100%
   oxc-parse.js100%100%100%100%
examples/markdown
   webpack.config.mjs100%100%100%100%
examples/module-federation
   test.filter.js100%100%100%100%
examples/reexport-components
   test.filter.js100%100%100%100%
examples/ssr
   test.filter.js100%100%100%100%
examples/typescript
   test.filter.js100%100%100%100%
examples/typescript-non-erasable
   test.filter.js50%100%100%50%5
examples/virtual-modules
   test.filter.js100%100%100%100%
examples/wasm-bindgen-esm
   test.filter.js100%100%100%100%
examples/wasm-complex
   test.filter.js100%100%100%100%
examples/wasm-emscripten
   test.filter.js100%100%100%100%
examples/wasm-simple
   test.filter.js100%100%100%100%
examples/wasm-simple-source-phase
   test.filter.js100%100%100%100%
lib
   Chunk.js100%100%100%100%
   Compilation.js98.60%100%100%98.60%1757, 2078, 2085, 2093, 2115, 2118, 23, 3075, 3578–3579, 3611, 4200, 4204, 4331, 4352, 4477, 4514, 4521, 4574–4575, 4579, 4584, 4600–4601, 4615–4616, 4621–4622, 5145, 5171, 6001, 6018, 6036, 6052, 6067, 6092, 6095, 6463, 6466–6467, 6472, 6487, 6570, 6594, 6708, 837–838
   Compiler.js98.81%100%100%98.81%1116, 1153, 1156, 1336–1337, 1345, 848, 934, 975
   Dependency.js100%100%100%100%
   DependencyTemplate.js100%100%100%100%
   EntryPlugin.js100%100%100%100%
   Entrypoint.js100%100%100%100%
   ExternalsPlugin.js100%100%100%100%
   FileSystemInfo.js100%100%100%100%
   InitFragment.js100%100%100%100%
   LibraryTemplatePlugin.js100%100%100%100%
   ModuleFilenameHelpers.js100%100%100%100%
   ModuleNotFoundError.js100%100%100%100%
   MultiCompiler.js98.88%100%100%98.88%460, 532, 719, 784
   NormalModule.js100%100%100%100%
   NullFactory.js100%100%100%100%
   ProgressPlugin.js100%100%100%100%
   RawModule.js100%100%100%100%
   RequestShortener.js100%100%100%100%
   SingleEntryPlugin.js100%100%100%100%
   WebpackError.js100%100%100%100%
   index.js99.78%100%100%99.78%206
   webpack.js97.84%100%100%97.84%278, 300, 302
lib/asset
   AssetBytesGenerator.js100%100%100%100%
   AssetBytesParser.js100%100%100%100%
   AssetGenerator.js100%100%100%100%
   AssetModule.js100%100%100%100%
   AssetModulesPlugin.js98.14%100%100%98.14%337, 361, 364, 494, 56, 61
   AssetParser.js100%100%100%100%
   AssetSourceGenerator.js100%100%100%100%
   AssetSourceParser.js100%100%100%100%
   RawDataUrlModule.js100%100%100%100%
   WebManifestGenerator.js100%100%100%100%
   WebManifestParser.js100%100%100%100%
lib/async-modules
   AsyncModuleHelpers.js100%100%100%100%
   AwaitDependenciesInitFragment.js100%100%100%100%
   InferAsyncModulesPlugin.js100%100%100%100%
   isGeneratorLowered.js100%100%100%100%
lib/bun
   BunTargetPlugin.js100%100%100%100%
lib/cache
   AddBuildDependenciesPlugin.js100%100%100%100%
   AddManagedPathsPlugin.js100%100%100%100%
   Cache.js96.49%100%100%96.49%10, 104
   CacheFacade.js100%100%100%100%
   IdleFileCachePlugin.js97.92%100%100%97.92%75, 87, 95
   MemoryCachePlugin.js92%100%100%92%33, 42
   MemoryWithGcCachePlugin.js93.42%100%100%93.42%106, 120–121, 130, 88
   PackFileCacheStrategy.js96.54%100%100%96.54%1304, 1404, 1408, 1470, 1716, 1800, 1823, 1853, 682, 701, 711–713, 715, 731–732, 737, 740, 742, 747, 752, 777, 783, 817, 823, 829, 834, 845, 854, 859–860, 862, 879, 885–886, 888
   ResolverCachePlugin.js100%100%100%100%
   getLazyHashedEtag.js100%100%100%100%
   mergeEtags.js100%100%100%100%
lib/config
   OptionsApply.js100%100%100%100%
   PlatformPlugin.js100%100%100%100%
   WebpackOptionsApply.js100%100%100%100%
   WebpackOptionsDefaulter.js100%100%100%100%
   browserslistTargetHandler.js100%100%100%100%
   cli.js98.61%100%100%98.61%116, 546, 578, 628, 9
   defaults.js99.44%100%100%99.44%1624–1626, 1634, 283, 286, 291, 295
   defineConfig.js100%100%100%100%
   getClaimedAssetTypes.js100%100%100%100%
   normalization.js99.12%100%100%99.12%279–280, 346, 361
   target.js100%100%100%100%
   validateSchema.js94.67%100%100%94.67%100, 87, 89, 98
lib/container
   ContainerEntryDependency.js100%100%100%100%
   ContainerEntryModule.js100%100%100%100%
   ContainerEntryModuleFactory.js100%100%100%100%
   ContainerExposedDependency.js100%100%100%100%
   ContainerPlugin.js100%100%100%100%
   ContainerReferencePlugin.js100%100%100%100%
   FallbackDependency.js100%100%100%100%
   FallbackItemDependency.js100%100%100%100%
   FallbackModule.js100%100%100%100%
   FallbackModuleFactory.js100%100%100%100%
   HoistContainerReferencesPlugin.js100%100%100%100%
   ModuleFederationPlugin.js100%100%100%100%
   RemoteModule.js100%100%100%100%
   RemoteRuntimeModule.js100%100%100%100%
   RemoteToExternalDependency.js100%100%100%100%
   declaredRemotes.js100%100%100%100%
   moduleFederationHooks.js100%100%100%100%
   options.js100%100%100%100%
lib/context
   ContextExclusionPlugin.js100%100%100%100%
   ContextModule.js99.88%100%100%99.88%1495
   ContextModuleFactory.js95.66%100%100%95.66%230, 257, 276, 280–281, 289, 465, 486, 491, 532, 541, 543, 547, 550, 556–557
   ContextReplacementPlugin.js100%100%100%100%
lib/css
   CssGenerator.js99.47%100%100%99.47%1089, 133, 846
   CssInjectStyleRuntimeModule.js100%100%100%100%
   CssLoadingRuntimeModule.js100%100%100%100%
   CssModule.js83.16%100%100%83.16%197, 202–210, 212–217
   CssModulesPlugin.js99.86%100%100%99.86%881
   CssParser.js99.48%100%100%99.48%1048, 1079, 24, 2410, 3057, 3060, 3066, 3107, 3110, 3149, 3152, 3489, 5048, 5051, 932
   CssServerStylesRuntimeModule.js100%100%100%100%
   conventions.js98.65%100%100%98.65%93
   cssMinify.js97.50%100%100%97.50%162, 171
   data.js99.84%100%100%99.84%229, 5601
   syntax-parser.js99.65%100%100%99.65%10098–10099, 10633–10634, 10671, 10866, 10912, 13134–13137, 13237–13238, 13335–13336, 13981, 14278, 14280, 14770, 15136, 17243, 2467, 3166, 3168, 3173, 3192, 3937, 3943, 7384, 7422, 7429
   syntax-printer.js99.31%100%100%99.31%2532–2533, 2535–2536, 2538–2539, 2544, 2811–2814, 2907, 3580, 367, 4000, 4109, 4797, 5098, 5884, 735
   syntax.js100%100%100%100%
   topologicalSort.js100%100%100%100%
lib/debug
   ProfilingPlugin.js96.22%100%100%96.22%169, 425, 554, 561, 570, 574, 583, 588, 594, 599, 92
lib/define
   DefinePlugin.js98.85%100%100%98.85%1495, 268–269, 285, 304, 378, 936, 938, 943, 967
   DotenvPlugin.js98.07%100%100%98.07%391, 439, 452–453
   EnvironmentPlugin.js97.14%100%100%97.14%49
lib/deno
   DenoTargetPlugin.js100%100%100%100%
lib/dependencies
   ConstDependency.js100%100%100%100%
   ContextElementDependency.js100%100%100%100%
   NullDependency.js100%100%100%100%
lib/dependencies/amd
   AMDDefineDependency.js100%100%100%100%
   AMDDefineDependencyParserPlugin.js100%100%100%100%
   AMDPlugin.js99.31%100%100%99.31%54
   AMDRequireArrayDependency.js100%100%100%100%
   AMDRequireContextDependency.js100%100%100%100%
   AMDRequireDependenciesBlock.js100%100%100%100

…truncated

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Reported by Code Size #4393 for 03032f7, computed by this pull request's own code.

Generated code size

Comparing base (07008e4) to head (03032f7). Merging this pull request will not change the code webpack generates.

What moved Tests Assets Gzip Raw Test edit
Changed, test untouched 0 0 — — —
Changed, test edited 0 0 — — —
New 0 0 — — —
Deleted 0 0 — — —
Unchanged — 8455 — — —

Read Changed, test untouched first: the test is byte-identical on both sides, so webpack generated the difference. Changed, test edited moved partly because the test did — Test edit is how many bytes the test gained, and a bundle that grew by less is not a regression. New and Deleted are whole files, not deltas — a new asset was created, not grown, so it reads gray (⚪ added) — and adding tests cannot bury a real change. Gzip decides, because that is what users download; raw is the tiebreak, and brotli and zstd are per asset below.

No asset changed size.

No runtime gained or lost a runtime module.

Built test/configCases with the defaults a user gets: 2209 test(s), 8455 asset(s), 99 emitted nothing.

@codspeed

codspeed Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Merging this PR will regress 1 benchmark

⚠️ 108 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

ℹ️ 41 benchmarks have no flame graph

The flamegraph could not be generated because too much time was spent in runtime internals, making the visualization not useful for performance analysis.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 3 improved benchmarks
❌ 1 regressed benchmark
✅ 402 untouched benchmarks
⏩ 6 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory benchmark "three-long", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 139.4 KB 446.8 KB -68.8%
⚡ Memory unit benchmark "html-parser-document-unit", parseHtml (skip text) 189.3 KB 55.8 KB ×3.4
⚡ Memory benchmark "no-exports-analysis", scenario '{"name":"mode-production","mode":"production"}' 1,319.2 KB 727.5 KB +81.32%
⚡ Memory benchmark "asset-modules-source", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 287.8 KB 198.9 KB +44.71%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing aryanraj45:fix/minify-annex-b-skipped-block-function (03032f7) with main (5ba5dba)

Open in CodSpeed

Footnotes

  1. 6 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. ↩

…n after its block in the minifier

Annex B sets a sloppy block's function's `var` where its block runs, but the minifier fixed the name to another function or `var` of it, so `{ function f() { return 1 } } f(); function f() { return 2 }` returned 2 and collapsing moved `var f = 2` past the block. 8 test262 Annex B files now pass.
@alexander-akait
alexander-akait merged commit 86da462 into webpack:main Oct 11, 2026
66 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

This PR is packaged and the instant preview is available (86da462).

Install it locally:

  • npm
npm i -D webpack@https://pkg.pr.new/webpack@86da462
  • yarn
yarn add -D webpack@https://pkg.pr.new/webpack@86da462
  • pnpm
pnpm add -D webpack@https://pkg.pr.new/webpack@86da462

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: parser JavaScript parsing and dependency analysis (lib/javascript, lib/dependencies)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants