@design-token-kit/core 0.2.1 → 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.
Files changed (3) hide show
  1. package/lib/index.d.ts +132 -17
  2. package/lib/index.js +724 -624
  3. package/package.json +3 -2
package/lib/index.d.ts CHANGED
@@ -109,10 +109,23 @@ export declare class DesignTokens {
109
109
  */
110
110
  export declare class Dtcg {
111
111
  #private;
112
- constructor(root: TokenGroup);
113
- /** Returns the top-level child token or group with the given name, or undefined. */
112
+ /**
113
+ * The origin of this token document.
114
+ *
115
+ * Can be a file path ({@code "tokens.json"}), a URL, or
116
+ * {@code "-"} for stdin.
117
+ * Used in validation diagnostics as the {@code sourcePath}.
118
+ */
119
+ readonly source?: string;
120
+ constructor(root: TokenGroup, source?: string);
121
+ /**
122
+ * Returns the top-level child token or group with the given name,
123
+ * or undefined.
124
+ */
114
125
  get(name: string): TokenGroup | TokenNode<unknown> | undefined;
115
- /** Returns all top-level child names in insertion order. */
126
+ /**
127
+ * Returns all top-level child names in insertion order.
128
+ */
116
129
  keys(): IterableIterator<string>;
117
130
  /** Returns all top-level child entries as [name, node] pairs in insertion order. */
118
131
  entries(): IterableIterator<[string, TokenGroup | TokenNode<unknown>]>;
@@ -124,7 +137,7 @@ export declare class Dtcg {
124
137
  * Pass the base document when validating a theme file, so that references to tokens
125
138
  * defined in the base are resolved correctly.
126
139
  */
127
- validate(base?: Dtcg): DtcgValidationIssue[];
140
+ validate(base?: Dtcg): ValidationIssue[];
128
141
  }
129
142
 
130
143
  /**
@@ -137,7 +150,7 @@ export declare class Dtcg {
137
150
  */
138
151
  export declare class DtcgJsonReader {
139
152
  #private;
140
- parse(content: string): Dtcg;
153
+ parse(content: string, source?: string): Dtcg;
141
154
  }
142
155
 
143
156
  /**
@@ -169,7 +182,28 @@ export declare class DtcgList {
169
182
  readonly base: Dtcg;
170
183
  readonly themes: ReadonlyMap<string, Dtcg>;
171
184
  constructor(base: Dtcg, themes?: Map<string, Dtcg>);
172
- validate(): DtcgValidationIssue[];
185
+ validate(): ValidationIssue[];
186
+ }
187
+
188
+ /**
189
+ * Loads and validates token sources, assembling a {@link DtcgList}.
190
+ *
191
+ * The first document is the base token set, subsequent documents are theme
192
+ * overrides.
193
+ *
194
+ * Sources in different formats may be mixed.
195
+ */
196
+ export declare class DtcgListLoader {
197
+ #private;
198
+ /**
199
+ * Validates and loads all sources into a {@link DtcgList}.
200
+ *
201
+ * @param sources - Paths to token files or {@code "-"} for stdin.
202
+ * @param forcedFormat - When set, all sources are treated as this
203
+ * format instead of auto-detecting from content.
204
+ * @throws TokenSyntaxError when schema validation fails.
205
+ */
206
+ load(sources: string[], forcedFormat?: Format): Promise<DtcgList>;
173
207
  }
174
208
 
175
209
  /**
@@ -182,7 +216,16 @@ export declare class DtcgSchemaValidator implements TokenValidator {
182
216
  validate(sources: string[]): Promise<ValidationIssue[]>;
183
217
  }
184
218
 
219
+ /**
220
+ * Converts token documents in any supported format to CSS custom properties.
221
+ *
222
+ * The first document is treated as the base token set, all subsequent
223
+ * documents as theme overrides.
224
+ * Base tokens are emitted under {@code :root}.
225
+ * Theme tokens are emitted under {@code :root[data-theme="name"]}.
226
+ */
185
227
  export declare class DtcgTokenCssConverter implements TokenCssConverter {
228
+ #private;
186
229
  convert(sources: string[]): Promise<string>;
187
230
  convertDocument(doc: Dtcg): string;
188
231
  convertList(list: DtcgList): string;
@@ -193,20 +236,69 @@ export declare class DtcgTokenCssConverter implements TokenCssConverter {
193
236
  *
194
237
  * Accepts one base file plus optional theme files. Validates references,
195
238
  * cycles, type mismatches, and deprecated token usage.
239
+ *
240
+ * Format is detected from content, not from file extension.
241
+ * HRDT sources may contain multiple YAML documents separated by {@code ---}.
242
+ * The first document is the base, subsequent documents are themes.
196
243
  */
197
244
  export declare class DtcgTokenValidator implements TokenValidator {
198
245
  #private;
199
246
  validate(sources: string[]): Promise<ValidationIssue[]>;
200
247
  }
201
248
 
202
- export declare interface DtcgValidationIssue {
203
- /** Dot-separated path to the token or group where the issue was found. */
204
- tokenPath: string;
205
- severity: DtcgValidationSeverity;
206
- message: string;
249
+ /**
250
+ * Token formats.
251
+ */
252
+ export declare enum Format {
253
+ /**
254
+ * DTCG (Design Tokens Community Group) JSON token format.
255
+ *
256
+ * @see https://tr.designtokens.org/format/
257
+ */
258
+ DTCG = "dtcg",
259
+ /**
260
+ * HRDT (Human-Readable Design Tokens) YAML token format.
261
+ */
262
+ HRDT = "hrdt",
263
+ /**
264
+ * CSS custom properties.
265
+ */
266
+ CSS = "css"
207
267
  }
208
268
 
209
- export declare type DtcgValidationSeverity = "error" | "warning";
269
+ /**
270
+ * Detects the token format from raw content by inspecting the content
271
+ * structure rather than relying on file extensions.
272
+ *
273
+ * Supported formats: {@link Format.DTCG} (JSON), {@link Format.HRDT} (YAML),
274
+ * and {@link Format.CSS}.
275
+ */
276
+ export declare class FormatDetector {
277
+ /**
278
+ * Detects the format of the given content.
279
+ *
280
+ * Detection rules:
281
+ * - If content starts with `{` and is valid JSON it is treated as DTCG.
282
+ * - If content contains CSS-specific patterns (`:root`, custom properties,
283
+ * `@layer`) it is treated as CSS.
284
+ * - Otherwise it is treated as HRDT.
285
+ */
286
+ static detect(content: string): Format;
287
+ /**
288
+ * Returns `true` when the content looks like a DTCG JSON document.
289
+ *
290
+ * The check is structural: content must start with `{` and be parseable
291
+ * as JSON.
292
+ */
293
+ static isDtcg(content: string): boolean;
294
+ /**
295
+ * Returns `true` when the content looks like CSS.
296
+ *
297
+ * Detects the presence of CSS custom properties (`--name:`),
298
+ * {@code :root} selector, or {@code @layer} at-rules.
299
+ */
300
+ static isCss(content: string): boolean;
301
+ }
210
302
 
211
303
  /**
212
304
  * Reads an HRDT token file and parses it directly into a {@link Dtcg} model.
@@ -218,7 +310,15 @@ export declare type DtcgValidationSeverity = "error" | "warning";
218
310
  export declare class HrdtTokenReader {
219
311
  #private;
220
312
  parseRaw(hrdtContent: string): unknown;
221
- parse(hrdtContent: string): Dtcg;
313
+ parse(hrdtContent: string, source?: string): Dtcg;
314
+ /**
315
+ * Parses a YAML string that may contain multiple documents separated by
316
+ * {@code ---} and returns a {@link Dtcg} for each document.
317
+ *
318
+ * If the content contains a single document the result is a single-element
319
+ * array.
320
+ */
321
+ parseAll(hrdtContent: string, source?: string): Dtcg[];
222
322
  parseFile(filePath: string): Promise<Dtcg>;
223
323
  }
224
324
 
@@ -268,7 +368,7 @@ declare type ScopedTokenEntry = TokenEntry & {
268
368
 
269
369
  /**
270
370
  * Data source.
271
- * Wraps URL, File or content.
371
+ * Wraps URL, File, stdin ({@code "-"}) or raw content.
272
372
  *
273
373
  * Allows returning any of these data sources as a file.
274
374
  * Used in tool implementations to simplify the interface.
@@ -278,6 +378,7 @@ export declare class Source {
278
378
  constructor(input: string);
279
379
  getInput(): string;
280
380
  getType(): SourceType;
381
+ getFormat(): Promise<Format>;
281
382
  getContent(): Promise<string>;
282
383
  getFile(): Promise<string>;
283
384
  }
@@ -285,7 +386,8 @@ export declare class Source {
285
386
  declare enum SourceType {
286
387
  URL = "url",
287
388
  FILE = "file",
288
- CONTENT = "content"
389
+ CONTENT = "content",
390
+ STDIN = "stdin"
289
391
  }
290
392
 
291
393
  declare type ThemeBucket = {
@@ -561,6 +663,17 @@ export declare class TokenReference {
561
663
  toString(): string;
562
664
  }
563
665
 
666
+ /**
667
+ * Thrown by {@link DtcgListLoader.load} when schema validation fails.
668
+ *
669
+ * The {@link issues} field contains individual validation diagnostics.
670
+ */
671
+ export declare class TokenSyntaxError extends Error {
672
+ readonly issues: ValidationIssue[];
673
+ constructor(issues: ValidationIssue[]);
674
+ formatIssues(): string;
675
+ }
676
+
564
677
  /**
565
678
  * All token types defined in the DTCG 2025.10 specification.
566
679
  *
@@ -590,9 +703,11 @@ export declare interface TokenValidator {
590
703
  */
591
704
  export declare interface ValidationIssue {
592
705
  /** Name of the validator that issued the diagnostic. */
593
- name: string;
706
+ name?: string;
594
707
  /** Path to the source file. */
595
- sourcePath: string;
708
+ sourcePath?: string;
709
+ /** Dot-separated path to the token or group where the issue was found. */
710
+ tokenPath?: string;
596
711
  /** Message text. */
597
712
  message: string;
598
713
  /** Severity level. */