|
1 | | -// Parity checks for the `validate` option's malformed-HTML warnings. |
| 1 | +// Parity checks for the `validate` option's malformed-HTML compile errors. |
2 | 2 | // |
3 | | -// Babel warns through `console.warn`; Oxc's port warns from Rust directly to |
4 | | -// process stderr, so both compiles run in child processes and the captured |
5 | | -// stderr is compared — both whether a warning fires and its exact content. |
| 3 | +// Since #3099 `validate` is a hard compile error in both compilers (the |
| 4 | +// emitted walk is guaranteed broken once the browser re-parses the markup |
| 5 | +// differently). Both compiles run in child processes; the runners catch the |
| 6 | +// thrown error and print its message to stderr so the harness can compare |
| 7 | +// whether each compiler fired and that the DOM diff content matches. |
6 | 8 |
|
7 | 9 | const { spawnSync } = require("child_process"); |
8 | 10 | const path = require("path"); |
9 | 11 |
|
10 | 12 | const compilerDir = path.resolve(__dirname, ".."); |
11 | 13 |
|
12 | 14 | const cases = { |
13 | | - pInDiv: { code: "const t = <p><div>bad</div></p>;", warns: true }, |
14 | | - nestedA: { code: "const t = <a><a>x</a></a>;", warns: true }, |
15 | | - tableNoTbody: { code: "const t = <table><tr><td>1</td></tr></table>;", warns: true }, |
16 | | - formInForm: { code: "const t = <form><form>x</form></form>;", warns: true }, |
17 | | - buttonInButton: { code: "const t = <button><button>x</button></button>;", warns: true }, |
18 | | - dynamicHole: { code: "const t = <p>{x()}<div>bad</div></p>;", warns: true }, |
| 15 | + pInDiv: { code: "const t = <p><div>bad</div></p>;", throws: true }, |
| 16 | + nestedA: { code: "const t = <a><a>x</a></a>;", throws: true }, |
| 17 | + tableNoTbody: { code: "const t = <table><tr><td>1</td></tr></table>;", throws: true }, |
| 18 | + formInForm: { code: "const t = <form><form>x</form></form>;", throws: true }, |
| 19 | + buttonInButton: { code: "const t = <button><button>x</button></button>;", throws: true }, |
| 20 | + dynamicHole: { code: "const t = <p>{x()}<div>bad</div></p>;", throws: true }, |
19 | 21 | hydratableMarkers: { |
20 | 22 | code: "const t = <p>{x()}<div>bad</div></p>;", |
21 | 23 | options: { hydratable: true }, |
22 | | - warns: true |
| 24 | + throws: true |
23 | 25 | }, |
24 | 26 | // Table partials are wrapped in the right context before validation. |
25 | | - tdPartial: { code: "const t = <td>cell</td>;", warns: false }, |
26 | | - trPartial: { code: "const t = <tr><td>c</td></tr>;", warns: false }, |
27 | | - colPartial: { code: "const t = <col />;", warns: false }, |
28 | | - theadPartial: { code: "const t = <thead><tr><th>h</th></tr></thead>;", warns: false }, |
29 | | - emptyTbody: { code: "const t = <tbody></tbody>;", warns: false }, |
| 27 | + tdPartial: { code: "const t = <td>cell</td>;", throws: false }, |
| 28 | + trPartial: { code: "const t = <tr><td>c</td></tr>;", throws: false }, |
| 29 | + colPartial: { code: "const t = <col />;", throws: false }, |
| 30 | + theadPartial: { code: "const t = <thead><tr><th>h</th></tr></thead>;", throws: false }, |
| 31 | + emptyTbody: { code: "const t = <tbody></tbody>;", throws: false }, |
30 | 32 | // Escaped text must not be re-interpreted as markup. |
31 | | - scriptEscape: { code: 'const t = <div>{"<script>a();</script>"}<b>ok</b></div>;', warns: false }, |
32 | | - liOrphan: { code: "const t = <li>item</li>;", warns: false }, |
33 | | - goodDiv: { code: "const t = <div><span>fine</span></div>;", warns: false }, |
34 | | - disabled: { code: "const t = <p><div>bad</div></p>;", options: { validate: false }, warns: false } |
| 33 | + scriptEscape: { code: 'const t = <div>{"<script>a();</script>"}<b>ok</b></div>;', throws: false }, |
| 34 | + liOrphan: { code: "const t = <li>item</li>;", throws: false }, |
| 35 | + goodDiv: { code: "const t = <div><span>fine</span></div>;", throws: false }, |
| 36 | + disabled: { |
| 37 | + code: "const t = <p><div>bad</div></p>;", |
| 38 | + options: { validate: false }, |
| 39 | + throws: false |
| 40 | + } |
35 | 41 | }; |
36 | 42 |
|
37 | 43 | const babelRunner = ` |
38 | 44 | const babel = require("@babel/core"); |
39 | 45 | const plugin = require("../babel-plugin"); |
40 | | -babel.transformSync(process.argv[1], { |
41 | | - filename: "a.jsx", |
42 | | - parserOpts: { plugins: ["jsx"] }, |
43 | | - plugins: [[plugin, JSON.parse(process.argv[2])]] |
44 | | -}); |
| 46 | +try { |
| 47 | + babel.transformSync(process.argv[1], { |
| 48 | + filename: "a.jsx", |
| 49 | + parserOpts: { plugins: ["jsx"] }, |
| 50 | + plugins: [[plugin, JSON.parse(process.argv[2])]] |
| 51 | + }); |
| 52 | +} catch (error) { |
| 53 | + console.error(error.message); |
| 54 | + process.exitCode = 42; |
| 55 | +} |
45 | 56 | `; |
46 | 57 |
|
47 | 58 | const oxcRunner = ` |
48 | 59 | const { transform } = require("./index.js"); |
49 | | -transform(process.argv[1], { filename: "a.jsx", ...JSON.parse(process.argv[2]) }); |
| 60 | +try { |
| 61 | + transform(process.argv[1], { filename: "a.jsx", ...JSON.parse(process.argv[2]) }); |
| 62 | +} catch (error) { |
| 63 | + console.error(error.message); |
| 64 | + process.exitCode = 42; |
| 65 | +} |
50 | 66 | `; |
51 | 67 |
|
52 | | -function stderrOf(runner, code, options) { |
| 68 | +function runCompile(runner, code, options) { |
53 | 69 | const result = spawnSync("node", ["-e", runner, code, JSON.stringify(options)], { |
54 | 70 | cwd: compilerDir, |
55 | 71 | encoding: "utf8" |
56 | 72 | }); |
57 | | - expect(result.status).toBe(0); |
58 | | - return result.stderr; |
| 73 | + expect([0, 42]).toContain(result.status); |
| 74 | + return { threw: result.status === 42, stderr: result.stderr }; |
| 75 | +} |
| 76 | + |
| 77 | +// The compilers format locations differently (Babel appends a code frame; |
| 78 | +// Oxc embeds line:col), so parity is asserted on the DOM diff itself. |
| 79 | +function domDiff(stderr) { |
| 80 | + const match = stderr.match(/User HTML:\n[^\n]*\n\s*Browser HTML:\n[^\n]*/); |
| 81 | + expect(match).not.toBeNull(); |
| 82 | + return match[0].replace(/\n\s+/g, "\n "); |
59 | 83 | } |
60 | 84 |
|
61 | | -describe("validate warning parity", () => { |
62 | | - for (const [name, { code, options = {}, warns }] of Object.entries(cases)) { |
| 85 | +describe("validate error parity", () => { |
| 86 | + for (const [name, { code, options = {}, throws }] of Object.entries(cases)) { |
63 | 87 | test(name, () => { |
64 | 88 | const fullOptions = { moduleName: "r-dom", ...options }; |
65 | | - const babelErr = stderrOf(babelRunner, code, fullOptions); |
66 | | - const oxcErr = stderrOf(oxcRunner, code, fullOptions); |
67 | | - expect(babelErr.includes("malformed")).toBe(warns); |
68 | | - expect(oxcErr.includes("malformed")).toBe(warns); |
69 | | - if (warns) { |
70 | | - expect(oxcErr.trim()).toBe(babelErr.trim()); |
| 89 | + const babel = runCompile(babelRunner, code, fullOptions); |
| 90 | + const oxc = runCompile(oxcRunner, code, fullOptions); |
| 91 | + expect(babel.threw).toBe(throws); |
| 92 | + expect(oxc.threw).toBe(throws); |
| 93 | + expect(babel.stderr.includes("malformed")).toBe(throws); |
| 94 | + expect(oxc.stderr.includes("malformed")).toBe(throws); |
| 95 | + if (throws) { |
| 96 | + expect(domDiff(oxc.stderr)).toBe(domDiff(babel.stderr)); |
71 | 97 | } |
72 | 98 | }); |
73 | 99 | } |
|
0 commit comments