Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345/** Deterministic coverage driver: exercises every exported function and its error paths with fixed inputs, so the V8 coverage it produces is reproducible. Run under `NODE_V8_COVERAGE` by the `fuzz:coverage` task, then summarized by `coverage-report.ts`. @module */
import { asCssSource, type CssAtRule, type CssNode, type CssVisitResult, isClosingToken, isCssAtRule, isCssDeclaration, isCssRule, isCssTrivia, isOpeningToken, isTokenIdent, isTokenString, isTokenURL, isTriviaToken, parseCss, rawTextOfTokens, stringifyCss, stringifyNodes, tokenData, transformNodes, transformStylesheet,} from '@monochromatic-dev/module-css-edit/ts';
//region Helpers
/** Runs a thunk that is expected to throw, swallowing the error so the driver keeps exercising remaining paths. Re-throws anything that is not an `Error`. @param thunk - Operation expected to throw. @example ```ts swallow(function bad() { parseCss({ source: asCssSource('}') }); }); ``` */function swallow(thunk: () => void,): void { try { thunk(); } catch (error: unknown) { if (!(Error.isError(error,))) throw error; }}
//endregion Helpers
//region Visitors
/** Keeps every node, proving the identity transform preserves reference identity. @param node - Visited node. @returns Same node. @example ```ts transformStylesheet({ root, visit: keep }) === root; // => true ``` */function keep(node: CssNode,): CssVisitResult { return node;}
/** Replaces each declaration with a fresh equal node, driving the changed single-node path. @param node - Visited node. @returns Fresh declaration, or the node untouched. @example ```ts transformStylesheet({ root, visit: replaceDeclarations }); ``` */function replaceDeclarations(node: CssNode,): CssVisitResult { return isCssDeclaration(node,) ? { kind: 'declaration' as const, tokens: node.tokens, } : node;}
/** Doubles each declaration, driving the non-empty splice path. @param node - Visited node. @returns Two-node splice, or the node untouched. @example ```ts transformNodes({ nodes, visit: spliceDeclarations }); ``` */function spliceDeclarations(node: CssNode,): CssVisitResult { return isCssDeclaration(node,) ? [ node, node, ] : node;}
/** Removes every declaration, driving prune-removal over the leading trivia. @param node - Visited node. @returns Empty splice for declarations; the node untouched otherwise. @example ```ts transformStylesheet({ root, visit: dropDeclarations, pruneTriviaBeforeRemoved: true }); ``` */function dropDeclarations(node: CssNode,): CssVisitResult { return isCssDeclaration(node,) ? [] : node;}
/** Removes declarations whose raw text starts with `b`, driving removal where the previously emitted node is a declaration rather than trivia. @param node - Visited node. @returns Empty splice for matching declarations; the node untouched otherwise. @example ```ts transformStylesheet({ root, visit: dropSecondDeclaration, pruneTriviaBeforeRemoved: true }); ``` */function dropSecondDeclaration(node: CssNode,): CssVisitResult { if (!isCssDeclaration(node,)) return node; /** Raw source text of the declaration run. */ const raw = rawTextOfTokens({ tokens: node.tokens, },); return raw.startsWith('b',) ? [] : node;}
/** Removes every qualified rule, driving removal without pruning. @param node - Visited node. @returns Empty splice for rules; the node untouched otherwise. @example ```ts transformNodes({ nodes, visit: dropRules }); ``` */function dropRules(node: CssNode,): CssVisitResult { return isCssRule(node,) ? [] : node;}
//endregion Visitors
//region Exercise
/** Document exercising every structural shape: trivia (comments, CDO/CDC), statement and block at-rules, an unknown at-rule, relaxed nesting with the restart-as-rule reclassification, a custom property keeping a `{}` value, function and url tokens, and a statement at-rule ended by its block. */const MAIN_SOURCE = asCssSource([ '<!-- --> /* head */', "@import url('reset.css') layer(base);", '@media (min-width: 640px) { .card { color: rgb(0 0 0 / 50%); } }', '@mixin --chip { padding: 1px; span:hover { top: 0; } }', ':root { --raw: { nested: token }; }', '.btn[data-x="y"] { background: url(a.png); margin: calc((1px + 2px) * 3) }', '.z { @apply --chip }',].join('\n',),);
/** Exercises parsing, stringification, token and node helpers, and the transform layer across keep, replace, splice, and prune-removal paths. @throws Error when the untouched round-trip is not byte-identical, which would make every downstream coverage number meaningless. @example ```ts exercise(); ``` */function exercise(): void { /** Parsed main document. */ const state = parseCss({ source: MAIN_SOURCE, },); if (stringifyCss({ state, },) !== MAIN_SOURCE) throw new Error('coverage driver round-trip diverged; driver inputs are stale',); /** Top-level children of the main document. */ const topChildren = state.root .children; stringifyNodes({ nodes: topChildren, },);
// Node guards over the top-level children. for (const child of topChildren) { isCssAtRule(child,); isCssRule(child,); isCssTrivia(child,); isCssDeclaration(child,); }
// Token helpers over the leading trivia run and the import prelude. /** Leading trivia run: CDO, CDC, whitespace, and the head comment. */ const [lead,] = topChildren; if ((lead !== undefined) && isCssTrivia(lead,)) { rawTextOfTokens({ tokens: lead.tokens, },); for (const token of lead.tokens) { isTriviaToken(token,); isOpeningToken(token,); isClosingToken(token,); } } /** Import at-rule, first structural child. */ const importRule = topChildren.find(function firstAtRule(child: CssNode,): child is CssAtRule { return isCssAtRule(child,); },); if (importRule !== undefined) { tokenData(importRule.atToken,); for (const token of importRule.preludeTokens) { isTokenIdent(token,); isTokenString(token,); isTokenURL(token,); } }
// Transform layer: keep (reference identity), replace, splice, and removals. /** Identity-transformed root, expected reference-equal to the input. */ const kept = transformStylesheet({ root: state.root, visit: keep, },); if (kept !== state.root) throw new Error('identity transform lost structural sharing',);
transformStylesheet({ root: state.root, visit: replaceDeclarations, },);
transformNodes({ nodes: topChildren, visit: spliceDeclarations, },);
/** Pruning fixture: whitespace-only, comment-then-whitespace, and comment-only runs before removed declarations. */ const pruneState = parseCss({ source: asCssSource( '.a { color: red; } .b { /* keep */ color: red; } .c {/* c */color: red;}', ), },); transformStylesheet({ root: pruneState.root, visit: dropDeclarations, pruneTriviaBeforeRemoved: true, },); /** Adjacent-declaration fixture: removal where the preceding emitted node is a declaration, not trivia. */ const adjacentState = parseCss({ source: asCssSource('.d { a:1;b:2 }',), },); transformStylesheet({ root: adjacentState.root, visit: dropSecondDeclaration, pruneTriviaBeforeRemoved: true, },); // Removal without pruning. transformNodes({ nodes: pruneState.root .children, visit: dropRules, },);
// Edge shapes: top-level declaration ending at EOF, statement at-rule // without a terminator at EOF. parseCss({ source: asCssSource('color: red',), },); parseCss({ source: asCssSource('@apply --chip',), },);
// Error paths. swallow(function tokenizerError() { parseCss({ source: asCssSource('/* open',), },); },); swallow(function strayClose() { parseCss({ source: asCssSource('}',), },); },); swallow(function unclosedBlock() { parseCss({ source: asCssSource('.a {',), },); },); swallow(function unbalancedRun() { parseCss({ source: asCssSource(') {}',), },); },); swallow(function unbalancedPrelude() { parseCss({ source: asCssSource('@media ) {}',), },); },); swallow(function semicolonPrelude() { parseCss({ source: asCssSource('.a ; {}',), },); },); swallow(function preludeHitsBlockEnd() { parseCss({ source: asCssSource('.b { .c }',), },); },); swallow(function preludeHitsEof() { parseCss({ source: asCssSource('.a',), },); },);}
//endregion Exercise
exercise();