maddox-engine 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -9
- package/fixtures/tokens-studio-export.json +30 -0
- package/package.json +2 -2
- package/src/cli.ts +13 -6
- package/src/core.ts +22 -0
- package/src/crawl.ts +92 -0
- package/src/format.ts +3 -3
- package/src/groundTruth.ts +14 -2
- package/src/suggestFix.ts +61 -0
- package/src/tokensStudio.ts +106 -0
- package/src/types.ts +7 -0
package/README.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# maddox
|
|
2
2
|
|
|
3
|
-
Scans source code for design-token, motion, and component-state drift against your own design system.
|
|
3
|
+
Scans source code — or a live deployed page — for design-token, motion, and component-state drift against your own design system.
|
|
4
4
|
|
|
5
5
|
This is the open-source scanning engine behind [Maddox Engine](https://www.maddoxengine.com) — the same code the hosted dashboard and GitHub Action run, extracted so you can run it locally or in CI with no account required.
|
|
6
6
|
|
|
7
7
|
## What it checks
|
|
8
8
|
|
|
9
|
-
- **Colors** — every hex literal in your source, matched against your `@theme` tokens by exact value, then by RGB distance for near-misses.
|
|
10
|
-
- **Motion** — durations and easings used in code, checked against your real motion tokens (duration against duration, ease against ease — never cross-compared).
|
|
11
|
-
- **Component states** — an explicit contract you write yourself (a JSON file naming which states each kind of component must cover: `disabled`, `loading`, `error`, and so on). Confirms the state is referenced in the file; it doesn't verify it renders correctly — that's a static source check, not a visual one.
|
|
9
|
+
- **Colors** — every hex literal in your source (or a live page's rendered HTML/CSS), matched against your `@theme` tokens by exact value, then by RGB distance for near-misses.
|
|
10
|
+
- **Motion** — durations and easings used in code, checked against your real motion tokens (duration against duration, ease against ease — never cross-compared). Source-scan only; see `--url` below.
|
|
11
|
+
- **Component states** — an explicit contract you write yourself (a JSON file naming which states each kind of component must cover: `disabled`, `loading`, `error`, and so on). Confirms the state is referenced in the file; it doesn't verify it renders correctly — that's a static source check, not a visual one. Source-scan only.
|
|
12
12
|
|
|
13
|
-
This is a
|
|
13
|
+
This is a source-code and rendered-output scanner, not a pixel/DOM visual-regression tool. It checks the values a page actually ships against the tokens that are supposed to govern them, so it catches drift that renders identically to a real token (and so produces zero visual diff) but was never written as one.
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
@@ -20,9 +20,11 @@ npx maddox-engine <target-source-dir> <path-to-globals.css-with-@theme-block> [o
|
|
|
20
20
|
|
|
21
21
|
Options:
|
|
22
22
|
|
|
23
|
-
- `--project <name>` — project name (defaults to the target directory's basename)
|
|
23
|
+
- `--project <name>` — project name (defaults to the target directory's basename, or the URL's hostname in `--url` mode)
|
|
24
24
|
- `--motion <path>` — path to a JSON file of motion tokens (e.g. a vendored copy of your motion-tokens export)
|
|
25
|
-
- `--states <path>` — path to a state-contract JSON file (see below)
|
|
25
|
+
- `--states <path>` — path to a state-contract JSON file (see below). No effect in `--url` mode.
|
|
26
|
+
- `--tokens-studio <path>` — path to a Tokens Studio / W3C Design Tokens JSON export (see below); merged with, and taking precedence over, `@theme` on any path both define
|
|
27
|
+
- `--url <page-url>` — scan a deployed page instead of local source (see below); pass a placeholder like `-` for `<target-source-dir>` when using this alone
|
|
26
28
|
- `--format text|json|markdown` — output format (default: `text`)
|
|
27
29
|
- `--fail-below <0-100>` — exit non-zero if the drift health score falls below this threshold; omit to never fail
|
|
28
30
|
|
|
@@ -45,6 +47,29 @@ An optional JSON file mapping a component-name pattern to the state names that c
|
|
|
45
47
|
|
|
46
48
|
Ground truth is explicit — nothing is inferred about which states a component "should" have.
|
|
47
49
|
|
|
50
|
+
### Tokens Studio ground truth
|
|
51
|
+
|
|
52
|
+
If your tokens live in Figma via the Tokens Studio plugin rather than (or alongside) a Tailwind `@theme` block, export them to JSON and point `--tokens-studio` at the file. A single-set export (the whole file is one token tree) and a multi-set export (top-level keys are set names, e.g. `global`, `dark`) are both supported — for a multi-set export, every set is merged, later sets overriding earlier ones by path. `{alias}` references are resolved automatically. Only token types that resolve to a single comparable value (`color`, `spacing`, `sizing`, `fontSizes`, `borderRadius`, `dimension`) are used — composite types like `typography` or `boxShadow` describe a bundle of properties, not one value to diff against, and are skipped rather than misclassified.
|
|
53
|
+
|
|
54
|
+
### Scanning a live page
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
npx maddox-engine - ./src/app/globals.css --url https://example.com --format text
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`--url` fetches the page's rendered HTML plus every same-origin `<link rel="stylesheet">` it links to, and runs the same color/font-size extraction against what's actually shipped — not what's in the repo at scan time. This is the difference between a source-linter and a production check: a stale CDN cache, a build step that silently drops a token, or a config typo can all make the deployed page diverge from what the source says, and only a live-URL scan catches that.
|
|
61
|
+
|
|
62
|
+
Two things don't carry over to `--url` mode:
|
|
63
|
+
|
|
64
|
+
- **Motion** — durations/eases only ever appear in rendered HTML/CSS as static literals if a component hardcodes them there, which real motion libraries don't do (they animate via the JS runtime). Motion findings are dropped from every `--url` scan rather than surfaced as noise from unrelated CSS.
|
|
65
|
+
- **Component states** — state-completeness is a "is this identifier referenced in this file's source" check; a fetched, already-rendered page has no source to check. `--states` is accepted but has no effect in `--url` mode.
|
|
66
|
+
|
|
67
|
+
Third-party stylesheets (a different origin than the page itself, e.g. a font CDN) are never fetched — only same-origin CSS, so findings only ever point at code the project actually owns.
|
|
68
|
+
|
|
69
|
+
### Suggested fixes
|
|
70
|
+
|
|
71
|
+
Every `near-miss` finding with a resolved nearest token gets a `suggestion` — the concrete replacement text (`var(--token-name)` for a CSS value, or `motionTokens.path.to.value` for a motion token). It's advisory text for a human to apply, not an automatic edit: nothing in this package rewrites your source files. An `unrecognized` value with no close match, and a `missing`-state finding, never get one — there's no safe mechanical fix for either.
|
|
72
|
+
|
|
48
73
|
### Health score
|
|
49
74
|
|
|
50
75
|
`match` counts fully, `near-miss` counts half (it drifted, but is still recognizably close to a real token), `unrecognized` and a missing required state count for nothing. A scan with zero checks scores 100.
|
|
@@ -65,10 +90,12 @@ This repo is also a GitHub Action — `omrdev1/maddox-cli` — that runs a scan,
|
|
|
65
90
|
|
|
66
91
|
Inputs:
|
|
67
92
|
|
|
68
|
-
- `target-dir` *(required)* — directory to scan
|
|
93
|
+
- `target-dir` *(required unless `url` is set)* — directory to scan
|
|
69
94
|
- `theme-css` *(required)* — path to the CSS file containing the `@theme` block
|
|
70
95
|
- `motion-tokens` — path to a motion-tokens JSON file
|
|
71
|
-
- `states` — path to a state-contract JSON file
|
|
96
|
+
- `states` — path to a state-contract JSON file. No effect when `url` is set.
|
|
97
|
+
- `tokens-studio` — path to a Tokens Studio / W3C Design Tokens JSON export
|
|
98
|
+
- `url` — a deployed page URL to scan instead of local source
|
|
72
99
|
- `github-token` *(required)* — for posting the PR comment, usually `${{ secrets.GITHUB_TOKEN }}`
|
|
73
100
|
- `fail-below` — fail the build below this health score; omit for comment-only
|
|
74
101
|
- `api-key` / `api-url` — optional, upload results to a [Maddox Engine](https://www.maddoxengine.com) dashboard account for scan history and drift trends across projects (a separate hosted product, not required to use the Action itself)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"global": {
|
|
3
|
+
"color": {
|
|
4
|
+
"brand": {
|
|
5
|
+
"500": { "value": "#4f46e5", "type": "color" },
|
|
6
|
+
"600": { "value": "{color.brand.500}", "type": "color" }
|
|
7
|
+
},
|
|
8
|
+
"neutral": {
|
|
9
|
+
"900": { "value": "#0a0a0b", "type": "color" }
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"spacing": {
|
|
13
|
+
"sm": { "value": "8px", "type": "spacing" },
|
|
14
|
+
"md": { "value": "16px", "type": "spacing" }
|
|
15
|
+
},
|
|
16
|
+
"typography": {
|
|
17
|
+
"heading": {
|
|
18
|
+
"value": { "fontFamily": "Inter", "fontSize": "32px" },
|
|
19
|
+
"type": "typography"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"dark": {
|
|
24
|
+
"color": {
|
|
25
|
+
"neutral": {
|
|
26
|
+
"900": { "value": "#000000", "type": "color" }
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "maddox-engine",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Scans source code for design-token, motion, and component-state drift against your own design system — the same engine behind Maddox Engine's CI checks.",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Scans source code or a live deployed page for design-token, motion, and component-state drift against your own design system — the same engine behind Maddox Engine's CI checks.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"bin": {
|
package/src/cli.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env -S npx tsx
|
|
2
2
|
|
|
3
3
|
import { readFileSync } from "node:fs";
|
|
4
|
-
import { audit, healthScore, loadGroundTruth, loadStateContract } from "./core.js";
|
|
4
|
+
import { audit, auditUrl, healthScore, loadGroundTruth, loadStateContract } from "./core.js";
|
|
5
5
|
import { summarize, toJson, toMarkdown, toText, type ScanSummary } from "./format.js";
|
|
6
6
|
|
|
7
7
|
async function uploadResult(apiUrl: string, apiKey: string, projectName: string, summary: ScanSummary) {
|
|
@@ -36,18 +36,23 @@ function flagValue(name: string): string | undefined {
|
|
|
36
36
|
async function main() {
|
|
37
37
|
const targetDir = process.argv[2];
|
|
38
38
|
const themeCssPath = process.argv[3];
|
|
39
|
+
const url = flagValue("--url");
|
|
39
40
|
|
|
40
|
-
if (!
|
|
41
|
+
if (!themeCssPath || (!targetDir && !url)) {
|
|
41
42
|
console.error(
|
|
42
43
|
"Usage: pnpm scan <target-source-dir> <path-to-globals.css-with-@theme-block> " +
|
|
43
44
|
"[--project <name>] [--motion <path-to-motion-tokens.json>] " +
|
|
44
45
|
"[--states <path-to-state-contract.json>] [--format text|json|markdown] " +
|
|
45
|
-
"[--fail-below <0-100>]"
|
|
46
|
+
"[--fail-below <0-100>] [--tokens-studio <path-to-tokens-studio-export.json>] " +
|
|
47
|
+
"[--url <live-page-url>]\n" +
|
|
48
|
+
" --url scans a deployed page's rendered HTML/CSS instead of local source " +
|
|
49
|
+
"(pass a placeholder for <target-source-dir>, e.g. '-', when using --url alone). " +
|
|
50
|
+
"--states has no effect in --url mode: state-completeness is a source-code check."
|
|
46
51
|
);
|
|
47
52
|
process.exit(1);
|
|
48
53
|
}
|
|
49
54
|
|
|
50
|
-
const projectName = flagValue("--project") ?? targetDir.split("/").pop() ?? "unnamed-project";
|
|
55
|
+
const projectName = flagValue("--project") ?? (url ? new URL(url).hostname : targetDir.split("/").pop()) ?? "unnamed-project";
|
|
51
56
|
const format = flagValue("--format") ?? "text";
|
|
52
57
|
|
|
53
58
|
// Motion tokens are an optional external JSON file (a plain object like
|
|
@@ -62,8 +67,10 @@ async function main() {
|
|
|
62
67
|
const statesPath = flagValue("--states");
|
|
63
68
|
const stateContract = statesPath ? loadStateContract(statesPath) : undefined;
|
|
64
69
|
|
|
65
|
-
const
|
|
66
|
-
const
|
|
70
|
+
const tokensStudioPath = flagValue("--tokens-studio");
|
|
71
|
+
const groundTruth = loadGroundTruth(themeCssPath, motionTokens, tokensStudioPath);
|
|
72
|
+
|
|
73
|
+
const result = url ? await auditUrl(url, groundTruth) : await audit(targetDir, groundTruth, stateContract);
|
|
67
74
|
const summary = summarize(result.findings, result.filesScanned);
|
|
68
75
|
|
|
69
76
|
if (format === "json") {
|
package/src/core.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
export * from "./types.js";
|
|
2
2
|
export * from "./groundTruth.js";
|
|
3
|
+
export * from "./tokensStudio.js";
|
|
3
4
|
export * from "./scan.js";
|
|
5
|
+
export * from "./crawl.js";
|
|
4
6
|
export * from "./diff.js";
|
|
5
7
|
export * from "./stateCheck.js";
|
|
6
8
|
export * from "./healthScore.js";
|
|
9
|
+
export * from "./suggestFix.js";
|
|
7
10
|
|
|
8
11
|
import { scanSource, scanFiles } from "./scan.js";
|
|
12
|
+
import { crawlUrl } from "./crawl.js";
|
|
9
13
|
import { diffUsages } from "./diff.js";
|
|
10
14
|
import { checkStates } from "./stateCheck.js";
|
|
15
|
+
import { attachSuggestions } from "./suggestFix.js";
|
|
11
16
|
import type { GroundTruth, ScanResult, StateContract } from "./types.js";
|
|
12
17
|
|
|
13
18
|
export async function audit(
|
|
@@ -25,5 +30,22 @@ export async function audit(
|
|
|
25
30
|
for (const f of files) scannedFiles.add(f.file);
|
|
26
31
|
}
|
|
27
32
|
|
|
33
|
+
attachSuggestions(findings);
|
|
34
|
+
|
|
28
35
|
return { findings, filesScanned: scannedFiles.size };
|
|
29
36
|
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The production-aware counterpart to audit(): fetches a live page (plus
|
|
40
|
+
* its same-origin stylesheets) instead of walking local source files, and
|
|
41
|
+
* diffs what's actually shipped against the same ground truth. No state
|
|
42
|
+
* contract here — component-state completeness is a source-code question
|
|
43
|
+
* (is the state referenced anywhere in this file's source), which has no
|
|
44
|
+
* equivalent for a fetched, already-rendered page.
|
|
45
|
+
*/
|
|
46
|
+
export async function auditUrl(pageUrl: string, groundTruth: GroundTruth): Promise<ScanResult> {
|
|
47
|
+
const { usages, sourcesScanned } = await crawlUrl(pageUrl);
|
|
48
|
+
const findings = diffUsages(usages, groundTruth);
|
|
49
|
+
attachSuggestions(findings);
|
|
50
|
+
return { findings, filesScanned: sourcesScanned.length };
|
|
51
|
+
}
|
package/src/crawl.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { extractFromContent, type RawUsage } from "./scan.js";
|
|
2
|
+
|
|
3
|
+
// Only ever follow relative/absolute-path stylesheet links resolved
|
|
4
|
+
// against the page's own origin — never a third-party CDN's CSS, since
|
|
5
|
+
// that's not the project's own design system and would only add noise
|
|
6
|
+
// (and, worse, false "unrecognized" findings for code the project never
|
|
7
|
+
// wrote).
|
|
8
|
+
function isSameOrigin(href: string, pageUrl: URL): boolean {
|
|
9
|
+
try {
|
|
10
|
+
const resolved = new URL(href, pageUrl);
|
|
11
|
+
return resolved.origin === pageUrl.origin;
|
|
12
|
+
} catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Matches <link ... rel="stylesheet" ... href="..."> and the reverse
|
|
18
|
+
// attribute order, case-insensitively, without a full HTML parser —
|
|
19
|
+
// good enough for the handful of <link> tags a real page head has.
|
|
20
|
+
const STYLESHEET_LINK_RE =
|
|
21
|
+
/<link\b[^>]*\brel=["']stylesheet["'][^>]*\bhref=["']([^"']+)["'][^>]*>|<link\b[^>]*\bhref=["']([^"']+)["'][^>]*\brel=["']stylesheet["'][^>]*>/gi;
|
|
22
|
+
|
|
23
|
+
function findStylesheetHrefs(html: string): string[] {
|
|
24
|
+
const hrefs: string[] = [];
|
|
25
|
+
STYLESHEET_LINK_RE.lastIndex = 0;
|
|
26
|
+
let m: RegExpExecArray | null;
|
|
27
|
+
while ((m = STYLESHEET_LINK_RE.exec(html)) !== null) {
|
|
28
|
+
hrefs.push(m[1] ?? m[2]);
|
|
29
|
+
}
|
|
30
|
+
return hrefs;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface CrawlResult {
|
|
34
|
+
usages: RawUsage[];
|
|
35
|
+
// The page URL plus every stylesheet URL actually fetched — reported
|
|
36
|
+
// back so a caller (or the CLI's own output) can show what was scanned,
|
|
37
|
+
// the live-URL equivalent of scanSource's file count.
|
|
38
|
+
sourcesScanned: string[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Fetches a live page's rendered HTML plus every same-origin linked
|
|
43
|
+
* stylesheet, and runs the same color/font-size/motion literal
|
|
44
|
+
* extraction scanSource runs over local source files. This is what makes
|
|
45
|
+
* Maddox "production-aware" rather than local-source-only: it checks
|
|
46
|
+
* what a real deployed page actually ships, not just what's in the repo
|
|
47
|
+
* at scan time (a config typo, a stale CDN cache, or a build step that
|
|
48
|
+
* silently drops a token could all cause the two to diverge).
|
|
49
|
+
*
|
|
50
|
+
* Framer Motion / GSAP usage is deliberately dropped from the result.
|
|
51
|
+
* Real motion libraries set durations/eases via the JS animation runtime,
|
|
52
|
+
* not static markup, so they never legitimately appear in rendered
|
|
53
|
+
* HTML/CSS — but framework-compiled CSS reliably DOES contain unrelated
|
|
54
|
+
* matches for the same shared regex (confirmed against a real deploy:
|
|
55
|
+
* Tailwind's own `--default-transition-duration:.15s` custom property
|
|
56
|
+
* matches the duration pattern, `transition-timing-function` matches the
|
|
57
|
+
* ease pattern). Rather than surface those as noisy false "unrecognized"
|
|
58
|
+
* findings, motion usages are filtered out of every crawl result before
|
|
59
|
+
* it's returned — motion checking stays a source-scan-only capability.
|
|
60
|
+
*/
|
|
61
|
+
export async function crawlUrl(pageUrl: string): Promise<CrawlResult> {
|
|
62
|
+
const url = new URL(pageUrl);
|
|
63
|
+
const usages: RawUsage[] = [];
|
|
64
|
+
const sourcesScanned: string[] = [];
|
|
65
|
+
|
|
66
|
+
const pageResponse = await fetch(url, { redirect: "follow" });
|
|
67
|
+
if (!pageResponse.ok) {
|
|
68
|
+
throw new Error(`Failed to fetch ${url}: ${pageResponse.status} ${pageResponse.statusText}`);
|
|
69
|
+
}
|
|
70
|
+
const html = await pageResponse.text();
|
|
71
|
+
usages.push(...extractFromContent(url.toString(), html));
|
|
72
|
+
sourcesScanned.push(url.toString());
|
|
73
|
+
|
|
74
|
+
const stylesheetHrefs = findStylesheetHrefs(html).filter((href) => isSameOrigin(href, url));
|
|
75
|
+
|
|
76
|
+
for (const href of stylesheetHrefs) {
|
|
77
|
+
const cssUrl = new URL(href, url).toString();
|
|
78
|
+
try {
|
|
79
|
+
const cssResponse = await fetch(cssUrl);
|
|
80
|
+
if (!cssResponse.ok) continue;
|
|
81
|
+
const css = await cssResponse.text();
|
|
82
|
+
usages.push(...extractFromContent(cssUrl, css));
|
|
83
|
+
sourcesScanned.push(cssUrl);
|
|
84
|
+
} catch {
|
|
85
|
+
// A single unreachable stylesheet shouldn't fail the whole crawl —
|
|
86
|
+
// same tolerance readScannedFiles already has for an unreadable
|
|
87
|
+
// local file.
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { usages: usages.filter((u) => u.kind !== "motion"), sourcesScanned };
|
|
92
|
+
}
|
package/src/format.ts
CHANGED
|
@@ -30,7 +30,7 @@ export function toText(summary: ScanSummary): string {
|
|
|
30
30
|
lines.push(
|
|
31
31
|
`${f.file}:${f.line} [${f.kind}] ${f.rawValue} → ${f.severity}${
|
|
32
32
|
f.nearestToken ? ` (nearest: ${f.nearestToken})` : ""
|
|
33
|
-
}`
|
|
33
|
+
}${f.suggestion ? ` fix: ${f.suggestion}` : ""}`
|
|
34
34
|
);
|
|
35
35
|
}
|
|
36
36
|
return lines.join("\n");
|
|
@@ -60,10 +60,10 @@ export function toMarkdown(summary: ScanSummary): string {
|
|
|
60
60
|
return lines.join("\n");
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
lines.push("<details><summary>Findings</summary>", "", "| Location | Kind | Value | Severity | Nearest token |", "
|
|
63
|
+
lines.push("<details><summary>Findings</summary>", "", "| Location | Kind | Value | Severity | Nearest token | Suggested fix |", "|---|---|---|---|---|---|");
|
|
64
64
|
for (const f of nonMatch.slice(0, 100)) {
|
|
65
65
|
lines.push(
|
|
66
|
-
`| \`${f.file}:${f.line}\` | ${f.kind} | \`${f.rawValue}\` | ${f.severity} | ${f.nearestToken ? `\`${f.nearestToken}\`` : "—"} |`
|
|
66
|
+
`| \`${f.file}:${f.line}\` | ${f.kind} | \`${f.rawValue}\` | ${f.severity} | ${f.nearestToken ? `\`${f.nearestToken}\`` : "—"} | ${f.suggestion ? `\`${f.suggestion}\`` : "—"} |`
|
|
67
67
|
);
|
|
68
68
|
}
|
|
69
69
|
if (nonMatch.length > 100) {
|
package/src/groundTruth.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
|
+
import { loadTokensStudioFile } from "./tokensStudio.js";
|
|
2
3
|
import type { GroundTruth, MotionToken, StateContract, TokenMap } from "./types.js";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -52,10 +53,21 @@ export function loadStateContract(path: string): StateContract {
|
|
|
52
53
|
|
|
53
54
|
export function loadGroundTruth(
|
|
54
55
|
themeCssPath: string,
|
|
55
|
-
motionTokensObj: Record<string, unknown
|
|
56
|
+
motionTokensObj: Record<string, unknown>,
|
|
57
|
+
tokensStudioPath?: string
|
|
56
58
|
): GroundTruth {
|
|
59
|
+
// When both a @theme CSS file and a Tokens Studio export are given, the
|
|
60
|
+
// Tokens Studio export (the design tool's source of truth) wins on any
|
|
61
|
+
// path both define — the CSS file is usually the codegen'd output of
|
|
62
|
+
// the same tokens, so a conflict means the CSS is stale, not that the
|
|
63
|
+
// design tokens are wrong.
|
|
64
|
+
const tokens = {
|
|
65
|
+
...parseThemeTokens(themeCssPath),
|
|
66
|
+
...(tokensStudioPath ? loadTokensStudioFile(tokensStudioPath) : {}),
|
|
67
|
+
};
|
|
68
|
+
|
|
57
69
|
return {
|
|
58
|
-
tokens
|
|
70
|
+
tokens,
|
|
59
71
|
motion: flattenMotionTokens(motionTokensObj),
|
|
60
72
|
};
|
|
61
73
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Finding } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Builds the suggested source replacement for a single Finding's raw
|
|
5
|
+
* value, given the token/motion-path name diff.ts already matched it
|
|
6
|
+
* against (`nearestToken`). Returns undefined when no safe mechanical
|
|
7
|
+
* suggestion exists — either there's no nearestToken to point at, or the
|
|
8
|
+
* finding kind isn't a value swap at all (state completeness).
|
|
9
|
+
*
|
|
10
|
+
* Deliberately conservative: this only ever proposes replacing the exact
|
|
11
|
+
* literal Maddox found with a reference to the token it's closest to. It
|
|
12
|
+
* never tries to rewrite surrounding code, infer intent for an
|
|
13
|
+
* "unrecognized" value with no near match, or auto-apply anything — the
|
|
14
|
+
* suggestion is text for a human (or a future codemod) to review, not an
|
|
15
|
+
* automatic edit.
|
|
16
|
+
*/
|
|
17
|
+
export function suggestFix(finding: Finding): string | undefined {
|
|
18
|
+
if (!finding.nearestToken) return undefined;
|
|
19
|
+
if (finding.severity !== "near-miss" && finding.severity !== "match") return undefined;
|
|
20
|
+
|
|
21
|
+
switch (finding.kind) {
|
|
22
|
+
case "color":
|
|
23
|
+
case "font-size":
|
|
24
|
+
case "spacing":
|
|
25
|
+
// nearestToken is a "--token-name" CSS custom property name — the
|
|
26
|
+
// replacement is how that token is actually consumed in Tailwind
|
|
27
|
+
// v4 / plain CSS: var(--token-name).
|
|
28
|
+
return `var(${finding.nearestToken})`;
|
|
29
|
+
|
|
30
|
+
case "motion": {
|
|
31
|
+
// nearestToken is a dotted motion-token path, e.g.
|
|
32
|
+
// "transition.duration" — how it's actually referenced depends on
|
|
33
|
+
// the project's own motion tokens object import, which Maddox has
|
|
34
|
+
// no fixed name for, so this suggests the access path relative to
|
|
35
|
+
// a generic `motionTokens` import rather than guessing a project's
|
|
36
|
+
// real variable name.
|
|
37
|
+
return `motionTokens.${finding.nearestToken}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
case "state":
|
|
41
|
+
// No mechanical value swap exists for a missing state — this needs
|
|
42
|
+
// a human to add real handling.
|
|
43
|
+
return undefined;
|
|
44
|
+
|
|
45
|
+
default:
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Applies suggestFix to every finding in place, populating `.suggestion`
|
|
52
|
+
* where a safe suggestion exists. Mutates and returns the same array —
|
|
53
|
+
* matches diffUsages' existing per-finding transform style rather than
|
|
54
|
+
* introducing a different pattern for one more derived field.
|
|
55
|
+
*/
|
|
56
|
+
export function attachSuggestions(findings: Finding[]): Finding[] {
|
|
57
|
+
for (const finding of findings) {
|
|
58
|
+
finding.suggestion = suggestFix(finding);
|
|
59
|
+
}
|
|
60
|
+
return findings;
|
|
61
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import type { TokenMap } from "./types.js";
|
|
3
|
+
|
|
4
|
+
// A single node in a Tokens Studio / W3C Design Tokens export: either a
|
|
5
|
+
// leaf token ({ value, type }) or a group of further-nested nodes. Groups
|
|
6
|
+
// and leaves are told apart by the presence of a "value" key, per the W3C
|
|
7
|
+
// Design Tokens Community Group format Tokens Studio exports.
|
|
8
|
+
interface TokenNode {
|
|
9
|
+
value?: string | number;
|
|
10
|
+
type?: string;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type TokenTree = { [key: string]: TokenNode | TokenTree };
|
|
15
|
+
|
|
16
|
+
// Only these Tokens Studio types resolve to the single raw CSS value that
|
|
17
|
+
// Maddox's TokenMap expects (a color hex, or a px/rem/em length) — the
|
|
18
|
+
// others (typography, boxShadow, border, composite tokens) describe a
|
|
19
|
+
// bundle of properties, not one comparable value, and are skipped rather
|
|
20
|
+
// than half-mapped into a shape that would silently misclassify usages.
|
|
21
|
+
const SUPPORTED_TYPES = new Set(["color", "fontSizes", "spacing", "sizing", "borderRadius", "dimension"]);
|
|
22
|
+
|
|
23
|
+
function isLeaf(node: TokenNode | TokenTree): node is TokenNode {
|
|
24
|
+
return typeof (node as TokenNode).value !== "undefined";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Resolves a Tokens Studio alias reference, e.g. "{color.brand.500}", by
|
|
29
|
+
* looking up the dotted path in the flattened token map already built so
|
|
30
|
+
* far. Aliases must point at a token defined earlier in traversal order;
|
|
31
|
+
* circular or forward references are left unresolved (returned as-is)
|
|
32
|
+
* rather than causing an infinite loop.
|
|
33
|
+
*/
|
|
34
|
+
function resolveAlias(raw: string, resolved: Map<string, string>, depth = 0): string {
|
|
35
|
+
const match = /^\{([^}]+)\}$/.exec(raw.trim());
|
|
36
|
+
if (!match || depth > 10) return raw;
|
|
37
|
+
|
|
38
|
+
const target = resolved.get(match[1]);
|
|
39
|
+
if (target === undefined) return raw;
|
|
40
|
+
return resolveAlias(target, resolved, depth + 1);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Flattens a Tokens Studio JSON tree (single set, e.g. the "global" set
|
|
45
|
+
* from a multi-set export, or the whole file for a single-set export)
|
|
46
|
+
* into a flat `path.to.token` -> raw-css-value map, resolving `{alias}`
|
|
47
|
+
* references along the way. Unsupported token types are skipped, not
|
|
48
|
+
* coerced.
|
|
49
|
+
*/
|
|
50
|
+
export function flattenTokensStudioTree(tree: TokenTree): TokenMap {
|
|
51
|
+
const resolved = new Map<string, string>();
|
|
52
|
+
|
|
53
|
+
function walk(node: TokenNode | TokenTree, path: string): void {
|
|
54
|
+
if (isLeaf(node)) {
|
|
55
|
+
if (node.type && !SUPPORTED_TYPES.has(node.type)) return;
|
|
56
|
+
const raw = String(node.value);
|
|
57
|
+
const value = raw.startsWith("{") ? resolveAlias(raw, resolved) : raw;
|
|
58
|
+
resolved.set(path, value);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
for (const [key, child] of Object.entries(node)) {
|
|
62
|
+
if (key.startsWith("$")) continue; // Tokens Studio metadata keys, e.g. $themes, $metadata
|
|
63
|
+
walk(child as TokenNode | TokenTree, path ? `${path}.${key}` : key);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
walk(tree, "");
|
|
68
|
+
|
|
69
|
+
const tokens: TokenMap = {};
|
|
70
|
+
for (const [path, value] of resolved) {
|
|
71
|
+
// Drop any alias that never resolved to a raw value (dangling/forward
|
|
72
|
+
// reference) — an unresolved "{...}" string is not a comparable CSS
|
|
73
|
+
// value and would never match a scanned usage anyway.
|
|
74
|
+
if (value.startsWith("{")) continue;
|
|
75
|
+
tokens[`--${path.replace(/\./g, "-")}`] = value;
|
|
76
|
+
}
|
|
77
|
+
return tokens;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Loads a Tokens Studio JSON export from disk and flattens it into a
|
|
82
|
+
* TokenMap. Handles both a single-set export (the whole file is one
|
|
83
|
+
* token tree) and a multi-set export (top-level keys are set names, e.g.
|
|
84
|
+
* "global", "dark" — every non-$-prefixed top-level key is merged, later
|
|
85
|
+
* sets overriding earlier ones by path, since Maddox compares against one
|
|
86
|
+
* flat ground truth rather than a per-theme one).
|
|
87
|
+
*/
|
|
88
|
+
export function loadTokensStudioFile(path: string): TokenMap {
|
|
89
|
+
const raw = JSON.parse(readFileSync(path, "utf-8")) as TokenTree;
|
|
90
|
+
|
|
91
|
+
const topLevelKeys = Object.keys(raw).filter((k) => !k.startsWith("$"));
|
|
92
|
+
const looksLikeMultiSet = topLevelKeys.every(
|
|
93
|
+
(k) => typeof raw[k] === "object" && raw[k] !== null && !isLeaf(raw[k] as TokenNode)
|
|
94
|
+
&& Object.values(raw[k] as TokenTree).some((v) => typeof v === "object")
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
if (!looksLikeMultiSet) {
|
|
98
|
+
return flattenTokensStudioTree(raw);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const merged: TokenMap = {};
|
|
102
|
+
for (const key of topLevelKeys) {
|
|
103
|
+
Object.assign(merged, flattenTokensStudioTree(raw[key] as TokenTree));
|
|
104
|
+
}
|
|
105
|
+
return merged;
|
|
106
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -24,6 +24,13 @@ export interface Finding {
|
|
|
24
24
|
rawValue: string;
|
|
25
25
|
severity: Severity;
|
|
26
26
|
nearestToken?: string;
|
|
27
|
+
// A suggested source replacement for `rawValue`, only ever set for
|
|
28
|
+
// "near-miss" or "match"-adjacent findings with a resolvable
|
|
29
|
+
// nearestToken — see suggestFix.ts. Left undefined (never an empty
|
|
30
|
+
// string) when no safe mechanical suggestion exists, e.g. a "missing"
|
|
31
|
+
// state finding, which needs a human to add real handling, not a
|
|
32
|
+
// value swap.
|
|
33
|
+
suggestion?: string;
|
|
27
34
|
}
|
|
28
35
|
|
|
29
36
|
export interface ScanResult {
|