Skip to content

Commit b5a5d8d

Browse files
fix(javascript): keep the line break after a comment whose code the minifier drops
A kept comment moved off a dropped `;` or declaration lost the line break after it, so a license banner printed `*/(function(){` where terser prints `*/\n(function(){`, and printing the output again added the break back. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4
1 parent 54ecf0c commit b5a5d8d

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7971,6 +7971,8 @@ const installMinify = (modules) => {
79717971
} else {
79727972
const next = /** @type {Token} */ (tables.of(sorted[low]));
79737973
next.comments_before = [...important, ...next.comments_before];
7974+
// The line break after them moves too, as in `keepCommentsOf`.
7975+
if (/** @type {Token} */ (tables.of(token)).nlb) next.nlb = true;
79747976
}
79757977
for (const comment of important) {
79767978
comments.splice(comments.indexOf(comment), 1);
@@ -28165,6 +28167,9 @@ const createTightenBody = (modules) => {
2816528167
if (!from || !to || from.comments_before.length === 0) return;
2816628168
to.comments_before = [...from.comments_before, ...to.comments_before];
2816728169
from.comments_before = [];
28170+
// The line break after them moves too: a banner before a dropped `;`
28171+
// still ends its line, as terser prints it.
28172+
if (from.nlb) to.nlb = true;
2816828173
}
2816928174

2817028175
/**

‎test/unitCases/syntax-printer.unittest.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6332,6 +6332,28 @@ describe("syntax-printer", () => {
63326332
).toBe(reference.code);
63336333
});
63346334

6335+
it("should keep the line break after a banner before a dropped `;`, as terser prints it", async () => {
6336+
const { minify } = await load();
6337+
const code = "/*! license\n */\n;(function () { f(); })();";
6338+
const { code: printed } = await minify(code, {});
6339+
expect(printed).toBe("/*! license\n */\nf();");
6340+
expect((await minify(/** @type {string} */ (printed), formatOnly)).code).toBe(
6341+
printed
6342+
);
6343+
expect((await terserReference().minify(code, {})).code).toBe(printed);
6344+
});
6345+
6346+
it("should keep the line break after a license whose code was dropped, wherever it moves", async () => {
6347+
const { minify } = await load();
6348+
const code =
6349+
"function g() {\n/*! license\n */\nvar n = 50; return h(0, n); }\ng();";
6350+
const { code: printed } = await minify(code, {});
6351+
expect(printed).toBe("function g(){return h(0,\n/*! license\n */\n50)}g();");
6352+
expect((await minify(/** @type {string} */ (printed), formatOnly)).code).toBe(
6353+
printed
6354+
);
6355+
});
6356+
63356357
it("should print a directive as it is spelled, where an encoding doubled each backslash", async () => {
63366358
/**
63376359
* @param {string} code a program

0 commit comments

Comments
 (0)