@terrazzo/parser 2.0.0-alpha.7 → 2.0.0-beta.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 (53) hide show
  1. package/dist/index.d.ts +39 -6
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +578 -512
  4. package/dist/index.js.map +1 -1
  5. package/package.json +3 -3
  6. package/src/build/index.ts +0 -209
  7. package/src/config.ts +0 -304
  8. package/src/index.ts +0 -95
  9. package/src/lib/code-frame.ts +0 -177
  10. package/src/lib/momoa.ts +0 -10
  11. package/src/lib/resolver-utils.ts +0 -35
  12. package/src/lint/index.ts +0 -142
  13. package/src/lint/plugin-core/index.ts +0 -103
  14. package/src/lint/plugin-core/lib/docs.ts +0 -3
  15. package/src/lint/plugin-core/rules/a11y-min-contrast.ts +0 -91
  16. package/src/lint/plugin-core/rules/a11y-min-font-size.ts +0 -66
  17. package/src/lint/plugin-core/rules/colorspace.ts +0 -108
  18. package/src/lint/plugin-core/rules/consistent-naming.ts +0 -65
  19. package/src/lint/plugin-core/rules/descriptions.ts +0 -43
  20. package/src/lint/plugin-core/rules/duplicate-values.ts +0 -85
  21. package/src/lint/plugin-core/rules/max-gamut.ts +0 -144
  22. package/src/lint/plugin-core/rules/required-children.ts +0 -106
  23. package/src/lint/plugin-core/rules/required-modes.ts +0 -75
  24. package/src/lint/plugin-core/rules/required-type.ts +0 -28
  25. package/src/lint/plugin-core/rules/required-typography-properties.ts +0 -65
  26. package/src/lint/plugin-core/rules/valid-boolean.ts +0 -41
  27. package/src/lint/plugin-core/rules/valid-border.ts +0 -57
  28. package/src/lint/plugin-core/rules/valid-color.ts +0 -265
  29. package/src/lint/plugin-core/rules/valid-cubic-bezier.ts +0 -83
  30. package/src/lint/plugin-core/rules/valid-dimension.ts +0 -199
  31. package/src/lint/plugin-core/rules/valid-duration.ts +0 -123
  32. package/src/lint/plugin-core/rules/valid-font-family.ts +0 -68
  33. package/src/lint/plugin-core/rules/valid-font-weight.ts +0 -89
  34. package/src/lint/plugin-core/rules/valid-gradient.ts +0 -79
  35. package/src/lint/plugin-core/rules/valid-link.ts +0 -41
  36. package/src/lint/plugin-core/rules/valid-number.ts +0 -63
  37. package/src/lint/plugin-core/rules/valid-shadow.ts +0 -67
  38. package/src/lint/plugin-core/rules/valid-string.ts +0 -41
  39. package/src/lint/plugin-core/rules/valid-stroke-style.ts +0 -104
  40. package/src/lint/plugin-core/rules/valid-transition.ts +0 -61
  41. package/src/lint/plugin-core/rules/valid-typography.ts +0 -67
  42. package/src/logger.ts +0 -213
  43. package/src/parse/index.ts +0 -124
  44. package/src/parse/load.ts +0 -172
  45. package/src/parse/normalize.ts +0 -163
  46. package/src/parse/process.ts +0 -251
  47. package/src/parse/token.ts +0 -553
  48. package/src/resolver/create-synthetic-resolver.ts +0 -86
  49. package/src/resolver/index.ts +0 -7
  50. package/src/resolver/load.ts +0 -215
  51. package/src/resolver/normalize.ts +0 -133
  52. package/src/resolver/validate.ts +0 -375
  53. package/src/types.ts +0 -468
