design-token-library 0.3.0 → 0.4.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/lib/css-reflector.d.ts +32 -24
  3. package/dist/lib/css-reflector.js +10 -26
  4. package/dist/lib/css-reflector.js.map +1 -1
  5. package/dist/lib/equality.d.ts +16 -0
  6. package/dist/lib/equality.js +90 -0
  7. package/dist/lib/equality.js.map +1 -0
  8. package/dist/lib/graph.d.ts +58 -0
  9. package/dist/lib/graph.js +104 -0
  10. package/dist/lib/graph.js.map +1 -0
  11. package/dist/lib/index.d.ts +4 -2
  12. package/dist/lib/index.js +2 -1
  13. package/dist/lib/index.js.map +1 -1
  14. package/dist/lib/library.d.ts +221 -10
  15. package/dist/lib/library.js +1256 -258
  16. package/dist/lib/library.js.map +1 -1
  17. package/dist/lib/notifier.d.ts +0 -15
  18. package/dist/lib/notifier.js +0 -23
  19. package/dist/lib/notifier.js.map +1 -1
  20. package/dist/lib/parse-document.d.ts +21 -0
  21. package/dist/lib/parse-document.js +31 -0
  22. package/dist/lib/parse-document.js.map +1 -0
  23. package/dist/lib/parse-expression.d.ts +45 -0
  24. package/dist/lib/parse-expression.js +148 -0
  25. package/dist/lib/parse-expression.js.map +1 -0
  26. package/dist/lib/parse-extension.d.ts +24 -0
  27. package/dist/lib/parse-extension.js +68 -0
  28. package/dist/lib/parse-extension.js.map +1 -0
  29. package/dist/lib/parse-resolve.d.ts +16 -0
  30. package/dist/lib/parse-resolve.js +281 -0
  31. package/dist/lib/parse-resolve.js.map +1 -0
  32. package/dist/lib/parse-value.d.ts +35 -0
  33. package/dist/lib/parse-value.js +330 -0
  34. package/dist/lib/parse-value.js.map +1 -0
  35. package/dist/lib/parse.d.ts +103 -0
  36. package/dist/lib/parse.js +562 -0
  37. package/dist/lib/parse.js.map +1 -0
  38. package/dist/lib/queue.d.ts +13 -2
  39. package/dist/lib/queue.js +18 -2
  40. package/dist/lib/queue.js.map +1 -1
  41. package/dist/lib/ref.d.ts +18 -1
  42. package/dist/lib/ref.js +22 -4
  43. package/dist/lib/ref.js.map +1 -1
  44. package/dist/lib/standard-values.d.ts +50 -0
  45. package/dist/lib/standard-values.js +73 -0
  46. package/dist/lib/standard-values.js.map +1 -0
  47. package/dist/lib/tsconfig.tsbuildinfo +1 -1
  48. package/dist/lib/utilities.d.ts +6 -0
  49. package/dist/lib/utilities.js +7 -1
  50. package/dist/lib/utilities.js.map +1 -1
  51. package/package.json +9 -5
  52. package/dist/lib/public.d.ts +0 -1405
  53. package/dist/lib/tsdoc-metadata.json +0 -11
  54. package/dist/lib/watcher.d.ts +0 -12
  55. package/dist/lib/watcher.js +0 -25
  56. package/dist/lib/watcher.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACH,sBAAsB,EACtB,OAAO,EACP,iBAAiB,EACjB,SAAS,EACT,cAAc,EACd,YAAY,GACf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AACpD,OAAO,EACH,OAAO,EACP,KAAK,EACL,YAAY,GAiBf,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACH,sBAAsB,EACtB,oBAAoB,EACpB,OAAO,EACP,iBAAiB,EACjB,SAAS,EACT,cAAc,EACd,YAAY,EACZ,WAAW,GACd,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE/C,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AACpD,OAAO,EACH,OAAO,EACP,KAAK,EACL,YAAY,GAgBf,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,MAAM,aAAa,CAAC"}
