diff --git a/packages/lint/src/rules/core.ts b/packages/lint/src/rules/core.ts index 0cf05550c7..52fd1ee4fd 100644 --- a/packages/lint/src/rules/core.ts +++ b/packages/lint/src/rules/core.ts @@ -1,5 +1,6 @@ import type { LintContext, HyperframeLintFinding } from "../context"; import postcss from "postcss"; +import { styleRoot } from "../styleRoots"; import selectorParser from "postcss-selector-parser"; import { GLSL_SCRIPT_TYPES } from "../shaderDomains"; import { @@ -116,7 +117,7 @@ function digitIdsTargetedBySelectors( const ids = new Set(); for (const style of styles) { try { - postcss.parse(style.content).walkRules((rule) => { + styleRoot(style).walkRules((rule) => { for (const selector of rule.selectors) addUnescapedDigitIds(selector, ids); }); } catch { @@ -686,7 +687,7 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [ if (!/transition/i.test(style.content)) continue; let root: postcss.Root; try { - root = postcss.parse(style.content); + root = styleRoot(style); } catch { // The CSS syntax rule reports malformed style blocks separately. continue; @@ -737,7 +738,7 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [ for (const style of styles) { let root: postcss.Root; try { - root = postcss.parse(style.content); + root = styleRoot(style); } catch (error) { findings.push({ ...locate( diff --git a/packages/lint/src/rules/fonts.ts b/packages/lint/src/rules/fonts.ts index b7161b8510..09e01269a2 100644 --- a/packages/lint/src/rules/fonts.ts +++ b/packages/lint/src/rules/fonts.ts @@ -1,4 +1,5 @@ import postcss from "postcss"; +import { styleRoot } from "../styleRoots"; import type { SourceLocation } from "../sourceCoordinates"; import { FONT_ALIAS_KEYS, @@ -34,9 +35,9 @@ const GENERIC_FAMILIES = new Set([ "revert", ]); -function parseFontCss(css: string): postcss.Root | null { +function parseFontCss(style: { content: string }): postcss.Root | null { try { - return postcss.parse(css); + return styleRoot(style); } catch { return null; } @@ -54,7 +55,7 @@ function isFontFaceDeclaration(decl: postcss.Declaration): boolean { export function extractFontFaceFamilies(styles: Array<{ content: string }>): Set { const families = new Set(); for (const style of styles) { - parseFontCss(style.content)?.walkDecls(/^font-family$/i, (decl) => { + parseFontCss(style)?.walkDecls(/^font-family$/i, (decl) => { if (!isFontFaceDeclaration(decl)) return; const name = normalizeUsedFontName(decl.value); if (name) families.add(name); @@ -85,7 +86,7 @@ function normalizeUsedFontName(part: string): string | null { function collectFontCustomProperties(styles: Array<{ content: string }>): Map { const properties = new Map(); for (const style of styles) { - parseFontCss(style.content)?.walkDecls(/^--/, (decl) => { + parseFontCss(style)?.walkDecls(/^--/, (decl) => { if (!isFontFaceDeclaration(decl)) properties.set(decl.prop, decl.value); }); } @@ -125,7 +126,7 @@ function extractUsedFontFamilies(styles: Array<{ content: string }>): string[] { const seen = new Set(); const properties = collectFontCustomProperties(styles); for (const style of styles) { - parseFontCss(style.content)?.walkDecls(FONT_FAMILY_PROPS, (decl) => { + parseFontCss(style)?.walkDecls(FONT_FAMILY_PROPS, (decl) => { for (const name of resolveUsedFontNames(usedFamilyList(decl), properties)) { if (!GENERIC_FAMILIES.has(name) && !seen.has(name)) { seen.add(name); @@ -267,7 +268,7 @@ export const fontRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [ if (isRegistrySourceFile(options.filePath) || isRegistryInstalledFile(rawSource)) return []; const findings: HyperframeLintFinding[] = []; for (const style of styles) { - parseFontCss(style.content)?.walkAtRules(/^font-face$/i, (rule) => { + parseFontCss(style)?.walkAtRules(/^font-face$/i, (rule) => { let family: string | null = null; let sources = ""; rule.walkDecls((decl) => { @@ -304,7 +305,7 @@ function uniqueFontLocation( const properties = collectFontCustomProperties(styles); for (const style of styles) { try { - postcss.parse(style.content).walkDecls(FONT_FAMILY_PROPS, (decl) => { + styleRoot(style).walkDecls(FONT_FAMILY_PROPS, (decl) => { const named = resolveUsedFontNames(usedFamilyList(decl), properties); if (!named.some((name) => families.includes(name))) return; locations.push(locate(style, decl.source?.start?.offset)); diff --git a/packages/lint/src/rules/gsap.ts b/packages/lint/src/rules/gsap.ts index ff9a620f5d..31586a0ee0 100644 --- a/packages/lint/src/rules/gsap.ts +++ b/packages/lint/src/rules/gsap.ts @@ -1,4 +1,5 @@ import postcss from "postcss"; +import { styleRoot } from "../styleRoots"; import { gsapCallOffset, containingCallOffset, gsapRunsWhenScriptRuns } from "../scriptPositions"; interface LintParsedGsap { animations: Array<{ @@ -1068,7 +1069,7 @@ function cssRuleSubjects( for (const style of styles) { let root: postcss.Root; try { - root = postcss.parse(style.content); + root = styleRoot(style); } catch { continue; } diff --git a/packages/lint/src/rules/textures.ts b/packages/lint/src/rules/textures.ts index 1ab1d7f92d..8e6082aab6 100644 --- a/packages/lint/src/rules/textures.ts +++ b/packages/lint/src/rules/textures.ts @@ -1,4 +1,5 @@ import postcss from "postcss"; +import { styleRoot } from "../styleRoots"; import type { LintContext, HyperframeLintFinding, OpenTag } from "../context"; import { readAttr, truncateSnippet } from "../utils"; @@ -70,7 +71,7 @@ function collectTextureCss(styles: LintContext["styles"]): { for (const style of styles) { let root: postcss.Root; try { - root = postcss.parse(style.content); + root = styleRoot(style); } catch { continue; } diff --git a/packages/lint/src/styleRoots.test.ts b/packages/lint/src/styleRoots.test.ts new file mode 100644 index 0000000000..1f3a16348c --- /dev/null +++ b/packages/lint/src/styleRoots.test.ts @@ -0,0 +1,23 @@ +import postcss from "postcss"; +import { afterEach, expect, it, vi } from "vitest"; +import { lintHyperframeHtml } from "./hyperframeLinter.js"; + +afterEach(() => vi.restoreAllMocks()); + +it("parses each style block once per lint, however many rules read it", async () => { + const css = `.title { font-family: "Inter", sans-serif; transition: opacity 1s; mask-image: url(a.png); }`; + const parse = vi.spyOn(postcss, "parse"); + await lintHyperframeHtml(` +

Hi

+ +`); + 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; +}