package/src/types.ts DELETED
@@ -1,468 +0,0 @@
1
- import type * as momoa from '@humanwhocodes/momoa';
2
- import type { InputSourceWithDocument } from '@terrazzo/json-schema-tools';
3
- import type {
4
- Group,
5
- TokenNormalized,
6
- TokenNormalizedSet,
7
- TokenTransformed,
8
- TokenTransformedBase,
9
- } from '@terrazzo/token-tools';
10
- import type ytm from 'yaml-to-momoa';
11
- import type Logger from './logger.js';
12
-
13
- // Export some types as a convenience, because they originally came from this package
14
- export type {
15
- Group,
16
- TokenNormalized,
17
- TokenNormalizedSet,
18
- TokenTransformed,
19
- TokenTransformedBase,
20
- } from '@terrazzo/token-tools';
21
-
22
- export interface PluginHookContext {
23
- logger: Logger;
24
- }
25
-
26
- export interface BuildHookOptions {
27
- /** Plugin hook context (provides access to shared logger) */
28
- context: PluginHookContext;
29
- /** Map of tokens */
30
- tokens: Record<string, TokenNormalized>;
31
- /** Query transformed values */
32
- getTransforms(params: TransformParams): TokenTransformed[];
33
- /** Momoa documents */
34
- sources: InputSourceWithDocument[];
35
- /** Resolver */
36
- resolver: Resolver;
37
- outputFile: (
38
- /** Filename to output (relative to outDir) */
39
- filename: string,
40
- /** Contents to write to file */
41
- contents: string | Buffer,
42
- ) => void;
43
- }
44
-
45
- export interface BuildRunnerResult {
46
- outputFiles: OutputFileExpanded[];
47
- }
48
-
49
- export interface BuildEndHookOptions {
50
- /** Plugin hook context (provides access to shared logger) */
51
- context: PluginHookContext;
52
- /** Map of tokens */
53
- tokens: Record<string, TokenNormalized>;
54
- /** Query transformed values */
55
- getTransforms(params: TransformParams): TokenTransformed[];
56
- /** Momoa documents */
57
- sources: InputSourceWithDocument[];
58
- /** Final files to be written */
59
- outputFiles: OutputFileExpanded[];
60
- }
61
-
62
- // loosey-goosey user-defined config
63
- export interface Config {
64
- /**
65
- * Path to tokens.json
66
- * @default "./tokens.json"
67
- */
68
- tokens?: string | string[];
69
- /**
70
- * Output directory
71
- * @default "./tokens/"
72
- */
73
- outDir?: string;
74
- /** Specify plugins */
75
- plugins?: Plugin[];
76
- /** Specify linting settings */
77
- lint?: {
78
- /** Configure build behavior */
79
- build?: {
80
- /**
81
- * Should linters run with `tz build`?
82
- * @default true
83
- */
84
- enabled?: boolean;
85
- };
86
- /** Configure lint rules */
87
- rules?: Record<string, LintRuleShorthand | LintRuleLonghand>;
88
- };
89
- /** Ignore token groups */
90
- ignore?: {
91
- /** Token patterns to ignore. Accepts globs. */
92
- tokens?: string[];
93
- /** Ignore deprecated tokens */
94
- deprecated?: boolean;
95
- };
96
- }
97
-
98
- export interface VisitorContext {
99
- parent?: momoa.AnyNode;
100
- filename: URL;
101
- path: string[];
102
- }
103
-
104
- export type Visitor<T extends momoa.AnyNode = momoa.ObjectNode | momoa.DocumentNode> = (
105
- node: T,
106
- context: VisitorContext,
107
- // biome-ignore lint/suspicious/noConfusingVoidType: TS requires void
108
- ) => T | void | null | undefined;
109
-
110
- export interface TransformVisitors {
111
- boolean?: Visitor;
112
- border?: Visitor;
113
- color?: Visitor;
114
- cubicBezier?: Visitor;
115
- dimension?: Visitor;
116
- duration?: Visitor;
117
- fontFamily?: Visitor;
118
- fontWeight?: Visitor;
119
- gradient?: Visitor;
120
- group?: Visitor;
121
- link?: Visitor;
122
- number?: Visitor;
123
- root?: Visitor;
124
- shadow?: Visitor;
125
- strokeStyle?: Visitor;
126
- token?: Visitor;
127
- transition?: Visitor;
128
- typography?: Visitor;
129
- [key: string]: Visitor | undefined;
130
- }
131
-
132
- // normalized, finalized config
133
- export interface ConfigInit {
134
- tokens: URL[];
135
- outDir: URL;
136
- plugins: Plugin[];
137
- lint: {
138
- build: NonNullable<NonNullable<Config['lint']>['build']>;
139
- rules: Record<string, LintRuleLonghand>;
140
- };
141
- ignore: {
142
- tokens: NonNullable<NonNullable<Config['ignore']>['tokens']>;
143
- deprecated: NonNullable<NonNullable<Config['ignore']>['deprecated']>;
144
- };
145
- }
146
-
147
- export interface ConfigOptions {
148
- logger?: Logger;
149
- /** @terrazzo/parser needs cwd so this can be run without Node.js. Importing defineConfig from @terrazzo/cli doesn’t need this. */
150
- cwd: URL;
151
- }
152
-
153
- export interface LintNotice {
154
- /** Lint message shown to the user */
155
- message: string;
156
- /** Erring node (used to point to a specific line) */
157
- node?: momoa.AnyNode;
158
- }
159
-
160
- export type LintRuleSeverity = 'error' | 'warn' | 'off';
161
- export type LintRuleShorthand = LintRuleSeverity | 0 | 1 | 2;
162
- export type LintRuleLonghand = [LintRuleSeverity | 0 | 1 | 2, any];
163
-
164
- export interface LintRuleNormalized<O = any> {
165
- id: string;
166
- severity: LintRuleSeverity;
167
- options?: O;
168
- }
169
-
170
- export type LintReportDescriptor<MessageIds extends string> = {
171
- /** To error on a specific token source file, provide a Momoa node */
172
- node?: momoa.AnyNode;
173
- /** To provide correct line numbers, specify the filename (usually found on `token.source.loc`) */
174
- filename?: string;
175
- /** Provide data for messages */
176
- data?: Record<string, unknown>;
177
- } & (
178
- | {
179
- /** Provide the error message to display */
180
- message: string;
181
- messageId?: never;
182
- }
183
- | {
184
- message?: never;
185
- /** Provide the error message ID */
186
- messageId: MessageIds;
187
- }
188
- );
189
-
190
- // Note: lint types intentionally steal the API from ESLint. Options were
191
- // omitted where they don’t make sense (or were deprecated from ESLint—we don’t
192
- // need to worry about backwards compat). The types also leave room in the
193
- // future to be expanded easily if needed.
194
-
195
- export interface LintRule<
196
- MessageIds extends string,
197
- LintRuleOptions extends Record<string, any> = Record<string, never>,
198
- LintRuleDocs = unknown,
199
- > {
200
- meta?: LintRuleMetaData<MessageIds, LintRuleOptions, LintRuleDocs>;
201
- /**
202
- * Function which returns an object with methods that ESLint calls to “visit”
203
- * nodes while traversing the abstract syntax tree.
204
- */
205
- create(context: Readonly<LintRuleContext<MessageIds, LintRuleOptions>>): void | Promise<void>;
206
- /**
207
- * Default options the rule will be run with
208
- */
209
- defaultOptions: LintRuleOptions;
210
- }
211
-
212
- export interface LintRuleContext<MessageIds extends string, LintRuleOptions extends object | undefined = undefined> {
213
- /** The rule ID. */
214
- id: string;
215
- /**
216
- * An array of the configured options for this rule. This array does not
217
- * include the rule severity.
218
- */
219
- options: LintRuleOptions;
220
- /** The current working directory. */
221
- cwd?: URL;
222
- /**
223
- * All source files present in this run. To find the original source, match a
224
- * token’s `source.loc` filename to one of the source’s `filename`s.
225
- */
226
- sources: InputSourceWithDocument[];
227
- /** Source file location. */
228
- filename?: URL;
229
- /** ID:Token map of all tokens. */
230
- tokens: Record<string, TokenNormalized>;
231
- /** Reports a problem in the code. */
232
- report(descriptor: LintReportDescriptor<MessageIds>): void;
233
- }
234
-
235
- export interface LintRuleMetaData<
236
- MessageIds extends string,
237
- LintRuleOptions extends object | undefined = undefined,
238
- LintRuleDocs = unknown,
239
- > {
240
- /**
241
- * Documentation for the rule
242
- */
243
- docs?: LintRuleDocs & LintRuleMetaDataDocs;
244
- /**
245
- * A map of messages which the rule can report. The key is the messageId, and
246
- * the string is the parameterized error string.
247
- */
248
- messages?: Record<MessageIds, string>;
249
- /**
250
- * Specifies default options for the rule. If present, any user-provided
251
- * options in their config will be merged on top of them recursively. This
252
- * merging will be applied directly to `context.options`.
253
- */
254
- defaultOptions?: LintRuleOptions;
255
- }
256
-
257
- export interface LintRuleMetaDataDocs {
258
- /** Concise description of the rule. */
259
- description: string;
260
- /** The URL of the rule's docs. */
261
- url?: string;
262
- }
263
-
264
- export interface OutputFile {
265
- /** Filename, relative to outDir */
266
- filename: string;
267
- /** File contents */
268
- contents: string | Buffer;
269
- /** Plugin name that generated the file */
270
- plugin?: string;
271
- /** Time taken to generate file */
272
- time?: number;
273
- }
274
-
275
- export interface OutputFileExpanded extends OutputFile {
276
- /** The `name` of the plugin that produced this file. */
277
- plugin: string;
278
- /** How long this output took to make. */
279
- time: number;
280
- }
281
-
282
- export interface ParseOptions {
283
- logger?: Logger;
284
- config: ConfigInit;
285
- /**
286
- * Handle requests to loading remote files, either from a remote URL or on the filesystem.
287
- * - Remote requests will have an "https:' protocol
288
- * - Filesystem files will have a "file:" protocol
289
- */
290
- req?: (src: URL, origin: URL) => Promise<string>;
291
- /**
292
- * Skip lint step
293
- * @default false
294
- */
295
- skipLint?: boolean;
296
- /**
297
- * Continue on error? (Useful for `tz check`)
298
- * @default false
299
- */
300
- continueOnError?: boolean;
301
- /** Provide yamlToMomoa module to parse YAML (by default, this isn’t shipped to cut down on package weight) */
302
- yamlToMomoa?: typeof ytm;
303
- /**
304
- * Transform API
305
- * @see https://terrazzo.app/docs/api/js#transform-api
306
- */
307
- transform?: TransformVisitors;
308
- /** (internal cache; do not use) */
309
- _sources?: Record<string, InputSourceWithDocument>;
310
- }
311
-
312
- export interface Plugin {
313
- name: string;
314
- /** Read config, and optionally modify */
315
- // biome-ignore lint/suspicious/noConfusingVoidType format: this helps plugins be a little looser on their typing
316
- config?(config: ConfigInit, context: PluginHookContext): void | ConfigInit | undefined;
317
- /**
318
- * Declare:
319
- * - `"pre"`: run this plugin BEFORE all others
320
- * - `"post"`: run this plugin AFTER all others
321
- * - (default) run this plugin in default order (array order)
322
- */
323
- enforce?: 'pre' | 'post';
324
- /** Throw lint errors/warnings */
325
- lint?(): Record<string, LintRule<any, any, any>>;
326
- transform?(options: TransformHookOptions): Promise<void>;
327
- build?(options: BuildHookOptions): Promise<void>;
328
- buildEnd?(options: BuildEndHookOptions): Promise<void>;
329
- }
330
-
331
- export interface ReferenceObject {
332
- $ref: string;
333
- }
334
-
335
- export interface Resolver<
336
- Inputs extends Record<string, string[]> = Record<string, string[]>,
337
- Input = Record<keyof Inputs, Inputs[keyof Inputs][number]>,
338
- > {
339
- /** Supply values to modifiers to produce a final tokens set */
340
- apply: (input: Partial<Input>) => TokenNormalizedSet;
341
- /** List all possible valid input combinations. Ignores default values, as they would duplicate some other permutations. */
342
- listPermutations: () => Input[];
343
- /** The original resolver document, simplified */
344
- source: ResolverSourceNormalized;
345
- /** Helper function for permutations—see if a particular input is valid. Automatically applies default values. */
346
- isValidInput: (input: Input) => boolean;
347
- }
348
-
349
- export interface ResolverSource {
350
- /** Human-friendly name of this resolver */
351
- name?: string;
352
- /** DTCG version */
353
- version: '2025.10';
354
- /** Description of this resolver */
355
- description?: string;
356
- /** Mapping of sets */
357
- sets?: Record<string, ResolverSet>;
358
- /** Mapping of modifiers */
359
- modifiers?: Record<string, ResolverModifier>;
360
- resolutionOrder: (ResolverSetInline | ResolverModifierInline | ReferenceObject)[];
361
- $extensions?: Record<string, unknown>;
362
- $defs?: Record<string, unknown>;
363
- }
364
-
365
- /** Resolver where all tokens are loaded and flattened in-memory, so only the final merging is left */
366
- export interface ResolverSourceNormalized {
367
- name: string | undefined;
368
- version: '2025.10';
369
- description: string | undefined;
370
- sets: Record<string, ResolverSet> | undefined;
371
- modifiers: Record<string, ResolverModifier> | undefined;
372
- /**
373
- * Array of all sets and modifiers that have been converted to inline,
374
- * regardless of original declaration. In a normalized resolver, only a single
375
- * pass over the resolutionOrder array is needed.
376
- */
377
- resolutionOrder: (ResolverSetNormalized | ResolverModifierNormalized)[];
378
- _source: {
379
- filename?: URL;
380
- document: momoa.DocumentNode;
381
- };
382
- }
383
-
384
- export interface ResolverModifier<Context extends string = string> {
385
- description?: string;
386
- contexts: Record<Context, (Group | ReferenceObject)[]>;
387
- default?: Context;
388
- $extensions?: Record<string, unknown>;
389
- $defs?: Record<string, unknown>;
390
- }
391
-
392
- export type ResolverModifierInline<Context extends string = string> = ResolverModifier<Context> & {
393
- name: string;
394
- type: 'modifier';
395
- };
396
-
397
- export interface ResolverModifierNormalized {
398
- name: string;
399
- type: 'modifier';
400
- description: string | undefined;
401
- contexts: Record<string, Group[]>;
402
- default: string | undefined;
403
- $extensions: Record<string, unknown> | undefined;
404
- $defs: Record<string, unknown> | undefined;
405
- }
406
-
407
- export interface ResolverSet {
408
- description?: string;
409
- sources: (Group | ReferenceObject)[];
410
- $extensions?: Record<string, unknown>;
411
- $defs?: Record<string, unknown>;
412
- }
413
-
414
- export type ResolverSetInline = ResolverSet & { name: string; type: 'set' };
415
-
416
- export interface ResolverSetNormalized {
417
- name: string;
418
- type: 'set';
419
- description: string | undefined;
420
- sources: Group[];
421
- $extensions: Record<string, unknown> | undefined;
422
- $defs: Record<string, unknown> | undefined;
423
- }
424
-
425
- export interface TransformParams {
426
- /** ID of an existing format */
427
- format: string;
428
- /** Glob of tokens to select (e.g. `"color.*"` to select all tokens starting with `"color."`) */
429
- id?: string | string[];
430
- /** $type(s) to filter for */
431
- $type?: string | string[];
432
- /**
433
- * Mode name, if selecting a mode
434
- * @default "."
435
- */
436
- mode?: string | string[];
437
- }
438
-
439
- export interface TransformHookOptions {
440
- /** Plugin hook context (provides access to shared logger) */
441
- context: PluginHookContext;
442
- /** Map of tokens */
443
- tokens: Record<string, TokenNormalized>;
444
- /** Query transformed values */
445
- getTransforms(params: TransformParams): TokenTransformed[];
446
- /** Update transformed values */
447
- setTransform(
448
- id: string,
449
- params: {
450
- format: string;
451
- localID?: string;
452
- value: string | Record<string, string>; // allow looser type for input (`undefined` will just get stripped)
453
- mode?: string;
454
- meta?: TokenTransformedBase['meta'];
455
- },
456
- ): void;
457
- /** Resolver */
458
- resolver: Resolver;
459
- /** Momoa documents */
460
- sources: InputSourceWithDocument[];
461
- }
462
-
463
- export interface RefMapEntry {
464
- filename: string;
465
- refChain: string[];
466
- }
467
-
468
- export type RefMap = Record<string, RefMapEntry>;