@terrazzo/parser 0.1.3 → 0.2.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 (118) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +17 -0
  3. package/CONTRIBUTING.md +0 -12
  4. package/dist/build/index.d.ts +19 -0
  5. package/dist/build/index.js +165 -0
  6. package/dist/build/index.js.map +1 -0
  7. package/dist/config.d.ts +7 -0
  8. package/dist/config.js +269 -0
  9. package/dist/config.js.map +1 -0
  10. package/{index.d.ts → dist/index.d.ts} +1 -5
  11. package/dist/index.js +13 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/lib/code-frame.d.ts +30 -0
  14. package/dist/lib/code-frame.js +108 -0
  15. package/dist/lib/code-frame.js.map +1 -0
  16. package/dist/lint/index.d.ts +11 -0
  17. package/dist/lint/index.js +102 -0
  18. package/dist/lint/index.js.map +1 -0
  19. package/dist/lint/plugin-core/index.d.ts +12 -0
  20. package/dist/lint/plugin-core/index.js +40 -0
  21. package/dist/lint/plugin-core/index.js.map +1 -0
  22. package/dist/lint/plugin-core/lib/docs.d.ts +1 -0
  23. package/dist/lint/plugin-core/lib/docs.js +4 -0
  24. package/dist/lint/plugin-core/lib/docs.js.map +1 -0
  25. package/dist/lint/plugin-core/rules/a11y-min-contrast.d.ts +39 -0
  26. package/dist/lint/plugin-core/rules/a11y-min-contrast.js +58 -0
  27. package/dist/lint/plugin-core/rules/a11y-min-contrast.js.map +1 -0
  28. package/dist/lint/plugin-core/rules/a11y-min-font-size.d.ts +13 -0
  29. package/dist/lint/plugin-core/rules/a11y-min-font-size.js +45 -0
  30. package/dist/lint/plugin-core/rules/a11y-min-font-size.js.map +1 -0
  31. package/dist/lint/plugin-core/rules/colorspace.d.ts +14 -0
  32. package/dist/lint/plugin-core/rules/colorspace.js +85 -0
  33. package/dist/lint/plugin-core/rules/colorspace.js.map +1 -0
  34. package/dist/lint/plugin-core/rules/consistent-naming.d.ts +11 -0
  35. package/dist/lint/plugin-core/rules/consistent-naming.js +49 -0
  36. package/dist/lint/plugin-core/rules/consistent-naming.js.map +1 -0
  37. package/dist/lint/plugin-core/rules/descriptions.d.ts +9 -0
  38. package/dist/lint/plugin-core/rules/descriptions.js +32 -0
  39. package/dist/lint/plugin-core/rules/descriptions.js.map +1 -0
  40. package/dist/lint/plugin-core/rules/duplicate-values.d.ts +9 -0
  41. package/dist/lint/plugin-core/rules/duplicate-values.js +65 -0
  42. package/dist/lint/plugin-core/rules/duplicate-values.js.map +1 -0
  43. package/dist/lint/plugin-core/rules/max-gamut.d.ts +14 -0
  44. package/dist/lint/plugin-core/rules/max-gamut.js +101 -0
  45. package/dist/lint/plugin-core/rules/max-gamut.js.map +1 -0
  46. package/dist/lint/plugin-core/rules/required-children.d.ts +18 -0
  47. package/dist/lint/plugin-core/rules/required-children.js +78 -0
  48. package/dist/lint/plugin-core/rules/required-children.js.map +1 -0
  49. package/dist/lint/plugin-core/rules/required-modes.d.ts +13 -0
  50. package/dist/lint/plugin-core/rules/required-modes.js +52 -0
  51. package/dist/lint/plugin-core/rules/required-modes.js.map +1 -0
  52. package/dist/lint/plugin-core/rules/required-typography-properties.d.ts +10 -0
  53. package/dist/lint/plugin-core/rules/required-typography-properties.js +38 -0
  54. package/dist/lint/plugin-core/rules/required-typography-properties.js.map +1 -0
  55. package/dist/logger.d.ts +76 -0
  56. package/dist/logger.js +123 -0
  57. package/dist/logger.js.map +1 -0
  58. package/dist/parse/alias.d.ts +51 -0
  59. package/dist/parse/alias.js +188 -0
  60. package/dist/parse/alias.js.map +1 -0
  61. package/dist/parse/index.d.ts +27 -0
  62. package/dist/parse/index.js +379 -0
  63. package/dist/parse/index.js.map +1 -0
  64. package/dist/parse/json.d.ts +36 -0
  65. package/dist/parse/json.js +88 -0
  66. package/dist/parse/json.js.map +1 -0
  67. package/dist/parse/normalize.d.ts +23 -0
  68. package/dist/parse/normalize.js +163 -0
  69. package/dist/parse/normalize.js.map +1 -0
  70. package/dist/parse/validate.d.ts +45 -0
  71. package/dist/parse/validate.js +601 -0
  72. package/dist/parse/validate.js.map +1 -0
  73. package/dist/types.d.ts +264 -0
  74. package/dist/types.js +2 -0
  75. package/dist/types.js.map +1 -0
  76. package/package.json +7 -7
  77. package/{build/index.js → src/build/index.ts} +47 -63
  78. package/src/config.ts +280 -0
  79. package/src/index.ts +18 -0
  80. package/{lib/code-frame.js → src/lib/code-frame.ts} +41 -8
  81. package/src/lint/index.ts +135 -0
  82. package/src/lint/plugin-core/index.ts +47 -0
  83. package/src/lint/plugin-core/lib/docs.ts +3 -0
  84. package/src/lint/plugin-core/rules/a11y-min-contrast.ts +91 -0
  85. package/src/lint/plugin-core/rules/a11y-min-font-size.ts +64 -0
  86. package/src/lint/plugin-core/rules/colorspace.ts +101 -0
  87. package/src/lint/plugin-core/rules/consistent-naming.ts +65 -0
  88. package/src/lint/plugin-core/rules/descriptions.ts +41 -0
  89. package/src/lint/plugin-core/rules/duplicate-values.ts +80 -0
  90. package/src/lint/plugin-core/rules/max-gamut.ts +121 -0
  91. package/src/lint/plugin-core/rules/required-children.ts +104 -0
  92. package/src/lint/plugin-core/rules/required-modes.ts +71 -0
  93. package/src/lint/plugin-core/rules/required-typography-properties.ts +53 -0
  94. package/{logger.js → src/logger.ts} +55 -16
  95. package/src/parse/alias.ts +224 -0
  96. package/src/parse/index.ts +457 -0
  97. package/src/parse/json.ts +106 -0
  98. package/{parse/normalize.js → src/parse/normalize.ts} +70 -24
  99. package/{parse/validate.js → src/parse/validate.ts} +154 -236
  100. package/src/types.ts +310 -0
  101. package/build/index.d.ts +0 -113
  102. package/config.d.ts +0 -64
  103. package/config.js +0 -206
  104. package/index.js +0 -35
  105. package/lib/code-frame.d.ts +0 -56
  106. package/lint/index.d.ts +0 -44
  107. package/lint/index.js +0 -59
  108. package/lint/plugin-core/index.d.ts +0 -3
  109. package/lint/plugin-core/index.js +0 -12
  110. package/lint/plugin-core/rules/duplicate-values.d.ts +0 -10
  111. package/lint/plugin-core/rules/duplicate-values.js +0 -68
  112. package/logger.d.ts +0 -71
  113. package/parse/index.d.ts +0 -45
  114. package/parse/index.js +0 -592
  115. package/parse/json.d.ts +0 -30
  116. package/parse/json.js +0 -94
  117. package/parse/normalize.d.ts +0 -3
  118. package/parse/validate.d.ts +0 -43
