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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313import { tokenize, } from '@csstools/css-tokenizer';import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';import { nonNullishOrThrow, } from '@monochromatic-dev/module-or-throw/ts';
import { type CssEditState, type CssNode, CssParseError, isClosingToken, isCssAtRule, isCssDeclaration, isOpeningToken, isTriviaToken, parseCss, rawTextOfTokens, type StringCss, stringifyCss, stringifyNodes, tokenData, transformNodes, transformStylesheet,} from '../dist/final/neutral/index.mjs';
//region Test helpers
/** Parses a plain string as CSS, asserting the brand at the test boundary. @param source - CSS text under test. @returns Parsed edit state. */function parse(source: string,): CssEditState { return parseCss({ source: source as StringCss, },);}
/** Visitor removing every at-rule with a given name via the empty-array form. @param name - At-rule name to remove. @returns Visitor for {@link transformNodes}. */function removeAtRule(name: string,) { return function visit(node: CssNode,) { return (isCssAtRule(node,) && (node.name === name)) ? [] : node; };}
/** First structural node parsed from a CSS fragment, for building replacements. @param source - Fragment to parse. @returns First non-trivia node. */function nodeFrom(source: string,): CssNode { return nonNullishOrThrow( parse(source,).root.children.find(function keepStructural(node,) { return node.kind !== 'trivia'; },), );}
//endregion Test helpers
await describe({ name: '', children: [ describe({ name: transformNodes.name, children: [ //region Removal and trivia pruning
it({ name: 'removes a node and its leading whitespace when pruning', fn: async () => { const root = transformStylesheet({ root: parse('.a { top: 0; }\n\n@mixin --x { top: 0; }\n.b { left: 0; }',).root, visit: removeAtRule('mixin',), pruneTriviaBeforeRemoved: true, },); expect(stringifyNodes({ nodes: root.children, },),).toBe( '.a { top: 0; }\n.b { left: 0; }', ); }, },),
it({ name: 'keeps comments in pruned leading trivia', fn: async () => { const root = transformStylesheet({ root: parse('/* keep me */\n@mixin --x { top: 0; }\n.b { left: 0; }',).root, visit: removeAtRule('mixin',), pruneTriviaBeforeRemoved: true, },); expect(stringifyNodes({ nodes: root.children, },),).toBe( '/* keep me */\n.b { left: 0; }', ); }, },),
it({ name: 'keeps a comment-terminated trivia run whole when pruning', fn: async () => { const root = transformStylesheet({ root: parse('/* keep */@mixin --x { top: 0; }.b { left: 0; }',).root, visit: removeAtRule('mixin',), pruneTriviaBeforeRemoved: true, },); expect(stringifyNodes({ nodes: root.children, },),).toBe( '/* keep */.b { left: 0; }', ); }, },),
it({ name: 'keeps leading trivia when not pruning', fn: async () => { const root = transformStylesheet({ root: parse('.a { top: 0; }\n\n@mixin --x { top: 0; }',).root, visit: removeAtRule('mixin',), },); expect(stringifyNodes({ nodes: root.children, },),).toBe( '.a { top: 0; }\n\n', ); }, },),
it({ name: 'removes a node at list start without a preceding sibling', fn: async () => { const root = transformStylesheet({ root: parse('@mixin --x { top: 0; }.b { left: 0; }',).root, visit: removeAtRule('mixin',), pruneTriviaBeforeRemoved: true, },); expect(stringifyNodes({ nodes: root.children, },),).toBe('.b { left: 0; }',); }, },),
it({ name: 'skips pruning when the preceding sibling is not trivia', fn: async () => { const root = transformStylesheet({ root: parse('.a { top: 0; }@mixin --x { top: 0; }',).root, visit: removeAtRule('mixin',), pruneTriviaBeforeRemoved: true, },); expect(stringifyNodes({ nodes: root.children, },),).toBe('.a { top: 0; }',); }, },),
//endregion Removal and trivia pruning
//region Replacement and splicing
it({ name: 'replaces a node with a visitor-returned node', fn: async () => { const replacement = nodeFrom('left: 1px;',); const nodes = transformNodes({ nodes: parse('.a { top: 0; }',).root.children, visit: function swapDeclarations(node,) { return isCssDeclaration(node,) ? replacement : node; }, },); expect(stringifyNodes({ nodes, },),).toBe('.a { left: 1px; }',); }, },),
it({ name: 'splices a multi-node array in place', fn: async () => { /** Replacement body including its interior trivia, so the splice carries its own separator whitespace. */ const body = parse('top: 0; left: 1px;',).root.children; const nodes = transformNodes({ nodes: parse('.a { @apply --card; }',).root.children, visit: function expandApply(node,) { return (isCssAtRule(node,) && (node.name === 'apply')) ? body : node; }, },); expect(stringifyNodes({ nodes, },),).toBe('.a { top: 0; left: 1px; }',); }, },),
it({ name: 'reaches nodes nested through at-rule and rule blocks bottom-up', fn: async () => { const nodes = transformNodes({ nodes: parse('@media (width > 1px) { .a { @apply --x; } }',).root.children, visit: removeAtRule('apply',), pruneTriviaBeforeRemoved: true, },); expect(stringifyNodes({ nodes, },),).toBe('@media (width > 1px) { .a { } }',); }, },),
//endregion Replacement and splicing
//region Structural sharing
it({ name: 'returns the same array and root when nothing changes', fn: async () => { const { root, } = parse('@import url(a.css);\n.a { top: 0; }',); const keptNodes = transformNodes({ nodes: root.children, visit: function keepAll(node,) { return node; }, },); expect(keptNodes,).toBe(root.children,); const keptRoot = transformStylesheet({ root, visit: function keepAll(node,) { return node; }, },); expect(keptRoot,).toBe(root,); }, },),
//endregion Structural sharing ], },),
describe({ name: stringifyCss.name, children: [ it({ name: 'renders statement at-rules without block or semicolon', fn: async () => { const source = '@apply --y'; expect(stringifyCss({ state: parse(source,), },),).toBe(source,); }, },), ], },),
describe({ name: CssParseError.name, children: [ it({ name: 'carries the failure offset in field and message', fn: async () => { const error = new CssParseError({ message: 'boom', offset: 7, },); expect(error.name,).toBe('CssParseError',); expect(error.offset,).toBe(7,); expect(error.message,).toContain('offset 7',); }, },), ], },),
describe({ name: 'token helpers', children: [ it({ name: 'classifies trivia, opening, and closing tokens', fn: async () => { const tokens = tokenize({ css: '/* c */ <!-- --> ({[ )}] .x', },); const trivia = tokens.filter( function keepTrivia(token,) { return isTriviaToken(token,); }, ); expect(trivia.length,).toBeGreaterThanOrEqual(4,); expect( tokens.filter( function keepOpening(token,) { return isOpeningToken(token,); }, ), ).toHaveLength(3,); expect( tokens.filter( function keepClosing(token,) { return isClosingToken(token,); }, ), ).toHaveLength(3,); }, },),
it({ name: 'joins raw token text byte-exactly', fn: async () => { const css = '.x { content: "}{"; }'; expect(rawTextOfTokens({ tokens: tokenize({ css, },), },),).toBe(css,); }, },),
it({ name: 'reads parsed token data', fn: async () => { const tokens = tokenize({ css: '@\\6d ixin', },); const atKeyword = nonNullishOrThrow(tokens[0],); expect(tokenData(atKeyword,),).toEqual({ value: 'mixin', },); }, },), ], },), ],},);