@oxy.so/bloom-language-codemod 0.0.0-stage → 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.
- package/CHANGELOG.md +32 -0
- package/LICENSE +673 -0
- package/README.md +42 -2
- package/lib/cli.d.ts +2 -0
- package/lib/cli.js +140 -0
- package/lib/components.d.ts +207 -0
- package/lib/components.js +824 -0
- package/lib/corpus.d.ts +10 -0
- package/lib/corpus.js +193 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.js +18 -0
- package/lib/overrides.d.ts +7 -0
- package/lib/overrides.js +98 -0
- package/lib/report.d.ts +41 -0
- package/lib/report.js +88 -0
- package/lib/resolve.d.ts +60 -0
- package/lib/resolve.js +483 -0
- package/lib/style.d.ts +68 -0
- package/lib/style.js +475 -0
- package/lib/table.d.ts +41 -0
- package/lib/table.js +96 -0
- package/lib/tailwind.d.ts +24 -0
- package/lib/tailwind.js +112 -0
- package/lib/transform.d.ts +61 -0
- package/lib/transform.js +4454 -0
- package/package.json +62 -4
package/lib/resolve.js
ADDED
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.heldVariantChanges = heldVariantChanges;
|
|
4
|
+
exports.cornersMeet = cornersMeet;
|
|
5
|
+
exports.edgesMeet = edgesMeet;
|
|
6
|
+
exports.splitVariants = splitVariants;
|
|
7
|
+
exports.resolveClassName = resolveClassName;
|
|
8
|
+
/**
|
|
9
|
+
* A `className` resolved to the modifiers of one language view, or the
|
|
10
|
+
* reasons it cannot be: every utility has to land exactly, through the
|
|
11
|
+
* className table, or the element stays JSX.
|
|
12
|
+
*
|
|
13
|
+
* A className styles one host at once; a chain stacks its modifiers in the
|
|
14
|
+
* language's layer order (spec `layers`), so the modifiers are written in that
|
|
15
|
+
* order and the view stays one host. Anything whose meaning depends on the
|
|
16
|
+
* order Tailwind writes its rules in (two paddings of one edge, two colours)
|
|
17
|
+
* is a conflict, never a guess.
|
|
18
|
+
*/
|
|
19
|
+
const node_fs_1 = require("node:fs");
|
|
20
|
+
const node_path_1 = require("node:path");
|
|
21
|
+
const table_1 = require("./table");
|
|
22
|
+
const spec = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(table_1.LANGUAGE_PACKAGE, 'spec/language.json'), 'utf8'));
|
|
23
|
+
const LAYER = new Map();
|
|
24
|
+
spec.layers.forEach((names, index) => {
|
|
25
|
+
for (const name of names)
|
|
26
|
+
LAYER.set(name, index);
|
|
27
|
+
});
|
|
28
|
+
const TRAITS = new Set(spec.environment.traits);
|
|
29
|
+
/** A frame side's Tailwind prefix, as the frame key the language names. */
|
|
30
|
+
const FRAME_PREFIX = {
|
|
31
|
+
w: 'width',
|
|
32
|
+
h: 'height',
|
|
33
|
+
'min-w': 'minWidth',
|
|
34
|
+
'min-h': 'minHeight',
|
|
35
|
+
'max-w': 'maxWidth',
|
|
36
|
+
'max-h': 'maxHeight',
|
|
37
|
+
};
|
|
38
|
+
/** The spec's space tokens by points, a named rung over a numeric token of the same value. */
|
|
39
|
+
const SPACE_BY_POINTS = new Map();
|
|
40
|
+
for (const [name, points] of Object.entries(spec.scales.space ?? {}))
|
|
41
|
+
if (typeof points === 'number' && (!/^\d/.test(name) || !SPACE_BY_POINTS.has(points)))
|
|
42
|
+
SPACE_BY_POINTS.set(points, name);
|
|
43
|
+
/**
|
|
44
|
+
* A frame length of Tailwind's spacing scale that no app writes as a class
|
|
45
|
+
* (so the table has no row for it) but a style writes (`height: 38` is
|
|
46
|
+
* `h-9.5`), whose points are a space token: the frame the table writes for
|
|
47
|
+
* the steps it holds (`h-11` is `.frame({ height: '44' })`).
|
|
48
|
+
*/
|
|
49
|
+
function frameStep(utility) {
|
|
50
|
+
const match = /^(min-w|min-h|max-w|max-h|w|h)-(\d+(?:\.\d+)?)$/.exec(utility);
|
|
51
|
+
const token = match ? SPACE_BY_POINTS.get(Number(match[2]) * 4) : undefined;
|
|
52
|
+
if (!match || token === undefined)
|
|
53
|
+
return undefined;
|
|
54
|
+
const frame = { [FRAME_PREFIX[match[1]]]: token };
|
|
55
|
+
const mods = [{ name: 'frame', args: [frame] }];
|
|
56
|
+
return { status: 'mapped', language: `.frame({ ${Object.keys(frame)[0]}: '${token}' })`, mods };
|
|
57
|
+
}
|
|
58
|
+
/** The faces `.font()` takes beside a size (`mono`, `display`). */
|
|
59
|
+
const FONT_FAMILY = new Set(spec.enums.fontFamily);
|
|
60
|
+
/** The modifiers a `when()` variant can change; any other is left out of it. */
|
|
61
|
+
const VARIES = new Set(spec.variants.modifiers);
|
|
62
|
+
/** Conditions that hold, or not, as the view renders: a platform. */
|
|
63
|
+
const HELD = new Set(['web', 'native', 'ios', 'android']);
|
|
64
|
+
/** The placement modifiers (render.tsx, PLACE_KINDS): a held variant leaves them out too. */
|
|
65
|
+
const PLACEMENT = new Set(['accessibilityOnly']);
|
|
66
|
+
/**
|
|
67
|
+
* Whether a variant under a condition that holds as the view renders (a
|
|
68
|
+
* platform, the app's own boolean) changes `mod`: one a variant varies, or
|
|
69
|
+
* anything but a box layer or a placement, which is then the chain's own
|
|
70
|
+
* (render.tsx, `heldAsOwn`).
|
|
71
|
+
*/
|
|
72
|
+
function heldVariantChanges(mod) {
|
|
73
|
+
return VARIES.has(mod.name) || (!LAYER.has(mod.name) && !PLACEMENT.has(mod.name));
|
|
74
|
+
}
|
|
75
|
+
/** Utilities NativeWind draws on the web only: `tabular-nums` sets a variable alone on native. */
|
|
76
|
+
const WEB_ONLY = new Set(['tabular-nums']);
|
|
77
|
+
const TEXT_ONLY = new Set(spec.nodes.text?.modifiers ?? []);
|
|
78
|
+
const STACK_ONLY = new Set(spec.nodes.stack?.modifiers ?? []);
|
|
79
|
+
/** Ramp steps with a tracking of their own, which a Tailwind text size does not write. */
|
|
80
|
+
const TRACKED = new Set(Object.entries(spec.typeRamp)
|
|
81
|
+
.filter(([, v]) => Array.isArray(v) && v[2] !== 0)
|
|
82
|
+
.map(([k]) => k.replace(/-(regular|medium|semibold|bold)$/, '')));
|
|
83
|
+
/**
|
|
84
|
+
* Table mappings the parity corpus measured drawing otherwise, each with what
|
|
85
|
+
* differs. They stay JSX until the language or the table changes.
|
|
86
|
+
*/
|
|
87
|
+
function measuredDifference(utility, mods) {
|
|
88
|
+
const font = mods.find((m) => m.name === 'font');
|
|
89
|
+
if (font && /^text-/.test(utility) && TRACKED.has(font.args[0]))
|
|
90
|
+
return `.font('${font.args[0]}') brings the ramp’s letter spacing, which ${utility} does not write`;
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
/** A text's size in points from its `.font()` token, if the chain says one. */
|
|
94
|
+
function fontSizeOf(token) {
|
|
95
|
+
const size = spec.scales.fontSize?.[token];
|
|
96
|
+
if (typeof size === 'number')
|
|
97
|
+
return size;
|
|
98
|
+
const step = spec.typeRamp[`${token}-regular`];
|
|
99
|
+
return Array.isArray(step) ? step[0] : undefined;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* A leading or tracking that is a ratio of the size measures, in the language,
|
|
103
|
+
* from the element's own size (Bloom's `Text` default when it sets none); the
|
|
104
|
+
* class measures from the size it inherits. Where the element sets no size the
|
|
105
|
+
* class stays JSX.
|
|
106
|
+
*/
|
|
107
|
+
function unsized(mods) {
|
|
108
|
+
if (mods.some((m) => m.name === 'font' && fontSizeOf(m.args[0]) !== undefined))
|
|
109
|
+
return undefined;
|
|
110
|
+
for (const mod of mods) {
|
|
111
|
+
const token = mod.args[0];
|
|
112
|
+
const ratio = mod.name === 'leading'
|
|
113
|
+
? spec.scales.leading?.[token]
|
|
114
|
+
: mod.name === 'tracking'
|
|
115
|
+
? (spec.scales.tracking?.[token] ??
|
|
116
|
+
(/^-?[\d.]+em$/.test(token) ? Number(token.slice(0, -2)) : undefined))
|
|
117
|
+
: undefined;
|
|
118
|
+
if (typeof ratio === 'number')
|
|
119
|
+
return `.${mod.name}('${token}') measures from a size this element does not set`;
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
const BREAKPOINTS = ['sm', 'md', 'lg', 'xl', '2xl'];
|
|
124
|
+
/** Tailwind variants that are a `when()` condition, and the condition. */
|
|
125
|
+
const CONDITIONS = {
|
|
126
|
+
...Object.fromEntries(BREAKPOINTS.map((b) => [b, b])),
|
|
127
|
+
dark: 'dark',
|
|
128
|
+
web: 'web',
|
|
129
|
+
native: 'native',
|
|
130
|
+
ios: 'ios',
|
|
131
|
+
android: 'android',
|
|
132
|
+
hover: 'hovered',
|
|
133
|
+
active: 'pressed',
|
|
134
|
+
'focus-visible': 'focused',
|
|
135
|
+
};
|
|
136
|
+
/** The view's own states: NativeWind answers them on a Pressable only, so the language may too. */
|
|
137
|
+
const STATES = new Set(['hovered', 'pressed', 'focused']);
|
|
138
|
+
/** Utilities on one physical side the language has no physical form for yet: scroll snapping, a reversed child spacing. */
|
|
139
|
+
const PHYSICAL = /^-?(scroll-[mp][lr]|space-x-reverse)(-|$)/;
|
|
140
|
+
/** The sides an edge writes: `s`/`e` follow the reading direction, `l`/`r` are physical. */
|
|
141
|
+
const SIDES = {
|
|
142
|
+
all: 'tbse',
|
|
143
|
+
horizontal: 'se',
|
|
144
|
+
vertical: 'tb',
|
|
145
|
+
top: 't',
|
|
146
|
+
bottom: 'b',
|
|
147
|
+
leading: 's',
|
|
148
|
+
trailing: 'e',
|
|
149
|
+
left: 'l',
|
|
150
|
+
right: 'r',
|
|
151
|
+
};
|
|
152
|
+
/** The corners a `corner()` side rounds: `t`/`b` and `s`/`e` (reading direction) or `l`/`r` (physical). */
|
|
153
|
+
const CORNERS = {
|
|
154
|
+
top: ['ts', 'te'],
|
|
155
|
+
bottom: ['bs', 'be'],
|
|
156
|
+
leading: ['ts', 'bs'],
|
|
157
|
+
trailing: ['te', 'be'],
|
|
158
|
+
'top-leading': ['ts'],
|
|
159
|
+
'top-trailing': ['te'],
|
|
160
|
+
'bottom-leading': ['bs'],
|
|
161
|
+
'bottom-trailing': ['be'],
|
|
162
|
+
left: ['tl', 'bl'],
|
|
163
|
+
right: ['tr', 'br'],
|
|
164
|
+
'top-left': ['tl'],
|
|
165
|
+
'top-right': ['tr'],
|
|
166
|
+
'bottom-left': ['bl'],
|
|
167
|
+
'bottom-right': ['br'],
|
|
168
|
+
};
|
|
169
|
+
/** Whether two corner sides round one corner: a physical corner meets either reading-direction one of its row right to left. */
|
|
170
|
+
function cornersMeet(a, b) {
|
|
171
|
+
const x = CORNERS[a] ?? [];
|
|
172
|
+
const y = CORNERS[b] ?? [];
|
|
173
|
+
return x.some((p) => y.some((q) => p === q || (p[0] === q[0] && /[se]/.test(p[1]) !== /[se]/.test(q[1]))));
|
|
174
|
+
}
|
|
175
|
+
/** Whether two edges write one side: a physical side meets either reading-direction side right to left. */
|
|
176
|
+
function edgesMeet(a, b) {
|
|
177
|
+
const x = SIDES[a] ?? '';
|
|
178
|
+
const y = SIDES[b] ?? '';
|
|
179
|
+
if ([...x].some((side) => y.includes(side)))
|
|
180
|
+
return true;
|
|
181
|
+
const logical = (sides) => /[se]/.test(sides);
|
|
182
|
+
const physical = (sides) => /[lr]/.test(sides);
|
|
183
|
+
return (logical(x) && physical(y)) || (physical(x) && logical(y));
|
|
184
|
+
}
|
|
185
|
+
/** Splits `md:hover:p-4` into its variants and utility; brackets keep their colons. */
|
|
186
|
+
function splitVariants(token) {
|
|
187
|
+
const parts = [];
|
|
188
|
+
let depth = 0;
|
|
189
|
+
let start = 0;
|
|
190
|
+
for (let i = 0; i < token.length; i++) {
|
|
191
|
+
const c = token[i];
|
|
192
|
+
if (c === '[' || c === '(')
|
|
193
|
+
depth++;
|
|
194
|
+
else if (c === ']' || c === ')')
|
|
195
|
+
depth--;
|
|
196
|
+
else if (c === ':' && depth === 0) {
|
|
197
|
+
parts.push(token.slice(start, i));
|
|
198
|
+
start = i + 1;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return { variants: parts, utility: token.slice(start) };
|
|
202
|
+
}
|
|
203
|
+
const newGroup = () => ({
|
|
204
|
+
mods: [],
|
|
205
|
+
widths: [],
|
|
206
|
+
colors: [],
|
|
207
|
+
absolute: null,
|
|
208
|
+
impliedLeading: null,
|
|
209
|
+
subjects: new Map(),
|
|
210
|
+
});
|
|
211
|
+
/** Resolves `className` on a host of `kind`. */
|
|
212
|
+
function resolveClassName(className, kind, table, options = {}) {
|
|
213
|
+
const reasons = [];
|
|
214
|
+
const fail = (subject, code, why) => reasons.push({ subject, code, why });
|
|
215
|
+
let axis = 'column';
|
|
216
|
+
const groups = new Map();
|
|
217
|
+
const group = (conditions) => {
|
|
218
|
+
const key = conditions.join(':');
|
|
219
|
+
const found = groups.get(key) ?? newGroup();
|
|
220
|
+
groups.set(key, found);
|
|
221
|
+
return found;
|
|
222
|
+
};
|
|
223
|
+
for (const token of className.split(/\s+/).filter(Boolean)) {
|
|
224
|
+
const { variants, utility: rawUtility } = splitVariants(token);
|
|
225
|
+
if (rawUtility.startsWith('!') || rawUtility.endsWith('!')) {
|
|
226
|
+
fail(token, 'important', 'an !important utility outranks every other rule');
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
// NativeWind draws these on the web only (on native their rule sets a variable alone).
|
|
230
|
+
const conditions = WEB_ONLY.has(rawUtility) ? ['web'] : [];
|
|
231
|
+
let ok = true;
|
|
232
|
+
for (const variant of variants) {
|
|
233
|
+
const condition = CONDITIONS[variant];
|
|
234
|
+
if (!condition) {
|
|
235
|
+
fail(token, `variant:${variant}`, `the variant \`${variant}:\` is no when() condition`);
|
|
236
|
+
ok = false;
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
if (STATES.has(condition) && kind !== 'pressable') {
|
|
240
|
+
fail(token, 'state-off-pressable', `\`${variant}:\` on a non-pressable host: NativeWind answers it on a Pressable only`);
|
|
241
|
+
ok = false;
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
conditions.push(condition);
|
|
245
|
+
}
|
|
246
|
+
if (!ok)
|
|
247
|
+
continue;
|
|
248
|
+
if (options.overridden?.has(token)) {
|
|
249
|
+
fail(token, 'app-theme', 'the app’s stylesheet draws this utility differently from Bloom’s (an app theme override)');
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const utility = rawUtility;
|
|
253
|
+
if (PHYSICAL.test(utility)) {
|
|
254
|
+
fail(token, 'physical-edge', 'a physical side: the language’s edges follow the reading direction');
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
const entry = table.utilities.get(utility) ?? frameStep(utility);
|
|
258
|
+
if (!entry) {
|
|
259
|
+
fail(token, 'unknown', 'not in the className table');
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
const different = measuredDifference(utility, entry.mods ?? []);
|
|
263
|
+
if (different) {
|
|
264
|
+
fail(token, 'measured-difference', different);
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
// Under a condition that holds as the view renders, what no variant changes is the
|
|
268
|
+
// chain's own, but a box layer or a placement (render.tsx, heldAsOwn).
|
|
269
|
+
const held = conditions.every((c) => HELD.has(c));
|
|
270
|
+
const fixed = conditions.length > 0
|
|
271
|
+
? entry.mods?.find((m) => !VARIES.has(m.name) && (!held || LAYER.has(m.name) || PLACEMENT.has(m.name)))
|
|
272
|
+
: undefined;
|
|
273
|
+
if (fixed) {
|
|
274
|
+
fail(token, 'not-varied', `a when() variant cannot change .${fixed.name}() (spec \`variants\`)`);
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
const into = group(conditions);
|
|
278
|
+
switch (entry.status) {
|
|
279
|
+
case 'mapped': {
|
|
280
|
+
if (entry.mods) {
|
|
281
|
+
for (const mod of entry.mods) {
|
|
282
|
+
into.mods.push(mod);
|
|
283
|
+
into.subjects.set(mod, token);
|
|
284
|
+
}
|
|
285
|
+
if (/^text-/.test(utility) &&
|
|
286
|
+
entry.mods.length === 2 &&
|
|
287
|
+
entry.mods[1]?.name === 'leading')
|
|
288
|
+
into.impliedLeading = entry.mods[1];
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
const role = /^\.border\(role(?:, ('[^']*'))?(?:, ('[^']*'))?\)$/.exec(entry.language);
|
|
292
|
+
if (role) {
|
|
293
|
+
const args = [];
|
|
294
|
+
if (role[1])
|
|
295
|
+
args.push(role[1].slice(1, -1));
|
|
296
|
+
if (role[2])
|
|
297
|
+
args.push(role[2].slice(1, -1));
|
|
298
|
+
into.widths.push({ subject: token, args });
|
|
299
|
+
}
|
|
300
|
+
else if (utility === 'absolute')
|
|
301
|
+
into.absolute = token;
|
|
302
|
+
else
|
|
303
|
+
fail(token, 'annotated', entry.language);
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
case 'structure':
|
|
307
|
+
if (kind === 'text' || conditions.length > 0) {
|
|
308
|
+
fail(token, 'structure', entry.language);
|
|
309
|
+
}
|
|
310
|
+
else if (utility === 'flex-row')
|
|
311
|
+
axis = 'row';
|
|
312
|
+
else if (utility === 'flex-col')
|
|
313
|
+
axis = 'column';
|
|
314
|
+
else if (utility !== 'flex')
|
|
315
|
+
fail(token, 'structure', entry.language);
|
|
316
|
+
break;
|
|
317
|
+
case 'drop':
|
|
318
|
+
// A pressable takes the pointer; anything else would lose it on the web.
|
|
319
|
+
if (utility === 'cursor-pointer' && kind !== 'pressable')
|
|
320
|
+
fail(token, 'cursor', 'cursor-pointer on a host that is not pressable');
|
|
321
|
+
break;
|
|
322
|
+
default:
|
|
323
|
+
fail(token, entry.status, entry.language);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
for (const { subject, mod } of options.styleMods ?? []) {
|
|
327
|
+
const into = group([]);
|
|
328
|
+
into.mods.push(mod);
|
|
329
|
+
into.subjects.set(mod, subject);
|
|
330
|
+
}
|
|
331
|
+
if (reasons.length > 0)
|
|
332
|
+
return { ok: false, reasons };
|
|
333
|
+
const resolved = [];
|
|
334
|
+
for (const [key, g] of groups) {
|
|
335
|
+
const mods = finish(g, kind, fail);
|
|
336
|
+
resolved.push({ conditions: key ? key.split(':') : [], mods });
|
|
337
|
+
}
|
|
338
|
+
// A style is inline, so a class's variant never overrides it, where the language's would.
|
|
339
|
+
const inline = new Set([
|
|
340
|
+
...(options.styleClasses ?? []),
|
|
341
|
+
...(options.styleMods ?? []).map((s) => s.subject),
|
|
342
|
+
]);
|
|
343
|
+
const unconditional = groups.get('');
|
|
344
|
+
for (const mod of unconditional?.mods ?? []) {
|
|
345
|
+
const subject = unconditional?.subjects.get(mod) ?? '';
|
|
346
|
+
if (!inline.has(subject))
|
|
347
|
+
continue;
|
|
348
|
+
// A variant the style writes itself (a Pressable's pressed style) is inline too, and wins as the JSX's does.
|
|
349
|
+
for (const [key, g] of groups)
|
|
350
|
+
if (key && g.mods.some((m) => m.name === mod.name && !inline.has(g.subjects.get(m) ?? '')))
|
|
351
|
+
fail(subject, 'style-variant', `a style a variant of the className sets too (.${mod.name}()): inline, the style wins`);
|
|
352
|
+
}
|
|
353
|
+
if (reasons.length > 0)
|
|
354
|
+
return { ok: false, reasons };
|
|
355
|
+
const base = resolved.find((v) => v.conditions.length === 0)?.mods ?? [];
|
|
356
|
+
const variants = resolved
|
|
357
|
+
.filter((v) => v.conditions.length > 0)
|
|
358
|
+
.sort((a, b) => rank(a.conditions) - rank(b.conditions));
|
|
359
|
+
return { ok: true, value: { axis, mods: base, variants } };
|
|
360
|
+
}
|
|
361
|
+
function rank(conditions) {
|
|
362
|
+
const first = conditions[0] ?? '';
|
|
363
|
+
const breakpoint = BREAKPOINTS.indexOf(first);
|
|
364
|
+
return breakpoint >= 0 ? breakpoint : BREAKPOINTS.length;
|
|
365
|
+
}
|
|
366
|
+
function finish(g, kind, fail) {
|
|
367
|
+
const subject = (mod) => g.subjects.get(mod) ?? mod.name;
|
|
368
|
+
let mods = g.mods;
|
|
369
|
+
// A text size's own leading gives way to an explicit leading class.
|
|
370
|
+
if (g.impliedLeading && mods.filter((m) => m.name === 'leading').length > 1)
|
|
371
|
+
mods = mods.filter((m) => m !== g.impliedLeading);
|
|
372
|
+
// Border: a width class and a colour class are one modifier.
|
|
373
|
+
const colors = mods.filter((m) => m.name === 'border');
|
|
374
|
+
mods = mods.filter((m) => m.name !== 'border');
|
|
375
|
+
if (g.widths.length > 0 || colors.length > 0) {
|
|
376
|
+
if (colors.length !== 1 || g.widths.length === 0) {
|
|
377
|
+
const what = colors.length === 0
|
|
378
|
+
? 'a border width with no colour class (Tailwind paints currentColor)'
|
|
379
|
+
: g.widths.length === 0
|
|
380
|
+
? 'a border colour with no width class (no border is drawn)'
|
|
381
|
+
: 'two border colours';
|
|
382
|
+
fail([...g.widths.map((w) => w.subject), ...colors.map(subject)].join(' '), 'border', what);
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
const color = colors[0];
|
|
386
|
+
for (const width of g.widths)
|
|
387
|
+
mods.push({ name: 'border', args: [color.args[0], ...width.args] });
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
// Position: `absolute` and its edges are one modifier.
|
|
391
|
+
const edges = mods.filter((m) => m.name === 'position');
|
|
392
|
+
mods = mods.filter((m) => m.name !== 'position');
|
|
393
|
+
if (g.absolute || edges.length > 0) {
|
|
394
|
+
if (!g.absolute)
|
|
395
|
+
fail(edges.map(subject).join(' '), 'offset-without-absolute', 'an inset without `absolute` offsets the view in its flow');
|
|
396
|
+
else if (edges.length === 0)
|
|
397
|
+
fail(g.absolute, 'absolute-without-edges', '`absolute` with no inset class');
|
|
398
|
+
else {
|
|
399
|
+
const merged = mergeObjects(edges, 'position', subject, fail);
|
|
400
|
+
if (merged)
|
|
401
|
+
mods.push(merged);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
// Frames join one object.
|
|
405
|
+
const frames = mods.filter((m) => m.name === 'frame');
|
|
406
|
+
if (frames.length > 1) {
|
|
407
|
+
const merged = mergeObjects(frames, 'frame', subject, fail);
|
|
408
|
+
mods = mods.filter((m) => m.name !== 'frame');
|
|
409
|
+
if (merged)
|
|
410
|
+
mods.push(merged);
|
|
411
|
+
}
|
|
412
|
+
// Two paddings (or margins) of one side: the language adds paddings and keeps
|
|
413
|
+
// the later margin, CSS keeps the rule Tailwind writes later. Either way, a conflict.
|
|
414
|
+
for (const name of ['padding', 'margin']) {
|
|
415
|
+
const seen = [];
|
|
416
|
+
for (const mod of mods.filter((m) => m.name === name)) {
|
|
417
|
+
const edge = mod.args.length === 2 ? mod.args[0] : 'all';
|
|
418
|
+
const prior = seen.find((p) => edgesMeet(p.args.length === 2 ? p.args[0] : 'all', edge));
|
|
419
|
+
if (prior)
|
|
420
|
+
fail(`${subject(prior)} ${subject(mod)}`, `${name}-overlap`, `two ${name}s of one side`);
|
|
421
|
+
seen.push(mod);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
// Two corners of one corner: which wins is Tailwind's rule order on the web and
|
|
425
|
+
// React Native's own precedence on native. (A corner of all sides is `borderRadius`,
|
|
426
|
+
// which a corner of some sides overrides everywhere.)
|
|
427
|
+
const sided = mods.filter((m) => m.name === 'corner' && m.args.length === 2);
|
|
428
|
+
sided.forEach((mod, i) => {
|
|
429
|
+
const prior = sided
|
|
430
|
+
.slice(0, i)
|
|
431
|
+
.find((p) => cornersMeet(p.args[1], mod.args[1]));
|
|
432
|
+
if (prior)
|
|
433
|
+
fail(`${subject(prior)} ${subject(mod)}`, 'corner-overlap', 'two corners of one corner');
|
|
434
|
+
});
|
|
435
|
+
// Any other modifier twice is decided by Tailwind's rule order. (A safe-area padding is one per side, from its style key;
|
|
436
|
+
// a face, `.font('mono')`, and a size, `.font('96')`, are two things a text keeps both of.)
|
|
437
|
+
const seen = new Map();
|
|
438
|
+
for (const mod of mods) {
|
|
439
|
+
if (['padding', 'margin', 'border', 'corner', 'when', 'safeAreaPadding'].includes(mod.name))
|
|
440
|
+
continue;
|
|
441
|
+
const key = mod.name === 'font' && FONT_FAMILY.has(mod.args[0]) ? 'font family' : mod.name;
|
|
442
|
+
const prior = seen.get(key);
|
|
443
|
+
if (prior)
|
|
444
|
+
fail(`${subject(prior)} ${subject(mod)}`, 'conflict', `two classes set .${mod.name}()`);
|
|
445
|
+
seen.set(key, mod);
|
|
446
|
+
}
|
|
447
|
+
const measuredFrom = kind === 'text' ? unsized(mods) : undefined;
|
|
448
|
+
if (measuredFrom)
|
|
449
|
+
fail(mods
|
|
450
|
+
.filter((m) => m.name === 'leading' || m.name === 'tracking')
|
|
451
|
+
.map(subject)
|
|
452
|
+
.join(' '), 'unsized-ratio', measuredFrom);
|
|
453
|
+
for (const mod of mods) {
|
|
454
|
+
if (kind !== 'text' && (TRAITS.has(mod.name) || TEXT_ONLY.has(mod.name)))
|
|
455
|
+
fail(subject(mod), 'text-on-view', 'a text style on a View: NativeWind does not hand it to the Text inside, the language’s environment would');
|
|
456
|
+
if (kind === 'text' && STACK_ONLY.has(mod.name))
|
|
457
|
+
fail(subject(mod), 'stack-on-text', 'a stack’s layout on a Text');
|
|
458
|
+
}
|
|
459
|
+
// The layer order keeps one view: padding, frame, fill, border, corner, opacity, shadow.
|
|
460
|
+
const layered = mods.filter((m) => LAYER.has(m.name));
|
|
461
|
+
const rest = mods.filter((m) => !LAYER.has(m.name));
|
|
462
|
+
layered.sort((a, b) => LAYER.get(a.name) - LAYER.get(b.name));
|
|
463
|
+
return [...layered, ...rest];
|
|
464
|
+
}
|
|
465
|
+
function mergeObjects(list, name, subject, fail) {
|
|
466
|
+
// A key's token, or points computed at render (`{ width: itemSize }`).
|
|
467
|
+
const out = {};
|
|
468
|
+
for (const mod of list) {
|
|
469
|
+
const value = mod.args[0];
|
|
470
|
+
if (typeof value !== 'object' || (0, table_1.isPoints)(value) || mod.args.length !== 1) {
|
|
471
|
+
fail(subject(mod), name, `an unexpected .${name}() form`);
|
|
472
|
+
return null;
|
|
473
|
+
}
|
|
474
|
+
for (const [key, v] of Object.entries(value)) {
|
|
475
|
+
if (key in out) {
|
|
476
|
+
fail(list.map(subject).join(' '), 'conflict', `two classes set .${name}({ ${key} })`);
|
|
477
|
+
return null;
|
|
478
|
+
}
|
|
479
|
+
out[key] = v;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return { name, args: [out] };
|
|
483
|
+
}
|
package/lib/style.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type Mod } from './table';
|
|
2
|
+
/**
|
|
3
|
+
* A text's own `fontSize` and `lineHeight` written out, as the language's
|
|
4
|
+
* size and line: `fontSize: 15` is `.font('md')` (a named rung over a numeric
|
|
5
|
+
* token of the same points, `fontSize: 14` `.font('14')`), `lineHeight: 20`
|
|
6
|
+
* `.leading('20px')`. A size or a line no token holds stays a reason.
|
|
7
|
+
*/
|
|
8
|
+
export declare function textDeclaration(key: string, value: AnyNode): Mod | undefined;
|
|
9
|
+
type AnyNode = any;
|
|
10
|
+
/** A declaration the caller reads as modifiers (a theme colour as its role, an inset's padding), or undefined. */
|
|
11
|
+
export type StyleRead = (key: string, value: AnyNode) => Mod | readonly Mod[] | undefined;
|
|
12
|
+
export type StyleResult = {
|
|
13
|
+
readonly ok: true;
|
|
14
|
+
readonly classes: readonly string[];
|
|
15
|
+
/** What no utility writes, by its style key (`flex: 1` is `.grow()`). */
|
|
16
|
+
readonly mods: readonly {
|
|
17
|
+
readonly subject: string;
|
|
18
|
+
readonly mod: Mod;
|
|
19
|
+
}[];
|
|
20
|
+
readonly literal: string;
|
|
21
|
+
/**
|
|
22
|
+
* Points the chain draws without carrying them (a half-square radius's
|
|
23
|
+
* side, drawn as `'full'`): the parity's before side writes their hole.
|
|
24
|
+
*/
|
|
25
|
+
readonly held: readonly AnyNode[];
|
|
26
|
+
} | {
|
|
27
|
+
readonly ok: false;
|
|
28
|
+
readonly subject: string;
|
|
29
|
+
readonly why: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* React Native's `StyleSheet.absoluteFill`: pinned to every edge of its parent
|
|
33
|
+
* (`absolute inset-0`), and the declarations it holds, written out for the
|
|
34
|
+
* parity's before side.
|
|
35
|
+
*/
|
|
36
|
+
export declare const ABSOLUTE_FILL: {
|
|
37
|
+
readonly classes: readonly ["absolute", "top-0", "right-0", "bottom-0", "left-0"];
|
|
38
|
+
readonly literal: "position: 'absolute', left: 0, right: 0, top: 0, bottom: 0";
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* A spread in a style the caller reads: the utilities and modifiers it writes,
|
|
42
|
+
* and what it is on the before side (`StyleSheet.absoluteFill` is its
|
|
43
|
+
* declarations written out; a Bloom call such as `fontFamilyStyle('display')`
|
|
44
|
+
* stays as written, its function bound there).
|
|
45
|
+
*/
|
|
46
|
+
export interface SpreadStyle {
|
|
47
|
+
readonly classes: readonly string[];
|
|
48
|
+
readonly mods: readonly Mod[];
|
|
49
|
+
/** The spread's declarations, written out on the before side; unset: the spread as written. */
|
|
50
|
+
readonly literal?: string;
|
|
51
|
+
}
|
|
52
|
+
export interface StyleReaders {
|
|
53
|
+
/** Declarations the caller reads as modifiers (a theme colour as its role). */
|
|
54
|
+
readonly read?: StyleRead;
|
|
55
|
+
/** A spread the caller reads (`StyleSheet.absoluteFill`, `fontFamilyStyle('mono')`). */
|
|
56
|
+
readonly spreadOf?: (node: AnyNode) => SpreadStyle | undefined;
|
|
57
|
+
/** A value that is a number the codemod knows (Bloom's `tokens.space.sm`), or undefined. */
|
|
58
|
+
readonly numberOf?: (node: AnyNode) => number | undefined;
|
|
59
|
+
/** Whether a value is a number the view computes as it renders (geometry's points). */
|
|
60
|
+
readonly computed?: (node: AnyNode) => boolean;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* An object literal of a style as utilities, and the literal itself (for the
|
|
64
|
+
* parity's before side, where `styles.x` is not in scope). A spread `spreadOf`
|
|
65
|
+
* reads is what it writes; any other is a style built at runtime.
|
|
66
|
+
*/
|
|
67
|
+
export declare function styleUtilities(object: AnyNode, { read, spreadOf, numberOf, computed }?: StyleReaders): StyleResult;
|
|
68
|
+
export {};
|