Skip to content

Commit aef7cf8

Browse files
refactor(css): make the streamed block's output bound its own
The `;` a `}` makes redundant is dropped by walking the output back from the end, bounded by where the block started. That bound was taken while the block's opener was still held back, so the piece it names can hold output from before the block — the walk stops short of it today only because the opener's piece ends in `{` and is never taken back, which is an invariant nothing states. Take the bound after cutting the tail, so it names the block's own first piece and the walk cannot reach past it either way. The bound now covers a case it did not: a run of retractable pieces already cuts after each, so a mark taken between them was mid-tail. Raised by CodeRabbit as a correctness risk. It is not reachable today — reverting either the cut or the bound entirely leaves every test passing — so this is the invariant made explicit rather than a fix. The new case pins what is observable: a declaration before a streamed block keeps its `;` when the block drops its own.
1 parent b09558a commit aef7cf8

4 files changed

Lines changed: 27 additions & 7 deletions

File tree

‎lib/css/syntax.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4154,7 +4154,7 @@ const _streamOpen = (d, rule) => {
41544154
`${_rulePrelude(A, writer, minify)}${minify ? "" : " "}{`
41554155
);
41564156
_frameSeenDeclarations[d] = null;
4157-
_frameFirstChunk[d] = writer.mark;
4157+
_frameFirstChunk[d] = writer.markCut();
41584158
// The outermost opener carries the whole run's source anchor: it is the one
41594159
// the kept comments precede and the mapping points at.
41604160
if (d === 0) {

‎lib/util/SourceProcessor.js‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,14 @@ class PrintContext {
231231
}
232232

233233
/**
234+
* Close off what has been emitted so far and return the index the next piece
235+
* will take. Cutting is the point: a caller bounding a later edit by this
236+
* (see {@link dropTrailing}) must not be able to reach output from before it,
237+
* which a mark taken while the tail was still open would sit in the middle of.
234238
* @returns {number} the index the next emitted piece will get
235239
*/
236-
get mark() {
240+
markCut() {
241+
this._cutTail();
237242
return this._chunks.length;
238243
}
239244

@@ -317,10 +322,8 @@ class PrintContext {
317322
* Move the accumulated tail into the finished pieces, flattened.
318323
*/
319324
_cutTail() {
320-
// Nothing has accumulated since the last cut — which is every retractable
321-
// piece after the first in a run of them. A piece holding no characters
322-
// would only pad the output, and an anchor's offset into an empty tail is
323-
// the start of whatever is cut next either way.
325+
// A run of retractable pieces cuts after each; an empty piece would only pad
326+
// the output, and an anchor into an empty tail already points past it.
324327
if (this._tailLength === 0) return;
325328
// Reading a character is what forces it flat; the value is not wanted, and
326329
// it is kept only so the read cannot be optimized away.

‎test/CssSyntax.unittest.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,16 @@ describe("CssSyntax — block streaming", () => {
10201020
expect(minify(src)).toBe(`.root{${middle}color:red}`);
10211021
});
10221022

1023+
it("takes back only its own output when it drops the last `;`", () => {
1024+
// The `;` a `}` makes redundant is dropped by walking back over the pieces
1025+
// the block emitted, past the empty one a later duplicate took back. What
1026+
// stands before the block keeps its own separator.
1027+
const mid = repeat(3000, (i) => `& .m${i}{color:red}`);
1028+
const src = `.root{lead:1;${mid}dup:2;dup:2;}`;
1029+
expect(childCount(src, NodeType.QualifiedRule)).toBe(0);
1030+
expect(minify(src)).toBe(`.root{lead:1;${mid}dup:2}`);
1031+
});
1032+
10231033
it("declines to stream a block a longhand family could still merge in", () => {
10241034
// `_mergeBoxLonghands` needs every declaration at once, and only runs in a
10251035
// block with no child rule — so such a block is never streamed, however far

‎types.d.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21596,7 +21596,14 @@ declare class PrintContext<TPath, TNode> {
2159621596
* Drop the innermost held-back opener — its node printed to nothing.
2159721597
*/
2159821598
dropPending(): void;
21599-
get mark(): number;
21599+
21600+
/**
21601+
* Close off what has been emitted so far and return the index the next piece
21602+
* will take. Cutting is the point: a caller bounding a later edit by this
21603+
* (see {@link dropTrailing}) must not be able to reach output from before it,
21604+
* which a mark taken while the tail was still open would sit in the middle of.
21605+
*/
21606+
markCut(): number;
2160021607

2160121608
/**
2160221609
* Forget every node's printed text. A node printed in pieces does this once

0 commit comments

Comments
 (0)