From 98c9a0dd30275c910b73fd1ac068fc43227323e9 Mon Sep 17 00:00:00 2001 From: shashwat-28410 Date: Thu, 12 Nov 2026 16:08:19 +0530 Subject: [PATCH] fix: preserve CRLF when rewriting README size table --- packages/core/test/size.test.ts | 40 +++++++++++++++++++++------------ scripts/size-table.mjs | 6 +++-- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/packages/core/test/size.test.ts b/packages/core/test/size.test.ts index 29f9321f..2875e6a5 100644 --- a/packages/core/test/size.test.ts +++ b/packages/core/test/size.test.ts @@ -8,20 +8,32 @@ const rows = [ { key: 'overlay', label: 'The badge', minified: 12_849, gzip: 4_772 }, ]; -test('the size table is written between its markers and read back to the tenth of a kilobyte', () => { - const table = formatTable(rows, 'rolldown 1.2.8'); - assert.ok(table.startsWith(START) && table.endsWith(END)); - assert.match(table, /\| `\/auto` \| 59\.0 KB \| 21\.2 KB \|/); - const readme = `# Title\n\nBefore.\n\n${START}\nold table\n${END}\n\nAfter.\n`; - const written = replaceTable(readme, table); - assert.ok(written.startsWith('# Title\n\nBefore.\n\n') && written.endsWith('\n\nAfter.\n')); - assert.deepEqual(parseTable(written), [ - { label: '`/auto`', minified: 59, gzip: 21.2 }, - { label: 'The badge', minified: 12.8, gzip: 4.8 }, - ]); - assert.equal(parseTable('# No table here'), null); - assert.throws(() => replaceTable('# No table here', table), /no /); -}); +for (const lineEnding of ['\n', '\r\n']) { + test(`the size table is written between its markers and read back to the tenth of a kilobyte (${lineEnding === '\n' ? 'LF' : 'CRLF'})`, () => { + const tool = 'rolldown 1.2.8'; + const table = formatTable(rows, tool); + assert.ok(table.startsWith(START) && table.endsWith(END)); + assert.match(table, /\| `\/auto` \| 59\.0 KB \| 21\.2 KB \|/); + const before = `# Title${lineEnding}${lineEnding}Before.${lineEnding}${lineEnding}`; + const after = `${lineEnding}${lineEnding}After.${lineEnding}`; + const readme = `${before}${START}${lineEnding}old table${lineEnding}${END}${after}`; + const written = replaceTable(readme, table); + assert.equal(written.slice(0, before.length), before); + assert.equal(written.slice(-after.length), after); + if (lineEnding === '\r\n') { + assert.equal(written.replaceAll('\r\n', '').includes('\n'), false); + } else { + assert.equal(written.includes('\r\n'), false); + } + assert.deepEqual(problems(written, rows, tool), []); + assert.deepEqual(parseTable(written), [ + { label: '`/auto`', minified: 59, gzip: 21.2 }, + { label: 'The badge', minified: 12.8, gzip: 4.8 }, + ]); + assert.equal(parseTable('# No table here'), null); + assert.throws(() => replaceTable('# No table here', table), /no /); + }); +} for (const lineEnding of ['\n', '\r\n']) { test(`the check accepts ${lineEnding === '\n' ? 'LF' : 'CRLF'} tables without relaxing stale checks`, () => { diff --git a/scripts/size-table.mjs b/scripts/size-table.mjs index 0d4f0f18..4664be3f 100644 --- a/scripts/size-table.mjs +++ b/scripts/size-table.mjs @@ -37,12 +37,14 @@ export function parseTable(readme) { return rows; } -/** `readme` with the table between the markers replaced by `table`. */ +/** `readme` with the table between the markers replaced by `table`, keeping the block's LF or CRLF line endings. */ export function replaceTable(readme, table) { const start = readme.indexOf(START); const end = readme.indexOf(END); if (start < 0 || end < start) throw new Error(`README.md has no ${START} ... ${END} block for the size table`); - return readme.slice(0, start) + table + readme.slice(end + END.length); + const crlf = readme.slice(start, end + END.length).includes('\r\n'); + const replacement = crlf ? table.replaceAll('\r\n', '\n').replaceAll('\n', '\r\n') : table.replaceAll('\r\n', '\n'); + return readme.slice(0, start) + replacement + readme.slice(end + END.length); } /** The table between the markers in `readme`, markers included, as written; null without them. */