GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(minify): close more one-pass minifier gaps to terser by alexander-akait · Pull Request #23503 · webpack/webpack

fix(minify): close more one-pass minifier gaps to terser #23503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
alexander-akait merged 77 commits into main from fix/minify-one-pass-gaps-round4
Nov 21, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

fix(minify): close more one-pass minifier gaps to terser #23503

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
2e59646
fix(minify): fold a known if test that a switch case holds
alexander-akait Nov 20, 2026
82b2c35
fix(minify): drop a var only a switch case that never runs sets
alexander-akait Nov 20, 2026
4e7550f
fix(minify): count no write that never runs
alexander-akait Nov 20, 2026
7c835f5
fix(minify): count no write a switch on a literal never reaches
alexander-akait Nov 20, 2026
405f1fe
fix(minify): keep a dot key as written unless a computed key spells it
alexander-akait Nov 20, 2026
82f11e4
fix(minify): fold the reads of a constant object holding a regular ex…
alexander-akait Nov 20, 2026
d54a0fd
fix(minify): move a function read once into a function called in place
alexander-akait Nov 20, 2026
dfaaf8f
fix(minify): move a function read once into a loop's head that runs once
alexander-akait Nov 20, 2026
42da2e2
fix(minify): read the key a literal or undefined names off an object …
alexander-akait Nov 20, 2026
e48cf98
fix(minify): construct the function a new reads off an object literal
alexander-akait Nov 20, 2026
8b794c5
fix(minify): drop a class extending a class or function made in place
alexander-akait Nov 20, 2026
aefa0ff
fix(minify): fold a read printing the name a value collapsed into
alexander-akait Nov 20, 2026
18792ce
fix(minify): read a var set to a sequence as its last constant
alexander-akait Nov 20, 2026
2aa67bf
fix(minify): collapse a value into a compound assignment of a fixed l…
alexander-akait Nov 20, 2026
9b287a7
fix(minify): collapse a value past a native static method callee
alexander-akait Nov 20, 2026
799bd25
fix(minify): print an unread call in place past declarations printing…
alexander-akait Nov 20, 2026
4723f01
fix(minify): print a call of an identity function as its argument
alexander-akait Nov 20, 2026
799bdcb
fix(minify): drop a use strict directive an arrow's body holds alone
alexander-akait Nov 20, 2026
2e1e3fb
fix(minify): print an object's method returning a value as an arrow
alexander-akait Nov 20, 2026
a3764a8
fix(minify): inline constants passed past a parameter defaulting to a…
alexander-akait Nov 20, 2026
61ac37c
fix(minify): read a parameter every call passes one constant as that …
alexander-akait Nov 20, 2026
4f1db97
fix(minify): fold the typeof of a name holding an object or function …
alexander-akait Nov 20, 2026
9e8fdd0
fix(minify): print a call passing nothing as the typeof its function …
alexander-akait Nov 20, 2026
e112498
fix(minify): fold the value a dropped write prints, a function's name…
alexander-akait Nov 20, 2026
2f6be50
fix(minify): join what a loop leaving at once declares past its break…
alexander-akait Nov 20, 2026
10dad71
fix(minify): count nothing a folded delete's operand reads
alexander-akait Nov 20, 2026
310c462
fix(minify): fold a regular expression literal's flag reads
alexander-akait Nov 20, 2026
0d1ab52
fix(minify): drop the boolean right side of a logical left read only …
alexander-akait Nov 20, 2026
93f1e1c
fix(minify): print every call of a function only called as the litera…
alexander-akait Nov 20, 2026
ed8627c
fix(minify): print a reduced call's postfix update without parentheses
alexander-akait Nov 20, 2026
133626b
fix(minify): keep printing a statement's unread call in place past it…
alexander-akait Nov 20, 2026
789d6b0
fix(minify): read a sloppy block's function name as unset before its …
alexander-akait Nov 20, 2026
a671241
test(minify): update the invariants expectations the merged fixes change
alexander-akait Nov 20, 2026
8ec26a4
fix(minify): drop more of an unread class: new of an inert declared c…
alexander-akait Nov 20, 2026
a6eddaa
fix(minify): drop a name only unread updates reference, with its updates
alexander-akait Nov 20, 2026
b6aa012
fix(minify): fold typeof of a function a body declares, and keep a de…
alexander-akait Nov 20, 2026
0c536f6
fix(minify): print a var setting plain parameters as assignments
alexander-akait Nov 20, 2026
9376d0a
fix(minify): drop an if past an exit, keeping the vars it declares
alexander-akait Nov 20, 2026
21ee8de
fix(minify): drop a var declarator of no value naming one before it
alexander-akait Nov 20, 2026
df5c212
fix(minify): print the one read of a rest parameter passed nothing as []
alexander-akait Nov 20, 2026
e69ba44
fix(minify): print a var declared again as what it sets
alexander-akait Nov 20, 2026
fe9eba7
fix(minify): print an unsafe RegExp call of strings as a literal
alexander-akait Nov 20, 2026
144c668
fix(minify): join a break naming its own loop's label into the loop's…
alexander-akait Nov 20, 2026
5e0387b
fix(minify): print an if leaving a labeled block by break as its else
alexander-akait Nov 20, 2026
7a5567b
fix(minify): print an if whose else leaves as a guard of the rest
alexander-akait Nov 20, 2026
0353c42
fix(minify): join neighboring using declarations of one kind
alexander-akait Nov 20, 2026
acfba07
fix(minify): print an if whose else throws or returns a value as a gu…
alexander-akait Nov 20, 2026
4d9c8db
fix(minify): keep an if as it is where its body guarded prints nothing
alexander-akait Nov 20, 2026
e9f3beb
fix(minify): read a sloppy block's function name as set only once its…
alexander-akait Nov 20, 2026
d327f2f
test(minify): cover a sloppy block's function read before its block runs
alexander-akait Nov 20, 2026
1cc8bf7
test(minify): retire the corpus rows the one-pass fixes close
alexander-akait Nov 20, 2026
2eb3e25
test(minify): retire the swc and oxc corpus rows the one-pass fixes c…
alexander-akait Nov 20, 2026
91e874e
chore(schemas): regenerate the precompiled checks the minifier prints
alexander-akait Nov 20, 2026
d400da1
test(minify): expect the shorter output the merged fixes print
alexander-akait Nov 20, 2026
b0cf3e1
fix(minify): read the arrows option a method printed as an arrow follows
alexander-akait Nov 20, 2026
7f9a1cc
chore: fold the one-pass minifier gaps into their changeset
alexander-akait Nov 21, 2026
a1e513f
test(minify): record the jsMinify source maps with this branch's webpack
alexander-akait Nov 21, 2026
4ee5ba1
chore(types): declare the unreached writes a minifier variable counts
alexander-akait Nov 21, 2026
1a184bf
fix(minify): type a regular expression's flag read without unknown
alexander-akait Nov 21, 2026
543ccc7
fix(minify): drop a labeled switch whose cases never run with its label
alexander-akait Nov 21, 2026
312b205
fix(minify): skip only a dropped switch where its label goes
alexander-akait Nov 21, 2026
32517c5
fix(minify): select native statics by each print's built-in edition
alexander-akait Nov 21, 2026
d2990b0
fix(minify): keep a value running code out of a logical assignment
alexander-akait Nov 21, 2026
d511dcf
perf(minify): track a var's declared names in a set
alexander-akait Nov 21, 2026
4633021
test(minify): expect the known rename-only pass on the logical assign…
alexander-akait Nov 21, 2026
09ffe97
test(minify): snapshot the native static edition fixture's prints
alexander-akait Nov 21, 2026
7da7628
fix(minify): drop a block whose every statement goes with its separator
alexander-akait Nov 21, 2026
1065e26
test(minify): name the block function fixture's second function apart…
alexander-akait Nov 21, 2026
a668a7f
fix(minify): keep a RegExp call whose source the output edition rejects
alexander-akait Nov 21, 2026
ade5bcb
fix(minify): keep a regexp's flags read after a flag accessor write
alexander-akait Nov 21, 2026
6abaaaa
fix(minify): parenthesize a boolean left that prints as an operation
alexander-akait Nov 21, 2026
a8c74c3
fix(minify): keep a member read of a quoted __proto__ key
alexander-akait Nov 21, 2026
179be2c
fix(minify): move an unreached write's count from an alias to its target
alexander-akait Nov 21, 2026
7b797f1
fix(minify): keep a computed pattern key's name read when its write n…
alexander-akait Nov 21, 2026
ec70530
fix(minify): keep a name read in its own initializer unfolded
alexander-akait Nov 21, 2026
30b1b8a
fix(minify): fold a typeof of a switch's let only after it in its own…
alexander-akait Nov 21, 2026
0da755b
test(minify): retire the oxc rows and the swc exec budget the fixes c…
alexander-akait Nov 21, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
fix(minify): drop a var only a switch case that never runs sets
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@alexander-akait
alexander-akait committed Nov 21, 2026
commit 82b2c3544dff3f28baf12e6fddbfbd2df595ba80

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