@@ -22,7 +22,18 @@ declare const TOKEN: unique symbol;
22
22
  export declare namespace Library {
23
23
  export interface Library<T extends {}, R extends {} = T> {
24
24
  tokens: TokenLibrary<T, R>;
25
- subscribe(subscriber: Library.Subscriber<R>): void;
25
+ /**
26
+ * Adds a subscriber, which is told of each change made after it's
27
+ * added.
28
+ *
29
+ * @remarks
30
+ * A library with a subscriber resolves its tokens, and a `set()`
31
+ * resolves again the tokens it reaches. An extension resolves its
32
+ * own tokens, and its subscriber is told of them; with
33
+ * `origin: "ancestor"` or `"all"`, it resolves its ancestor's tokens
34
+ * too, and the subscriber is told of those whose values change.
35
+ */
36
+ subscribe(subscriber: Library.Subscriber<R>, options?: Library.SubscribeOptions): void;
26
37
  unsubscribe(subscriber: Library.Subscriber<R>): void;
27
38
  /**
28
39
  * Creates a library with the values of the tokens replaced, and
@@ -36,7 +47,7 @@ export declare namespace Library {
36
47
  * extension that nothing refers to is collected, along with its
37
48
  * subscribers.
38
49
  */
39
- extend(config: ExtendConfig<T, R>, options?: ExtendOptions): Library<T, R>;
50
+ extend(config: ExtendConfig<T, R>, ...options: ExtendArgs<{}, T>): Library<T, R>;
40
51
  /**
41
52
  * Creates a library with the values of tokens replaced, new tokens,
42
53
  * and recipes overridden.
@@ -46,7 +57,7 @@ export declare namespace Library {
46
57
  * extension that nothing refers to is collected, along with its
47
58
  * subscribers.
48
59
  */
49
- extend<K extends NoRoot<K> = {}>(config: ExtendConfig<T, R & NewTokens<K, T>> & Config<K, R & NewTokens<K, T>, never, T> & NoInfer<ConfigValueCheck<K, R & NewTokens<K, T>>> & (ConfigTypeHint | {}), options?: ExtendOptions): Library<T & NewTokens<K, T>, R & NewTokens<K, T>>;
60
+ extend<K extends NoRoot<K> = {}>(config: ExtendConfig<T, R & NewTokens<K, T>> & Config<K, R & NewTokens<K, T>, never, T> & NoInfer<ConfigValueCheck<K, R & NewTokens<K, T>>> & (ConfigTypeHint | {}), ...options: ExtendArgs<NewTokens<K, T>, T>): Library<T & NewTokens<K, T>, R & NewTokens<K, T>>;
50
61
  }
51
62
  /**
52
63
  * The tokens and groups of `K` that are new relative to a source library
@@ -125,8 +136,64 @@ export declare namespace Library {
125
136
  */
126
137
  filter?(entry: EntryOf<S, R>): FilterResult;
127
138
  }
139
+ /**
140
+ * What changed of a token, in a batch.
141
+ *
142
+ * @public
143
+ */
144
+ export interface Change<R extends {}> {
145
+ /**
146
+ * The token, of the library subscribed to.
147
+ */
148
+ readonly token: TokenRecord<R>;
149
+ /**
150
+ * Its value changed.
151
+ */
152
+ readonly valueChanged: boolean;
153
+ /**
154
+ * Where its value comes from: an extension's own token, or one whose
155
+ * value is its ancestor's. Always `"own"` in a library made by
156
+ * `create()`.
157
+ */
158
+ readonly origin: "ancestor" | "own";
159
+ /**
160
+ * Its origin changed: an extension's token became its own.
161
+ */
162
+ readonly originChanged: boolean;
163
+ }
164
+ /**
165
+ * @public
166
+ */
167
+ export interface SubscribeOptions {
168
+ /**
169
+ * Which tokens a subscriber is told of, by where their values come
170
+ * from, as {@link (Library:namespace).Change.origin} says: the
171
+ * library's own (the default), its ancestor's, or all. A library
172
+ * made by `create()` has only its own.
173
+ */
174
+ readonly origin?: "own" | "ancestor" | "all";
175
+ }
176
+ /**
177
+ * @public
178
+ */
128
179
  export interface Subscriber<R extends {}> {
129
- onChange(records: ReadonlyArray<Library.TokenRecord<R>>): void;
180
+ /**
181
+ * Called with what changed, once for the changes made before a
182
+ * microtask runs. Each change has its value or its origin changed.
183
+ */
184
+ onChange(changes: ReadonlyArray<Library.Change<R>>): void;
185
+ /**
186
+ * Called with an error that a token threw as the library resolved
187
+ * it on its own: as it was subscribed, or after a change. Reading
188
+ * the token throws to its reader instead.
189
+ *
190
+ * @remarks
191
+ * It's called in a microtask after the batch the error was found
192
+ * in, once for each error, and the subscriber is still told of
193
+ * later changes. An error that no subscriber with `onError` is told
194
+ * of is thrown from a microtask.
195
+ */
196
+ onError?(error: TokenResolutionError): void;
130
197
  }
