Skip to content

Commit a890b28

Browse files
alexander-akaitclaude
authored andcommitted
fix(javascript): keep what the one pass dropped wrongly, and allocate less per print
test262 under future defaults showed the one pass dropping what runs or throws: an unread `using` binding, which is disposed of; a comparison or computed key converting an object under `pure_conversions: false`; a write to a class's or a strict function expression's own name, or to a `let` in its dead zone; a derived constructor's `this` before `super()`; and an async generator's `return undefined`, which awaits. A direct `eval` in a class or function a call returns keeps the call. A global read in a test no longer crashes the folding. The token table is sized to the tree once, the arrays by node are kept from one print to the next, and the printer's node states widen once. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent 3d1662a commit a890b28

3 files changed

Lines changed: 271 additions & 63 deletions

File tree

‎lib/javascript/side-effects.js‎

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const { Field, Flag, NodeType, Part } = require("./syntax-parser");
2121
* @property {(node: number) => PrimitiveKind | undefined} nameKind what a name's value converts as, undefined where it is not known
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
24+
* @property {boolean=} comparesConvert whether a comparison or an object's computed key converting what may be an object runs code, as the compressor's `pure_conversions: false` has it
2425
* @property {((node: number) => boolean | undefined)=} decides what the caller answers for a node before the rules read it, undefined to leave it to them
2526
* @property {((node: number) => number)=} readValue the value a name prints as where it is read, 0 for none
2627
*/
@@ -128,6 +129,29 @@ const unwrap = (tree, node) => {
128129
return node;
129130
};
130131

132+
/**
133+
* @param {SyntaxReader} tree the tree
134+
* @param {number} node an expression
135+
* @returns {boolean} whether it is `null`, `undefined` or `void` of a literal
136+
*/
137+
const isNullish = (tree, node) => {
138+
node = unwrap(tree, node);
139+
switch (tree.type(node)) {
140+
case NodeType.Literal:
141+
return tree.textOf(Part.raw, node) === "null";
142+
case NodeType.Identifier:
143+
return tree.name(node) === "undefined";
144+
case NodeType.UnaryExpression:
145+
return (
146+
tree.textOf(Part.operator, node) === "void" &&
147+
tree.type(unwrap(tree, tree.field(0, Field.argument, node))) ===
148+
NodeType.Literal
149+
);
150+
default:
151+
return false;
152+
}
153+
};
154+
131155
/**
132156
* terser's `_dot_throw` where `pure_getters` is "strict", the default: only
133157
* a value known to be no `null`, `undefined` or object with a getter is safe.
@@ -418,6 +442,30 @@ const binaryConverts = (tree, operator, left, right, context) => {
418442
return !(becomesNumber(leftKind) && becomesNumber(rightKind));
419443
};
420444

445+
/**
446+
* The compressor's `comparesUnknown`: a strict comparison converts nothing,
447+
* and nothing equals `null` or `undefined` loosely but by its type.
448+
* @param {SyntaxReader} tree the tree
449+
* @param {string} operator a comparison's operator
450+
* @param {number} left its left side
451+
* @param {number} right its right side
452+
* @param {SideEffectsContext} context what names and annotations do
453+
* @returns {boolean} whether it may convert what may be an object, running its methods
454+
*/
455+
const comparisonConverts = (tree, operator, left, right, context) => {
456+
if (operator === "===" || operator === "!==") return false;
457+
if (
458+
(operator === "==" || operator === "!=") &&
459+
(isNullish(tree, left) || isNullish(tree, right))
460+
) {
461+
return false;
462+
}
463+
return (
464+
convertedKind(tree, left, context) === undefined ||
465+
convertedKind(tree, right, context) === undefined
466+
);
467+
};
468+
421469
/**
422470
* @param {SyntaxReader} tree the tree
423471
* @param {number} field a list `Field`
@@ -448,11 +496,15 @@ const listHasSideEffects = (tree, field, node, context) => {
448496
* @returns {boolean} whether defining it has effects
449497
*/
450498
const memberHasSideEffects = (tree, node, valueRuns, context) => {
451-
if (
452-
tree.flag(Flag.computed, node) &&
453-
hasSideEffects(tree, tree.field(0, Field.key, node), context)
454-
) {
455-
return true;
499+
if (tree.flag(Flag.computed, node)) {
500+
const key = tree.field(0, Field.key, node);
501+
if (
502+
(context.comparesConvert === true &&
503+
convertedKind(tree, key, context) === undefined) ||
504+
hasSideEffects(tree, key, context)
505+
) {
506+
return true;
507+
}
456508
}
457509
const value = tree.field(0, Field.value, node);
458510
return valueRuns && value !== 0 && hasSideEffects(tree, value, context);
@@ -550,6 +602,13 @@ const ruledSideEffects = (tree, node, context) => {
550602
const right = tree.field(0, Field.right, node);
551603
// `in` and `instanceof` throw on what is no object, or run a getter.
552604
if (operator === "in" || operator === "instanceof") return true;
605+
if (
606+
context.comparesConvert === true &&
607+
RELATION_OPERATORS.has(operator) &&
608+
comparisonConverts(tree, operator, left, right, context)
609+
) {
610+
return true;
611+
}
553612
if (
554613
context.pureConversions !== true &&
555614
!RELATION_OPERATORS.has(operator) &&

0 commit comments

Comments
 (0)