135 changes: 115 additions & 20 deletions 135 lib/javascript/syntax-printer.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13152,6 +13152,10 @@ const createNodePrinter = (modules) => {
// which declare their names and never set them.
/** @type {Set<number>} */
const deadInits = new Set();
// The cases of a `switch` on a literal before the one it enters, which
// never run.
/** @type {Set<number>} */
const deadCases = new Set();
// Each such read guarding the one write, `S || (S = {})`, and its `var`,
// which folds once the walk finds it declared bare in the function's body.
/** @type {(number | MinifierVariable)[]} */
Expand Down Expand Up @@ -19529,6 +19533,101 @@ const createNodePrinter = (modules) => {
return true;
};

/**
* terser's `reduce_vars`: a `var` declared only in code that goes, which
* nothing sets and one read at most reads, needs no declaration left.
* @param {number[]} names the names declared in code that goes, those declared still kept in place
* @returns {MinifierVariable[]} the definitions of those taken out, each read printing `undefined`
*/
const takeVoidedNames = (names) => {
/** @type {MinifierVariable[]} */
const voided = [];
if (!compresses || keepsGlobalNames || undefinedNames.size === 0) {
return voided;
}
let count = 0;
for (const name of names) {
const definition = /** @type {TreeDefinition[]} */ (mangledNames)[name];
if (
isMinifierVariable(definition) &&
undefinedNames.has(definition) &&
definition.identifiers.length <= 1 &&
definition.referenceCount <= 1 &&
!namedReads.has(definition)
) {
voided.push(definition);
} else {
names[count++] = name;
}
}
names.length = count;
return voided;
};

/**
* @param {number} statement a statement
* @returns {boolean} whether it is a `var` of plain names holding no comment
*/
const declaresVarNamesOnly = (statement) =>
typeOf(statement) === NodeType.VariableDeclaration &&
A.textOf(Part.kind, statement) === "var" &&
!holdsComments(statement) &&
declaresNamesOnly(statement);

/**
* terser's `dead_code` of a `var` that never runs: it declares its names,
* setting none.
* @param {number} statement the `var`
* @param {number[]} candidates the declarators that may print nothing
* @returns {void}
*/
const markDeadInits = (statement, candidates) => {
const declarations = A.fieldCount(Field.declarations, statement);
for (let j = 0; j < declarations; j++) {
const declarator = A.field(j, Field.declarations, statement);
const init = childOf(Field.init, declarator);
if (init !== 0) {
deadInits.add(declarator);
findNode(init, discountRead);
}
candidates.push(declarator);
keepsEffect[declarator] = 2;
}
};

/**
* terser's `dead_code` of a `switch` on a literal: the cases before the one
* it enters, the one matching or else the `default`, never run.
* @param {number} node the `switch`
* @returns {void}
*/
const noteDeadCases = (node) => {
if (!dropsDeadCode) return;
const discriminant = childOf(Field.discriminant, node);
const value = isLiteralTree(discriminant)
? evaluateConstant(discriminant)
: undefined;
if (value === undefined) return;
const count = A.fieldCount(Field.cases, node);
let start = -1;
let fallback = count;
for (let i = 0; i < count && start === -1; i++) {
const test = childOf(Field.test, A.field(i, Field.cases, node));
if (test === 0) {
fallback = i;
continue;
}
// A test it may match or one running code leaves the rest unknown.
const tested = isLiteralTree(test) ? evaluateConstant(test) : undefined;
if (tested === undefined) return;
if (tested === value) start = i;
}
if (start === -1) start = fallback;
for (let i = 0; i < start; i++) {
deadCases.add(A.field(i, Field.cases, node));
}
};

/**
* @param {number} read a read noted as `undefined`
* @returns {void}
Expand Down Expand Up @@ -20073,6 +20172,7 @@ const createNodePrinter = (modules) => {
const call = optionalCallOf(node);
if (call !== node) noteUnreadCall(call, parent);
} else if (type === NodeType.SwitchStatement) {
noteDeadCases(node);
laterSwitches.push(node, parent);
} else if (type === NodeType.SequenceExpression) {
// Its items run one after another, each folding into the next, where
Expand Down Expand Up @@ -20337,14 +20437,18 @@ const createNodePrinter = (modules) => {
// A case's statements run in order once it is entered.
const count = A.fieldCount(Field.consequent, node);
// Whether a statement before always leaves, terser's `dead_code`.
let exited = false;
let exited = deadCases.size !== 0 && deadCases.has(node);
for (let i = 0; i < count; i++) {
const statement = A.field(i, Field.consequent, node);
if (exited && neverReached(statement)) {
dropped[statement] = 1;
findNode(statement, discountRead);
continue;
}
if (exited && declaresVarNamesOnly(statement)) {
markDeadInits(statement, candidates);
continue;
}
if (dropsDeadCode && exitsAlways(statement)) exited = true;
// Another case may read a `let` or `const` in its dead zone; one
// nothing names goes as a list's does.
Expand Down Expand Up @@ -20401,25 +20505,8 @@ const createNodePrinter = (modules) => {
findNode(statement, discountRead);
continue;
}
if (
exited &&
typeOf(statement) === NodeType.VariableDeclaration &&
A.textOf(Part.kind, statement) === "var" &&
!holdsComments(statement) &&
declaresNamesOnly(statement)
) {
// terser's `dead_code`: the `var` declares its names, setting none.
const declarations = A.fieldCount(Field.declarations, statement);
for (let j = 0; j < declarations; j++) {
const declarator = A.field(j, Field.declarations, statement);
const init = childOf(Field.init, declarator);
if (init !== 0) {
deadInits.add(declarator);
findNode(init, discountRead);
}
candidates.push(declarator);
keepsEffect[declarator] = 2;
}
if (exited && declaresVarNamesOnly(statement)) {
markDeadInits(statement, candidates);
continue;
}
if (dropsDeadCode && exitsAlways(statement)) exited = true;
Expand Down Expand Up @@ -24122,6 +24209,11 @@ const createNodePrinter = (modules) => {
// Sloppy code declares a function an `if` holds only once that runs.
if (typeOf(kept) === NodeType.FunctionDeclaration) return;
keptBranches.set(statement, kept);
if (hoisted !== undefined) {
for (const definition of takeVoidedNames(hoisted)) {
voidedVars.add(definition);
}
}
if (hoisted !== undefined && hoisted.length !== 0) {
hoistedFromDropped.set(statement, hoisted);
for (const name of hoisted) {
Expand Down Expand Up @@ -24406,6 +24498,7 @@ const createNodePrinter = (modules) => {
hoisted.push(name);
}
}
const voided = takeVoidedNames(hoisted);
// A label needs a statement to label; what runs first joins expressions.
if (
(kept.size === 0 &&
Expand All @@ -24429,6 +24522,7 @@ const createNodePrinter = (modules) => {
}
}
foldedSwitches.set(node, kept);
for (const definition of voided) voidedVars.add(definition);
if (tests) {
testedSwitches.add(node);
// What it tests prints as a statement's expression.
Expand Down Expand Up @@ -26931,6 +27025,7 @@ const createNodePrinter = (modules) => {
readInDeadZone.clear();
unsetReads.clear();
deadInits.clear();
deadCases.clear();
readsAsObject.clear();
readsNested.clear();
nestedReaders.clear();
Expand Down
10 changes: 10 additions & 0 deletions 10 test/fixtures/javascript/one-pass-dead-case-var/expected.json
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"relation": "idempotence",
"contains": "rename-only",
"presets": [
"default"
],
"why": "known bug: the mangler ranks the characters it names from by how often the source holds them, counted before the one pass prints, so the code compressing rewrites or drops still counts: its output ranks them otherwise and a second pass renames"
}
]
42 changes: 42 additions & 0 deletions 42 test/fixtures/javascript/one-pass-dead-case-var/index.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function before() {
switch (1) {
case 0:
var a = true;
break;
default:
if (typeof a === "undefined") console.log("PASS");
else console.log("FAIL");
}
}
function matched() {
switch ("x") {
case "y":
var b = 1;
break;
case "x":
console.log(b);
}
}
function fallsThrough() {
switch (9) {
case 1:
var c = 1;
default:
console.log(c);
case 2:
var d = 2;
}
console.log(c, d);
}
function readTwice() {
switch (2) {
default:
var e = 1;
case 2:
console.log(e, e);
}
}
before();
matched();
fallsThrough();
readTwice();
1 change: 1 addition & 0 deletions 1 test/fixtures/javascript/one-pass-dead-case-var/options.json
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"mode":"minify","compress":{"toplevel":true},"mangle":false,"behavior":true},{"mode":"minify","compress":{},"mangle":false}]
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.