131
198
  /**
132
199
  * A union of all tokens in a library of shape `T`.
@@ -289,6 +356,42 @@ export declare namespace Library {
289
356
  readonly recipe: Recipe<any, unknown>;
290
357
  readonly fn: (...args: any[]) => unknown;
291
358
  }
359
+ /**
360
+ * What a library knows of a custom type: how to tell whether two of its
361
+ * values are equal.
362
+ *
363
+ * @public
364
+ */
365
+ export interface TypeOptions<V = unknown> {
366
+ /**
367
+ * Tells whether a value a token of the type resolved to is equal to
368
+ * the one it had. When it is, the token keeps the value it had, so it
369
+ * is the same object. It's called only by a library with a
370
+ * subscriber, and not when a token resolves its first value.
371
+ */
372
+ isEqual(previous: V, next: V): boolean;
373
+ }
374
+ /**
375
+ * The custom types a library registers, by name. A DTCG type has its own
376
+ * equality, so it can't be registered.
377
+ *
378
+ * @public
379
+ */
380
+ export type Types = {
381
+ readonly [name: string]: TypeOptions<any>;
382
+ };
383
+ /**
384
+ * Options of `create()`.
385
+ *
386
+ * @public
387
+ */
388
+ export interface CreateOptions {
389
+ /**
390
+ * The custom types of the library's tokens. Every custom type a token
391
+ * has must be registered.
392
+ */
393
+ readonly types?: Types;
394
+ }
292
395
  /**
293
396
  * Options of `extend()`.
294
397
  *
@@ -301,7 +404,73 @@ export declare namespace Library {
301
404
  * doesn't restate.
302
405
  */
303
406
  readonly recipes?: readonly RecipeOverride[];
407
+ /**
408
+ * The custom types that the extension's new tokens have and the
409
+ * library it extends doesn't register. A type the library registers
410
+ * is inherited, and can't be registered again.
411
+ */
412
+ readonly types?: Types;
304
413
  }
414
+ /**
415
+ * The custom type names in a library of shape `T`: the `$type` of each
416
+ * of its tokens and groups that isn't a DTCG type.
417
+ *
418
+ * @public
419
+ */
420
+ export type CustomTypeNamesOf<T> = 0 extends 1 & T ? never : T extends readonly unknown[] | Function ? never : T extends object ? {
421
+ [K in keyof T]-?: K extends "$type" ? T[K] extends string ? string extends T[K] ? never : Exclude<T[K], DesignToken.Type | DesignToken.ReservedTypeName> : never : K extends `$${string}` ? never : CustomTypeNamesOf<T[K]>;
422
+ }[keyof T] : never;
423
+ /**
424
+ * The values of the tokens of type `N` in a library of shape `T`, as a
425
+ * token gives them. `G` is the type inherited from ancestor groups.
426
+ *
427
+ * @public
428
+ */
429
+ export type ValueOfType<T, N extends string, G extends string = never> = {
430
+ [K in keyof T]-?: K extends `$${string}` ? never : T[K] extends DesignToken.Shape ? DesignToken.TypeByToken<T[K], G> extends N ? DesignToken.ReadonlyValueByToken<T[K]> : never : T[K] extends object ? ValueOfType<T[K], N, GroupType<T[K], G>> : never;
431
+ }[keyof T];
432
+ /**
433
+ * The `types` a library of shape `T` registers: each of the custom
434
+ * types `N` that `I`, the types it inherits, leave out.
435
+ *
436
+ * @public
437
+ */
438
+ export type TypesOf<T, N extends string, I extends string = never> = {
439
+ readonly [P in Exclude<N, I>]: TypeOptions<ValueOfType<T, P>>;
440
+ } & {
441
+ readonly [P in DesignToken.ReservedTypeName | I]?: never;
442
+ } & Types;
443
+ /**
444
+ * The options of `create()` for a library of shape `T`: required, with
445
+ * its custom types, when it has any.
446
+ *
447
+ * @public
448
+ */
449
+ export type CreateArgs<T> = [CustomTypeNamesOf<T>] extends [never] ? [options?: CreateOptions & {
450
+ readonly types?: TypesOf<T, never>;
451
+ }] : [
452
+ options: CreateOptions & {
453
+ readonly types: TypesOf<T, CustomTypeNamesOf<T>>;
454
+ }
455
+ ];
456
+ /**
457
+ * The options of `extend()` for new tokens of shape `K` in a library of
458
+ * shape `T`: required, with the custom types `T` doesn't have, when they
459
+ * have any.
460
+ *
461
+ * @public
462
+ */
463
+ export type ExtendArgs<K, T> = [
464
+ Exclude<CustomTypeNamesOf<K>, CustomTypeNamesOf<T>>
465
+ ] extends [never] ? [
466
+ options?: ExtendOptions & {
467
+ readonly types?: TypesOf<K, never, CustomTypeNamesOf<T>>;
468
+ }
469
+ ] : [
470
+ options: ExtendOptions & {
471
+ readonly types: TypesOf<K, CustomTypeNamesOf<K>, CustomTypeNamesOf<T>>;
472
+ }
473
+ ];
305
474
  /**
306
475
  * A {@link (Library:namespace).Ref} to each token of a library of shape
307
476
  * `R`, at the path of the token in `T`, the shape of a group of it. Made by
@@ -453,7 +622,7 @@ export declare namespace Library {
453
622
  /**
454
623
  * @public
455
624
  */
