Stop mutate doc.parts when printing fill (#13315)

Co-authored-by: Alexander Kachkaev <alexander@kachkaev.ru>
This commit is contained in:
fisker CheungandAlexander Kachkaev authored and GitHub committed 2024-07-26 19:51:00 +08:00
1 parent 407f1a0056
commit c60eb83d59
6 files changed
+98 -10

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
// TODO: Find a better way to test the performance
import { fill, join, line } from "../../src/document/builders.js";
import { printDocToString } from "../../src/document/printer.js";
test("`printDocToString` should not manipulate docs", () => {
const printOptions = { printWidth: 40, tabWidth: 2 };
const doc = fill(
join(
line,
Array.from({ length: 255 }, (_, index) => String(index + 1)),
),
);
expect(doc.parts.length).toBe(255 + 254);
const { formatted: firstPrint } = printDocToString(doc, printOptions);
expect(doc.parts.length).toBe(255 + 254);
const { formatted: secondPrint } = printDocToString(doc, printOptions);
expect(firstPrint).toBe(secondPrint);
{
// About 1000 lines, #3263
const WORD = "word";
const hugeParts = join(
line,
Array.from(
{ length: 1000 * Math.ceil(printOptions.printWidth / WORD.length) },
() => WORD,
),
);
const orignalLength = hugeParts.length;
const startTime = performance.now();
const { formatted } = printDocToString(fill(hugeParts), printOptions);
const endTime = performance.now();
expect(hugeParts.length).toBe(orignalLength);
const lines = formatted.split("\n");
expect(lines.length).toBeGreaterThan(1000);
expect(endTime - startTime).toBeLessThan(1000);
}
});