Skip to content

Commit d99fce6

Browse files
fix(javascript): keep isPure's verdicts while it shares the side-effects rules
The shared rules found more expressions pure than `isPure` did, which grew bundles: a pure binary expression with an import cost a kept `#__PURE__` comment, and one bailed module concatenation. `isPure` now leaves conversions, member reads and derived classes with a constructor to the evaluator as before, so `1n / 0n` stays impure too. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent 8884e29 commit d99fce6

10 files changed

Lines changed: 99 additions & 186 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Fold conditionals, returns and values further in the one-pass JavaScript minifier.

‎.changeset/068-shared-purity-rules.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎lib/javascript/JavascriptParser.js‎

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {
2525
LEGACY_ASSERT_ATTRIBUTES,
2626
MAX_MASKED_INDEX,
2727
MODULE_DECLARATIONS,
28+
NodeType,
2829
WebpackParser,
2930
buildLineStarts,
3031
declaresIntoBlock,
@@ -219,6 +220,73 @@ const getSideEffects = () =>
219220
? (sideEffects = require("./side-effects"))
220221
: sideEffects;
221222

223+
// What `isPure` judges by the shared rules, beside the operators below; the
224+
// evaluator judges the rest, which it alone may find constant.
225+
const RULED_PURITY_TYPES = /** @type {(keyof typeof NodeType)[]} */ ([
226+
"ArrayExpression",
227+
"ArrowFunctionExpression",
228+
"CallExpression",
229+
"ChainExpression",
230+
"ClassDeclaration",
231+
"ClassExpression",
232+
"ConditionalExpression",
233+
"FunctionDeclaration",
234+
"FunctionExpression",
235+
"Identifier",
236+
"Literal",
237+
"LogicalExpression",
238+
"MetaProperty",
239+
"NewExpression",
240+
"ObjectExpression",
241+
"PrivateIdentifier",
242+
"SequenceExpression",
243+
"TaggedTemplateExpression",
244+
"TemplateLiteral",
245+
"ThisExpression",
246+
"VariableDeclaration"
247+
]);
248+
249+
/** @type {Uint8Array | undefined} */
250+
let ruledPurityNodeTypes;
251+
252+
/**
253+
* @param {Node} node a node `isPure` reads
254+
* @param {number} type its `NodeType`
255+
* @returns {boolean} whether the evaluator judges it: an operator that may convert its operands, or a form the rules don't
256+
*/
257+
const isLeftToEvaluator = (node, type) => {
258+
switch (type) {
259+
case NodeType.UnaryExpression: {
260+
const { operator } = /** @type {UnaryExpression} */ (node);
261+
return operator !== "typeof" && operator !== "void" && operator !== "!";
262+
}
263+
case NodeType.BinaryExpression: {
264+
const { operator } = /** @type {BinaryExpression} */ (node);
265+
return operator !== "===" && operator !== "!==";
266+
}
267+
default:
268+
if (ruledPurityNodeTypes === undefined) {
269+
ruledPurityNodeTypes = new Uint8Array(256);
270+
for (const name of RULED_PURITY_TYPES) {
271+
ruledPurityNodeTypes[NodeType[name]] = 1;
272+
}
273+
}
274+
return ruledPurityNodeTypes[type] !== 1;
275+
}
276+
};
277+
278+
/**
279+
* @param {Node} node a node `isPure` reads
280+
* @returns {boolean} whether it is a class with a superclass and its own constructor, which `isPure` keeps
281+
*/
282+
const isDerivedWithConstructor = (node) =>
283+
(node.type === "ClassDeclaration" || node.type === "ClassExpression") &&
284+
Boolean(node.superClass) &&
285+
node.body.body.some(
286+
(member) =>
287+
member.type === "MethodDefinition" && member.kind === "constructor"
288+
);
289+
222290
// Syntax: https://developer.mozilla.org/en/SpiderMonkey/Parser_API
223291
let parser = WebpackParser;
224292

@@ -6574,7 +6642,7 @@ class JavascriptParser extends Parser {
65746642

65756643
/**
65766644
* The `side-effects` context of an `isPure` query: its hooks answer first,
6577-
* names and conversions run nothing, and what still may run code is evaluated.
6645+
* the evaluator judges what the rules leave to it, and names run nothing.
65786646
* @param {EstreeView} view the query's view
65796647
* @returns {SideEffectsContext} the context
65806648
*/
@@ -6594,12 +6662,14 @@ class JavascriptParser extends Parser {
65946662
/** @type {Expression} */ (object),
65956663
Math.min(this._pureCommentsStartPos, view.start(node))
65966664
);
6597-
return typeof result === "boolean" ? !result : undefined;
6598-
},
6599-
confirms: (node) =>
6600-
this.evaluateExpression(
6601-
/** @type {Expression} */ (view.object(node))
6602-
).couldHaveSideEffects()
6665+
if (typeof result === "boolean") return !result;
6666+
if (isLeftToEvaluator(object, view.type(node))) {
6667+
return this.evaluateExpression(
6668+
/** @type {Expression} */ (object)
6669+
).couldHaveSideEffects();
6670+
}
6671+
return isDerivedWithConstructor(object) || undefined;
6672+
}
66036673
};
66046674
}
66056675

