design-constraint-validator 1.0.0 → 2.0.1
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/LICENSE +21 -21
- package/README.md +229 -659
- package/adapters/README.md +46 -46
- package/adapters/css.ts +116 -116
- package/adapters/decisionthemes.d.ts +44 -0
- package/adapters/decisionthemes.d.ts.map +1 -0
- package/adapters/decisionthemes.js +35 -0
- package/adapters/decisionthemes.ts +59 -0
- package/adapters/js.ts +14 -14
- package/adapters/json.ts +45 -45
- package/cli/build-css.ts +32 -32
- package/cli/commands/build.ts +65 -65
- package/cli/commands/graph.d.ts.map +1 -1
- package/cli/commands/graph.js +26 -10
- package/cli/commands/graph.ts +180 -137
- package/cli/commands/index.ts +7 -7
- package/cli/commands/patch-apply.ts +80 -80
- package/cli/commands/patch.ts +22 -22
- package/cli/commands/set.d.ts.map +1 -1
- package/cli/commands/set.js +12 -4
- package/cli/commands/set.ts +239 -225
- package/cli/commands/utils.ts +50 -50
- package/cli/commands/validate.d.ts.map +1 -1
- package/cli/commands/validate.js +89 -33
- package/cli/commands/validate.ts +180 -115
- package/cli/commands/why.d.ts.map +1 -1
- package/cli/commands/why.js +86 -20
- package/cli/commands/why.ts +158 -46
- package/cli/config-schema.ts +27 -27
- package/cli/config.ts +35 -35
- package/cli/constraint-registry.d.ts +101 -0
- package/cli/constraint-registry.d.ts.map +1 -0
- package/cli/constraint-registry.js +225 -0
- package/cli/constraint-registry.ts +304 -0
- package/cli/constraints-loader.d.ts.map +1 -0
- package/cli/cross-axis-loader.d.ts +91 -0
- package/cli/cross-axis-loader.d.ts.map +1 -0
- package/cli/cross-axis-loader.js +222 -0
- package/cli/cross-axis-loader.ts +289 -0
- package/cli/dcv.js +4 -0
- package/cli/dcv.ts +111 -107
- package/cli/engine-helpers.d.ts.map +1 -1
- package/cli/graph-poset.ts +74 -74
- package/cli/json-output.d.ts +69 -0
- package/cli/json-output.d.ts.map +1 -0
- package/cli/json-output.js +109 -0
- package/cli/json-output.ts +184 -0
- package/cli/result.ts +27 -27
- package/cli/run.ts +54 -54
- package/cli/smoke-test.ts +40 -40
- package/cli/types.d.ts +6 -0
- package/cli/types.d.ts.map +1 -1
- package/cli/types.ts +84 -78
- package/cli/version-banner.d.ts +20 -0
- package/cli/version-banner.d.ts.map +1 -0
- package/cli/version-banner.js +49 -0
- package/cli/version-banner.ts +61 -0
- package/core/breakpoints.ts +50 -50
- package/core/cli-format.ts +31 -31
- package/core/color.ts +148 -148
- package/core/constraints/cross-axis.ts +114 -114
- package/core/constraints/monotonic-lightness.ts +38 -38
- package/core/constraints/monotonic.ts +74 -74
- package/core/constraints/threshold.ts +43 -43
- package/core/constraints/wcag.ts +70 -70
- package/core/cross-axis-config.d.ts.map +1 -1
- package/core/engine.d.ts +95 -0
- package/core/engine.d.ts.map +1 -1
- package/core/engine.js +22 -0
- package/core/engine.ts +167 -65
- package/core/flatten.ts +116 -116
- package/core/image-export.ts +48 -48
- package/core/index.d.ts +9 -30
- package/core/index.d.ts.map +1 -1
- package/core/index.js +7 -54
- package/core/index.ts +10 -72
- package/core/patch.ts +134 -134
- package/core/poset.ts +311 -311
- package/core/why.ts +63 -63
- package/package.json +96 -90
- package/themes/color.lg.order.json +15 -15
- package/themes/color.md.order.json +15 -15
- package/themes/color.order.json +15 -15
- package/themes/color.sm.order.json +15 -15
- package/themes/cross-axis.rules.json +35 -35
- package/themes/cross-axis.sm.rules.json +12 -12
- package/themes/layout.lg.order.json +18 -18
- package/themes/layout.md.order.json +18 -18
- package/themes/layout.order.json +18 -18
- package/themes/layout.sm.order.json +18 -18
- package/themes/spacing.order.json +14 -14
- package/themes/typography.lg.order.json +15 -15
- package/themes/typography.md.order.json +15 -15
- package/themes/typography.order.json +15 -15
- package/themes/typography.sm.order.json +15 -15
- package/cli/engine-helpers.d.ts +0 -8
- package/cli/engine-helpers.js +0 -70
- package/cli/engine-helpers.ts +0 -61
- package/core/cross-axis-config.d.ts +0 -5
- package/core/cross-axis-config.js +0 -144
- package/core/cross-axis-config.ts +0 -152
- package/dist/test-overrides-removal.json +0 -4
- package/dist/tmp.patch.json +0 -35
- package/tokens/overrides/base.json +0 -22
- package/tokens/overrides/lg.json +0 -20
- package/tokens/overrides/md.json +0 -16
- package/tokens/overrides/sm.json +0 -16
- package/tokens/overrides/viol.color.json +0 -6
- package/tokens/overrides/viol.typography.json +0 -6
- package/tokens/tokens.demo-violations.json +0 -116
- package/tokens/tokens.example.json +0 -128
- package/tokens/tokens.json +0 -67
- package/tokens/tokens.multi-violations.json +0 -21
- package/tokens/tokens.schema.d.ts +0 -2298
- package/tokens/tokens.schema.d.ts.map +0 -1
- package/tokens/tokens.schema.js +0 -148
- package/tokens/tokens.schema.ts +0 -196
- package/tokens/tokens.test.json +0 -38
- package/tokens/tokens.touch-violation.json +0 -8
- package/tokens/typography.classes.css +0 -11
- package/tokens/typography.css +0 -20
package/cli/commands/why.ts
CHANGED
|
@@ -1,46 +1,158 @@
|
|
|
1
|
-
import { readFileSync } from 'node:fs';
|
|
2
|
-
import { flattenTokens } from '../../core/flatten.js';
|
|
3
|
-
import { explain } from '../../core/why.js';
|
|
4
|
-
import type { WhyOptions } from '../types.js';
|
|
5
|
-
import { loadTokens } from './utils.js';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { flattenTokens, type FlatToken } from '../../core/flatten.js';
|
|
3
|
+
import { explain } from '../../core/why.js';
|
|
4
|
+
import type { WhyOptions } from '../types.js';
|
|
5
|
+
import { loadTokens } from './utils.js';
|
|
6
|
+
import { Engine } from '../../core/engine.js';
|
|
7
|
+
import { loadConfig } from '../config.js';
|
|
8
|
+
import type { ConstraintIssue } from '../../core/engine.js';
|
|
9
|
+
import { setupConstraints } from '../constraint-registry.js';
|
|
10
|
+
|
|
11
|
+
export async function whyCommand(options: WhyOptions): Promise<void> {
|
|
12
|
+
const tokensPath = options.tokens || 'tokens/tokens.json';
|
|
13
|
+
const tokens = loadTokens(tokensPath);
|
|
14
|
+
const { flat, edges } = flattenTokens(tokens);
|
|
15
|
+
const target = options.tokenId;
|
|
16
|
+
|
|
17
|
+
if (!flat[target]) {
|
|
18
|
+
console.error(`Token not found: ${target}`);
|
|
19
|
+
const { suggestIds } = await import('../../core/cli-format.js');
|
|
20
|
+
const suggestions = suggestIds(target, Object.keys(flat));
|
|
21
|
+
if (suggestions.length > 0) {
|
|
22
|
+
console.log('\nDid you mean:');
|
|
23
|
+
suggestions.slice(0, 5).forEach((s) => console.log(` ${s.id}`));
|
|
24
|
+
} else {
|
|
25
|
+
console.log('\nAvailable tokens:');
|
|
26
|
+
Object.keys(flat)
|
|
27
|
+
.sort()
|
|
28
|
+
.slice(0, 10)
|
|
29
|
+
.forEach((id) => console.log(` ${id}`));
|
|
30
|
+
if (Object.keys(flat).length > 10) {
|
|
31
|
+
console.log(` ... and ${Object.keys(flat).length - 10} more`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function safeLoad(p: string) {
|
|
38
|
+
try {
|
|
39
|
+
return JSON.parse(readFileSync(p, 'utf8'));
|
|
40
|
+
} catch {
|
|
41
|
+
return {};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const overrides = safeLoad('tokens/overrides/local.json');
|
|
46
|
+
const theme = safeLoad('themes/theme.json');
|
|
47
|
+
|
|
48
|
+
const baseReport = explain(target, flat, edges, {
|
|
49
|
+
overrides: (overrides as any)?.overrides ?? overrides,
|
|
50
|
+
theme,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Best-effort constraint summary: which rules currently implicate this token
|
|
54
|
+
let constraintsSummary:
|
|
55
|
+
| {
|
|
56
|
+
ruleId: string;
|
|
57
|
+
level: 'error' | 'warn';
|
|
58
|
+
message: string;
|
|
59
|
+
where?: string;
|
|
60
|
+
}[]
|
|
61
|
+
| undefined;
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
const cfgRes = loadConfig(options.config);
|
|
65
|
+
if (cfgRes.ok) {
|
|
66
|
+
const config = cfgRes.value;
|
|
67
|
+
|
|
68
|
+
// Create engine with flattened tokens
|
|
69
|
+
const init: Record<string, string | number> = {};
|
|
70
|
+
for (const t of Object.values(flat)) {
|
|
71
|
+
init[(t as FlatToken).id] = (t as FlatToken).value;
|
|
72
|
+
}
|
|
73
|
+
const engine = new Engine(init, edges);
|
|
74
|
+
const knownIds = new Set(Object.keys(init));
|
|
75
|
+
|
|
76
|
+
// Discover and attach all constraints via centralized registry
|
|
77
|
+
setupConstraints(
|
|
78
|
+
engine,
|
|
79
|
+
{ config, constraintsDir: 'themes' },
|
|
80
|
+
{ knownIds },
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const candidates = new Set<string>([target]);
|
|
84
|
+
const allIssues: ConstraintIssue[] = engine.evaluate(candidates);
|
|
85
|
+
if (allIssues.length) {
|
|
86
|
+
const related = allIssues.filter((issue) => {
|
|
87
|
+
const parts = String(issue.id).split('|');
|
|
88
|
+
return parts.includes(target);
|
|
89
|
+
});
|
|
90
|
+
if (related.length) {
|
|
91
|
+
constraintsSummary = related.map((issue) => ({
|
|
92
|
+
ruleId: issue.rule,
|
|
93
|
+
level: issue.level,
|
|
94
|
+
message: issue.message,
|
|
95
|
+
where: issue.where,
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
} catch {
|
|
101
|
+
// If constraint analysis fails, fall back to provenance-only report.
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const report: any = constraintsSummary ? { ...baseReport, constraints: constraintsSummary } : baseReport;
|
|
105
|
+
|
|
106
|
+
const format = options.format || 'json';
|
|
107
|
+
if (format === 'table') {
|
|
108
|
+
const { pad, trunc } = await import('../../core/cli-format.js');
|
|
109
|
+
console.log(`\n=== Token Analysis: ${target} ===`);
|
|
110
|
+
console.log(`Value: ${report.value}`);
|
|
111
|
+
console.log(`Raw: ${report.raw ?? 'N/A'}`);
|
|
112
|
+
console.log(`Provenance: ${report.provenance}`);
|
|
113
|
+
|
|
114
|
+
if (report.dependsOn && report.dependsOn.length > 0) {
|
|
115
|
+
console.log(`\nDependencies (${report.dependsOn.length}):`);
|
|
116
|
+
console.log(pad('TOKEN', 30) + pad('VALUE', 20) + 'TYPE');
|
|
117
|
+
console.log('-'.repeat(70));
|
|
118
|
+
report.dependsOn.forEach((depId: string) => {
|
|
119
|
+
const dep = flat[depId] as any;
|
|
120
|
+
if (dep) {
|
|
121
|
+
console.log(
|
|
122
|
+
pad(trunc(depId, 28), 30) + pad(trunc(String(dep.value), 18), 20) + (dep.type || 'unknown'),
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (report.dependents && report.dependents.length > 0) {
|
|
129
|
+
console.log(`\nDependents (${report.dependents.length}):`);
|
|
130
|
+
console.log(pad('TOKEN', 30) + pad('VALUE', 20) + 'TYPE');
|
|
131
|
+
console.log('-'.repeat(70));
|
|
132
|
+
report.dependents.forEach((depId: string) => {
|
|
133
|
+
const dep = flat[depId] as any;
|
|
134
|
+
if (dep) {
|
|
135
|
+
console.log(
|
|
136
|
+
pad(trunc(depId, 28), 30) + pad(trunc(String(dep.value), 18), 20) + (dep.type || 'unknown'),
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (report.refs && report.refs.length > 0) {
|
|
143
|
+
console.log(`\nReferences: ${report.refs.join(', ')}`);
|
|
144
|
+
}
|
|
145
|
+
if (report.chain && report.chain.length > 0) {
|
|
146
|
+
console.log(`\nReference Chain: ${report.chain.join(' -> ')}`);
|
|
147
|
+
}
|
|
148
|
+
if (constraintsSummary && constraintsSummary.length > 0) {
|
|
149
|
+
console.log('\nConstraints (violations involving this token):');
|
|
150
|
+
constraintsSummary.forEach((c) => {
|
|
151
|
+
const where = c.where ? ` @ ${c.where}` : '';
|
|
152
|
+
console.log(`- [${c.level}] ${c.ruleId}${where}: ${c.message}`);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
console.log(JSON.stringify(report, null, 2));
|
|
157
|
+
}
|
|
158
|
+
}
|
package/cli/config-schema.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
|
|
3
|
-
export const WcagRuleSchema = z.object({
|
|
4
|
-
foreground: z.string(),
|
|
5
|
-
background: z.string(),
|
|
6
|
-
ratio: z.number().positive().optional(),
|
|
7
|
-
description: z.string().optional()
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
export const ConstraintsSchema = z.object({
|
|
11
|
-
wcag: z.array(WcagRuleSchema).optional()
|
|
12
|
-
}).passthrough();
|
|
13
|
-
|
|
14
|
-
export const DcvConfigSchema = z.object({
|
|
15
|
-
version: z.string().optional(),
|
|
16
|
-
constraints: ConstraintsSchema.optional()
|
|
17
|
-
}).passthrough();
|
|
18
|
-
|
|
19
|
-
export type DcvConfigParsed = z.infer<typeof DcvConfigSchema>;
|
|
20
|
-
|
|
21
|
-
export function validateConfig(raw: unknown): { value?: DcvConfigParsed; errors?: string[] } {
|
|
22
|
-
const res = DcvConfigSchema.safeParse(raw);
|
|
23
|
-
if (!res.success) {
|
|
24
|
-
return { errors: res.error.errors.map(e => `${e.path.join('.')||'<root>'}: ${e.message}`) };
|
|
25
|
-
}
|
|
26
|
-
return { value: res.data };
|
|
27
|
-
}
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
export const WcagRuleSchema = z.object({
|
|
4
|
+
foreground: z.string(),
|
|
5
|
+
background: z.string(),
|
|
6
|
+
ratio: z.number().positive().optional(),
|
|
7
|
+
description: z.string().optional()
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export const ConstraintsSchema = z.object({
|
|
11
|
+
wcag: z.array(WcagRuleSchema).optional()
|
|
12
|
+
}).passthrough();
|
|
13
|
+
|
|
14
|
+
export const DcvConfigSchema = z.object({
|
|
15
|
+
version: z.string().optional(),
|
|
16
|
+
constraints: ConstraintsSchema.optional()
|
|
17
|
+
}).passthrough();
|
|
18
|
+
|
|
19
|
+
export type DcvConfigParsed = z.infer<typeof DcvConfigSchema>;
|
|
20
|
+
|
|
21
|
+
export function validateConfig(raw: unknown): { value?: DcvConfigParsed; errors?: string[] } {
|
|
22
|
+
const res = DcvConfigSchema.safeParse(raw);
|
|
23
|
+
if (!res.success) {
|
|
24
|
+
return { errors: res.error.errors.map(e => `${e.path.join('.')||'<root>'}: ${e.message}`) };
|
|
25
|
+
}
|
|
26
|
+
return { value: res.data };
|
|
27
|
+
}
|
package/cli/config.ts
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
import { readFileSync, existsSync } from 'node:fs';
|
|
2
|
-
import { validateConfig } from './config-schema.js';
|
|
3
|
-
import { ok, err, type Result } from './result.js';
|
|
4
|
-
import type { DcvConfig } from './types.js';
|
|
5
|
-
|
|
6
|
-
export function loadConfig(configPath?: string): Result<DcvConfig, string> {
|
|
7
|
-
const candidates = configPath ? [configPath] : [
|
|
8
|
-
'dcv.config.json',
|
|
9
|
-
'dcv.config.js',
|
|
10
|
-
'.dcvrc.json',
|
|
11
|
-
'package.json'
|
|
12
|
-
];
|
|
13
|
-
for (const p of candidates) {
|
|
14
|
-
if (!existsSync(p)) continue;
|
|
15
|
-
try {
|
|
16
|
-
const rawTxt = readFileSync(p, 'utf8');
|
|
17
|
-
let raw: unknown = JSON.parse(rawTxt);
|
|
18
|
-
if (p === 'package.json' && raw && typeof raw === 'object') {
|
|
19
|
-
const pkg = raw as Record<string, unknown>;
|
|
20
|
-
if ('dcv' in pkg) {
|
|
21
|
-
raw = pkg.dcv;
|
|
22
|
-
} else {
|
|
23
|
-
continue; // No dcv config in package.json
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
const { value, errors } = validateConfig(raw);
|
|
27
|
-
if (errors) return err(`Config validation failed in ${p}:\n - ${errors.join('\n - ')}`);
|
|
28
|
-
return ok(value!);
|
|
29
|
-
} catch (e) {
|
|
30
|
-
const msg = e instanceof Error ? e.message : String(e);
|
|
31
|
-
return err(`Failed reading config ${p}: ${msg}`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return ok({});
|
|
35
|
-
}
|
|
1
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
2
|
+
import { validateConfig } from './config-schema.js';
|
|
3
|
+
import { ok, err, type Result } from './result.js';
|
|
4
|
+
import type { DcvConfig } from './types.js';
|
|
5
|
+
|
|
6
|
+
export function loadConfig(configPath?: string): Result<DcvConfig, string> {
|
|
7
|
+
const candidates = configPath ? [configPath] : [
|
|
8
|
+
'dcv.config.json',
|
|
9
|
+
'dcv.config.js',
|
|
10
|
+
'.dcvrc.json',
|
|
11
|
+
'package.json'
|
|
12
|
+
];
|
|
13
|
+
for (const p of candidates) {
|
|
14
|
+
if (!existsSync(p)) continue;
|
|
15
|
+
try {
|
|
16
|
+
const rawTxt = readFileSync(p, 'utf8');
|
|
17
|
+
let raw: unknown = JSON.parse(rawTxt);
|
|
18
|
+
if (p === 'package.json' && raw && typeof raw === 'object') {
|
|
19
|
+
const pkg = raw as Record<string, unknown>;
|
|
20
|
+
if ('dcv' in pkg) {
|
|
21
|
+
raw = pkg.dcv;
|
|
22
|
+
} else {
|
|
23
|
+
continue; // No dcv config in package.json
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const { value, errors } = validateConfig(raw);
|
|
27
|
+
if (errors) return err(`Config validation failed in ${p}:\n - ${errors.join('\n - ')}`);
|
|
28
|
+
return ok(value!);
|
|
29
|
+
} catch (e) {
|
|
30
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
31
|
+
return err(`Failed reading config ${p}: ${msg}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return ok({});
|
|
35
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized constraint discovery and loading.
|
|
3
|
+
*
|
|
4
|
+
* This module provides a single source of truth for determining which constraints
|
|
5
|
+
* are active for a given validation run. It replaces the scattered constraint-loading
|
|
6
|
+
* logic previously split across engine-helpers.ts and constraints-loader.ts.
|
|
7
|
+
*
|
|
8
|
+
* Design principles:
|
|
9
|
+
* - Constraints are discovered from config and filesystem in ONE place
|
|
10
|
+
* - Core modules receive in-memory data (no filesystem access)
|
|
11
|
+
* - All entry points (validate, set, graph) use this registry for consistency
|
|
12
|
+
*/
|
|
13
|
+
import type { Engine } from '../core/engine.js';
|
|
14
|
+
import type { Breakpoint } from '../core/breakpoints.js';
|
|
15
|
+
import type { DcvConfig } from './types.js';
|
|
16
|
+
export type OrderRule = [string, '<=' | '>=', string];
|
|
17
|
+
export type WcagRule = {
|
|
18
|
+
fg: string;
|
|
19
|
+
bg: string;
|
|
20
|
+
min: number;
|
|
21
|
+
where: string;
|
|
22
|
+
backdrop?: string;
|
|
23
|
+
};
|
|
24
|
+
export type ThresholdRule = {
|
|
25
|
+
id: string;
|
|
26
|
+
op: '<=' | '>=';
|
|
27
|
+
valuePx: number;
|
|
28
|
+
where?: string;
|
|
29
|
+
level?: 'error' | 'warn';
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Represents a constraint source discovered from config or filesystem.
|
|
33
|
+
*/
|
|
34
|
+
export type ConstraintSource = {
|
|
35
|
+
type: 'builtin-wcag';
|
|
36
|
+
enabled: boolean;
|
|
37
|
+
} | {
|
|
38
|
+
type: 'builtin-threshold';
|
|
39
|
+
enabled: boolean;
|
|
40
|
+
} | {
|
|
41
|
+
type: 'config-wcag';
|
|
42
|
+
rules: WcagRule[];
|
|
43
|
+
} | {
|
|
44
|
+
type: 'order-file';
|
|
45
|
+
axis: string;
|
|
46
|
+
orders: OrderRule[];
|
|
47
|
+
path: string;
|
|
48
|
+
} | {
|
|
49
|
+
type: 'lightness-file';
|
|
50
|
+
orders: OrderRule[];
|
|
51
|
+
path: string;
|
|
52
|
+
} | {
|
|
53
|
+
type: 'cross-axis-file';
|
|
54
|
+
path: string;
|
|
55
|
+
bp?: Breakpoint;
|
|
56
|
+
} | {
|
|
57
|
+
type: 'custom-threshold';
|
|
58
|
+
rules: ThresholdRule[];
|
|
59
|
+
};
|
|
60
|
+
export type DiscoveryOptions = {
|
|
61
|
+
config: DcvConfig;
|
|
62
|
+
basePath?: string;
|
|
63
|
+
bp?: Breakpoint;
|
|
64
|
+
constraintsDir?: string;
|
|
65
|
+
};
|
|
66
|
+
export type AttachOptions = {
|
|
67
|
+
knownIds: Set<string>;
|
|
68
|
+
crossAxisDebug?: boolean;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Discover all constraint sources for a given configuration and breakpoint.
|
|
72
|
+
*
|
|
73
|
+
* This function scans the filesystem and config to determine which constraints
|
|
74
|
+
* should be active, but does not load or attach them yet.
|
|
75
|
+
*
|
|
76
|
+
* @param opts Discovery options (config, basePath, breakpoint)
|
|
77
|
+
* @returns Array of constraint sources
|
|
78
|
+
*/
|
|
79
|
+
export declare function discoverConstraints(opts: DiscoveryOptions): ConstraintSource[];
|
|
80
|
+
/**
|
|
81
|
+
* Attach constraint plugins to an engine based on discovered sources.
|
|
82
|
+
*
|
|
83
|
+
* This function takes the output of `discoverConstraints()` and registers
|
|
84
|
+
* the appropriate plugins on the engine.
|
|
85
|
+
*
|
|
86
|
+
* @param engine Engine to attach plugins to
|
|
87
|
+
* @param sources Constraint sources (from discoverConstraints)
|
|
88
|
+
* @param opts Attachment options (knownIds, debug flags)
|
|
89
|
+
*/
|
|
90
|
+
export declare function attachConstraints(engine: Engine, sources: ConstraintSource[], opts: AttachOptions): void;
|
|
91
|
+
/**
|
|
92
|
+
* Discover and attach constraints in one call.
|
|
93
|
+
*
|
|
94
|
+
* This is the main entry point for most use cases.
|
|
95
|
+
*
|
|
96
|
+
* @param engine Engine to attach plugins to
|
|
97
|
+
* @param discoveryOpts Discovery options
|
|
98
|
+
* @param attachOpts Attachment options
|
|
99
|
+
*/
|
|
100
|
+
export declare function setupConstraints(engine: Engine, discoveryOpts: DiscoveryOptions, attachOpts: AttachOptions): ConstraintSource[];
|
|
101
|
+
//# sourceMappingURL=constraint-registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constraint-registry.d.ts","sourceRoot":"","sources":["constraint-registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAY5C,MAAM,MAAM,SAAS,GAAG,CAAC,MAAM,EAAE,IAAI,GAAG,IAAI,EAAE,MAAM,CAAC,CAAC;AAEtD,MAAM,MAAM,QAAQ,GAAG;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,IAAI,GAAG,IAAI,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;CAC1B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gBAAgB,GACxB;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,GAC1C;IAAE,IAAI,EAAE,mBAAmB,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,GAC/C;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,KAAK,EAAE,QAAQ,EAAE,CAAA;CAAE,GAC1C;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,SAAS,EAAE,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACvE;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,MAAM,EAAE,SAAS,EAAE,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC7D;IAAE,IAAI,EAAE,iBAAiB,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,CAAC,EAAE,UAAU,CAAA;CAAE,GAC1D;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,KAAK,EAAE,aAAa,EAAE,CAAA;CAAE,CAAC;AAEzD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,EAAE,SAAS,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,EAAE,CAAC,EAAE,UAAU,CAAC;IAChB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACtB,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAC;AAMF;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,gBAAgB,GAAG,gBAAgB,EAAE,CA8F9E;AAMD;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,EAAE,IAAI,EAAE,aAAa,GAAG,IAAI,CAsFxG;AAMD;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,EACd,aAAa,EAAE,gBAAgB,EAC/B,UAAU,EAAE,aAAa,GACxB,gBAAgB,EAAE,CAIpB"}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized constraint discovery and loading.
|
|
3
|
+
*
|
|
4
|
+
* This module provides a single source of truth for determining which constraints
|
|
5
|
+
* are active for a given validation run. It replaces the scattered constraint-loading
|
|
6
|
+
* logic previously split across engine-helpers.ts and constraints-loader.ts.
|
|
7
|
+
*
|
|
8
|
+
* Design principles:
|
|
9
|
+
* - Constraints are discovered from config and filesystem in ONE place
|
|
10
|
+
* - Core modules receive in-memory data (no filesystem access)
|
|
11
|
+
* - All entry points (validate, set, graph) use this registry for consistency
|
|
12
|
+
*/
|
|
13
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import { MonotonicPlugin, parseSize as parseSizePx } from '../core/constraints/monotonic.js';
|
|
16
|
+
import { MonotonicLightness } from '../core/constraints/monotonic-lightness.js';
|
|
17
|
+
import { WcagContrastPlugin } from '../core/constraints/wcag.js';
|
|
18
|
+
import { ThresholdPlugin } from '../core/constraints/threshold.js';
|
|
19
|
+
import { CrossAxisPlugin } from '../core/constraints/cross-axis.js';
|
|
20
|
+
import { loadCrossAxisRules } from './cross-axis-loader.js';
|
|
21
|
+
// ============================================================================
|
|
22
|
+
// Discovery
|
|
23
|
+
// ============================================================================
|
|
24
|
+
/**
|
|
25
|
+
* Discover all constraint sources for a given configuration and breakpoint.
|
|
26
|
+
*
|
|
27
|
+
* This function scans the filesystem and config to determine which constraints
|
|
28
|
+
* should be active, but does not load or attach them yet.
|
|
29
|
+
*
|
|
30
|
+
* @param opts Discovery options (config, basePath, breakpoint)
|
|
31
|
+
* @returns Array of constraint sources
|
|
32
|
+
*/
|
|
33
|
+
export function discoverConstraints(opts) {
|
|
34
|
+
const { config, basePath = '.', bp, constraintsDir = 'themes' } = opts;
|
|
35
|
+
const sources = [];
|
|
36
|
+
const constraintsCfg = config.constraints ?? {};
|
|
37
|
+
// 1. Built-in WCAG defaults
|
|
38
|
+
const enableBuiltInWcag = constraintsCfg.enableBuiltInWcagDefaults === undefined ? true : !!constraintsCfg.enableBuiltInWcagDefaults;
|
|
39
|
+
if (enableBuiltInWcag) {
|
|
40
|
+
sources.push({ type: 'builtin-wcag', enabled: true });
|
|
41
|
+
}
|
|
42
|
+
// 2. Built-in threshold (44px touch target)
|
|
43
|
+
const enableBuiltInThreshold = constraintsCfg.enableBuiltInThreshold === undefined ? true : !!constraintsCfg.enableBuiltInThreshold;
|
|
44
|
+
if (enableBuiltInThreshold) {
|
|
45
|
+
sources.push({ type: 'builtin-threshold', enabled: true });
|
|
46
|
+
}
|
|
47
|
+
// 3. Config-defined WCAG rules
|
|
48
|
+
if (constraintsCfg.wcag && Array.isArray(constraintsCfg.wcag) && constraintsCfg.wcag.length > 0) {
|
|
49
|
+
const rules = constraintsCfg.wcag.map((r) => ({
|
|
50
|
+
fg: r.foreground,
|
|
51
|
+
bg: r.background,
|
|
52
|
+
min: r.ratio || 4.5,
|
|
53
|
+
where: r.description || 'Unknown',
|
|
54
|
+
backdrop: r.backdrop,
|
|
55
|
+
}));
|
|
56
|
+
sources.push({ type: 'config-wcag', rules });
|
|
57
|
+
}
|
|
58
|
+
// 4. Config-defined threshold rules
|
|
59
|
+
if (constraintsCfg.thresholds && Array.isArray(constraintsCfg.thresholds) && constraintsCfg.thresholds.length > 0) {
|
|
60
|
+
const rules = constraintsCfg.thresholds.map((r) => ({
|
|
61
|
+
id: r.id,
|
|
62
|
+
op: r.op,
|
|
63
|
+
valuePx: r.valuePx,
|
|
64
|
+
where: r.where,
|
|
65
|
+
}));
|
|
66
|
+
sources.push({ type: 'custom-threshold', rules });
|
|
67
|
+
}
|
|
68
|
+
// 5. Order files (monotonic constraints)
|
|
69
|
+
const axes = ['typography', 'spacing', 'layout'];
|
|
70
|
+
for (const axis of axes) {
|
|
71
|
+
const suffix = bp ? `.${bp}` : '';
|
|
72
|
+
const orderPath = join(basePath, constraintsDir, `${axis}${suffix}.order.json`);
|
|
73
|
+
if (existsSync(orderPath)) {
|
|
74
|
+
try {
|
|
75
|
+
const data = JSON.parse(readFileSync(orderPath, 'utf8'));
|
|
76
|
+
const orders = data.order || [];
|
|
77
|
+
if (orders.length > 0) {
|
|
78
|
+
sources.push({ type: 'order-file', axis, orders, path: orderPath });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// Silently skip malformed order files (consistent with old behavior)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// 6. Color lightness order files
|
|
87
|
+
const suffix = bp ? `.${bp}` : '';
|
|
88
|
+
const colorOrderPath = join(basePath, constraintsDir, `color${suffix}.order.json`);
|
|
89
|
+
if (existsSync(colorOrderPath)) {
|
|
90
|
+
try {
|
|
91
|
+
const data = JSON.parse(readFileSync(colorOrderPath, 'utf8'));
|
|
92
|
+
const orders = data.order || [];
|
|
93
|
+
if (orders.length > 0) {
|
|
94
|
+
sources.push({ type: 'lightness-file', orders, path: colorOrderPath });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
// Silently skip malformed color order files
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// 7. Cross-axis rules (global)
|
|
102
|
+
const crossAxisPath = join(basePath, constraintsDir, 'cross-axis.rules.json');
|
|
103
|
+
if (existsSync(crossAxisPath)) {
|
|
104
|
+
sources.push({ type: 'cross-axis-file', path: crossAxisPath });
|
|
105
|
+
}
|
|
106
|
+
// 8. Cross-axis rules (breakpoint-specific)
|
|
107
|
+
if (bp) {
|
|
108
|
+
const crossAxisBpPath = join(basePath, constraintsDir, `cross-axis.${bp}.rules.json`);
|
|
109
|
+
if (existsSync(crossAxisBpPath)) {
|
|
110
|
+
sources.push({ type: 'cross-axis-file', path: crossAxisBpPath, bp });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return sources;
|
|
114
|
+
}
|
|
115
|
+
// ============================================================================
|
|
116
|
+
// Attachment
|
|
117
|
+
// ============================================================================
|
|
118
|
+
/**
|
|
119
|
+
* Attach constraint plugins to an engine based on discovered sources.
|
|
120
|
+
*
|
|
121
|
+
* This function takes the output of `discoverConstraints()` and registers
|
|
122
|
+
* the appropriate plugins on the engine.
|
|
123
|
+
*
|
|
124
|
+
* @param engine Engine to attach plugins to
|
|
125
|
+
* @param sources Constraint sources (from discoverConstraints)
|
|
126
|
+
* @param opts Attachment options (knownIds, debug flags)
|
|
127
|
+
*/
|
|
128
|
+
export function attachConstraints(engine, sources, opts) {
|
|
129
|
+
const { knownIds, crossAxisDebug = false } = opts;
|
|
130
|
+
for (const source of sources) {
|
|
131
|
+
try {
|
|
132
|
+
switch (source.type) {
|
|
133
|
+
case 'builtin-wcag': {
|
|
134
|
+
if (source.enabled) {
|
|
135
|
+
const defaultWcagPairs = [
|
|
136
|
+
{
|
|
137
|
+
fg: 'color.role.text.default',
|
|
138
|
+
bg: 'color.role.bg.surface',
|
|
139
|
+
min: 4.5,
|
|
140
|
+
where: 'Body text on surface',
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
fg: 'color.role.accent.default',
|
|
144
|
+
bg: 'color.role.bg.surface',
|
|
145
|
+
min: 3.0,
|
|
146
|
+
where: 'Accent on surface',
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
fg: 'color.role.focus.ring',
|
|
150
|
+
bg: 'color.role.bg.surface',
|
|
151
|
+
min: 3.0,
|
|
152
|
+
where: 'Focus ring on surface',
|
|
153
|
+
backdrop: '#ffffff',
|
|
154
|
+
},
|
|
155
|
+
];
|
|
156
|
+
engine.use(WcagContrastPlugin(defaultWcagPairs));
|
|
157
|
+
}
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
case 'builtin-threshold': {
|
|
161
|
+
if (source.enabled) {
|
|
162
|
+
const defaultThresholds = [
|
|
163
|
+
{
|
|
164
|
+
id: 'control.size.min',
|
|
165
|
+
op: '>=',
|
|
166
|
+
valuePx: 44,
|
|
167
|
+
where: 'Touch target (WCAG / Apple HIG)',
|
|
168
|
+
},
|
|
169
|
+
];
|
|
170
|
+
engine.use(ThresholdPlugin(defaultThresholds, 'threshold'));
|
|
171
|
+
}
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
case 'config-wcag': {
|
|
175
|
+
engine.use(WcagContrastPlugin(source.rules));
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
case 'custom-threshold': {
|
|
179
|
+
engine.use(ThresholdPlugin(source.rules, 'custom-threshold'));
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
case 'order-file': {
|
|
183
|
+
const pluginId = `monotonic-${source.axis}`;
|
|
184
|
+
engine.use(MonotonicPlugin(source.orders, parseSizePx, pluginId));
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
case 'lightness-file': {
|
|
188
|
+
engine.use(MonotonicLightness(source.orders));
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
case 'cross-axis-file': {
|
|
192
|
+
// Phase 3B: Load rules from filesystem in CLI layer, pass to core plugin
|
|
193
|
+
const rules = loadCrossAxisRules(source.path, {
|
|
194
|
+
bp: source.bp,
|
|
195
|
+
knownIds,
|
|
196
|
+
debug: crossAxisDebug,
|
|
197
|
+
});
|
|
198
|
+
engine.use(CrossAxisPlugin(rules, source.bp));
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
// Silently skip failed constraint attachments (consistent with old behavior)
|
|
205
|
+
// In the future, we may want to surface these as warnings
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// ============================================================================
|
|
210
|
+
// Convenience
|
|
211
|
+
// ============================================================================
|
|
212
|
+
/**
|
|
213
|
+
* Discover and attach constraints in one call.
|
|
214
|
+
*
|
|
215
|
+
* This is the main entry point for most use cases.
|
|
216
|
+
*
|
|
217
|
+
* @param engine Engine to attach plugins to
|
|
218
|
+
* @param discoveryOpts Discovery options
|
|
219
|
+
* @param attachOpts Attachment options
|
|
220
|
+
*/
|
|
221
|
+
export function setupConstraints(engine, discoveryOpts, attachOpts) {
|
|
222
|
+
const sources = discoverConstraints(discoveryOpts);
|
|
223
|
+
attachConstraints(engine, sources, attachOpts);
|
|
224
|
+
return sources;
|
|
225
|
+
}
|