@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
@@ -1,4 +1,15 @@
1
- import { isAlias, parseColor } from '@terrazzo/token-tools';
1
+ import {
2
+ type CubicBezierValue,
3
+ type DimensionValue,
4
+ type GradientStopNormalized,
5
+ type GradientValueNormalized,
6
+ type ShadowValueNormalized,
7
+ type Token,
8
+ type TransitionValue,
9
+ type TypographyValueNormalized,
10
+ isAlias,
11
+ parseColor,
12
+ } from '@terrazzo/token-tools';
2
13
 
3
14
  export const FONT_WEIGHT_MAP = {
4
15
  thin: 100,
@@ -21,9 +32,13 @@ export const FONT_WEIGHT_MAP = {
21
32
  'ultra-black': 950,
22
33
  };
23
34
 
35
+ // Note: because we’re handling a lot of input values, the type inference gets lost.
36
+ // This file is expected to have a lot of `@ts-ignore` comments.
37
+
24
38
  const NUMBER_WITH_UNIT_RE = /(-?\d*\.?\d+)(.*)/;
25
39
 
26
- export default function normalizeValue(token) {
40
+ /** Fill in defaults, and return predictable shapes for tokens */
41
+ export default function normalizeValue<T extends Token>(token: T): T['$value'] {
27
42
  if (isAlias(token.$value)) {
28
43
  return token.$value;
29
44
  }
@@ -32,6 +47,9 @@ export default function normalizeValue(token) {
32
47
  return !!token.$value;
33
48
  }
34
49
  case 'border': {
50
+ if (typeof token.$value === 'string') {
51
+ return token.$value;
52
+ }
35
53
  return {
36
54
  color: normalizeValue({ $type: 'color', $value: token.$value.color ?? '#000000' }),
37
55
  style: normalizeValue({ $type: 'strokeStyle', $value: token.$value.style ?? 'solid' }),
@@ -45,29 +63,32 @@ export default function normalizeValue(token) {
45
63
  return 'alpha' in token.$value ? token.$value : { ...token.$value, alpha: 1 };
46
64
  }
47
65
  case 'cubicBezier': {
66
+ if (typeof token.$value === 'string') {
67
+ return token.$value;
68
+ }
48
69
  return token.$value.map((value) =>
49
70
  typeof value === 'number' ? normalizeValue({ $type: 'number', $value: value }) : value,
50
- );
71
+ ) as CubicBezierValue;
51
72
  }
52
73
  case 'dimension': {
53
- if (token.$value === 0) {
74
+ if ((token as any).$value === 0) {
54
75
  return { value: 0, unit: 'px' };
55
76
  }
56
77
  // Backwards compat: handle string
57
78
  if (typeof token.$value === 'string') {
58
79
  const match = token.$value.match(NUMBER_WITH_UNIT_RE);
59
- return { value: Number.parseFloat(match?.[1] || token.$value), unit: match[2] || 'px' };
80
+ return { value: Number.parseFloat(match?.[1] || token.$value), unit: match?.[2] || 'px' };
60
81
  }
61
82
  return token.$value;
62
83
  }
63
84
  case 'duration': {
64
- if (token.$value === 0) {
85
+ if ((token as any).$value === 0) {
65
86
  return { value: 0, unit: 'ms' };
66
87
  }
67
88
  // Backwards compat: handle string
68
89
  if (typeof token.$value === 'string') {
69
90
  const match = token.$value.match(NUMBER_WITH_UNIT_RE);
70
- return { value: Number.parseFloat(match?.[1] || token.$value), unit: match[2] || 'ms' };
91
+ return { value: Number.parseFloat(match?.[1] || token.$value), unit: match?.[2] || 'ms' };
71
92
  }
72
93
  return token.$value;
73
94
  }
@@ -75,16 +96,22 @@ export default function normalizeValue(token) {
75
96
  return Array.isArray(token.$value) ? token.$value : [token.$value];
76
97
  }
77
98
  case 'fontWeight': {
78
- if (typeof token.$value === 'string' && FONT_WEIGHT_MAP[token.$value]) {
79
- return FONT_WEIGHT_MAP[token.$value];
99
+ if (typeof token.$value === 'string' && FONT_WEIGHT_MAP[token.$value as keyof typeof FONT_WEIGHT_MAP]) {
100
+ return FONT_WEIGHT_MAP[token.$value as keyof typeof FONT_WEIGHT_MAP];
80
101
  }
81
- return Number.parseInt(token.$value);
102
+ return Math.min(
103
+ 999,
104
+ Math.max(1, typeof token.$value === 'string' ? Number.parseInt(token.$value) : token.$value),
105
+ );
82
106
  }
83
107
  case 'gradient': {
84
- const output = [];
108
+ if (typeof token.$value === 'string') {
109
+ return token.$value;
110
+ }
111
+ const output: GradientValueNormalized = [];
85
112
  for (let i = 0; i < token.$value.length; i++) {
86
- const stop = { ...token.$value[i] };
87
- stop.color = normalizeValue({ $type: 'color', $value: stop.color });
113
+ const stop = { ...(token.$value[i] as GradientStopNormalized) };
114
+ stop.color = normalizeValue({ $type: 'color', $value: stop.color! });
88
115
  if (stop.position === undefined) {
89
116
  stop.position = i / (token.$value.length - 1);
90
117
  }
@@ -96,14 +123,24 @@ export default function normalizeValue(token) {
96
123
  return typeof token.$value === 'number' ? token.$value : Number.parseFloat(token.$value);
97
124
  }
98
125
  case 'shadow': {
99
- return (Array.isArray(token.$value) ? token.$value : [token.$value]).map((layer) => ({
100
- color: normalizeValue({ $type: 'color', $value: layer.color }),
101
- offsetX: normalizeValue({ $type: 'dimension', $value: layer.offsetX ?? 0 }),
102
- offsetY: normalizeValue({ $type: 'dimension', $value: layer.offsetY ?? 0 }),
103
- blur: normalizeValue({ $type: 'dimension', $value: layer.blur ?? 0 }),
104
- spread: normalizeValue({ $type: 'dimension', $value: layer.spread ?? 0 }),
105
- inset: layer.inset === true,
106
- }));
126
+ if (typeof token.$value === 'string') {
127
+ return token.$value;
128
+ }
129
+ return (Array.isArray(token.$value) ? token.$value : [token.$value]).map(
130
+ (layer) =>
131
+ ({
132
+ color: normalizeValue({ $type: 'color', $value: layer.color }),
133
+ // @ts-ignore
134
+ offsetX: normalizeValue({ $type: 'dimension', $value: layer.offsetX ?? 0 }),
135
+ // @ts-ignore
136
+ offsetY: normalizeValue({ $type: 'dimension', $value: layer.offsetY ?? 0 }),
137
+ // @ts-ignore
138
+ blur: normalizeValue({ $type: 'dimension', $value: layer.blur ?? 0 }),
139
+ // @ts-ignore
140
+ spread: normalizeValue({ $type: 'dimension', $value: layer.spread ?? 0 }),
141
+ inset: layer.inset === true,
142
+ }) as ShadowValueNormalized,
143
+ );
107
144
  }
108
145
  case 'strokeStyle': {
109
146
  return token.$value;
@@ -112,19 +149,28 @@ export default function normalizeValue(token) {
112
149
  return String(token.$value);
113
150
  }
114
151
  case 'transition': {
152
+ if (typeof token.$value === 'string') {
153
+ return token.$value;
154
+ }
115
155
  return {
156
+ // @ts-ignore
116
157
  duration: normalizeValue({ $type: 'duration', $value: token.$value.duration ?? 0 }),
158
+ // @ts-ignore
117
159
  delay: normalizeValue({ $type: 'duration', $value: token.$value.delay ?? 0 }),
160
+ // @ts-ignore
118
161
  timingFunction: normalizeValue({ $type: 'cubicBezier', $value: token.$value.timingFunction }),
119
- };
162
+ } as TransitionValue;
120
163
  }
121
164
  case 'typography': {
122
- const output = {};
165
+ if (typeof token.$value === 'string') {
166
+ return token.$value;
167
+ }
168
+ const output: TypographyValueNormalized = {};
123
169
  for (const k in token.$value) {
124
170
  switch (k) {
125
171
  case 'letterSpacing':
126
172
  case 'fontSize':
127
- output[k] = normalizeValue({ $type: 'dimension', $value: token.$value[k] });
173
+ output[k] = normalizeValue({ $type: 'dimension', $value: token.$value[k] as DimensionValue });
128
174
  break;
129
175
  default:
130
176
  output[k] = token.$value[k];