Skip to content

Commit 7477293

Browse files
test(css): compare the css-parsing-tests trees rather than only running them (#22243)
* test(css): compare the css-parsing-tests trees rather than only running them The suite compiled all 8366 corpus inputs but compared only the 13 that `color3_keywords.json` and friends state a color for. The other six files state the tree each entry point should read, in a JSON form webpack's nodes can be written back into, so they are now asserted rather than smoke-run. Adds `test/helpers/serializeCssTree.js`, which writes a webpack CSS node in upstream's representation, and a `css-parsing-tests trees` describe over `component_value_list`, `one_component_value`, `one_declaration`, `one_rule`, `rule_list` and `stylesheet`: 82 comparable cases, 58 agreeing. The 24 that differ are listed in `KNOWN_DIVERGENCES` with the reason, and asserted to still differ — as `html5lib.spectest.js` does — so fixing one fails the suite rather than leaving the list to rot. They fall in five families: unicode-range and match tokens the corpus predates CSS Syntax 3 dropping, a backslash at end of input, an escaped newline in a string, CDO/CDC in a prelude, and a trailing token in a declaration value. * test(css): compare the corpus cases whose only error is a token Upstream writes two unrelated things as `["error", …]`: an entry point's failure return — `empty`, `extra-input`, `invalid`, `eof-in-string`, `eof-in-url` — which webpack answers with the nodes it read instead, and a token the tokenizer produced, which the serializer states like any other. Skipping on the word alone dropped four cases whose only error entries are a stray closer, a bad string or a bad url, so the stray-closer arm of `serializeComponentValue` was never compared against anything. They agree, and the arm is now load-bearing: mistyping one of the three closers fails the run.
1 parent 459a017 commit 7477293

2 files changed

Lines changed: 333 additions & 1 deletion

File tree

‎test/helpers/serializeCssTree.js‎

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
"use strict";
2+
3+
// webpack's CSS nodes in the JSON form css-parsing-tests states its
4+
// expectations in, so the corpus can be compared against rather than only run
5+
// through. The shapes are upstream's; see its README for the representation.
6+
7+
const {
8+
NodeType,
9+
parseAListOfComponentValues
10+
} = require("../../lib/css/syntax-parser");
11+
12+
/** The bracket a simple block opened with, as upstream names it. */
13+
const BLOCK_NAMES = new Map([
14+
["{", "{}"],
15+
["[", "[]"],
16+
["(", "()"]
17+
]);
18+
19+
/** A closer with nothing open is upstream's one error inside a value list. */
20+
const STRAY_CLOSERS = new Map([
21+
[NodeType.RightParenthesis, ")"],
22+
[NodeType.RightSquareBracket, "]"],
23+
[NodeType.RightCurlyBracket, "}"]
24+
]);
25+
26+
/** Upstream states a number's representation without its unit or `%` sign. */
27+
const NUMBER_PREFIX = /^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/;
28+
29+
/**
30+
* @param {EXPECTED_ANY} node a numeric component value
31+
* @returns {string} the source's own spelling of the number, unit excluded
32+
*/
33+
const representation = (node) => {
34+
const matched = NUMBER_PREFIX.exec(node.toString());
35+
return matched === null ? node.toString() : matched[0];
36+
};
37+
38+
/**
39+
* A number token's value. `-0` and `0` are the same number and upstream's JSON
40+
* states the mathematical one, so the IEEE sign a token like `-0rgba()` carries
41+
* is not a difference to report.
42+
* @param {EXPECTED_ANY} node a numeric component value
43+
* @returns {number} the value upstream states
44+
*/
45+
const numericValue = (node) =>
46+
node.numericValue === 0 ? 0 : node.numericValue;
47+
48+
/**
49+
* A dimension's unit with its escapes resolved. The node states the whole
50+
* token unescaped rather than the unit alone, so the number's own spelling
51+
* comes off the front.
52+
* @param {EXPECTED_ANY} node a dimension
53+
* @returns {string} the unit
54+
*/
55+
const unescapedUnit = (node) => {
56+
const number = representation(node);
57+
const whole = node.unescaped;
58+
return whole.startsWith(number) ? whole.slice(number.length) : node.unit;
59+
};
60+
61+
/**
62+
* One component value in upstream's JSON form. A preserved token is a bare
63+
* string and everything else a tagged array.
64+
* @param {EXPECTED_ANY} node a node from webpack's CSS parser
65+
* @returns {EXPECTED_ANY} the value upstream states for it
66+
*/
67+
const serializeComponentValue = (node) => {
68+
const stray = STRAY_CLOSERS.get(node.type);
69+
if (stray !== undefined) return ["error", stray];
70+
switch (node.type) {
71+
case NodeType.Ident:
72+
return ["ident", node.unescaped];
73+
case NodeType.AtKeyword:
74+
return ["at-keyword", node.unescaped];
75+
case NodeType.Hash:
76+
return ["hash", node.unescaped, node.typeFlag];
77+
case NodeType.String:
78+
return ["string", node.unescaped];
79+
case NodeType.BadString:
80+
return ["error", "bad-string"];
81+
case NodeType.Url:
82+
return ["url", node.unescaped];
83+
case NodeType.BadUrl:
84+
return ["error", "bad-url"];
85+
case NodeType.Number:
86+
return [
87+
"number",
88+
representation(node),
89+
numericValue(node),
90+
node.typeFlag
91+
];
92+
case NodeType.Percentage:
93+
return [
94+
"percentage",
95+
representation(node),
96+
numericValue(node),
97+
node.typeFlag
98+
];
99+
case NodeType.Dimension:
100+
return [
101+
"dimension",
102+
representation(node),
103+
numericValue(node),
104+
node.typeFlag,
105+
unescapedUnit(node)
106+
];
107+
case NodeType.Function:
108+
return [
109+
"function",
110+
node.unescapedName,
111+
...serializeComponentValues(node.value)
112+
];
113+
case NodeType.SimpleBlock:
114+
return [
115+
BLOCK_NAMES.get(node.toString()[0]),
116+
...serializeComponentValues(node.value)
117+
];
118+
// Whitespace collapses to one space however it was spelled, and every
119+
// other preserved token is written as itself.
120+
case NodeType.Whitespace:
121+
return " ";
122+
default:
123+
return node.toString();
124+
}
125+
};
126+
127+
/**
128+
* @param {EXPECTED_ANY[]} nodes component values, comments included
129+
* @returns {EXPECTED_ANY[]} the list upstream states, comments dropped
130+
*/
131+
const serializeComponentValues = (nodes) => {
132+
const out = [];
133+
for (const node of nodes) {
134+
if (node.type === NodeType.Comment) continue;
135+
out.push(serializeComponentValue(node));
136+
}
137+
return out;
138+
};
139+
140+
/**
141+
* @param {EXPECTED_ANY} node a declaration
142+
* @returns {EXPECTED_ANY} upstream's `["declaration", name, value, important]`
143+
*/
144+
const serializeDeclaration = (node) => [
145+
"declaration",
146+
node.unescapedName,
147+
serializeComponentValues(node.value),
148+
Boolean(node.important)
149+
];
150+
151+
/**
152+
* A rule in upstream's form. webpack reads a block into declarations and child
153+
* rules rather than keeping the component values the spec states, so the block
154+
* is read back off the source it spans.
155+
* @param {EXPECTED_ANY} node an at-rule or qualified rule
156+
* @param {string} source the stylesheet the node was parsed from
157+
* @returns {EXPECTED_ANY} the value upstream states for it
158+
*/
159+
const serializeRule = (node, source) => {
160+
const prelude = serializeComponentValues(node.prelude);
161+
let contents = null;
162+
if (node.blockStart !== -1) {
163+
// The closer is absent on a block the source never closed, and the
164+
// contents then run to the end of what the block spans.
165+
const closed = source[node.blockEnd - 1] === "}";
166+
const inner = source.slice(
167+
node.blockStart + 1,
168+
closed ? node.blockEnd - 1 : node.blockEnd
169+
);
170+
contents = serializeComponentValues(parseAListOfComponentValues(inner));
171+
}
172+
return node.type === NodeType.AtRule
173+
? ["at-rule", node.unescapedName, prelude, contents]
174+
: ["qualified rule", prelude, contents];
175+
};
176+
177+
module.exports = {
178+
serializeComponentValue,
179+
serializeComponentValues,
180+
serializeDeclaration,
181+
serializeRule
182+
};

‎test/specCases/cssParsing-webpack.spectest.js‎

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,22 @@ const { Volume, createFsFromVolume } = require("memfs");
1515
const webpack = require("../..");
1616
const { COLOR_NAME_TO_RGB } = require("../../lib/css/data");
1717
const { SourceProcessor } = require("../../lib/css/syntax");
18-
const { parseABlocksContents } = require("../../lib/css/syntax-parser");
18+
const {
19+
parseABlocksContents,
20+
parseAComponentValue,
21+
parseADeclaration,
22+
parseAListOfComponentValues,
23+
parseARule,
24+
parseAStylesheet,
25+
parseAStylesheetsContents
26+
} = require("../../lib/css/syntax-parser");
1927
const expectNoDeprecations = require("../helpers/expectNoDeprecations");
28+
const {
29+
serializeComponentValue,
30+
serializeComponentValues,
31+
serializeDeclaration,
32+
serializeRule
33+
} = require("../helpers/serializeCssTree");
2034

2135
const casesDir = path.resolve(__dirname, "../external/css-parsing-tests");
2236
const MODES = ["development", "production"];
@@ -272,3 +286,139 @@ describe("css-parsing-tests colors", () => {
272286
}
273287
}
274288
});
289+
290+
// The corpus states a parse as a tree, and webpack's parser entry points are
291+
// the spec's own algorithms, so the two compare directly once the nodes are
292+
// written in upstream's JSON form. What is not compared says why below.
293+
describe("css-parsing-tests trees", () => {
294+
const ENTRY_POINTS = [
295+
[
296+
"component_value_list",
297+
(source) =>
298+
serializeComponentValues(parseAListOfComponentValues(source))
299+
],
300+
[
301+
"one_component_value",
302+
(source) => serializeComponentValue(parseAComponentValue(source))
303+
],
304+
["one_declaration", (source) => serializeDeclaration(parseADeclaration(source))],
305+
["one_rule", (source) => serializeRule(parseARule(source), source)],
306+
[
307+
"rule_list",
308+
(source) =>
309+
parseAStylesheetsContents(source).map((rule) =>
310+
serializeRule(rule, source)
311+
)
312+
],
313+
[
314+
"stylesheet",
315+
(source) =>
316+
parseAStylesheet(source).rules.map((rule) => serializeRule(rule, source))
317+
]
318+
];
319+
320+
// Each case webpack reads differently, with the reason. A listed one is
321+
// asserted to *still* diverge, so fixing it fails here rather than leaving
322+
// the list to rot.
323+
const KNOWN_DIVERGENCES = new Map([
324+
// CSS Syntax 3 dropped `<unicode-range-token>` and the `~=`-style match
325+
// tokens; this corpus predates that, so it still states them.
326+
["component_value_list #38", "unicode-range token"],
327+
["component_value_list #39", "unicode-range token"],
328+
["component_value_list #40", "unicode-range token"],
329+
["component_value_list #41", "unicode-range token"],
330+
["component_value_list #42", "unicode-range token"],
331+
["component_value_list #43", "unicode-range token"],
332+
["component_value_list #44", "unicode-range token"],
333+
["component_value_list #45", "unicode-range token"],
334+
["component_value_list #46", "unicode-range token"],
335+
["component_value_list #47", "match token"],
336+
["component_value_list #48", "match token"],
337+
// A `\` with nothing after it is a parse error the spec resolves to
338+
// U+FFFD; webpack keeps the character it was written as.
339+
["component_value_list #7", "backslash at end of input"],
340+
["component_value_list #10", "backslash at end of input"],
341+
["component_value_list #11", "backslash at end of input"],
342+
// A `\` before a newline inside a string is a line continuation the spec
343+
// removes, and webpack keeps.
344+
["component_value_list #12", "escaped newline in a string"],
345+
// CDO and CDC are preserved tokens in a prelude the spec reads at the top
346+
// level; webpack drops them.
347+
["rule_list #10", "CDO/CDC in a prelude"],
348+
// A declaration's value is the run webpack will print, so the trailing
349+
// whitespace and `;` the spec keeps as component values are not in it.
350+
["one_declaration #11", "trailing token in a value"],
351+
["one_declaration #12", "trailing token in a value"],
352+
["one_declaration #14", "trailing token in a value"],
353+
["one_declaration #15", "trailing token in a value"],
354+
["one_declaration #16", "trailing token in a value"],
355+
["one_declaration #17", "trailing token in a value"],
356+
["one_declaration #18", "trailing token in a value"],
357+
["one_declaration #19", "trailing token in a value"]
358+
]);
359+
360+
// Upstream writes two unrelated things as `["error", …]`: an entry point's
361+
// failure return, which webpack answers with the nodes it read instead, and a
362+
// token the tokenizer produced, which the serializer states like any other.
363+
const PARSE_FAILURES = new Set([
364+
"empty",
365+
"eof-in-string",
366+
"eof-in-url",
367+
"extra-input",
368+
"invalid"
369+
]);
370+
371+
/**
372+
* Whether upstream states a failure return for a case, at any depth — a rule
373+
* list holds its invalid rule inside the list. A stray closer, a bad string
374+
* and a bad url are tokens rather than failures, so they stay comparable.
375+
* @param {EXPECTED_ANY} value an expected value
376+
* @returns {boolean} true when nothing webpack produces answers it
377+
*/
378+
const statesAFailure = (value) =>
379+
Array.isArray(value) &&
380+
(value[0] === "error"
381+
? PARSE_FAILURES.has(value[1])
382+
: value.some(statesAFailure));
383+
384+
for (const [file, parse] of ENTRY_POINTS) {
385+
const path_ = path.join(casesDir, `${file}.json`);
386+
if (!fs.existsSync(path_)) {
387+
it(`submodule not initialized (${file})`, () => {
388+
// No-op: the conformance data is an optional git submodule.
389+
});
390+
391+
continue;
392+
}
393+
const data = JSON.parse(fs.readFileSync(path_, "utf8"));
394+
for (let i = 0; i < data.length; i += 2) {
395+
const source = data[i];
396+
const expected = data[i + 1];
397+
if (typeof source !== "string") continue;
398+
if (statesAFailure(expected)) continue;
399+
// A NUL is preprocessed to U+FFFD by the spec and not by webpack, and
400+
// the character has no place in a test name either.
401+
if (source.includes("\u0000")) continue;
402+
403+
const name = `${file} #${i / 2}`;
404+
const divergence = KNOWN_DIVERGENCES.get(name);
405+
const label = divergence
406+
? `${name} still diverges: ${divergence}`
407+
: `${name} reads ${JSON.stringify(source).slice(0, 60)}`;
408+
409+
it(label, () => {
410+
let actual;
411+
try {
412+
actual = parse(source);
413+
} catch (err) {
414+
actual = `threw: ${/** @type {Error} */ (err).message}`;
415+
}
416+
if (divergence) {
417+
expect(actual).not.toEqual(expected);
418+
} else {
419+
expect(actual).toEqual(expected);
420+
}
421+
});
422+
}
423+
}
424+
});

0 commit comments

Comments
 (0)