From 32cacab74bb52076efb9e84258f382200b56b19e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Thu, 12 Nov 2026 06:23:06 +0000 Subject: [PATCH] perf(lint): parse each style block once per lint instead of once per rule (#7593) Ten rules each parsed the same +`); + expect(parse.mock.calls.filter(([source]) => source === css)).toHaveLength(1); +}); + +it("reports a malformed style block once and still lints the rest", async () => { + const result = await lintHyperframeHtml(` +
+ +`); + expect(result.findings.filter((f) => f.code === "css_parse_error")).toHaveLength(1); +}); diff --git a/packages/lint/src/styleRoots.ts b/packages/lint/src/styleRoots.ts new file mode 100644 index 0000000000..62cdf29fd2 --- /dev/null +++ b/packages/lint/src/styleRoots.ts @@ -0,0 +1,19 @@ +import postcss from "postcss"; + +// Blocks belong to a single lint context. Weak keys release each parsed tree after the pass. +const parsed = new WeakMap(); + +/** The block's CSS tree, parsed once and shared by every rule, so rules must not change it. Throws the parse error. */ +export function styleRoot(style: { content: string }): postcss.Root { + let entry = parsed.get(style); + if (!entry) { + try { + entry = { root: postcss.parse(style.content) }; + } catch (error) { + entry = { error }; + } + parsed.set(style, entry); + } + if ("error" in entry) throw entry.error; + return entry.root; +}