Skip to content

fix: count source appends and prepends in Bundle#length - #344

Open
wahidrizka wants to merge 1 commit into
Rich-Harris:masterfrom
wahidrizka:fix-bundle-length-source-edges
Open

fix: count source appends and prepends in Bundle#length#344
wahidrizka wants to merge 1 commit into
Rich-Harris:masterfrom
wahidrizka:fix-bundle-length-source-edges

Conversation

@wahidrizka

Copy link
Copy Markdown
Contributor

Bundle#length() is meant to equal Bundle#toString().length. Its existing tests assert exactly that for the default separator, a custom separator, a per-source separator, and the intro.

However, it summed each source with MagicString#length(), which by design does not count text appended or prepended at the edges of a source. toString() renders each source with source.content.toString(), so that edge content appeared in the output but was missing from the length.

import MagicString, { Bundle } from 'magic-string';

const b = new Bundle({ separator: ';' });
b.addSource(new MagicString('abc').append('X'));
b.addSource(new MagicString('def').prepend('Y'));

b.toString(); // "abcX;Ydef" (length 9)
b.length();   // 7 before this fix, 9 after

The fix measures each source's rendered length (source.content.toString().length), mirroring what toString() already does per source. I added a test that fails on the old code and passes now; the suite goes from 262 to 263 passing, and tsc and eslint are clean.

Bundle#length summed each source's MagicString#length(), which by design
does not count text appended or prepended at a source's edges. Since
toString() renders every source in full, length() undercounted whenever a
source had such edge content. It now mirrors toString() by measuring the
rendered length of each source.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant