Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions e2e/rollup-app/fixtures/check-root/Badge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ds } from './ds';
import { label } from './generated/labels';

export const Badge = ds.styles({ color: 'ink' }).asElement('span');

export const App = () => <Badge>{label}</Badge>;
7 changes: 7 additions & 0 deletions e2e/rollup-app/fixtures/check-root/Card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ds } from './ds';

export const Card = ds
.styles({ color: 'ink', '--gap': 'var(--gap)' })
.asElement('div');

export const App = () => <Card>card</Card>;
4 changes: 4 additions & 0 deletions e2e/rollup-app/fixtures/check-root/animus.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"system": "./ds.ts",
"diagnostics": { "animus.property.self-reference": "error" }
}
12 changes: 12 additions & 0 deletions e2e/rollup-app/fixtures/check-root/ds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { color } from '@animus-ui/core/groups';
import { createSystem, createTheme } from '@animus-ui/react';

export const theme = createTheme()
.addColors({ gray: { 100: '#f5f5f5', 900: '#171717' } })
.addColorModes('light', {
light: { ink: 'gray.900' },
dark: { ink: 'gray.100' },
})
.build();

export const ds = createSystem().addGroup('color', color).build(theme).seal();
38 changes: 38 additions & 0 deletions e2e/rollup-app/scripts/assert-agent.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { spawnSync } from 'node:child_process';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

Expand Down Expand Up @@ -96,6 +98,42 @@ check(
file?.components.some((component) => component.name === 'Draft') === true
);

const checkRoot = join(lane, 'fixtures', 'check-root');
const checkOut = mkdtempSync(join(tmpdir(), 'animus-rollup-check-'));
try {
const built = run(['build', '--root', checkRoot, '--out-dir', checkOut]);
const fromBuild = [
...built.stderr.matchAll(
/^\[animus\] [⚠✖ℹ] (\S+?\.tsx)\b.*\[([\w.-]+)\]$/gmu
),
]
.map(([, path, code]) => `${path} ${code}`)
.sort();
const rechecked = run([
'check',
'--root',
checkRoot,
join(checkRoot, 'Card.tsx'),
join(checkRoot, 'Badge.tsx'),
]);
const fromCheck = (parsed(rechecked)?.files ?? [])
.flatMap((entry) =>
[...entry.tokenMisses, ...entry.diagnostics].map(
(diagnostic) => `${entry.file} ${diagnostic.code}`
)
)
.sort();
check(
'check reports for a source what build reports for it',
fromBuild.includes('Card.tsx animus.property.self-reference') &&
fromCheck.join() === fromBuild.join() &&
rechecked.status === built.status,
`build ${built.status} ${JSON.stringify(fromBuild)}, check ${rechecked.status} ${JSON.stringify(fromCheck)}`
);
} finally {
rmSync(checkOut, { recursive: true, force: true });
}

const exported = run([
'tokens',
'export',
Expand Down
39 changes: 39 additions & 0 deletions packages/_integration/__tests__/source-map.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { chainSourceMap } from '@animus-ui/extract/pipeline';
import { SourceMap } from 'node:module';
import { beforeAll, describe, expect, test } from 'vitest';

Expand Down Expand Up @@ -72,3 +73,41 @@ describe('a transformed module source map', () => {
});
});
});

describe('a map composed with an incoming map', () => {
const ours = {
version: 3,
file: 'a.js',
sources: ['a.js'],
names: [],
mappings: 'AAAA,UACA',
};

test('keeps the incoming source root and ignore lists', () => {
const chained = chainSourceMap(ours, {
version: 3,
sourceRoot: '../src/',
sources: ['a.ts', 'vendor.ts'],
names: [],
mappings: 'AAAA;MACA',
ignoreList: [1],
x_google_ignoreList: [1],
});
expect(chained).toMatchObject({
sourceRoot: '../src/',
sources: ['a.ts', 'vendor.ts'],
ignoreList: [1],
x_google_ignoreList: [1],
});
});

test('leaves code the incoming map does not cover unmapped', () => {
const chained = chainSourceMap(ours, {
version: 3,
sources: ['a.ts'],
names: [],
mappings: 'AAAA;MACA',
});
expect(chained.mappings).toBe('AAAA,U');
});
});
22 changes: 22 additions & 0 deletions packages/_integration/__tests__/target-support.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,25 @@ export const App = () => <Ring />;
`;
expect(reported({ safari: 15 << 16 }, outlined)).toEqual([]);
});

test("an unsupported at-rule is reported at the at-rule's key", () => {
const queried = `import { ds } from './setup';
export const Grid = ds
.styles({
'@container (min-width: 10px)': { display: 'grid' },
})
.asElement('div');
export const App = () => <Grid />;
`;
const lacking = reported({ safari: 15 << 16 }, queried).filter(
(d) => d.reason === 'unsupported-at-rule'
);
expect(lacking).toHaveLength(1);
expect(lacking[0]).toMatchObject({
component: 'Grid',
line: 4,
column: 5,
value: "'@container (min-width: 10px)'",
});
expect(lacking[0]!.property).toBeUndefined();
});
46 changes: 34 additions & 12 deletions packages/_integration/__tests__/view-transition-usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,28 @@ const transitions = {
},
};

const sheetFor = (page: string): string => {
const system = {
path: 'fixtures/transitions.ts',
source: `import { createViewTransitions } from './setup';\nexport const transitions = createViewTransitions({ slide: { old: { animationDuration: '250ms' } } });\n`,
};

const rule = '::view-transition-old(*.animus-vt-slide)';

const sheetOf = (files: { path: string; source: string }[]): string => {
clearAnalysisCache();
const manifest = JSON.parse(
analyzeProjectFull(
JSON.stringify([
{
path: 'fixtures/transitions.ts',
source: `import { createViewTransitions } from './setup';\nexport const transitions = createViewTransitions({ slide: { old: { animationDuration: '250ms' } } });\n`,
},
{ path: 'fixtures/page.tsx', source: page },
]),
{ viewTransitionsJson: JSON.stringify(transitions), devMode: false }
)
analyzeProjectFull(JSON.stringify(files), {
viewTransitionsJson: JSON.stringify(transitions),
devMode: false,
})
) as { sheets: { global: string } };
return manifest.sheets.global;
};

const sheetFor = (page: string): string =>
sheetOf([system, { path: 'fixtures/page.tsx', source: page }]);

test('a view transition referenced only from <ViewTransition> keeps its CSS under pruning', () => {
const rule = '::view-transition-old(*.animus-vt-slide)';
const used = sheetFor(
`import { ViewTransition } from '@animus-ui/react/view-transition';\nimport { transitions } from './transitions';\nexport const Page = () => <ViewTransition enter={transitions.slide}><main /></ViewTransition>;\n`
);
Expand All @@ -40,3 +43,22 @@ test('a view transition referenced only from <ViewTransition> keeps its CSS unde
expect(used).toContain(rule);
expect(unused).not.toContain(rule);
});

test('a view transition is kept whenever its collection is reached in a way the analysis cannot bound', () => {
const namespace = sheetFor(
`import { ViewTransition } from '@animus-ui/react/view-transition';\nimport * as sys from './transitions';\nexport const Page = () => <ViewTransition enter={sys.transitions.slide}><main /></ViewTransition>;\n`
);
const dynamic = sheetFor(
`export const load = () => import('./transitions').then((m) => m.transitions.slide);\n`
);
const unanalysedPackage = sheetOf([
{
path: 'fixtures/page.tsx',
source: `import { ViewTransition } from '@animus-ui/react/view-transition';\nimport { transitions } from '@acme/ds';\nexport const Page = () => <ViewTransition enter={transitions.slide}><main /></ViewTransition>;\n`,
},
]);

expect(namespace).toContain(rule);
expect(dynamic).toContain(rule);
expect(unanalysedPackage).toContain(rule);
});
111 changes: 63 additions & 48 deletions packages/cli/src/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const LEGACY_PROTOCOL_VERSIONS = [
'2025-03-26',
'2024-11-05',
];
const SUPPORTED = [...MODERN_PROTOCOL_VERSIONS, ...LEGACY_PROTOCOL_VERSIONS];
const BATCHING_PROTOCOL_VERSION = '2025-03-26';
const PROTOCOL_VERSION_KEY = 'io.modelcontextprotocol/protocolVersion';
const SERVER_INFO_KEY = 'io.modelcontextprotocol/serverInfo';
const UNSUPPORTED_PROTOCOL_VERSION = -32022;
Expand Down Expand Up @@ -253,20 +253,20 @@ export async function runMcpServer(
const send = (message: JsonRecord): void => {
output.write(`${JSON.stringify(message)}\n`);
};
const respond = (
const response = (
id: string | number | null,
outcome: { result: JsonRecord } | { error: RpcError }
): void => {
send(
'result' in outcome
? { jsonrpc: '2.0', id, result: outcome.result }
: {
jsonrpc: '2.0',
id,
error: JSON.parse(JSON.stringify(outcome.error)) as JsonRecord,
}
);
};
): JsonRecord =>
'result' in outcome
? { jsonrpc: '2.0', id, result: outcome.result }
: {
jsonrpc: '2.0',
id,
error: JSON.parse(JSON.stringify(outcome.error)) as JsonRecord,
};
const invalid = (): JsonRecord =>
response(null, { error: { code: -32600, message: 'Invalid Request' } });
let negotiated: string | null = null;

const handle = async (message: RpcMessage): Promise<JsonRecord> => {
const params = message.params;
Expand All @@ -275,7 +275,7 @@ export async function runMcpServer(
throw new ProtocolError(
UNSUPPORTED_PROTOCOL_VERSION,
'Unsupported protocol version',
{ supported: SUPPORTED, requested: modern }
{ supported: MODERN_PROTOCOL_VERSIONS, requested: modern }
);
}
const finish = (result: JsonRecord): JsonRecord =>
Expand All @@ -289,20 +289,20 @@ export async function runMcpServer(
switch (message.method) {
case 'initialize': {
const asked = params?.protocolVersion;
negotiated =
typeof asked === 'string' && LEGACY_PROTOCOL_VERSIONS.includes(asked)
? asked
: LEGACY_PROTOCOL_VERSIONS[0]!;
return {
protocolVersion:
typeof asked === 'string' &&
LEGACY_PROTOCOL_VERSIONS.includes(asked)
? asked
: LEGACY_PROTOCOL_VERSIONS[0]!,
protocolVersion: negotiated,
capabilities: { tools: {} },
serverInfo,
instructions: INSTRUCTIONS,
};
}
case 'server/discover':
return finish({
supportedVersions: SUPPORTED,
supportedVersions: MODERN_PROTOCOL_VERSIONS,
capabilities: { tools: {} },
instructions: INSTRUCTIONS,
});
Expand Down Expand Up @@ -352,44 +352,59 @@ export async function runMcpServer(
}
};

let queue = Promise.resolve();
const lines = createInterface({ input, crlfDelay: Infinity });
for await (const line of lines) {
if (line.trim() === '') continue;
let message: RpcMessage;
try {
message = JSON.parse(line) as RpcMessage;
} catch {
respond(null, { error: { code: -32700, message: 'Parse error' } });
continue;
}
const reply = async (message: JsonValue): Promise<JsonRecord | null> => {
if (
message === null ||
typeof message !== 'object' ||
Array.isArray(message)
) {
respond(null, { error: { code: -32600, message: 'Invalid Request' } });
continue;
return invalid();
}
if (typeof message.method !== 'string') {
if (message.id === undefined) {
respond(null, { error: { code: -32600, message: 'Invalid Request' } });
}
const rpc: RpcMessage = message;
if (typeof rpc.method !== 'string') {
return rpc.id === undefined ? invalid() : null;
}
const id = rpc.id;
if (id === undefined) return null;
try {
return response(id, { result: await handle(rpc) });
} catch (error) {
return response(id, {
error:
error instanceof ProtocolError
? { code: error.code, message: error.message, data: error.data }
: { code: -32603, message: String(error) },
});
}
};

let queue = Promise.resolve();
const lines = createInterface({ input, crlfDelay: Infinity });
for await (const line of lines) {
if (line.trim() === '') continue;
let message: JsonValue;
try {
message = JSON.parse(line) as JsonValue;
} catch {
send(response(null, { error: { code: -32700, message: 'Parse error' } }));
continue;
}
const id = message.id;
if (id === undefined) continue;
queue = queue.then(async () => {
try {
respond(id, { result: await handle(message) });
} catch (error) {
respond(id, {
error:
error instanceof ProtocolError
? { code: error.code, message: error.message, data: error.data }
: { code: -32603, message: String(error) },
});
if (
Array.isArray(message) &&
message.length > 0 &&
negotiated === BATCHING_PROTOCOL_VERSION
) {
const replies: JsonRecord[] = [];
for (const entry of message) {
const answer = await reply(entry);
if (answer !== null) replies.push(answer);
}
if (replies.length > 0) output.write(`${JSON.stringify(replies)}\n`);
return;
}
const answer = await reply(message);
if (answer !== null) send(answer);
});
}
await queue;
Expand Down
Loading
Loading