Skip to content

Commit 3bc0878

Browse files
alexander-akaitclaude
authored andcommitted
perf(javascript): drop else after leaving, tail breaks, fold into members
In the one pass, after a body that always leaves, what the `else` holds prints after the `if` where a list holds it; a `switch` drops the `break` ending it and a `default` doing nothing last; and a value folds into a member assignment whose object and key are stable, and into `import()`. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent d3dc1b0 commit 3bc0878

2 files changed

Lines changed: 201 additions & 7 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 182 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63541,6 +63541,17 @@ const createNodePrinter = (modules) => {
6354163541
}
6354263542
};
6354363543

63544+
/**
63545+
* @param {number} node what an assignment sets, not a name
63546+
* @param {MinifierVariable} definition the name being folded in
63547+
* @returns {boolean} whether it is a member whose object and key are stable
63548+
*/
63549+
const isStableTarget = (node, definition) =>
63550+
typeOf(node) === NodeType.MemberExpression &&
63551+
isStable(childOf(Field.object, node), definition) &&
63552+
(!tree.flag(Flag.computed, node) ||
63553+
isStable(childOf(Field.property, node), definition));
63554+
6354463555
/**
6354563556
* @param {number} field the list's field
6354663557
* @param {number} node what holds the list
@@ -63634,12 +63645,14 @@ const createNodePrinter = (modules) => {
6363463645
node = childOf(Field.left, node);
6363563646
break;
6363663647
case NodeType.AssignmentExpression: {
63637-
// A compound assignment reads its name before its value.
63648+
// A compound assignment reads its target before its value, and
63649+
// a member's object and key are evaluated before it.
6363863650
const left = childOf(Field.left, node);
6363963651
if (
6364063652
tree.textOf(Part.operator, node) !== "=" ||
63641-
typeOf(left) !== NodeType.Identifier ||
63642-
names[left] === definition
63653+
(typeOf(left) === NodeType.Identifier
63654+
? names[left] === definition
63655+
: !isStableTarget(left, definition))
6364363656
) {
6364463657
return 0;
6364563658
}
@@ -63678,6 +63691,9 @@ const createNodePrinter = (modules) => {
6367863691
case NodeType.AwaitExpression:
6367963692
node = childOf(Field.argument, node);
6368063693
break;
63694+
case NodeType.ImportExpression:
63695+
node = childOf(Field.source, node);
63696+
break;
6368163697
case NodeType.SequenceExpression:
6368263698
node = firstUnstable(Field.expressions, node, definition);
6368363699
break;
@@ -63998,6 +64014,8 @@ const createNodePrinter = (modules) => {
6399864014
}
6399964015
if (statement === 0) return 0;
6400064016
const type = typeOf(statement);
64017+
// An `if` whose `else` prints after it is two statements.
64018+
if (type === NodeType.IfStatement && liftsElse(statement, block)) return 0;
6400164019
if (
6400264020
type === NodeType.VariableDeclaration
6400364021
? tree.textOf(Part.kind, statement) !== "var" || printsApart(statement)
@@ -64175,6 +64193,138 @@ const createNodePrinter = (modules) => {
6417564193
if (!branch) output.semicolon();
6417664194
};
6417764195

64196+
// What holds a list of statements, by `NodeType`; a function's body
64197+
// statements are walked with the function as their parent.
64198+
const LISTS_STATEMENTS = new Uint8Array(256);
64199+
for (const type of [
64200+
NodeType.Program,
64201+
NodeType.BlockStatement,
64202+
NodeType.StaticBlock,
64203+
NodeType.SwitchCase
64204+
]) {
64205+
LISTS_STATEMENTS[type] = 1;
64206+
}
64207+
64208+
/**
64209+
* terser's `if_return` of an `else`: after a body that always leaves, what
64210+
* the `else` holds prints after the `if`, in the list the `if` is in.
64211+
* @param {number} node an `if` with an `else`
64212+
* @param {number} parent what holds it
64213+
* @returns {boolean} whether its `else` prints so
64214+
*/
64215+
const liftsElse = (node, parent) => {
64216+
const parentType = typeOf(parent);
64217+
if (
64218+
(LISTS_STATEMENTS[parentType] !== 1 &&
64219+
FUNCTION_TYPES[parentType] !== 1) ||
64220+
ifForm(node) !== 0
64221+
) {
64222+
return false;
64223+
}
64224+
const alternate = tree.field(0, Field.alternate, node);
64225+
return (
64226+
!hasEdgeComments(alternate) &&
64227+
alwaysLeaves(tree.field(0, Field.consequent, node)) &&
64228+
(typeOf(alternate) === NodeType.BlockStatement
64229+
? !scopesLexically(alternate)
64230+
: !declaresLexically(alternate))
64231+
);
64232+
};
64233+
64234+
/**
64235+
* @param {number} statement a statement
64236+
* @returns {boolean} whether control never runs past it: a `return`, `throw`, `break` or `continue`, last of a block
64237+
*/
64238+
const alwaysLeaves = (statement) => {
64239+
let last = statement;
64240+
if (typeOf(statement) === NodeType.BlockStatement) {
64241+
last = 0;
64242+
for (let i = tree.fieldCount(Field.body, statement) - 1; i >= 0; i--) {
64243+
const item = tree.field(i, Field.body, statement);
64244+
if (!printsNothing(item)) {
64245+
last = item;
64246+
break;
64247+
}
64248+
}
64249+
}
64250+
switch (typeOf(last)) {
64251+
case NodeType.ReturnStatement:
64252+
case NodeType.ThrowStatement:
64253+
case NodeType.BreakStatement:
64254+
case NodeType.ContinueStatement:
64255+
return true;
64256+
default:
64257+
return false;
64258+
}
64259+
};
64260+
64261+
/**
64262+
* @param {number} statement a statement
64263+
* @returns {boolean} whether it declares a name only its block sees: `let`, `const`, `using`, a class or a function
64264+
*/
64265+
const declaresLexically = (statement) => {
64266+
switch (typeOf(statement)) {
64267+
case NodeType.VariableDeclaration:
64268+
return tree.textOf(Part.kind, statement) !== "var";
64269+
case NodeType.ClassDeclaration:
64270+
case NodeType.FunctionDeclaration:
64271+
return true;
64272+
default:
64273+
return false;
64274+
}
64275+
};
64276+
64277+
/**
64278+
* @param {number} block a block
64279+
* @returns {boolean} whether its braces scope a name
64280+
*/
64281+
const scopesLexically = (block) => {
64282+
const count = tree.fieldCount(Field.body, block);
64283+
for (let i = 0; i < count; i++) {
64284+
if (declaresLexically(tree.field(i, Field.body, block))) return true;
64285+
}
64286+
return false;
64287+
};
64288+
64289+
/**
64290+
* @param {number} statement a statement
64291+
* @returns {boolean} whether it is a `break` of no label, with no comment kept
64292+
*/
64293+
const isPlainBreak = (statement) =>
64294+
typeOf(statement) === NodeType.BreakStatement &&
64295+
tree.field(0, Field.label, statement) === 0 &&
64296+
!hasEdgeComments(statement);
64297+
64298+
/**
64299+
* terser's `dead_code` of a `switch`: a `default` last, doing nothing.
64300+
* @param {number} item a case
64301+
* @returns {boolean} whether it goes
64302+
*/
64303+
const doesNothing = (item) => {
64304+
if (childOf(Field.test, item) !== 0 || hasEdgeComments(item)) return false;
64305+
const count = tree.fieldCount(Field.consequent, item);
64306+
return (
64307+
count === 0 ||
64308+
(count === 1 && isPlainBreak(tree.field(0, Field.consequent, item)))
64309+
);
64310+
};
64311+
64312+
/**
64313+
* @param {number} item a case being printed
64314+
* @returns {boolean} whether it is the last its `switch` prints, where a `break` ending it does nothing
64315+
*/
64316+
const endsSwitch = (item) => {
64317+
const parent = parentAt(0);
64318+
const count = tree.fieldCount(Field.cases, parent);
64319+
const last = tree.field(count - 1, Field.cases, parent);
64320+
return (
64321+
last === item ||
64322+
(count > 1 &&
64323+
tree.field(count - 2, Field.cases, parent) === item &&
64324+
doesNothing(last))
64325+
);
64326+
};
64327+
6417864328
/**
6417964329
* terser's `make_then`: a parse binds each `else` to the nearest `if`, so
6418064330
* no consequent needs braces to keep it, but `ie8`'s `do … while`.
@@ -65071,6 +65221,15 @@ const createNodePrinter = (modules) => {
6507165221
};
6507265222
generators[NodeType.BlockStatement] = (node) => {
6507365223
const parent = parentAt(0);
65224+
if (
65225+
compresses &&
65226+
typeOf(parent) === NodeType.IfStatement &&
65227+
tree.field(0, Field.alternate, parent) === node &&
65228+
liftsElse(parent, parentAt(1))
65229+
) {
65230+
displayBody(node);
65231+
return;
65232+
}
6507465233
const statement = compresses ? unwrappedStatement(node, parent) : 0;
6507565234
if (statement !== 0) {
6507665235
const count = tree.fieldCount(Field.body, node);
@@ -65240,6 +65399,11 @@ const createNodePrinter = (modules) => {
6524065399
printMaybeBracedBody(tree.field(0, Field.consequent, node));
6524165400
return;
6524265401
}
65402+
if (compresses && liftsElse(node, parentAt(0))) {
65403+
printMaybeBracedBody(tree.field(0, Field.consequent, node));
65404+
printNode(alternate);
65405+
return;
65406+
}
6524365407
makeThen(node);
6524465408
output.space();
6524565409
output.print("else");
@@ -65258,7 +65422,14 @@ const createNodePrinter = (modules) => {
6525865422
return;
6525965423
}
6526065424
output.print("{");
65261-
for (let i = 0; i < count; i++) printNode(tree.field(i, Field.cases, node));
65425+
for (let i = 0; i < count; i++) {
65426+
const item = tree.field(i, Field.cases, node);
65427+
if (i === count - 1 && i > 0 && compresses && doesNothing(item)) {
65428+
/** @type {JavascriptPrintContext} */ (writer).consume(item);
65429+
} else {
65430+
printNode(item);
65431+
}
65432+
}
6526265433
output.print("}");
6526365434
};
6526465435
generators[NodeType.SwitchCase] = (node) => {
@@ -65272,8 +65443,14 @@ const createNodePrinter = (modules) => {
6527265443
output.print("default:");
6527365444
}
6527465445
const count = tree.fieldCount(Field.consequent, node);
65446+
const last = count > 0 && compresses && endsSwitch(node) ? count - 1 : -1;
6527565447
for (let i = 0; i < count; i++) {
65276-
printNode(tree.field(i, Field.consequent, node));
65448+
const statement = tree.field(i, Field.consequent, node);
65449+
if (i === last && isPlainBreak(statement)) {
65450+
/** @type {JavascriptPrintContext} */ (writer).consume(statement);
65451+
} else {
65452+
printNode(statement);
65453+
}
6527765454
}
6527865455
};
6527965456
generators[NodeType.TryStatement] = (node) => {

‎test/unitCases/javascript/printer.unittest.js‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15152,9 +15152,9 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1515215152
],
1515315153
// A branch that is no expression keeps the `if`, a sequence test is parenthesized, and a dead `if` declaring only in its own scopes goes.
1515415154
[
15155-
"function f(a, b){ const T = 1; if (a) throw 1; else y(); if ((a, b)) x(); else y(); if (0) { let w = () => { var z } } g([T ? a : 2]) }",
15155+
"function f(a, b){ const T = 1; if (a) for (;;) g(); else y(); if ((a, b)) x(); else y(); if (0) { let w = () => { var z } } g([T ? a : 2]) }",
1515615156
{},
15157-
"function f(a,e){if(a)throw 1;else y();(a,e)?x():y();g([a])}"
15157+
"function f(f,a){if(f)for(;;)g();else y();(f,a)?x():y();g([f])}"
1515815158
],
1515915159
// A statement that runs nothing goes, but not a directive, a comment kept, a `#__PURE__` call with `side_effects: false` nor a global a `try` probes for.
1516015160
[
@@ -15201,6 +15201,23 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1520115201
{},
1520215202
"function f(){let d;j(i(h(g())),d)}"
1520315203
],
15204+
// After a body that always leaves, what the `else` holds prints after the `if` where a list holds it, but not a block scoping a name or keeping a comment.
15205+
[
15206+
"function f(a, b){ if (a) return 1; else b(); if (b) { x(); return 2 } else { y(); z() } if (a) throw 1; else { let q = g(); h(q) } while (a) { if (b) break; else w() } for (;;) { if (a) continue; else { /*! c */ v() } } if (b) return; else if (a) v(); else u(); } for (;;) if (a) break; else b(); l: if (a) break l; else b(); switch (a) { case 1: if (b) break; else c() }",
15207+
{},
15208+
"function f(e,a){if(e)return 1;a();if(a){x();return 2}y();z();if(e)throw 1;h(g());while(e){if(a)break;w()}for(;;)if(e)continue;else{/*! c */v()}if(a)return;e?v():u()}for(;;)if(a)break;else b();e:if(a)break e;else b();switch(a){case 1:if(b)break;c()}"
15209+
],
15210+
[
15211+
"function f(a){ if (a) return 1; else { class C {} g(new C) } if (a) return 2; else { function q(){} q() } if (a) return 3; else /*! c */ { h() } }",
15212+
{},
15213+
"function f(e){if(e)return 1;else{class f{}g(new f)}if(e)return 2;else{function n(){}n()}if(e)return 3;else/*! c */{h()}}"
15214+
],
15215+
// A `switch` drops a `break` ending it and a `default` doing nothing last, and a value folds into `import()`.
15216+
[
15217+
"function f(c){ switch (c) { case -1: c = 1; break; case 0: c = 2; break; default: break } switch (c) { case 1: x(); break } switch (c) { default: break } switch (c) { case 1: y(); break; default: } a: switch (c) { case 1: z(); break a } switch (c) { case 1: y(); /*! c */ break } var i = r(c); return import(i) }",
15218+
{},
15219+
"function f(c){switch(c){case-1:c=1;break;case 0:c=2}switch(c){case 1:x()}switch(c){default:}switch(c){case 1:y()}c:switch(c){case 1:z();break c}switch(c){case 1:y();/*! c */break}return import(r(c))}"
15220+
],
1520415221
[
1520515222
"function f(a){ if (0) x(); Math.PI }",
1520615223
{ compress: {toplevel: false,builtins_ecma: 2020},format: {comments: false} },

0 commit comments

Comments
 (0)