perf(lint): parse each style block once per lint instead of once per rule (#7593)

Ten rules each parsed the same <style> block with postcss, so a large
style block cost ten parses and ten trees for the garbage collector.
The 40 000-rule linear-time test ran close to its 2 s limit on CI and
failed there by chance. One shared parse per block brings that lint
from about 400 ms to about 60 ms.
This commit is contained in:
Miguel Ángel
2026-11-12 06:23:06 +00:00
committed by GitHub
parent 748bfe3b2b
commit 32cacab74b
6 changed files with 58 additions and 12 deletions
+4 -3
View File
@@ -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<string>();
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(
+8 -7
View File
@@ -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<string> {
const families = new Set<string>();
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<string, string> {
const properties = new Map<string, string>();
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<string>();
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));
+2 -1
View File
@@ -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;
}
+2 -1
View File
@@ -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;
}
+23
View File
@@ -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(`<html><body>
<div data-composition-id="c1" data-width="1920" data-height="1080"><h1 class="title">Hi</h1></div>
<style>${css}</style>
</body></html>`);
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(`<html><body>
<div data-composition-id="c1" data-width="1920" data-height="1080"></div>
<style>.a { color: red</style>
</body></html>`);
expect(result.findings.filter((f) => f.code === "css_parse_error")).toHaveLength(1);
});
+19
View File
@@ -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<object, { root: postcss.Root } | { error: unknown }>();
/** 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;
}