Skip to content

Commit 1b98b3e

Browse files
fix(javascript): print a function moved into one printed first where that one prints (#23050)
A function declaration read once before it is declared prints at its read, the walk printing it first. One read inside such a function prints when that function does, so a function it reads in turn must be printed by then; minifying TypeScript's own `typescript.js` threw `Cannot read properties of undefined (reading 'length')`. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent 2bc96ba commit 1b98b3e

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11725,6 +11725,24 @@ const createNodePrinter = (modules) => {
1172511725
return true;
1172611726
};
1172711727

11728+
/**
11729+
* Where the walk prints the function a read prints first: at the read, or
11730+
* where a function printed first holding the read is printed.
11731+
* @param {number} read a read in `earlyDefuns`
11732+
* @returns {number} the offset the walk prints it at
11733+
*/
11734+
const earlyPrintStart = (read) => {
11735+
for (const [outerRead, outer] of earlyDefuns) {
11736+
if (
11737+
tree.start(read) > tree.start(outer) &&
11738+
tree.end(read) <= tree.end(outer)
11739+
) {
11740+
return earlyPrintStart(outerRead);
11741+
}
11742+
}
11743+
return tree.start(read);
11744+
};
11745+
1172811746
/**
1172911747
* Finds what prints nothing, terser's `unused`: a declaration of a name
1173011748
* only what prints nothing reads, found again until no more go.
@@ -11911,13 +11929,14 @@ const createNodePrinter = (modules) => {
1191111929
// A function printed ahead of its place prints what it reads moved
1191211930
// there too, so each must be printed by then; else it stays in place.
1191311931
for (const [read, defun] of earlyDefuns) {
11932+
const printedFrom = earlyPrintStart(read);
1191411933
for (let i = 0; i < moves.length; i += 2) {
1191511934
const inner = moves[i + 1];
1191611935
if (
1191711936
dropped[moves[i]] === 1 &&
1191811937
tree.start(inner) > tree.start(defun) &&
1191911938
tree.end(inner) <= tree.end(defun) &&
11920-
tree.end(moves[i]) > tree.start(read) &&
11939+
tree.end(moves[i]) > printedFrom &&
1192111940
!earlyDefuns.has(inner)
1192211941
) {
1192311942
dropped[defun] = 0;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,12 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
952952
{},
953953
"function f(n){n.e=t;function t(){return function(){h();return g(1)}()}return n}"
954954
],
955+
// One printed first inside another printed first prints where that one does.
956+
[
957+
"function f() { function w() { return b() } function a() { return x() } function b() { return c() } function c() { return a() } return w() }",
958+
{},
959+
"function f(){function n(){return x()}return n()}"
960+
],
955961
[
956962
"u(); function u() { return g() }",
957963
{ compress: { toplevel: true }, mangle: { toplevel: true } },

0 commit comments

Comments
 (0)