@@ -0,0 +1,64 @@
1
+ import { isTokenMatch } from '@terrazzo/token-tools';
2
+ import type { LintRule } from '../../../types.js';
3
+ import { docsLink } from '../lib/docs.js';
4
+
5
+ export const A11Y_MIN_FONT_SIZE = 'a11y/min-font-size';
6
+
7
+ export interface RuleA11yMinFontSizeOptions {
8
+ /** Minimum font size (pixels) */
9
+ minSizePx?: number;
10
+ /** Minimum font size (rems) */
11
+ minSizeRem?: number;
12
+ /** Token IDs to ignore. Accepts globs. */
13
+ ignore?: string[];
14
+ }
15
+
16
+ export const ERROR_TOO_SMALL = 'TOO_SMALL';
17
+
18
+ const rule: LintRule<typeof ERROR_TOO_SMALL, RuleA11yMinFontSizeOptions> = {
19
+ meta: {
20
+ messages: {
21
+ [ERROR_TOO_SMALL]: '{{ id }} font size too small. Expected minimum of {{ min }}',
22
+ },
23
+ docs: {
24
+ description: 'Enforce font sizes are no smaller than the given value.',
25
+ url: docsLink(A11Y_MIN_FONT_SIZE),
26
+ },
27
+ },
28
+ defaultOptions: {},
29
+ create({ tokens, options, report }) {
30
+ if (!options.minSizePx && !options.minSizeRem) {
31
+ throw new Error('Must specify at least one of minSizePx or minSizeRem');
32
+ }
33
+
34
+ for (const t of Object.values(tokens)) {
35
+ if (options.ignore && isTokenMatch(t.id, options.ignore)) {
36
+ continue;
37
+ }
38
+
39
+ // skip aliases
40
+ if (t.aliasOf) {
41
+ continue;
42
+ }
43
+
44
+ if (t.$type === 'typography' && 'fontSize' in t.$value) {
45
+ const fontSize = t.$value.fontSize!;
46
+
47
+ if (
48
+ (fontSize.unit === 'px' && options.minSizePx && fontSize.value < options.minSizePx) ||
49
+ (fontSize.unit === 'rem' && options.minSizeRem && fontSize.value < options.minSizeRem)
50
+ ) {
51
+ report({
52
+ messageId: ERROR_TOO_SMALL,
53
+ data: {
54
+ id: t.id,
55
+ min: options.minSizePx ? `${options.minSizePx}px` : `${options.minSizeRem}rem`,
56
+ },
57
+ });
58
+ }
59
+ }
60
+ }
61
+ },
62
+ };
63
+
64
+ export default rule;
@@ -0,0 +1,101 @@
1
+ import { type ColorValueNormalized, isTokenMatch } from '@terrazzo/token-tools';
2
+ import type { LintRule } from '../../../types.js';
3
+ import { docsLink } from '../lib/docs.js';
4
+
5
+ export const COLORSPACE = 'core/colorspace';
6
+
7
+ export interface RuleColorspaceOptions {
8
+ colorSpace: ColorValueNormalized['colorSpace'];
9
+ /** (optional) Token IDs to ignore. Supports globs (`*`). */
10
+ ignore?: string[];
11
+ }
12
+
13
+ const ERROR_COLOR = 'COLOR';
14
+ const ERROR_BORDER = 'BORDER';
15
+ const ERROR_GRADIENT = 'GRADIENT';
16
+ const ERROR_SHADOW = 'SHADOW';
17
+
18
+ const rule: LintRule<
19
+ typeof ERROR_COLOR | typeof ERROR_BORDER | typeof ERROR_GRADIENT | typeof ERROR_SHADOW,
20
+ RuleColorspaceOptions
21
+ > = {
22
+ meta: {
23
+ messages: {
24
+ [ERROR_COLOR]: 'Color {{ id }} not in colorspace {{ colorSpace }}',
25
+ [ERROR_BORDER]: 'Border {{ id }} not in colorspace {{ colorSpace }}',
26
+ [ERROR_GRADIENT]: 'Gradient {{ id }} not in colorspace {{ colorSpace }}',
27
+ [ERROR_SHADOW]: 'Shadow {{ id }} not in colorspace {{ colorSpace }}',
28
+ },
29
+ docs: {
30
+ description: 'Enforce that all colors are in a specific colorspace.',
31
+ url: docsLink(COLORSPACE),
32
+ },
33
+ },
34
+ defaultOptions: { colorSpace: 'srgb' },
35
+ create({ tokens, options, report }) {
36
+ if (!options.colorSpace) {
37
+ return;
38
+ }
39
+
40
+ for (const t of Object.values(tokens)) {
41
+ // skip ignored tokens
42
+ if (options?.ignore && isTokenMatch(t.id, options.ignore)) {
43
+ continue;
44
+ }
45
+
46
+ // skip aliases
47
+ if (t.aliasOf) {
48
+ continue;
49
+ }
50
+
51
+ switch (t.$type) {
52
+ case 'color': {
53
+ if (t.$value.colorSpace !== options.colorSpace) {
54
+ report({
55
+ messageId: ERROR_COLOR,
56
+ data: { id: t.id, colorSpace: options.colorSpace },
57
+ node: t.source.node,
58
+ });
59
+ }
60
+ break;
61
+ }
62
+ case 'border': {
63
+ if (!t.partialAliasOf?.color && t.$value.color.colorSpace !== options.colorSpace) {
64
+ report({
65
+ messageId: ERROR_BORDER,
66
+ data: { id: t.id, colorSpace: options.colorSpace },
67
+ node: t.source.node,
68
+ });
69
+ }
70
+ break;
71
+ }
72
+ case 'gradient': {
73
+ for (let stopI = 0; stopI < t.$value.length; stopI++) {
74
+ if (!t.partialAliasOf?.[stopI]?.color && t.$value[stopI]!.color.colorSpace !== options.colorSpace) {
75
+ report({
76
+ messageId: ERROR_GRADIENT,
77
+ data: { id: t.id, colorSpace: options.colorSpace },
78
+ node: t.source.node,
79
+ });
80
+ }
81
+ }
82
+ break;
83
+ }
84
+ case 'shadow': {
85
+ for (let shadowI = 0; shadowI < t.$value.length; shadowI++) {
86
+ if (!t.partialAliasOf?.[shadowI]?.color && t.$value[shadowI]!.color.colorSpace !== options.colorSpace) {
87
+ report({
88
+ messageId: ERROR_SHADOW,
89
+ data: { id: t.id, colorSpace: options.colorSpace },
90
+ node: t.source.node,
91
+ });
92
+ }
93
+ }
94
+ break;
95
+ }
96
+ }
97
+ }
98
+ },
99
+ };
100
+
101
+ export default rule;
@@ -0,0 +1,65 @@
1
+ import { camelCase, kebabCase, pascalCase, snakeCase } from 'scule';
2
+ import type { LintRule } from '../../../types.js';
3
+ import { docsLink } from '../lib/docs.js';
4
+
5
+ export const CONSISTENT_NAMING = 'core/consistent-naming';
6
+ export const ERROR_WRONG_FORMAT = 'ERROR_WRONG_FORMAT';
7
+
8
+ export interface RuleConsistentNamingOptions {
9
+ /** Specify format, or custom naming validator */
10
+ format:
11
+ | 'kebab-case'
12
+ | 'camelCase'
13
+ | 'PascalCase'
14
+ | 'snake_case'
15
+ | 'SCREAMING_SNAKE_CASE'
16
+ | ((tokenID: string) => boolean);
17
+ /** Token IDs to ignore. Supports globs (`*`). */
18
+ ignore?: string[];
19
+ }
20
+
21
+ const rule: LintRule<typeof ERROR_WRONG_FORMAT, RuleConsistentNamingOptions> = {
22
+ meta: {
23
+ messages: {
24
+ [ERROR_WRONG_FORMAT]: '{{ id }} doesn’t match format {{ format }}',
25
+ },
26
+ docs: {
27
+ description: 'Enforce consistent naming for tokens.',
28
+ url: docsLink(CONSISTENT_NAMING),
29
+ },
30
+ },
31
+ defaultOptions: { format: 'kebab-case' },
32
+ create({ tokens, options, report }) {
33
+ const basicFormatter = {
34
+ 'kebab-case': kebabCase,
35
+ camelCase,
36
+ PascalCase: pascalCase,
37
+ snake_case: snakeCase,
38
+ SCREAMING_SNAKE_CASE: (name: string) => snakeCase(name).toLocaleUpperCase(),
39
+ }[String(options.format)];
40
+
41
+ for (const t of Object.values(tokens)) {
42
+ if (basicFormatter) {
43
+ const parts = t.id.split('.');
44
+ if (!parts.every((part) => basicFormatter(part) === part)) {
45
+ report({
46
+ messageId: ERROR_WRONG_FORMAT,
47
+ data: { id: t.id, format: options.format },
48
+ node: t.source.node,
49
+ });
50
+ }
51
+ } else if (typeof options.format === 'function') {
52
+ const result = options.format(t.id);
53
+ if (result) {
54
+ report({
55
+ messageId: ERROR_WRONG_FORMAT,
56
+ data: { id: t.id, format: '(custom)' },
57
+ node: t.source.node,
58
+ });
59
+ }
60
+ }
61
+ }
62
+ },
63
+ };
64
+
65
+ export default rule;
@@ -0,0 +1,41 @@
1
+ import { isTokenMatch } from '@terrazzo/token-tools';
2
+ import type { LintRule } from '../../../types.js';
3
+ import { docsLink } from '../lib/docs.js';
4
+
5
+ export const DESCRIPTIONS = 'core/descriptions';
6
+
7
+ export interface RuleDescriptionsOptions {
8
+ /** Token IDs to ignore. Supports globs (`*`). */
9
+ ignore?: string[];
10
+ }
11
+
12
+ const ERROR_MISSING_DESCRIPTION = 'MISSING_DESCRIPTION';
13
+
14
+ const rule: LintRule<typeof ERROR_MISSING_DESCRIPTION, RuleDescriptionsOptions> = {
15
+ meta: {
16
+ messages: {
17
+ [ERROR_MISSING_DESCRIPTION]: '{{ id }} missing description',
18
+ },
19
+ docs: {
20
+ description: 'Enforce tokens have descriptions.',
21
+ url: docsLink(DESCRIPTIONS),
22
+ },
23
+ },
24
+ defaultOptions: {},
25
+ create({ tokens, options, report }) {
26
+ for (const t of Object.values(tokens)) {
27
+ if (options.ignore && isTokenMatch(t.id, options.ignore)) {
28
+ continue;
29
+ }
30
+ if (!t.$description) {
31
+ report({
32
+ messageId: ERROR_MISSING_DESCRIPTION,
33
+ data: { id: t.id },
34
+ node: t.source.node,
35
+ });
36
+ }
37
+ }
38
+ },
39
+ };
40
+
41
+ export default rule;
@@ -0,0 +1,80 @@
1
+ import { isAlias, isTokenMatch } from '@terrazzo/token-tools';
2
+ import type { LintRule } from '../../../types.js';
3
+ import { docsLink } from '../lib/docs.js';
4
+
5
+ export const DUPLICATE_VALUES = 'core/duplicate-values';
6
+
7
+ export interface RuleDuplicateValueOptions {
8
+ /** Token IDs to ignore. Supports globs (`*`). */
9
+ ignore?: string[];
10
+ }
11
+
12
+ const ERROR_DUPLICATE_VALUE = 'ERROR_DUPLICATE_VALUE';
13
+
14
+ const rule: LintRule<typeof ERROR_DUPLICATE_VALUE, RuleDuplicateValueOptions> = {
15
+ meta: {
16
+ messages: {
17
+ [ERROR_DUPLICATE_VALUE]: '{{ id }} declared a duplicate value',
18
+ },
19
+ docs: {
20
+ description: 'Enforce tokens can’t redeclare the same value (excludes aliases).',
21
+ url: docsLink(DUPLICATE_VALUES),
22
+ },
23
+ },
24
+ defaultOptions: {},
25
+ create({ report, tokens, options }) {
26
+ const values: Record<string, Set<any>> = {};
27
+
28
+ for (const t of Object.values(tokens)) {
29
+ // skip ignored tokens
30
+ if (options.ignore && isTokenMatch(t.id, options.ignore)) {
31
+ continue;
32
+ }
33
+
34
+ if (!values[t.$type]) {
35
+ values[t.$type] = new Set();
36
+ }
37
+
38
+ // primitives: direct comparison is easy
39
+ if (
40
+ t.$type === 'boolean' ||
41
+ t.$type === 'duration' ||
42
+ t.$type === 'fontWeight' ||
43
+ t.$type === 'link' ||
44
+ t.$type === 'number' ||
45
+ t.$type === 'string'
46
+ ) {
47
+ // skip aliases (note: $value will be resolved)
48
+ if (isAlias(t.aliasOf)) {
49
+ continue;
50
+ }
51
+
52
+ if (values[t.$type]?.has(t.$value)) {
53
+ report({
54
+ messageId: ERROR_DUPLICATE_VALUE,
55
+ data: { id: t.id },
56
+ node: t.source.node,
57
+ });
58
+ }
59
+
60
+ values[t.$type]?.add(t.$value);
61
+ } else {
62
+ // everything else: use deepEqual
63
+ for (const v of values[t.$type]!.values() ?? []) {
64
+ // TODO: don’t JSON.stringify
65
+ if (JSON.stringify(t.$value) === JSON.stringify(v)) {
66
+ report({
67
+ messageId: ERROR_DUPLICATE_VALUE,
68
+ data: { id: t.id },
69
+ node: t.source.node,
70
+ });
71
+ break;
72
+ }
73
+ }
74
+ values[t.$type]!.add(t.$value);
75
+ }
76
+ }
77
+ },
78
+ };
79
+
80
+ export default rule;
@@ -0,0 +1,121 @@
1
+ import { type ColorValueNormalized, isTokenMatch, tokenToCulori } from '@terrazzo/token-tools';
2
+ import { type Color, clampChroma } from 'culori';
3
+ import type { LintRule } from '../../../types.js';
4
+ import { docsLink } from '../lib/docs.js';
5
+
6
+ export const MAX_GAMUT = 'core/max-gamut';
7
+
8
+ export interface RuleMaxGamutOptions {
9
+ /** Gamut to constrain color tokens to. */
10
+ gamut: 'srgb' | 'p3' | 'rec2020';
11
+ /** (optional) Token IDs to ignore. Supports globs (`*`). */
12
+ ignore?: string[];
13
+ }
14
+
15
+ const TOLERANCE = 0.000001; // threshold above which it counts as an error (take rounding errors into account)
16
+
17
+ /** is a Culori-parseable color within the specified gamut? */
18
+ function isWithinGamut(color: ColorValueNormalized, gamut: RuleMaxGamutOptions['gamut']): boolean {
19
+ const parsed = tokenToCulori(color);
20
+ if (!parsed) {
21
+ return false;
22
+ }
23
+ if (['rgb', 'hsl', 'hsv', 'hwb'].includes(parsed.mode)) {
24
+ return true;
25
+ }
26
+ const clamped = clampChroma(parsed, parsed.mode, gamut === 'srgb' ? 'rgb' : gamut);
27
+ return isWithinThreshold(parsed, clamped);
28
+ }
29
+
30
+ /** is Color A close enough to Color B? */
31
+ function isWithinThreshold(a: Color, b: Color, tolerance = TOLERANCE) {
32
+ for (const k in a) {
33
+ if (k === 'mode' || k === 'alpha') {
34
+ continue;
35
+ }
36
+ if (!(k in b)) {
37
+ throw new Error(`Can’t compare ${a.mode} to ${b.mode}`);
38
+ }
39
+ if (Math.abs((a as any)[k] - (b as any)[k]) > tolerance) {
40
+ return false;
41
+ }
42
+ }
43
+ return true;
44
+ }
45
+
46
+ const ERROR_COLOR = 'COLOR';
47
+ const ERROR_BORDER = 'BORDER';
48
+ const ERROR_GRADIENT = 'GRADIENT';
49
+ const ERROR_SHADOW = 'SHADOW';
50
+
51
+ const rule: LintRule<
52
+ typeof ERROR_COLOR | typeof ERROR_BORDER | typeof ERROR_GRADIENT | typeof ERROR_SHADOW,
53
+ RuleMaxGamutOptions
54
+ > = {
55
+ meta: {
56
+ messages: {
57
+ [ERROR_COLOR]: 'Color {{ id }} is outside {{ gamut }} gamut',
58
+ [ERROR_BORDER]: 'Border {{ id }} is outside {{ gamut }} gamut',
59
+ [ERROR_GRADIENT]: 'Gradient {{ id }} is outside {{ gamut }} gamut',
60
+ [ERROR_SHADOW]: 'Shadow {{ id }} is outside {{ gamut }} gamut',
61
+ },
62
+ docs: {
63
+ description: 'Enforce colors are within the specified gamut.',
64
+ url: docsLink(MAX_GAMUT),
65
+ },
66
+ },
67
+ defaultOptions: { gamut: 'rec2020' },
68
+ create({ tokens, options, report }) {
69
+ if (!options?.gamut) {
70
+ return;
71
+ }
72
+ if (options.gamut !== 'srgb' && options.gamut !== 'p3' && options.gamut !== 'rec2020') {
73
+ throw new Error(`Unknown gamut "${options.gamut}". Options are "srgb", "p3", or "rec2020"`);
74
+ }
75
+
76
+ for (const t of Object.values(tokens)) {
77
+ // skip ignored tokens
78
+ if (options.ignore && isTokenMatch(t.id, options.ignore)) {
79
+ continue;
80
+ }
81
+
82
+ // skip aliases
83
+ if (t.aliasOf) {
84
+ continue;
85
+ }
86
+
87
+ switch (t.$type) {
88
+ case 'color': {
89
+ if (!isWithinGamut(t.$value, options.gamut)) {
90
+ report({ messageId: ERROR_COLOR, data: { id: t.id, gamut: options.gamut }, node: t.source.node });
91
+ }
92
+ break;
93
+ }
94
+ case 'border': {
95
+ if (!t.partialAliasOf?.color && !isWithinGamut(t.$value.color, options.gamut)) {
96
+ report({ messageId: ERROR_BORDER, data: { id: t.id, gamut: options.gamut }, node: t.source.node });
97
+ }
98
+ break;
99
+ }
100
+ case 'gradient': {
101
+ for (let stopI = 0; stopI < t.$value.length; stopI++) {
102
+ if (!t.partialAliasOf?.[stopI]?.color && !isWithinGamut(t.$value[stopI]!.color, options.gamut)) {
103
+ report({ messageId: ERROR_GRADIENT, data: { id: t.id, gamut: options.gamut }, node: t.source.node });
104
+ }
105
+ }
106
+ break;
107
+ }
108
+ case 'shadow': {
109
+ for (let shadowI = 0; shadowI < t.$value.length; shadowI++) {
110
+ if (!t.partialAliasOf?.[shadowI]?.color && !isWithinGamut(t.$value[shadowI]!.color, options.gamut)) {
111
+ report({ messageId: ERROR_SHADOW, data: { id: t.id, gamut: options.gamut }, node: t.source.node });
112
+ }
113
+ }
114
+ break;
115
+ }
116
+ }
117
+ }
118
+ },
119
+ };
120
+
121
+ export default rule;
@@ -0,0 +1,104 @@
1
+ import { isTokenMatch } from '@terrazzo/token-tools';
2
+ import type { LintRule } from '../../../types.js';
3
+ import { docsLink } from '../lib/docs.js';
4
+
5
+ export const REQUIRED_CHILDREN = 'core/required-children';
6
+
7
+ export interface RequiredChildrenMatch {
8
+ /** Glob of tokens/groups to match */
9
+ match: string[];
10
+ /** Required token IDs to match (this only looks at the very last segment of a token ID!) */
11
+ requiredTokens?: string[];
12
+ /** Required groups to match (this only looks at the beginning/middle segments of a token ID!) */
13
+ requiredGroups?: string[];
14
+ }
15
+
16
+ export interface RuleRequiredChildrenOptions {
17
+ matches: RequiredChildrenMatch[];
18
+ }
19
+
20
+ export const ERROR_EMPTY_MATCH = 'EMPTY_MATCH';
21
+ export const ERROR_MISSING_REQUIRED_TOKENS = 'MISSING_REQUIRED_TOKENS';
22
+ export const ERROR_MISSING_REQUIRED_GROUP = 'MISSING_REQUIRED_GROUP';
23
+
24
+ const rule: LintRule<
25
+ typeof ERROR_EMPTY_MATCH | typeof ERROR_MISSING_REQUIRED_TOKENS | typeof ERROR_MISSING_REQUIRED_GROUP,
26
+ RuleRequiredChildrenOptions
27
+ > = {
28
+ meta: {
29
+ messages: {
30
+ [ERROR_EMPTY_MATCH]: 'No tokens matched {{ matcher }}',
31
+ [ERROR_MISSING_REQUIRED_TOKENS]: 'Match {{ index }}: some groups missing required token "{{ token }}"',
32
+ [ERROR_MISSING_REQUIRED_GROUP]: 'Match {{ index }}: some tokens missing required group "{{ group }}"',
33
+ },
34
+ docs: {
35
+ description: 'Enforce token groups have specific children, whether tokens and/or groups.',
36
+ url: docsLink(REQUIRED_CHILDREN),
37
+ },
38
+ },
39
+ defaultOptions: { matches: [] },
40
+ create({ tokens, options, report }) {
41
+ if (!options.matches?.length) {
42
+ throw new Error('Invalid config. Missing `matches: […]`');
43
+ }
44
+
45
+ // note: in many other rules, the operation can be completed in one iteration through all tokens
46
+ // in this rule, however, we have to scan all tokens every time per-match, because they may overlap
47
+
48
+ for (let matchI = 0; matchI < options.matches.length; matchI++) {
49
+ const { match, requiredTokens, requiredGroups } = options.matches[matchI]!;
50
+
51
+ // validate
52
+ if (!match.length) {
53
+ throw new Error(`Match ${matchI}: must declare \`match: […]\``);
54
+ }
55
+ if (!requiredTokens?.length && !requiredGroups?.length) {
56
+ throw new Error(`Match ${matchI}: must declare either \`requiredTokens: […]\` or \`requiredGroups: […]\``);
57
+ }
58
+
59
+ const matchGroups: string[] = [];
60
+ const matchTokens: string[] = [];
61
+ let tokensMatched = false;
62
+ for (const t of Object.values(tokens)) {
63
+ if (!isTokenMatch(t.id, match)) {
64
+ continue;
65
+ }
66
+ tokensMatched = true;
67
+ const groups = t.id.split('.');
68
+ matchTokens.push(groups.pop()!);
69
+ matchGroups.push(...groups);
70
+ }
71
+
72
+ if (!tokensMatched) {
73
+ report({
74
+ messageId: ERROR_EMPTY_MATCH,
75
+ data: { matcher: JSON.stringify(match) },
76
+ });
77
+ continue;
78
+ }
79
+
80
+ if (requiredTokens) {
81
+ for (const id of requiredTokens) {
82
+ if (!matchTokens.includes(id)) {
83
+ report({
84
+ messageId: ERROR_MISSING_REQUIRED_TOKENS,
85
+ data: { index: matchI, token: id },
86
+ });
87
+ }
88
+ }
89
+ }
90
+ if (requiredGroups) {
91
+ for (const groupName of requiredGroups) {
92
+ if (!matchGroups.includes(groupName)) {
93
+ report({
94
+ messageId: ERROR_MISSING_REQUIRED_GROUP,
95
+ data: { index: matchI, group: groupName },
96
+ });
97
+ }
98
+ }
99
+ }
100
+ }
101
+ },
102
+ };
103
+
104
+ export default rule;
@@ -0,0 +1,71 @@
1
+ import { isTokenMatch } from '@terrazzo/token-tools';
2
+ import type { LintRule } from '../../../types.js';
3
+ import { docsLink } from '../lib/docs.js';
4
+
5
+ export const REQUIRED_MODES = 'core/required-modes';
6
+
7
+ export type RequiredModesMatch = {
8
+ /** Glob of tokens/groups to match */
9
+ match: string[];
10
+ /** Required modes */
11
+ modes: string[];
12
+ };
13
+
14
+ export interface RuleRequiredModesOptions {
15
+ matches: RequiredModesMatch[];
16
+ }
17
+
18
+ const rule: LintRule<never, RuleRequiredModesOptions> = {
19
+ meta: {
20
+ docs: {
21
+ description: 'Enforce certain tokens have specific modes.',
22
+ url: docsLink(REQUIRED_MODES),
23
+ },
24
+ },
25
+ defaultOptions: { matches: [] },
26
+ create({ tokens, options, report }) {
27
+ if (!options?.matches?.length) {
28
+ throw new Error('Invalid config. Missing `matches: […]`');
29
+ }
30
+
31
+ // note: in many other rules, the operation can be completed in one iteration through all tokens
32
+ // in this rule, however, we have to scan all tokens every time per-match, because they may overlap
33
+ for (let matchI = 0; matchI < options.matches.length; matchI++) {
34
+ const { match, modes } = options.matches[matchI]!;
35
+
36
+ // validate
37
+ if (!match.length) {
38
+ throw new Error(`Match ${matchI}: must declare \`match: […]\``);
39
+ }
40
+ if (!modes?.length) {
41
+ throw new Error(`Match ${matchI}: must declare \`modes: […]\``);
42
+ }
43
+
44
+ let tokensMatched = false;
45
+ for (const t of Object.values(tokens)) {
46
+ if (!isTokenMatch(t.id, match)) {
47
+ continue;
48
+ }
49
+ tokensMatched = true;
50
+
51
+ for (const mode of modes) {
52
+ if (!t.mode?.[mode]) {
53
+ report({
54
+ message: `Token ${t.id}: missing required mode "${mode}"`,
55
+ node: t.source.node,
56
+ });
57
+ }
58
+ }
59
+
60
+ if (!tokensMatched) {
61
+ report({
62
+ message: `Match "${matchI}": no tokens matched ${JSON.stringify(match)}`,
63
+ node: t.source.node,
64
+ });
65
+ }
66
+ }
67
+ }
68
+ },
69
+ };
70
+
71
+ export default rule;