‎lib/javascript/side-effects.js‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const { Field, Flag, NodeType, Part } = require("./syntax-parser");
2222
* @property {(node: number) => boolean} isPureCall whether a call or `new` is marked pure, only its arguments then running code
2323
* @property {boolean=} pureConversions whether converting a value to a primitive is taken to run no code, as webpack's `isPure` takes it
2424
* @property {((node: number) => boolean | undefined)=} decides what the caller answers for a node before the rules read it, undefined to leave it to them
25-
* @property {((node: number) => boolean)=} confirms whether a node the rules find an effect in still has one, as the caller's own evaluation may know it has not
2625
*/
2726

2827
/**
@@ -480,9 +479,6 @@ const extendsNoConstructor = (tree, node) => {
480479
}
481480
};
482481

483-
// What the last `hasSideEffects` answered, for its caller's own answer.
484-
let lastHadSideEffects = false;
485-
486482
/**
487483
* terser's `has_side_effects`, the compressor's rules, read off a
488484
* `SyntaxTree`; a spread and a `typeof` of a name in its dead zone count.
@@ -495,15 +491,9 @@ const hasSideEffects = (tree, node, context) => {
495491
node = unwrap(tree, node);
496492
if (context.decides !== undefined) {
497493
const decided = context.decides(node);
498-
if (decided !== undefined) return (lastHadSideEffects = decided);
494+
if (decided !== undefined) return decided;
499495
}
500-
// A child's effect, when the last read found one, is confirmed already.
501-
lastHadSideEffects = false;
502-
return (lastHadSideEffects =
503-
ruledSideEffects(tree, node, context) &&
504-
(context.confirms === undefined ||
505-
lastHadSideEffects ||
506-
context.confirms(node)));
496+
return ruledSideEffects(tree, node, context);
507497
};
508498

509499
/**

‎lib/javascript/syntax-parser.js‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20786,6 +20786,9 @@ class EstreeView {
2078620786
constructor() {
2078720787
/** @type {(import("estree").Node | undefined)[]} */
2078820788
this._objects = [undefined];
20789+
// Each node's `NodeType`, read when it takes its id.
20790+
/** @type {number[]} */
20791+
this._types = [0];
2078920792
this._count = 1;
2079020793
if (typeByName === undefined) {
2079120794
typeByName = new Map(Object.entries(NodeType));
@@ -20800,7 +20803,11 @@ class EstreeView {
2080020803
* @returns {number} its id in this view
2080120804
*/
2080220805
add(object) {
20806+
const type = /** @type {Map<string, number>} */ (typeByName).get(
20807+
object.type
20808+
);
2080320809
this._objects[this._count] = object;
20810+
this._types[this._count] = type === undefined ? 0 : type;
2080420811
return this._count++;
2080520812
}
2080620813

@@ -20818,7 +20825,8 @@ class EstreeView {
2081820825
*/
2081920826
reset() {
2082020827
// Kept at its size, as the next read is likely as large.
20821-
this._objects.fill(undefined, 1, this._count);
20828+
const objects = this._objects;
20829+
for (let i = 1; i < this._count; i++) objects[i] = undefined;
2082220830
this._count = 1;
2082320831
}
2082420832

@@ -20835,18 +20843,17 @@ class EstreeView {
2083520843
* @returns {number} its `NodeType`, 0 for a type the tree has none for
2083620844
*/
2083720845
type(node) {
20838-
const type = /** @type {Map<string, number>} */ (typeByName).get(
20839-
this._properties(node).type
20840-
);
20841-
return type === undefined ? 0 : type;
20846+
return this._types[node];
2084220847
}
2084320848

2084420849
/**
2084520850
* @param {number} node the node
2084620851
* @returns {number} where it starts in the input
2084720852
*/
2084820853
start(node) {
20849-
return this._properties(node).range[0];
20854+
const object = this._properties(node);
20855+
// `range` is made on first read, and only some parsers give `start`.
20856+
return typeof object.start === "number" ? object.start : object.range[0];
2085020857
}
2085120858

2085220859
/**

‎test/configCases/inner-graph/pure-expressions-shared/module.js‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎test/configCases/inner-graph/pure-expressions-shared/webpack.config.js‎

Lines changed: 0 additions & 36 deletions
This file was deleted.

‎test/external/oxc‎

Submodule oxc updated 1968 files

‎test/external/swc‎

Submodule swc updated 1134 files

‎test/unitCases/javascript/estree-view.unittest.js‎

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)