Skip to content

Commit 44405d7

Browse files
build(tooling): add a parser-agreement column to the CSS, HTML and XML parse tables (#22907)
* build(tooling): add a parser-agreement column to the CSS, HTML and XML parse tables Hold webpack (and postcss, xmldom, htmlparser2 where they build a comparable tree) to css-tree, parse5 and @rgrove/parse-xml, naming the first differing node. Table-only. * test(tooling): load the parse-xml stand-in's tree from JSON rather than built source
1 parent 0f3637b commit 44405d7

10 files changed

Lines changed: 1228 additions & 14 deletions

‎cspell.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"astring",
2323
"asyncloader",
2424
"atlaskit",
25+
"atrule",
2526
"ATTLIST",
2627
"autocapitalize",
2728
"autoclean",

‎test/unitCases/tooling/compare-css.unittest.js‎

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,25 @@ const installed = jest.fn(() => Promise.resolve(CACHE_NAME));
77
// A real install would `npm ci` the corpus into a contributor's cache, and every
88
// packed stylesheet and built fixture is taken as there, so `--setup` fetches
99
// and builds nothing: what is under test is that it installs and stops.
10+
// css-tree is installed into that same cache, so its tree is written out by
11+
// hand; postcss arrives with the `css-loader` dev dependency, so it is real.
12+
const mockCssTree = { parse: jest.fn() };
13+
1014
jest.mock("../../../tooling/compare-tools-harness", () => ({
1115
...jest.requireActual("../../../tooling/compare-tools-harness"),
1216
exists: () => Promise.resolve(true),
13-
installPackages: installed
17+
installPackages: installed,
18+
loaderFor: () => (/** @type {string} */ name) =>
19+
name === "css-tree" ? mockCssTree : require(name)
1420
}));
1521

1622
const { SourceProcessor } = require("../../../lib/css/syntax");
1723
const {
1824
COLD_BRANCHES,
25+
TREE_READERS,
1926
runMode
2027
} = require("../../../tooling/compare-css-tools");
28+
const { agreement } = require("../../../tooling/compare-tools-harness");
2129

2230
// Read before anything clears the mock: an unguarded script installs while
2331
// being required, and a `beforeEach` would erase exactly that evidence.
@@ -136,3 +144,95 @@ describe("compare-css-tools — the cold branches fixture", () => {
136144
}
137145
});
138146
});
147+
148+
describe("compare-css-tools — agreeing with css-tree", () => {
149+
const css =
150+
'/* c */ .a /* s */ .d { color : red /* v */ !important; .b{x:y;z:} } @media (x)/* m */,(y){.c{d:e}} @font-face{} @import "x";';
151+
152+
/**
153+
* @param {string} text a part of the stylesheet
154+
* @param {string=} before what it follows, where the text alone isn't unique
155+
* @returns {{ loc: { start: { offset: number }, end: { offset: number } } }} where it is
156+
*/
157+
const at = (text, before = "") => {
158+
const offset = css.indexOf(before + text) + before.length;
159+
return { loc: { start: { offset }, end: { offset: offset + text.length } } };
160+
};
161+
162+
/**
163+
* @param {string} prelude a rule's prelude
164+
* @param {EXPECTED_ANY[]} children what its block holds
165+
* @returns {EXPECTED_ANY} a css-tree rule
166+
*/
167+
const rule = (prelude, children) => ({
168+
type: "Rule",
169+
prelude: at(prelude),
170+
block: { children }
171+
});
172+
173+
/**
174+
* What css-tree builds for the stylesheet, where it read `.b{x:y}`.
175+
* @param {EXPECTED_ANY} nested the node standing for `.b{x:y}`
176+
* @returns {EXPECTED_ANY} the css-tree stylesheet
177+
*/
178+
const parsed = (nested) => ({
179+
children: [
180+
rule(".a /* s */ .d", [
181+
{
182+
type: "Declaration",
183+
property: "color",
184+
important: true,
185+
value: at("red /* v */")
186+
},
187+
nested
188+
]),
189+
{
190+
type: "Atrule",
191+
name: "media",
192+
prelude: at("(x)/* m */,(y)"),
193+
block: {
194+
children: [
195+
rule(".c", [
196+
{
197+
type: "Declaration",
198+
property: "d",
199+
important: false,
200+
value: at("e", "d:")
201+
}
202+
])
203+
]
204+
}
205+
},
206+
{
207+
type: "Atrule",
208+
name: "font-face",
209+
prelude: null,
210+
block: { children: [] }
211+
},
212+
{ type: "Atrule", name: "import", prelude: at('"x"'), block: null }
213+
]
214+
});
215+
216+
const nestedRule = rule(".b", [
217+
{ type: "Declaration", property: "x", important: false, value: at("y") },
218+
{ type: "Declaration", property: "z", important: false, value: at("", "z:") }
219+
]);
220+
221+
it("holds webpack and postcss to css-tree's rules, at-rules and declarations", async () => {
222+
mockCssTree.parse.mockReturnValue(parsed(nestedRule));
223+
const cell = agreement("css-tree", TREE_READERS, css);
224+
expect(await cell("css-tree")).toBe("reference");
225+
expect(await cell("stylis")).toBe("own dialect");
226+
expect(await cell("webpack")).toBe("yes");
227+
expect(await cell("postcss")).toBe("yes");
228+
});
229+
230+
it("names a block css-tree left unparsed", async () => {
231+
mockCssTree.parse.mockReturnValue(
232+
parsed({ type: "Raw", ...at(".b{x:y;z:}") })
233+
);
234+
expect(await agreement("css-tree", TREE_READERS, css)("webpack")).toBe(
235+
"no — /.a .d[1]: css-tree unparsed .b{x:y;z:}, tool rule .b"
236+
);
237+
});
238+
});

