@lostgradient/cinder 0.27.1 → 0.28.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/CHANGELOG.md +35 -0
- package/components.json +1 -1
- package/dist/_internal/code-highlighter-context.d.ts +28 -0
- package/dist/cli/index.js +12 -8
- package/dist/components/approval-card/index.js +4 -4
- package/dist/components/button/button.css +25 -0
- package/dist/components/calendar/calendar.css +14 -0
- package/dist/components/capability-gate/capability-gate.css +5 -0
- package/dist/components/checkbox/checkbox.css +19 -0
- package/dist/components/chip/chip.css +14 -0
- package/dist/components/code-block/code-block.d.svelte.ts +1 -1
- package/dist/components/code-block/code-block.examples.json +1 -1
- package/dist/components/code-block/code-block.schema.js +3 -3
- package/dist/components/code-block/code-block.svelte.d.ts +1 -1
- package/dist/components/code-block/code-block.types.d.ts +14 -12
- package/dist/components/code-block/index.js +2 -2
- package/dist/components/color-picker/color-picker.css +2 -0
- package/dist/components/combobox/combobox.css +9 -1
- package/dist/components/command-palette/command-palette.css +7 -5
- package/dist/components/file-upload/file-upload.css +2 -1
- package/dist/components/guidance-region/index.css +1 -1
- package/dist/components/image/image.examples.json +1 -1
- package/dist/components/input/input.css +20 -6
- package/dist/components/json-schema-editor/index.js +5 -5
- package/dist/components/json-schema-editor/json-view-highlighter.d.ts +14 -0
- package/dist/components/key-value-editor/index.css +1 -1
- package/dist/components/modal-region/index.css +1 -1
- package/dist/components/multi-select/multi-select.css +9 -0
- package/dist/components/pagination/pagination.css +12 -0
- package/dist/components/pin-input/pin-input.css +9 -1
- package/dist/components/radio-group/radio-group.css +16 -0
- package/dist/components/run-step-timeline/index.js +2 -2
- package/dist/components/schema-form/index.js +2 -2
- package/dist/components/schema-form/schema-form.css +9 -0
- package/dist/components/segmented-control/segmented-control.css +3 -3
- package/dist/components/selection-popover/selection-popover.css +7 -1
- package/dist/components/tag-input/tag-input.css +10 -1
- package/dist/components/terminal-frame/index.js +2 -2
- package/dist/components/terminal-frame/terminal-frame.schema.js +2 -2
- package/dist/components/terminal-frame/terminal-frame.types.d.ts +8 -0
- package/dist/components/textarea/textarea.css +1 -1
- package/dist/components/toggle/toggle.css +16 -0
- package/dist/components/tree/tree.examples.json +1 -1
- package/dist/components/virtual-list/index.js +2 -2
- package/dist/components/virtual-list/virtual-list.css +42 -9
- package/dist/highlighters/shiki/default.d.ts +13 -1
- package/dist/highlighters/shiki/default.js +2 -2
- package/dist/highlighters/shiki/index.d.ts +20 -12
- package/dist/highlighters/shiki/index.js +2 -2
- package/dist/index.browser.css +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +23 -23
- package/dist/scripts/knowledge/index.d.ts +5 -1
- package/dist/server/components/approval-card/index.js +1 -1
- package/dist/server/components/code-block/index.js +1 -1
- package/dist/server/components/color-field/index.js +2 -2
- package/dist/server/components/color-picker/index.js +2 -2
- package/dist/server/components/json-schema-editor/index.js +2 -2
- package/dist/server/components/run-step-timeline/index.js +1 -1
- package/dist/server/components/terminal-frame/index.js +1 -1
- package/dist/server/components/virtual-list/index.js +1 -1
- package/dist/server/github-dark-e7nywpbe.js +3 -0
- package/dist/server/github-light-bqtmm1zq.js +3 -0
- package/dist/server/highlighters/shiki/default.js +2 -2
- package/dist/server/highlighters/shiki/index.js +2 -2
- package/dist/server/index.js +6 -6
- package/dist/server/{index.server-4qyyw4y5.js → index.server-043cfznf.js} +1 -1
- package/dist/server/{index.server-p46tn4eb.js → index.server-09tjs48p.js} +2 -2
- package/dist/server/index.server-0hyvdf74.js +8 -0
- package/dist/server/{index.server-dxgkh9tk.js → index.server-17thvpd3.js} +1 -1
- package/dist/server/index.server-19z6hqd6.js +4 -0
- package/dist/server/index.server-31020qgk.js +3 -0
- package/dist/server/{index.server-w3nqzde5.js → index.server-45wmvjrn.js} +3 -3
- package/dist/server/{index.server-3tkgm2gn.js → index.server-6mb7w5g4.js} +2 -2
- package/dist/server/{index.server-2vxmn7x1.js → index.server-c5yajkkc.js} +4 -4
- package/dist/server/index.server-e6fknxd9.js +4 -0
- package/dist/server/index.server-fev4kwm0.js +4 -0
- package/dist/server/index.server-k4vjk75r.js +4 -0
- package/dist/server/index.server-mjbtd1gs.js +7 -0
- package/dist/server/{index.server-2bfkhzzq.js → index.server-rkp4py5n.js} +2 -2
- package/dist/server/index.server-wdz9tsq4.js +3 -0
- package/dist/server/index.server-zj9909hd.js +4 -0
- package/dist/server/json-68twtta3.js +3 -0
- package/dist/server/tokens/registry.generated.js +1 -1
- package/dist/server/tokens/runtime/index.js +92 -0
- package/dist/styles/check-token-contrast-test-values.d.ts +1 -3
- package/dist/styles/token-contrast-test-readers.d.ts +1 -1
- package/dist/tokens/registry.generated.js +2 -2
- package/dist/tokens/resolved/dark-reduced-motion.json +104 -43
- package/dist/tokens/resolved/dark.json +104 -43
- package/dist/tokens/resolved/light-reduced-motion.json +104 -43
- package/dist/tokens/resolved/light.json +104 -43
- package/dist/tokens/runtime/authoring-catalogue.d.ts +19 -0
- package/dist/tokens/runtime/authoring-catalogue.generated.d.ts +8 -0
- package/dist/tokens/runtime/authoring-palette.d.ts +25 -0
- package/dist/tokens/runtime/authoring-profiles.d.ts +48 -0
- package/dist/tokens/runtime/authoring-provenance.d.ts +31 -0
- package/dist/tokens/runtime/authoring-types.d.ts +182 -0
- package/dist/tokens/runtime/base-corpus.d.ts +29 -0
- package/dist/tokens/runtime/base-corpus.generated.d.ts +8 -0
- package/dist/tokens/runtime/context-order.d.ts +60 -0
- package/dist/tokens/runtime/context-stack.d.ts +11 -0
- package/dist/tokens/runtime/contrast/color-difference.d.ts +16 -0
- package/dist/{styles/color-contrast-utilities.d.ts → tokens/runtime/contrast/color-math.d.ts} +2 -17
- package/dist/tokens/runtime/contrast/color-mix.d.ts +3 -0
- package/dist/tokens/runtime/contrast/css-color.d.ts +29 -0
- package/dist/tokens/runtime/contrast/index.d.ts +7 -0
- package/dist/tokens/runtime/contrast/measure.d.ts +45 -0
- package/dist/tokens/runtime/contrast/pairings.d.ts +36 -0
- package/dist/tokens/runtime/contrast/thresholds.d.ts +47 -0
- package/dist/tokens/runtime/corpus-documents.d.ts +36 -0
- package/dist/tokens/runtime/corpus-validation.d.ts +10 -0
- package/dist/tokens/runtime/corpus.d.ts +35 -0
- package/dist/tokens/runtime/css-configuration.d.ts +1 -0
- package/dist/tokens/runtime/css-stylelint.d.ts +2 -0
- package/dist/tokens/runtime/css-support.d.ts +110 -0
- package/dist/tokens/runtime/evaluate.d.ts +95 -0
- package/dist/tokens/runtime/evaluated-provenance.d.ts +36 -0
- package/dist/tokens/runtime/index.d.ts +38 -0
- package/dist/tokens/runtime/index.js +95 -0
- package/dist/tokens/runtime/package-version.generated.d.ts +9 -0
- package/dist/tokens/runtime/projection.d.ts +32 -0
- package/dist/tokens/runtime/registry.d.ts +125 -0
- package/dist/tokens/runtime/resolve-extends.d.ts +30 -0
- package/dist/tokens/runtime/resolve-references.d.ts +49 -0
- package/dist/tokens/runtime/resolve-shared.d.ts +34 -0
- package/dist/tokens/runtime/resolve.d.ts +44 -0
- package/dist/tokens/runtime/resolver-order.d.ts +89 -0
- package/dist/tokens/runtime/source-digest.d.ts +20 -0
- package/dist/tokens/runtime/stylesheet-skeleton.d.ts +31 -0
- package/dist/tokens/runtime/theme-runtime.test-support.d.ts +24 -0
- package/dist/tokens/runtime/theme-sources.d.ts +46 -0
- package/dist/tokens/runtime/theme-stylesheet.d.ts +31 -0
- package/dist/tokens/runtime/types.d.ts +115 -0
- package/dist/tokens/runtime/validate-resolver.d.ts +31 -0
- package/dist/tokens/runtime/validate-schema.d.ts +22 -0
- package/dist/tokens/runtime/validate-tokens.d.ts +25 -0
- package/dist/tokens/runtime/validate-values.d.ts +33 -0
- package/dist/tokens/runtime/validate.d.ts +3 -0
- package/dist/tokens/runtime/value-color-parse.d.ts +128 -0
- package/dist/tokens/runtime/value-color.d.ts +85 -0
- package/dist/tokens/runtime/value-entry.d.ts +3 -0
- package/dist/tokens/runtime/value-formatters.d.ts +59 -0
- package/dist/tokens/sets/colors.tokens.json +65 -13
- package/dist/tokens/sets/foundation.tokens.json +23 -0
- package/dist/tokens/sets/semantic.tokens.json +136 -30
- package/dist/utilities/highlighter.d.ts +5 -5
- package/dist/utilities/retrying-loader-cache.d.ts +3 -6
- package/package.json +15 -3
- package/src/_internal/code-highlighter-context.ts +41 -0
- package/src/components/button/button.css +25 -0
- package/src/components/calendar/calendar.css +14 -0
- package/src/components/capability-gate/capability-gate.css +5 -0
- package/src/components/checkbox/checkbox.css +19 -0
- package/src/components/chip/chip.css +14 -0
- package/src/components/code-block/code-block.examples.json +1 -1
- package/src/components/code-block/code-block.schema.json +2 -2
- package/src/components/code-block/code-block.schema.ts +2 -2
- package/src/components/code-block/code-block.svelte +15 -19
- package/src/components/code-block/code-block.types.ts +14 -12
- package/src/components/color-picker/color-picker.css +2 -0
- package/src/components/combobox/combobox.css +9 -1
- package/src/components/command-palette/command-palette.css +7 -5
- package/src/components/file-upload/file-upload.css +2 -1
- package/src/components/image/image.examples.json +1 -1
- package/src/components/input/input.css +20 -6
- package/src/components/json-schema-editor/json-view-highlighter.ts +23 -0
- package/src/components/json-schema-editor/json-view.svelte +2 -0
- package/src/components/multi-select/multi-select.css +9 -0
- package/src/components/pagination/pagination.css +12 -0
- package/src/components/pin-input/pin-input.css +9 -1
- package/src/components/radio-group/radio-group.css +16 -0
- package/src/components/schema-form/schema-form.css +9 -0
- package/src/components/segmented-control/segmented-control.css +3 -3
- package/src/components/selection-popover/selection-popover.css +7 -1
- package/src/components/tag-input/tag-input.css +10 -1
- package/src/components/terminal-frame/terminal-frame.schema.json +5 -0
- package/src/components/terminal-frame/terminal-frame.schema.ts +6 -0
- package/src/components/terminal-frame/terminal-frame.svelte +6 -2
- package/src/components/terminal-frame/terminal-frame.types.ts +8 -0
- package/src/components/textarea/textarea.css +1 -1
- package/src/components/toggle/toggle.css +16 -0
- package/src/components/tree/tree.examples.json +1 -1
- package/src/components/virtual-list/virtual-list.css +42 -9
- package/src/components/virtual-list/virtual-list.svelte +54 -20
- package/src/highlighters/shiki/default.ts +13 -1
- package/src/highlighters/shiki/index.ts +20 -12
- package/src/index.ts +5 -0
- package/src/styles/foundation.css +33 -1
- package/src/styles/tokens-base.css +4 -4
- package/src/tokens/registry.generated.json +3 -3
- package/src/tokens/resolved/dark-reduced-motion.json +104 -43
- package/src/tokens/resolved/dark.json +104 -43
- package/src/tokens/resolved/light-reduced-motion.json +104 -43
- package/src/tokens/resolved/light.json +104 -43
- package/src/tokens/runtime/authoring-catalogue.generated.ts +19300 -0
- package/src/tokens/runtime/authoring-catalogue.ts +256 -0
- package/src/tokens/runtime/authoring-palette.ts +100 -0
- package/src/tokens/runtime/authoring-profiles.ts +288 -0
- package/src/tokens/runtime/authoring-provenance.ts +290 -0
- package/src/tokens/runtime/authoring-types.ts +199 -0
- package/src/tokens/runtime/base-corpus.generated.ts +10 -0
- package/src/tokens/runtime/base-corpus.ts +78 -0
- package/src/tokens/runtime/context-order.ts +182 -0
- package/src/tokens/runtime/context-stack.ts +52 -0
- package/src/tokens/runtime/contrast/color-difference.ts +114 -0
- package/src/tokens/runtime/contrast/color-math.ts +89 -0
- package/src/tokens/runtime/contrast/color-mix.ts +20 -0
- package/src/tokens/runtime/contrast/css-color.ts +312 -0
- package/src/tokens/runtime/contrast/index.ts +7 -0
- package/src/tokens/runtime/contrast/measure.ts +185 -0
- package/src/tokens/runtime/contrast/pairings.ts +324 -0
- package/src/tokens/runtime/contrast/thresholds.ts +81 -0
- package/src/tokens/runtime/corpus-documents.ts +289 -0
- package/src/tokens/runtime/corpus-validation.ts +336 -0
- package/src/tokens/runtime/corpus.ts +179 -0
- package/src/tokens/runtime/css-configuration.ts +1 -0
- package/src/tokens/runtime/css-stylelint.ts +15 -0
- package/src/tokens/runtime/css-support.ts +407 -0
- package/src/tokens/runtime/evaluate.ts +262 -0
- package/src/tokens/runtime/evaluated-provenance.ts +116 -0
- package/src/tokens/runtime/format-validator.generated.d.ts +18 -0
- package/src/tokens/runtime/format-validator.generated.js +13029 -0
- package/src/tokens/runtime/index.ts +78 -0
- package/src/tokens/runtime/package-version.generated.ts +9 -0
- package/src/tokens/runtime/projection.ts +65 -0
- package/src/tokens/runtime/registry.ts +365 -0
- package/src/tokens/runtime/resolve-extends.ts +211 -0
- package/src/tokens/runtime/resolve-references.ts +313 -0
- package/src/tokens/runtime/resolve-shared.ts +186 -0
- package/src/tokens/runtime/resolve.ts +246 -0
- package/src/tokens/runtime/resolver-order.ts +326 -0
- package/src/tokens/runtime/resolver-validator.generated.d.ts +18 -0
- package/src/tokens/runtime/resolver-validator.generated.js +15492 -0
- package/src/tokens/runtime/source-digest.ts +49 -0
- package/src/tokens/runtime/stylesheet-skeleton.ts +123 -0
- package/src/tokens/runtime/theme-sources.ts +121 -0
- package/src/tokens/runtime/theme-stylesheet.ts +177 -0
- package/src/tokens/runtime/types.ts +140 -0
- package/src/tokens/runtime/validate-resolver.ts +338 -0
- package/src/tokens/runtime/validate-schema.ts +269 -0
- package/src/tokens/runtime/validate-tokens.ts +181 -0
- package/src/tokens/runtime/validate-values.ts +396 -0
- package/src/tokens/runtime/validate.ts +57 -0
- package/src/tokens/runtime/value-color-parse.ts +309 -0
- package/src/tokens/runtime/value-color.ts +217 -0
- package/src/tokens/runtime/value-entry.ts +52 -0
- package/src/tokens/runtime/value-formatters.ts +414 -0
- package/src/tokens/sets/colors.tokens.json +65 -13
- package/src/tokens/sets/foundation.tokens.json +23 -0
- package/src/tokens/sets/semantic.tokens.json +136 -30
- package/src/utilities/highlighter.ts +5 -5
- package/src/utilities/retrying-loader-cache.ts +3 -6
- package/dist/components/code-block/code-block-default-highlighter.d.ts +0 -40
- package/dist/server/index.server-298z43yn.js +0 -4
- package/dist/server/index.server-3h11wjyh.js +0 -8
- package/dist/server/index.server-a3aerbfr.js +0 -4
- package/dist/server/index.server-kge3zp6n.js +0 -4
- package/dist/server/index.server-syf81nwz.js +0 -6
- package/dist/server/index.server-zs08f5te.js +0 -3
- package/src/components/code-block/code-block-default-highlighter.ts +0 -63
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import type { DesignToken, TokenGroup } from './types.ts';
|
|
2
|
+
import { TokenValidationError } from './types.ts';
|
|
3
|
+
|
|
4
|
+
export type JsonObject = Record<string, unknown>;
|
|
5
|
+
export type ResolvedTokens = Map<string, DesignToken>;
|
|
6
|
+
// Origins follow group objects through the same merges and clones as values.
|
|
7
|
+
// A path-only map would confuse an earlier prefix with later lookup overrides.
|
|
8
|
+
export type ExtensionOrigin = { source: string; path: string };
|
|
9
|
+
export type ExtensionOrigins = WeakMap<object, ExtensionOrigin>;
|
|
10
|
+
/**
|
|
11
|
+
* Every token's ORIGINAL `$ref` string, captured once in `buildTokenIndex`
|
|
12
|
+
* before any resolution runs. `resolveRefToken` deletes `$ref` from a
|
|
13
|
+
* token's live entry in `ResolvedTokens` once resolved (so a resolved token
|
|
14
|
+
* never carries a leftover alias pointer) -- a pointer that targets another
|
|
15
|
+
* alias token's OWN `$ref` property (`#/alias/$ref`) would otherwise always
|
|
16
|
+
* find it already gone, regardless of processing order, since
|
|
17
|
+
* `resolveDocuments`'s top-level loop may resolve `alias` before anything
|
|
18
|
+
* ever asks to read its `$ref`. This snapshot is immune to that mutation.
|
|
19
|
+
*/
|
|
20
|
+
export type RawRefs = Map<string, string>;
|
|
21
|
+
|
|
22
|
+
export function isObject(value: unknown): value is JsonObject {
|
|
23
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function isToken(value: unknown): value is DesignToken {
|
|
27
|
+
// `$ref` is a DTCG 2025.10 whole-token alias -- mutually exclusive with
|
|
28
|
+
// `$value`, so a node declaring either is token-shaped. Recognising only
|
|
29
|
+
// `$value` here was the CIN-463 "live trap": `collectTokens` below falls
|
|
30
|
+
// through to `isTokenGroup` for anything `isToken` rejects, so a `$ref`
|
|
31
|
+
// token was walked as an empty group and silently vanished from the
|
|
32
|
+
// resolved output instead of resolving or raising a named error.
|
|
33
|
+
return isObject(value) && ('$value' in value || '$ref' in value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function isTokenGroup(value: unknown): value is TokenGroup {
|
|
37
|
+
return isObject(value) && !isToken(value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function clone<T>(value: T, extensionOrigins?: ExtensionOrigins): T {
|
|
41
|
+
const copy = structuredClone(value);
|
|
42
|
+
normalizeObjectPrototypes(copy);
|
|
43
|
+
if (extensionOrigins) copyExtensionOrigins(value, copy, extensionOrigins);
|
|
44
|
+
return copy;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function copyExtensionOrigins(
|
|
48
|
+
value: unknown,
|
|
49
|
+
copy: unknown,
|
|
50
|
+
origins: ExtensionOrigins,
|
|
51
|
+
): void {
|
|
52
|
+
if (!isObject(value) || !isObject(copy)) return;
|
|
53
|
+
const origin = origins.get(value);
|
|
54
|
+
if (origin) origins.set(copy, origin);
|
|
55
|
+
for (const [key, child] of Object.entries(value)) copyExtensionOrigins(child, copy[key], origins);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function normalizeObjectPrototypes(value: unknown): void {
|
|
59
|
+
if (Array.isArray(value)) {
|
|
60
|
+
for (const entry of value) normalizeObjectPrototypes(entry);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (!isObject(value)) return;
|
|
64
|
+
Object.setPrototypeOf(value, null);
|
|
65
|
+
for (const entry of Object.values(value)) normalizeObjectPrototypes(entry);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function withResolvedType(
|
|
69
|
+
token: DesignToken,
|
|
70
|
+
inheritedType?: DesignToken['$type'],
|
|
71
|
+
): DesignToken {
|
|
72
|
+
const resolved = clone(token);
|
|
73
|
+
const type = resolved.$type ?? inheritedType;
|
|
74
|
+
if (type) resolved.$type = type;
|
|
75
|
+
return resolved;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function issue(path: string, reason: string): never {
|
|
79
|
+
throw new TokenValidationError([{ path, reason }]);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function tokenPathFromReference(reference: string): string {
|
|
83
|
+
if (/^\{[^{}]+\}$/.test(reference)) return reference.slice(1, -1);
|
|
84
|
+
if (reference === '#/') return issue(reference, 'reference target does not exist');
|
|
85
|
+
if (reference.startsWith('#/')) return pointerSegments(reference).join('.');
|
|
86
|
+
return issue(reference, 'reference must use curly-brace or JSON Pointer syntax');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function pointerSegments(reference: string): string[] {
|
|
90
|
+
if (!reference.startsWith('#/')) return [];
|
|
91
|
+
let fragment: string;
|
|
92
|
+
try {
|
|
93
|
+
fragment = decodeURIComponent(reference.slice(2));
|
|
94
|
+
} catch {
|
|
95
|
+
return issue(reference, 'JSON Pointer contains invalid percent encoding');
|
|
96
|
+
}
|
|
97
|
+
return fragment.split('/').map((segment) => {
|
|
98
|
+
if (/~(?:[^01]|$)/.test(segment))
|
|
99
|
+
issue(reference, 'JSON Pointer contains invalid tilde escape');
|
|
100
|
+
return segment.replaceAll('~1', '/').replaceAll('~0', '~');
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function getByPath(value: unknown, segments: string[]): unknown {
|
|
105
|
+
let current = value;
|
|
106
|
+
for (const segment of segments) {
|
|
107
|
+
if (Array.isArray(current)) {
|
|
108
|
+
if (!/^(0|[1-9][0-9]*)$/.test(segment)) return undefined;
|
|
109
|
+
const index = Number(segment);
|
|
110
|
+
if (!Number.isInteger(index)) return undefined;
|
|
111
|
+
current = current[index];
|
|
112
|
+
} else if (isObject(current)) current = current[segment];
|
|
113
|
+
else return undefined;
|
|
114
|
+
}
|
|
115
|
+
return current;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function collectGroups(
|
|
119
|
+
group: TokenGroup,
|
|
120
|
+
prefix: string,
|
|
121
|
+
groups: Map<string, TokenGroup>,
|
|
122
|
+
): void {
|
|
123
|
+
groups.set(prefix, group);
|
|
124
|
+
for (const [name, value] of Object.entries(group)) {
|
|
125
|
+
if (name.startsWith('$') || !isObject(value)) continue;
|
|
126
|
+
const path = prefix ? `${prefix}.${name}` : name;
|
|
127
|
+
if (isTokenGroup(value)) collectGroups(value, path, groups);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function collectExtensionOrigins(
|
|
132
|
+
group: TokenGroup,
|
|
133
|
+
prefix: string,
|
|
134
|
+
source: string | undefined,
|
|
135
|
+
origins: ExtensionOrigins,
|
|
136
|
+
): void {
|
|
137
|
+
if (source !== undefined && typeof group.$extends === 'string')
|
|
138
|
+
origins.set(group, { source, path: prefix });
|
|
139
|
+
for (const [name, value] of Object.entries(group)) {
|
|
140
|
+
if (name.startsWith('$') || !isObject(value) || !isTokenGroup(value)) continue;
|
|
141
|
+
collectExtensionOrigins(value, prefix ? `${prefix}.${name}` : name, source, origins);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function extensionIssue(
|
|
146
|
+
groupPath: string,
|
|
147
|
+
reason: string,
|
|
148
|
+
group: TokenGroup,
|
|
149
|
+
extensionOrigins?: ExtensionOrigins,
|
|
150
|
+
): never {
|
|
151
|
+
const origin = extensionOrigins?.get(group);
|
|
152
|
+
throw new TokenValidationError(
|
|
153
|
+
[
|
|
154
|
+
{
|
|
155
|
+
path: origin
|
|
156
|
+
? `${origin.source}${origin.path ? `.${origin.path}` : ''}.$extends`
|
|
157
|
+
: groupPath,
|
|
158
|
+
reason,
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
origin !== undefined,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function collectTokens(
|
|
166
|
+
group: TokenGroup,
|
|
167
|
+
prefix: string,
|
|
168
|
+
tokens: ResolvedTokens,
|
|
169
|
+
rootTokenPaths: Set<string>,
|
|
170
|
+
inheritedType?: DesignToken['$type'],
|
|
171
|
+
): void {
|
|
172
|
+
if (group.$root) {
|
|
173
|
+
tokens.set(prefix, withResolvedType(group.$root, group.$type ?? inheritedType));
|
|
174
|
+
// Marks `prefix` as a GROUP's redirected root token, not an ordinary
|
|
175
|
+
// token that merely happens to be indexed at the same path -- see
|
|
176
|
+
// `resolveReference`'s use of this set for why the distinction matters.
|
|
177
|
+
rootTokenPaths.add(prefix);
|
|
178
|
+
}
|
|
179
|
+
for (const [name, value] of Object.entries(group)) {
|
|
180
|
+
if (name.startsWith('$') || !isObject(value)) continue;
|
|
181
|
+
const path = prefix ? `${prefix}.${name}` : name;
|
|
182
|
+
if (isToken(value)) tokens.set(path, withResolvedType(value, group.$type ?? inheritedType));
|
|
183
|
+
else if (isTokenGroup(value))
|
|
184
|
+
collectTokens(value, path, tokens, rootTokenPaths, group.$type ?? inheritedType);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { resolveExtends } from './resolve-extends.ts';
|
|
2
|
+
import { resolveToken, resolveValue } from './resolve-references.ts';
|
|
3
|
+
import {
|
|
4
|
+
type ExtensionOrigins,
|
|
5
|
+
type RawRefs,
|
|
6
|
+
type ResolvedTokens,
|
|
7
|
+
clone,
|
|
8
|
+
collectExtensionOrigins,
|
|
9
|
+
collectGroups,
|
|
10
|
+
collectTokens,
|
|
11
|
+
isObject,
|
|
12
|
+
isToken,
|
|
13
|
+
isTokenGroup,
|
|
14
|
+
} from './resolve-shared.ts';
|
|
15
|
+
import type { DesignToken, TokenDocument, TokenExtensions, TokenGroup } from './types.ts';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Merges documents and applies `$extends` group inheritance (missing members copied in,
|
|
19
|
+
* `$type` propagated) across the merged tree, WITHOUT resolving any alias reference --
|
|
20
|
+
* factored out of `buildTokenIndex` so a caller that must keep raw, unresolved `$value`s (a
|
|
21
|
+
* `{a.b.c}` string means "emit `var(--other-property)`", not "inline a literal") can still get
|
|
22
|
+
* `$extends` applied before walking the tree, the same way `resolveDocuments` and
|
|
23
|
+
* `createValueResolver` do.
|
|
24
|
+
*
|
|
25
|
+
* `lookupDocuments` (defaults to `documents` itself) is where `$extends` TARGETS are looked up --
|
|
26
|
+
* distinct from `documents`, which is both what gets merged/mutated and what gets returned. A
|
|
27
|
+
* caller that must return only ITS OWN documents' tree (an override context, whose returned shape
|
|
28
|
+
* determines which tokens that context is considered to "define") but whose `$extends` may
|
|
29
|
+
* reference a group that lives only in a broader document set (e.g. a foundation group the
|
|
30
|
+
* override document never itself contains) passes that broader set as `lookupDocuments`. Own
|
|
31
|
+
* groups are collected AFTER (and so take precedence over) the lookup groups, so `resolveExtends`
|
|
32
|
+
* still mutates and returns the caller's own tree -- a target found only in `lookupDocuments`
|
|
33
|
+
* contributes members by being copied in, never by becoming part of the returned tree itself.
|
|
34
|
+
*/
|
|
35
|
+
export function mergeAndExpandExtends(
|
|
36
|
+
documents: TokenDocument[],
|
|
37
|
+
lookupDocuments: TokenDocument[] = documents,
|
|
38
|
+
sourceByDocument?: ReadonlyMap<object, string>,
|
|
39
|
+
): TokenDocument {
|
|
40
|
+
const extensionOrigins: ExtensionOrigins | undefined = sourceByDocument
|
|
41
|
+
? new WeakMap()
|
|
42
|
+
: undefined;
|
|
43
|
+
if (extensionOrigins)
|
|
44
|
+
for (const document of new Set([...lookupDocuments, ...documents]))
|
|
45
|
+
collectExtensionOrigins(document, '', sourceByDocument?.get(document), extensionOrigins);
|
|
46
|
+
const merged = mergeDocuments(documents, extensionOrigins);
|
|
47
|
+
const groups = new Map<string, TokenGroup>();
|
|
48
|
+
const lookupGroups = new Map<string, TokenGroup>();
|
|
49
|
+
const ownGroups = new Map<string, TokenGroup>();
|
|
50
|
+
if (lookupDocuments !== documents) {
|
|
51
|
+
const lookupMerged = mergeDocuments(lookupDocuments, extensionOrigins);
|
|
52
|
+
collectGroups(lookupMerged, '', lookupGroups);
|
|
53
|
+
collectGroups(lookupMerged, '', groups);
|
|
54
|
+
}
|
|
55
|
+
collectGroups(merged, '', ownGroups);
|
|
56
|
+
for (const [groupPath, group] of ownGroups) groups.set(groupPath, group);
|
|
57
|
+
// Resolve only groups contributed by the authored documents. Lookup groups
|
|
58
|
+
// remain indexed so an authored `$extends` can resolve forward, but an
|
|
59
|
+
// unrelated later lookup group must not make an earlier source fail while
|
|
60
|
+
// it is being validated.
|
|
61
|
+
for (const groupPath of ownGroups.keys())
|
|
62
|
+
resolveExtends(
|
|
63
|
+
groupPath,
|
|
64
|
+
groups,
|
|
65
|
+
new Set(),
|
|
66
|
+
new Set(),
|
|
67
|
+
lookupDocuments === documents ? groups : lookupGroups,
|
|
68
|
+
extensionOrigins,
|
|
69
|
+
);
|
|
70
|
+
return merged;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Builds the resolved token index (group inheritance and `$extends` already applied) that both `resolveDocuments` and `createValueResolver` resolve references against, plus the pre-resolution `$ref` snapshot (see `RawRefs`) and the set of paths that are a group's redirected `$root` token (see `collectTokens`'s `rootTokenPaths` comment) rather than an ordinary token that happens to share that path. */
|
|
74
|
+
function buildTokenIndex(documents: TokenDocument[]): {
|
|
75
|
+
tokens: ResolvedTokens;
|
|
76
|
+
rawRefs: RawRefs;
|
|
77
|
+
rootTokenPaths: Set<string>;
|
|
78
|
+
groups: Map<string, TokenGroup>;
|
|
79
|
+
} {
|
|
80
|
+
const tokens: ResolvedTokens = new Map();
|
|
81
|
+
const rootTokenPaths = new Set<string>();
|
|
82
|
+
const merged = mergeAndExpandExtends(documents);
|
|
83
|
+
const groups = new Map<string, TokenGroup>();
|
|
84
|
+
collectGroups(merged, '', groups);
|
|
85
|
+
collectTokens(merged, '', tokens, rootTokenPaths);
|
|
86
|
+
const rawRefs: RawRefs = new Map();
|
|
87
|
+
for (const [path, token] of tokens) {
|
|
88
|
+
if (typeof token.$ref === 'string') rawRefs.set(path, token.$ref);
|
|
89
|
+
}
|
|
90
|
+
return { tokens, rawRefs, rootTokenPaths, groups };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Resolves group inheritance, whole-token aliases, and property-level aliases. */
|
|
94
|
+
export function resolveDocuments(documents: TokenDocument[]): Record<string, DesignToken> {
|
|
95
|
+
const { tokens, rawRefs, rootTokenPaths, groups } = buildTokenIndex(documents);
|
|
96
|
+
const completed = new Set<string>();
|
|
97
|
+
const resolved: Record<string, DesignToken> = Object.create(null);
|
|
98
|
+
for (const path of tokens.keys())
|
|
99
|
+
resolved[path] = clone(
|
|
100
|
+
resolveToken(path, tokens, rawRefs, rootTokenPaths, new Set(), groups, completed),
|
|
101
|
+
);
|
|
102
|
+
return resolved;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export type ValueResolver = (value: unknown) => unknown;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Builds a resolver, against the given documents, for arbitrary raw value trees -- not just
|
|
109
|
+
* whole tokens. `resolveDocuments` only exposes references already resolved at the top level of
|
|
110
|
+
* each token's `$value`; a reference nested inside a composite member (a shadow layer's
|
|
111
|
+
* `inset`, one component of a color, ...) needs the same reference machinery applied to an
|
|
112
|
+
* arbitrary sub-value, including property-path splitting (`{a.b.c}` / `#/a/b/c` may name a
|
|
113
|
+
* whole token OR a property within one) and cycle detection. Reuses `resolveValue` --
|
|
114
|
+
* `resolveDocuments` calls the exact same function on each token's `$value` -- rather than a
|
|
115
|
+
* second resolver.
|
|
116
|
+
*/
|
|
117
|
+
export function createValueResolver(documents: TokenDocument[]): ValueResolver {
|
|
118
|
+
const { tokens, rawRefs, rootTokenPaths, groups } = buildTokenIndex(documents);
|
|
119
|
+
// Keep completion state for the lifetime of this resolver. Each invocation
|
|
120
|
+
// may resolve a different composite value, but the token index is shared;
|
|
121
|
+
// resetting completion state per call can revisit and mutate an already
|
|
122
|
+
// resolved alias differently during generator validation versus emission.
|
|
123
|
+
const completed = new Set<string>();
|
|
124
|
+
return (value: unknown) =>
|
|
125
|
+
resolveValue(value, tokens, rawRefs, rootTokenPaths, new Set(), groups, completed);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Merges ordered documents. A later document's token wins on VALUE and on
|
|
130
|
+
* generation metadata, but no longer replaces the earlier token wholesale:
|
|
131
|
+
* `mergeToken` keeps identity and documentation from the token being overridden
|
|
132
|
+
* when the override does not restate them. "Last occurrence wins" therefore
|
|
133
|
+
* still describes `$value`, and no longer describes the whole token.
|
|
134
|
+
*/
|
|
135
|
+
export function mergeDocuments(
|
|
136
|
+
documents: TokenDocument[],
|
|
137
|
+
extensionOrigins?: ExtensionOrigins,
|
|
138
|
+
): TokenDocument {
|
|
139
|
+
const result: TokenDocument = Object.create(null);
|
|
140
|
+
for (const document of documents) mergeGroup(result, document, extensionOrigins);
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function mergeGroup(
|
|
145
|
+
target: TokenGroup,
|
|
146
|
+
source: TokenGroup,
|
|
147
|
+
extensionOrigins?: ExtensionOrigins,
|
|
148
|
+
): void {
|
|
149
|
+
for (const [key, value] of Object.entries(source)) {
|
|
150
|
+
const existing = target[key];
|
|
151
|
+
if (isTokenGroup(existing) && isTokenGroup(value))
|
|
152
|
+
mergeGroup(existing, value, extensionOrigins);
|
|
153
|
+
else if (isToken(existing) && isToken(value)) target[key] = mergeToken(existing, value);
|
|
154
|
+
else target[key] = clone(value, extensionOrigins);
|
|
155
|
+
if (key === '$extends') {
|
|
156
|
+
const origin = extensionOrigins?.get(source);
|
|
157
|
+
if (origin) extensionOrigins?.set(target, origin);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The vendor-extension keys that describe what a token IS, as opposed to how
|
|
164
|
+
* this particular context writes it. These are inherited by an override that
|
|
165
|
+
* does not restate them; everything else in the namespace is taken from the
|
|
166
|
+
* overriding document.
|
|
167
|
+
*
|
|
168
|
+
* `cssRecipe` is deliberately absent: it is generation metadata, and inheriting
|
|
169
|
+
* it is actively wrong. `shadow.small`'s base carries a two-arm `light-dark()`
|
|
170
|
+
* recipe while its light override is a plain literal, so an inherited recipe
|
|
171
|
+
* would contradict the `$value` sitting beside it. An omitted `cssRecipe` on an
|
|
172
|
+
* override therefore means "no recipe", not "keep the base's".
|
|
173
|
+
*/
|
|
174
|
+
const INHERITED_EXTENSION_KEYS = [
|
|
175
|
+
'cssProperty',
|
|
176
|
+
'public',
|
|
177
|
+
'category',
|
|
178
|
+
'component',
|
|
179
|
+
'contrastPairs',
|
|
180
|
+
'scale',
|
|
181
|
+
'palette',
|
|
182
|
+
] as const;
|
|
183
|
+
|
|
184
|
+
const CINDER_EXTENSION_NAMESPACE = 'com.lostgradient.cinder';
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Merges a token over the one it overrides, keeping identity and documentation
|
|
188
|
+
* from the base while taking value and generation metadata from the override.
|
|
189
|
+
*
|
|
190
|
+
* Replacing wholesale -- what this did before -- dropped `$description` and the
|
|
191
|
+
* whole `$extensions` block for every override, so 96 of the 216 tokens in
|
|
192
|
+
* `resolved/dark.json` came out with no `cssProperty` and no description at
|
|
193
|
+
* all. A consumer importing a resolved context could not map most tokens back
|
|
194
|
+
* to a CSS custom property, which is the main thing a resolved artifact is for.
|
|
195
|
+
*
|
|
196
|
+
* A shallow merge of the `$extensions` OBJECT is not enough either: an override
|
|
197
|
+
* carrying its own namespace entry (a light-only `color-mix()` recipe on
|
|
198
|
+
* `surface.raised.hover`, say) would still wipe the `cssProperty`, `public`,
|
|
199
|
+
* and `category` it does not restate. The split has to be per key.
|
|
200
|
+
*/
|
|
201
|
+
function mergeToken(base: DesignToken, override: DesignToken): DesignToken {
|
|
202
|
+
const merged = clone(override);
|
|
203
|
+
if (merged.$type === undefined && base.$type !== undefined) merged.$type = base.$type;
|
|
204
|
+
if (merged.$description === undefined && base.$description !== undefined) {
|
|
205
|
+
merged.$description = base.$description;
|
|
206
|
+
}
|
|
207
|
+
if (merged.$deprecated === undefined && base.$deprecated !== undefined) {
|
|
208
|
+
merged.$deprecated = base.$deprecated;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const baseExtensions = base.$extensions;
|
|
212
|
+
if (!baseExtensions) return merged;
|
|
213
|
+
|
|
214
|
+
// Every namespace the base declares survives, not just Cinder's: the format
|
|
215
|
+
// requires unknown extension data to survive resolution, and an override has
|
|
216
|
+
// no way to restate a namespace it knows nothing about.
|
|
217
|
+
const extensions: TokenExtensions = { ...clone(baseExtensions), ...clone(merged.$extensions) };
|
|
218
|
+
|
|
219
|
+
const baseCinder = baseExtensions[CINDER_EXTENSION_NAMESPACE];
|
|
220
|
+
if (isObject(baseCinder)) {
|
|
221
|
+
// Rebuilt from the override's OWN namespace entry -- an empty one when it
|
|
222
|
+
// has no `$extensions` at all -- rather than from the spread above. The
|
|
223
|
+
// spread inherits every key, including `cssRecipe`, which is exactly the
|
|
224
|
+
// case that matters: `shadow.small`'s light override is a bare `$value`
|
|
225
|
+
// with no extensions, so spreading gave the light context the base's
|
|
226
|
+
// two-arm `light-dark()` recipe, contradicting the literal `$value` beside
|
|
227
|
+
// it. Starting from the override and pulling back only the identity keys
|
|
228
|
+
// makes "absent" mean "no recipe" whether the override omitted the key or
|
|
229
|
+
// the whole block.
|
|
230
|
+
const overrideCinder = merged.$extensions?.[CINDER_EXTENSION_NAMESPACE];
|
|
231
|
+
const cinder: Record<string, unknown> = isObject(overrideCinder) ? clone(overrideCinder) : {};
|
|
232
|
+
for (const key of INHERITED_EXTENSION_KEYS) {
|
|
233
|
+
if (cinder[key] === undefined && baseCinder[key] !== undefined) {
|
|
234
|
+
cinder[key] = clone(baseCinder[key]);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
extensions[CINDER_EXTENSION_NAMESPACE] = cinder;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
merged.$extensions = extensions;
|
|
241
|
+
return merged;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function resolveDocument(document: TokenDocument): Record<string, DesignToken> {
|
|
245
|
+
return resolveDocuments([document]);
|
|
246
|
+
}
|