@terrazzo/parser 0.1.3 → 0.2.1
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.
- package/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +23 -0
- package/CONTRIBUTING.md +0 -12
- package/dist/build/index.d.ts +19 -0
- package/dist/build/index.js +165 -0
- package/dist/build/index.js.map +1 -0
- package/dist/config.d.ts +7 -0
- package/dist/config.js +269 -0
- package/dist/config.js.map +1 -0
- package/{index.d.ts → dist/index.d.ts} +1 -5
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/code-frame.d.ts +30 -0
- package/dist/lib/code-frame.js +108 -0
- package/dist/lib/code-frame.js.map +1 -0
- package/dist/lint/index.d.ts +11 -0
- package/dist/lint/index.js +102 -0
- package/dist/lint/index.js.map +1 -0
- package/dist/lint/plugin-core/index.d.ts +12 -0
- package/dist/lint/plugin-core/index.js +40 -0
- package/dist/lint/plugin-core/index.js.map +1 -0
- package/dist/lint/plugin-core/lib/docs.d.ts +1 -0
- package/dist/lint/plugin-core/lib/docs.js +4 -0
- package/dist/lint/plugin-core/lib/docs.js.map +1 -0
- package/dist/lint/plugin-core/rules/a11y-min-contrast.d.ts +39 -0
- package/dist/lint/plugin-core/rules/a11y-min-contrast.js +58 -0
- package/dist/lint/plugin-core/rules/a11y-min-contrast.js.map +1 -0
- package/dist/lint/plugin-core/rules/a11y-min-font-size.d.ts +13 -0
- package/dist/lint/plugin-core/rules/a11y-min-font-size.js +45 -0
- package/dist/lint/plugin-core/rules/a11y-min-font-size.js.map +1 -0
- package/dist/lint/plugin-core/rules/colorspace.d.ts +14 -0
- package/dist/lint/plugin-core/rules/colorspace.js +85 -0
- package/dist/lint/plugin-core/rules/colorspace.js.map +1 -0
- package/dist/lint/plugin-core/rules/consistent-naming.d.ts +11 -0
- package/dist/lint/plugin-core/rules/consistent-naming.js +49 -0
- package/dist/lint/plugin-core/rules/consistent-naming.js.map +1 -0
- package/dist/lint/plugin-core/rules/descriptions.d.ts +9 -0
- package/dist/lint/plugin-core/rules/descriptions.js +32 -0
- package/dist/lint/plugin-core/rules/descriptions.js.map +1 -0
- package/dist/lint/plugin-core/rules/duplicate-values.d.ts +9 -0
- package/dist/lint/plugin-core/rules/duplicate-values.js +65 -0
- package/dist/lint/plugin-core/rules/duplicate-values.js.map +1 -0
- package/dist/lint/plugin-core/rules/max-gamut.d.ts +14 -0
- package/dist/lint/plugin-core/rules/max-gamut.js +101 -0
- package/dist/lint/plugin-core/rules/max-gamut.js.map +1 -0
- package/dist/lint/plugin-core/rules/required-children.d.ts +18 -0
- package/dist/lint/plugin-core/rules/required-children.js +78 -0
- package/dist/lint/plugin-core/rules/required-children.js.map +1 -0
- package/dist/lint/plugin-core/rules/required-modes.d.ts +13 -0
- package/dist/lint/plugin-core/rules/required-modes.js +52 -0
- package/dist/lint/plugin-core/rules/required-modes.js.map +1 -0
- package/dist/lint/plugin-core/rules/required-typography-properties.d.ts +10 -0
- package/dist/lint/plugin-core/rules/required-typography-properties.js +38 -0
- package/dist/lint/plugin-core/rules/required-typography-properties.js.map +1 -0
- package/dist/logger.d.ts +76 -0
- package/dist/logger.js +123 -0
- package/dist/logger.js.map +1 -0
- package/dist/parse/alias.d.ts +51 -0
- package/dist/parse/alias.js +188 -0
- package/dist/parse/alias.js.map +1 -0
- package/dist/parse/index.d.ts +27 -0
- package/dist/parse/index.js +379 -0
- package/dist/parse/index.js.map +1 -0
- package/dist/parse/json.d.ts +36 -0
- package/dist/parse/json.js +88 -0
- package/dist/parse/json.js.map +1 -0
- package/dist/parse/normalize.d.ts +23 -0
- package/dist/parse/normalize.js +163 -0
- package/dist/parse/normalize.js.map +1 -0
- package/dist/parse/validate.d.ts +45 -0
- package/dist/parse/validate.js +601 -0
- package/dist/parse/validate.js.map +1 -0
- package/dist/types.d.ts +264 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +7 -7
- package/{build/index.js → src/build/index.ts} +47 -63
- package/src/config.ts +280 -0
- package/src/index.ts +18 -0
- package/{lib/code-frame.js → src/lib/code-frame.ts} +41 -8
- package/src/lint/index.ts +135 -0
- package/src/lint/plugin-core/index.ts +47 -0
- package/src/lint/plugin-core/lib/docs.ts +3 -0
- package/src/lint/plugin-core/rules/a11y-min-contrast.ts +91 -0
- package/src/lint/plugin-core/rules/a11y-min-font-size.ts +64 -0
- package/src/lint/plugin-core/rules/colorspace.ts +101 -0
- package/src/lint/plugin-core/rules/consistent-naming.ts +65 -0
- package/src/lint/plugin-core/rules/descriptions.ts +41 -0
- package/src/lint/plugin-core/rules/duplicate-values.ts +80 -0
- package/src/lint/plugin-core/rules/max-gamut.ts +121 -0
- package/src/lint/plugin-core/rules/required-children.ts +104 -0
- package/src/lint/plugin-core/rules/required-modes.ts +71 -0
- package/src/lint/plugin-core/rules/required-typography-properties.ts +53 -0
- package/{logger.js → src/logger.ts} +55 -16
- package/src/parse/alias.ts +224 -0
- package/src/parse/index.ts +457 -0
- package/src/parse/json.ts +106 -0
- package/{parse/normalize.js → src/parse/normalize.ts} +70 -24
- package/{parse/validate.js → src/parse/validate.ts} +154 -236
- package/src/types.ts +310 -0
- package/build/index.d.ts +0 -113
- package/config.d.ts +0 -64
- package/config.js +0 -206
- package/index.js +0 -35
- package/lib/code-frame.d.ts +0 -56
- package/lint/index.d.ts +0 -44
- package/lint/index.js +0 -59
- package/lint/plugin-core/index.d.ts +0 -3
- package/lint/plugin-core/index.js +0 -12
- package/lint/plugin-core/rules/duplicate-values.d.ts +0 -10
- package/lint/plugin-core/rules/duplicate-values.js +0 -68
- package/logger.d.ts +0 -71
- package/parse/index.d.ts +0 -45
- package/parse/index.js +0 -592
- package/parse/json.d.ts +0 -30
- package/parse/json.js +0 -94
- package/parse/normalize.d.ts +0 -3
- package/parse/validate.d.ts +0 -43
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
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];
|