‎test/unitCases/tooling/compare-harness.unittest.js‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const path = require("path");
77
const { Readable } = require("stream");
88
const { pathToFileURL } = require("url");
99
const {
10+
TreeEvents,
11+
agreement,
1012
compress,
1113
cutsAt,
1214
exists,
@@ -28,6 +30,7 @@ const {
2830
run,
2931
sweepExitCode,
3032
sweepMode,
33+
treeDifference,
3134
wantedExpectations,
3235
wantedRespelling
3336
} = require("../../../tooling/compare-tools-harness");
@@ -1037,3 +1040,124 @@ describe("profileShares", () => {
10371040
).toEqual([["terser minify.js", 0]]);
10381041
});
10391042
});
1043+
1044+
describe("parser agreement", () => {
1045+
/**
1046+
* @param {string[]} texts what each `<p>` holds
1047+
* @returns {TreeEvents} a body of paragraphs
1048+
*/
1049+
const paragraphs = (texts) => {
1050+
const events = new TreeEvents();
1051+
events.open("body", "<body>");
1052+
for (const text of texts) {
1053+
events.open("p", "<p>");
1054+
events.text(text);
1055+
events.close("</p>");
1056+
}
1057+
events.leaf("comment");
1058+
events.close("</body>");
1059+
return events;
1060+
};
1061+
1062+
it("numbers each node among its same-named siblings and folds text runs", () => {
1063+
const events = new TreeEvents();
1064+
events.open("a", "<a>");
1065+
expect(events.depth).toBe(1);
1066+
events.text("x");
1067+
events.text("y");
1068+
events.open("b", "<b>");
1069+
events.close("</b>");
1070+
events.text("z");
1071+
events.open("b", "<b>");
1072+
events.close("</b>");
1073+
events.close("</a>");
1074+
events.open("a", "<a>");
1075+
events.close("</a>");
1076+
expect(events.depth).toBe(0);
1077+
expect(events.list).toEqual([
1078+
["/a[1]", "open", "<a>"],
1079+
["/a[1]", "text", "xy"],
1080+
["/a[1]/b[1]", "open", "<b>"],
1081+
["/a[1]/b[1]", "close", "</b>"],
1082+
["/a[1]", "text", "z"],
1083+
["/a[1]/b[2]", "open", "<b>"],
1084+
["/a[1]/b[2]", "close", "</b>"],
1085+
["/a[1]", "close", "</a>"],
1086+
["/a[2]", "open", "<a>"],
1087+
["/a[2]", "close", "</a>"]
1088+
]);
1089+
});
1090+
1091+
it("names the first event two trees differ at, and what each side says there", () => {
1092+
expect(
1093+
treeDifference(paragraphs(["a", "b"]), paragraphs(["a", "b"]), "ref")
1094+
).toBeUndefined();
1095+
expect(
1096+
treeDifference(paragraphs(["a", "b"]), paragraphs(["a", "c"]), "ref")
1097+
).toBe('/body[1]/p[2]: ref text "b", tool text "c"');
1098+
expect(
1099+
treeDifference(paragraphs(["a"]), paragraphs(["a", "b"]), "ref")
1100+
).toBe("/body[1]: ref comment, tool <p>");
1101+
const empty = new TreeEvents();
1102+
expect(treeDifference(empty, paragraphs([]), "ref")).toBe(
1103+
"/body[1]: ref nothing, tool <body>"
1104+
);
1105+
const loose = new TreeEvents();
1106+
loose.leaf("comment");
1107+
expect(treeDifference(loose, empty, "ref")).toBe(
1108+
"/: ref comment, tool nothing"
1109+
);
1110+
expect(
1111+
treeDifference(paragraphs(["x".repeat(80)]), paragraphs(["y"]), "ref")
1112+
).toBe(`/body[1]/p[1]: ref text "${"x".repeat(41)}…, tool text "y"`);
1113+
/**
1114+
* @param {string} said what the innermost node says
1115+
* @returns {TreeEvents} it under twenty nested sections
1116+
*/
1117+
const deep = (said) => {
1118+
const events = new TreeEvents();
1119+
for (let i = 0; i < 20; i++) events.open("section", "<section>");
1120+
events.leaf(said);
1121+
return events;
1122+
};
1123+
expect(treeDifference(deep("a"), deep("b"), "ref")).toBe(
1124+
`…${"/section[1]".repeat(20).slice(-59)}: ref a, tool b`
1125+
);
1126+
});
1127+
1128+
it("answers each parser's cell, reading the reference once", async () => {
1129+
const reference = jest.fn((/** @type {string} */ source) =>
1130+
paragraphs(source.split(" "))
1131+
);
1132+
/** @typedef {import("../../../tooling/compare-tools-harness").TreeReader} TreeReader */
1133+
const readers = new Map(
1134+
/** @type {[string, TreeReader][]} */ ([
1135+
["ref", reference],
1136+
["same", (source) => Promise.resolve(paragraphs(source.split(" ")))],
1137+
["other", () => paragraphs(["no"])],
1138+
[
1139+
"throws",
1140+
() => {
1141+
throw new Error("bad input\nmore");
1142+
}
1143+
]
1144+
])
1145+
);
1146+
const cell = agreement("ref", readers, "a b");
1147+
expect(await cell("ref")).toBe("reference");
1148+
expect(await cell("unread")).toBe("own dialect");
1149+
expect(await cell("same")).toBe("yes");
1150+
expect(await cell("other")).toBe(
1151+
'no — /body[1]/p[1]: ref text "a", tool text "no"'
1152+
);
1153+
expect(await cell("throws")).toBe("could not compare: bad input");
1154+
expect(reference).toHaveBeenCalledTimes(1);
1155+
1156+
readers.set("ref", () => {
1157+
throw new Error("not well-formed");
1158+
});
1159+
expect(await agreement("ref", readers, "a")("same")).toBe(
1160+
"could not compare: ref rejects it: not well-formed"
1161+
);
1162+
});
1163+
});

0 commit comments

Comments
 (0)