Skip to content

Commit c0e13f8

Browse files
refactor(lint): split the stylesheet zoom walk into selector, rule, walk
The first split took `Fallow audit` from critical (CRAP 116.3, cyclomatic 21) to minor (37.1, cyclomatic 11) — right direction, still over the threshold of 30. CRAP weights complexity against COVERAGE, and I cannot measure coverage on this machine, so rather than tune to the margin this splits the stylesheet walk into the three things it was doing: find the first canvas-targeting selector in a comma list (`firstCanvasSelector`), turn one CSS rule into at most one hit (`canvasZoomInRule`), and walk the rules. No function in the rule now exceeds 6 decision points, which clears the threshold at any plausible coverage rather than at the measured one. `firstCanvasSelector` returning the first match preserves the previous `break` exactly: one hit per rule, not one per selector. Behaviour re-verified against the ORIGINAL revision of this branch — identical on all 16 inputs, with the comparison shown to detect a real difference by dropping the `targetsCanvasRoot` guard, which diverges on two cases. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 09a7d00 commit c0e13f8

1 file changed

Lines changed: 41 additions & 18 deletions

File tree

‎packages/lint/src/rules/composition.ts‎

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -286,33 +286,56 @@ function inlineCanvasZoomHits(rootTag: OpenTag, tags: OpenTag[]): CanvasZoomHit[
286286
return hits;
287287
}
288288

289+
/**
290+
* The first selector in a comma list that targets the canvas, or null. Only the
291+
* first matters: the rest of the list describes the same declaration block, so
292+
* one hit per RULE is what the caller wants.
293+
*/
294+
function firstCanvasSelector(
295+
header: string,
296+
rootId: string | null,
297+
rootClasses: string[],
298+
): string | null {
299+
for (const selector of header.split(",")) {
300+
const trimmed = selector.trim();
301+
if (trimmed && targetsCanvasRoot(trimmed, rootId, rootClasses)) return trimmed;
302+
}
303+
return null;
304+
}
305+
306+
/** A canvas-level rescaling `zoom` declared by one CSS rule, or null. */
307+
function canvasZoomInRule(
308+
header: string,
309+
body: string,
310+
rootId: string | null,
311+
rootClasses: string[],
312+
): CanvasZoomHit | null {
313+
const trimmedHeader = header.trim();
314+
// An at-rule's "header" is `@media ...`, not a selector list.
315+
if (!trimmedHeader || trimmedHeader.startsWith("@")) return null;
316+
const value = firstRescalingZoom(body);
317+
if (!value) return null;
318+
const selector = firstCanvasSelector(trimmedHeader, rootId, rootClasses);
319+
if (!selector) return null;
320+
return {
321+
where: `\`${selector}\``,
322+
value,
323+
snippet: truncateSnippet(`${selector} { zoom: ${value} }`),
324+
};
325+
}
326+
289327
function stylesheetCanvasZoomHits(
290328
styles: ExtractedBlock[],
291329
rootId: string | null,
292330
rootClasses: string[],
293331
): CanvasZoomHit[] {
294332
const hits: CanvasZoomHit[] = [];
295333
for (const style of styles) {
296-
const noComments = stripCssComments(style.content);
297334
const ruleWithBody = /([^{}]+)\{([^{}]*)\}/g;
298335
let match: RegExpExecArray | null;
299-
while ((match = ruleWithBody.exec(noComments)) !== null) {
300-
const header = (match[1] ?? "").trim();
301-
if (!header || header.startsWith("@")) continue;
302-
const value = firstRescalingZoom(match[2] ?? "");
303-
if (!value) continue;
304-
// One hit per RULE: the first selector in the comma list that targets the
305-
// canvas is enough to establish it, and the rest describe the same block.
306-
for (const selector of header.split(",")) {
307-
const trimmed = selector.trim();
308-
if (!trimmed || !targetsCanvasRoot(trimmed, rootId, rootClasses)) continue;
309-
hits.push({
310-
where: `\`${trimmed}\``,
311-
value,
312-
snippet: truncateSnippet(`${trimmed} { zoom: ${value} }`),
313-
});
314-
break;
315-
}
336+
while ((match = ruleWithBody.exec(stripCssComments(style.content))) !== null) {
337+
const hit = canvasZoomInRule(match[1] ?? "", match[2] ?? "", rootId, rootClasses);
338+
if (hit) hits.push(hit);
316339
}
317340
}
318341
return hits;

0 commit comments

Comments
 (0)