456
- export const create: <T extends NoRoot<T> = any>(config: Library.Config<T, T> & NoInfer<ConfigValueCheck<T, T>> & (ConfigTypeHint | {})) => Library.Library<T>;
625
+ export const create: <T extends NoRoot<T> = any>(config: Library.Config<T, T> & NoInfer<ConfigValueCheck<T, T>> & (ConfigTypeHint | {}), ...options: NoInfer<CreateArgs<T>>) => Library.Library<T>;
457
626
  export {};
458
627
  }
459
628
  /**
@@ -471,6 +640,25 @@ export declare class CircularReferenceError extends Error {
471
640
  readonly chain: string[];
472
641
  constructor(chain: string[]);
473
642
  }
643
+ /**
644
+ * Given to a subscriber's `onError` when a token throws as a subscribed
645
+ * library resolves it on its own. The error thrown is its `cause`.
646
+ *
647
+ * @public
648
+ */
649
+ export declare class TokenResolutionError extends Error {
650
+ readonly name = "TokenResolutionError";
651
+ /**
652
+ * The name of the token that threw.
653
+ */
654
+ readonly tokenName: string;
655
+ /**
656
+ * What threw: its type's `isEqual`, comparing the value it resolved with
657
+ * the one it had, or resolving its value.
658
+ */
659
+ readonly phase: "isEqual" | "resolve";
660
+ constructor(tokenName: string, phase: "isEqual" | "resolve", cause: unknown);
661
+ }
474
662
  /**
475
663
  * A token's value as it was configured, or last given to `set()`, with its
476
664
  * refs and computed values unresolved. A token of a library made by
@@ -600,14 +788,12 @@ export declare function entriesOf<T extends {}, R extends {}>(library: Library.L
600
788
  *
601
789
  * This reads the configured values and not the values, so it resolves no
602
790
  * token, and so it is known of a library whose tokens have not been read. It
603
- * is found for the whole library once and kept until a token is set to a
604
- * different value, so each call after the first is a lookup. A value a recipe
791
+ * is found for the whole library once, kept as tokens are set, and shared
792
+ * with the libraries that extend it, so each call after the first is a
793
+ * lookup, and an extension makes only the tokens it lists. A value a recipe
605
794
  * reads other than through its arguments is not seen, as for
606
795
  * {@link dependenciesOf}.
607
796
  *
608
- * It throws when a ref in a token of the library isn't a token of it, as
609
- * reading the token's `$value` does.
610
- *
611
797
  * @example
612
798
  * ```ts
613
799
  * // hover: { $value: Recipes.darken(theme.colors.primary, 0.2) }
@@ -630,4 +816,29 @@ export declare function entriesOf<T extends {}, R extends {}>(library: Library.L
630
816
  * @public
631
817
  */
632
818
  export declare const dependentsOf: <T extends DesignToken.Shape, R extends {}, G extends string = never>(token: Library.Token<T, R, G>) => ReadonlyArray<Library.TokenRecord<R>>;
819
+ /**
820
+ * The tokens of a library whose values are its own, in the order of the
821
+ * library: every token of a library made by `create()`, and an extension's
822
+ * tokens that don't come from its ancestors.
823
+ *
824
+ * @remarks
825
+ * An extension's token is its own when it has a value of its own (one its
826
+ * config gives, one that is set, or a token new in it), when it calls a
827
+ * recipe the extension overrides, or when a token it depends on is its own.
828
+ * A token depends on the tokens its configured value refers to, and on
829
+ * those a ref in a recipe's result is to, so tokens of its ancestors that
830
+ * call recipes may be resolved to find them.
831
+ *
832
+ * It's what a first render of an extension writes, such as with
833
+ * {@link toCSS}'s `filter`.
834
+ *
835
+ * @example
836
+ * ```ts
837
+ * const own = new Set(ownTokensOf(extension));
838
+ * toCSS(extension, { filter: (token) => own.has(token) });
839
+ * ```
840
+ *
841
+ * @public
842
+ */
843
+ export declare const ownTokensOf: <R extends {}>(library: Library.Library<any, R>) => ReadonlyArray<Library.TokenRecord<R>>;
633
844
  export {};