Skip to content

Commit e969356

Browse files
fix(css): drop a write to a rule whose block streams
Its text is written as it is read, so the replacement went unused and the next rule printed it instead.
1 parent 366a9d0 commit e969356

5 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎docs/syntax.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Conventions every language keeps, and a new member follows:
9292
- **Positions come as numbers and as ranges**, like acorn's `start`/`end` and `range`: `start`, `end`, `startOf` and `endOf` allocate nothing, so `CssParser` and `HtmlParser` read every position through them; `range`, `rangeOf` and `loc` (`[line, column, endLine, endColumn]`) are a fresh array per call.
9393
- **Walk children one way everywhere**: `for (let i = 0, c = path.child(0); c !== 0; c = path.child(++i))`. HTML keeps where the last `child` read stopped per parent, so this takes one step per child even with nested loops.
9494
- **Nothing is stored for a read.** A member reads the columns the parser already fills or derives its answer when called: HTML counts `index` along the sibling links and makes `loc`'s converter on its first call in a parse. The walk writes no more than its position, so a read nobody makes costs nothing.
95-
- **Writes act on the current node and only while printing.** `replace(text)` and `remove()` take effect when the node prints after its visitors, which still run its printer for what it records; on a walk that prints nothing, or on a comment, which prints from the token stream, they do nothing. In CSS, what holds a replaced node prints the text its children printed as, without the minifier's rewrites, which read fields the replacement no longer matches; the rest of the sheet minifies as before. HTML streams most elements to the output before their `exit` and JavaScript decides parentheses and spacing from the original node, so neither has writes yet.
95+
- **Writes act on the current node and only while printing.** `replace(text)` and `remove()` take effect when the node prints after its visitors, which still run its printer for what it records; on a walk that prints nothing, or on a comment, which prints from the token stream, they do nothing, and a rule whose block is large enough to stream prints as written. In CSS, what holds a replaced node prints the text its children printed as, without the minifier's rewrites, which read fields the replacement no longer matches; the rest of the sheet minifies as before. HTML streams most elements to the output before their `exit` and JavaScript decides parentheses and spacing from the original node, so neither has writes yet.
9696
- **Reads go through the path, and webpack's own plugins read through it.** `CssParser` and `HtmlParser` use the members above and nothing else from a node, so what they do a plugin can; only the printers keep raw readers of their own (`_rawName`, `_attributeList`, …).
9797

9898
## Generated tables

‎lib/css/syntax-parser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6056,7 +6056,11 @@ const _streamConsumeBlock = (ts, rule) => {
60566056
_visitors[_types[_nodeIndex(rule)]],
60576057
undefined
60586058
);
6059-
if (_stream.writer !== undefined) _streamClose(d, rule);
6059+
if (_stream.writer !== undefined) {
6060+
// Its text is written already, so what a visitor gave it goes unused.
6061+
_stream.writer.discardReplacement(rule);
6062+
_streamClose(d, rule);
6063+
}
60606064
_S._inPropertyRule = _framePrevProperty[d] === 1;
60616065
_S._inFunctionRule = _framePrevFunction[d] === 1;
60626066
_S._inFeatureValuesRule = _framePrevFeatureValues[d] === 1;

‎lib/util/SourceProcessor.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,18 @@ class PrintContext {
737737
this._replacements.set(/** @type {number} */ (node), text);
738738
}
739739

740+
/**
741+
* Drop what {@link replace} gave `node`, for a node the grammar writes out
742+
* without {@link printNode}.
743+
* @param {TNode} node the node
744+
* @returns {void}
745+
*/
746+
discardReplacement(node) {
747+
if (this._replacements !== null) {
748+
this._replacements.delete(/** @type {number} */ (node));
749+
}
750+
}
751+
740752
/**
741753
* Store `text` as what `node` printed, where the grammar already knows what
742754
* the node printer would return and so does not call it.

‎test/unitCases/css/parser.unittest.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,29 @@ describe("CssSyntax — SourceProcessor", () => {
709709
).toBe("/*! x */a{color:red}b{top:0}");
710710
});
711711

712+
it("prints a rule large enough to stream as written, and what follows it too", () => {
713+
// Past the streaming threshold the block is written out as it is read.
714+
let body = "";
715+
for (let i = 0; i < 1800; i++) {
716+
body += `.c${i}>d${i}:hover{color:red;margin:${i + 1}px}`;
717+
}
718+
const css = `a{${body}}b{color:red}`;
719+
const plain = print(css, {});
720+
for (const event of ["enter", "exit"]) {
721+
expect(
722+
print(css, {
723+
[NodeType.QualifiedRule]: {
724+
[event]: (
725+
/** @type {import("../../../lib/css/syntax-parser").CssPath} */ path
726+
) => {
727+
if (path.source().startsWith("a{")) path.replace("z{q:1}");
728+
}
729+
}
730+
})
731+
).toBe(plain);
732+
}
733+
});
734+
712735
it("does nothing on a walk that prints nothing", () => {
713736
let seen = 0;
714737
new SourceProcessor()

‎types.d.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27540,6 +27540,12 @@ declare class PrintContext<TPath, TNode, TPrintOptions = object> {
2754027540
*/
2754127541
replace(node: TNode, text: string): void;
2754227542

27543+
/**
27544+
* Drop what {@link replace} gave `node`, for a node the grammar writes out
27545+
* without {@link printNode}.
27546+
*/
27547+
discardReplacement(node: TNode): void;
27548+
2754327549
/**
2754427550
* Store `text` as what `node` printed, where the grammar already knows what
2754527551
* the node printer would return and so does not call it.

0 commit comments

Comments
 (0)