@oxy.so/bloom-language-codemod 0.0.0-stage → 0.1.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 +20 -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 +182 -0
- package/lib/components.js +770 -0
- package/lib/corpus.d.ts +10 -0
- package/lib/corpus.js +186 -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 +448 -0
- package/lib/style.d.ts +24 -0
- package/lib/style.js +173 -0
- package/lib/table.d.ts +29 -0
- package/lib/table.js +89 -0
- package/lib/tailwind.d.ts +24 -0
- package/lib/tailwind.js +112 -0
- package/lib/transform.d.ts +59 -0
- package/lib/transform.js +2683 -0
- package/package.json +62 -4
package/lib/resolve.js
ADDED
|
@@ -0,0 +1,448 @@
|
|
|
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
|
+
/** The modifiers a `when()` variant can change; any other is left out of it. */
|
|
30
|
+
const VARIES = new Set(spec.variants.modifiers);
|
|
31
|
+
/** Conditions that hold, or not, as the view renders: a platform. */
|
|
32
|
+
const HELD = new Set(['web', 'native', 'ios', 'android']);
|
|
33
|
+
/** The placement modifiers (render.tsx, PLACE_KINDS): a held variant leaves them out too. */
|
|
34
|
+
const PLACEMENT = new Set(['accessibilityOnly']);
|
|
35
|
+
/**
|
|
36
|
+
* Whether a variant under a condition that holds as the view renders (a
|
|
37
|
+
* platform, the app's own boolean) changes `mod`: one a variant varies, or
|
|
38
|
+
* anything but a box layer or a placement, which is then the chain's own
|
|
39
|
+
* (render.tsx, `heldAsOwn`).
|
|
40
|
+
*/
|
|
41
|
+
function heldVariantChanges(mod) {
|
|
42
|
+
return VARIES.has(mod.name) || (!LAYER.has(mod.name) && !PLACEMENT.has(mod.name));
|
|
43
|
+
}
|
|
44
|
+
/** Utilities NativeWind draws on the web only: `tabular-nums` sets a variable alone on native. */
|
|
45
|
+
const WEB_ONLY = new Set(['tabular-nums']);
|
|
46
|
+
const TEXT_ONLY = new Set(spec.nodes.text?.modifiers ?? []);
|
|
47
|
+
const STACK_ONLY = new Set(spec.nodes.stack?.modifiers ?? []);
|
|
48
|
+
/** Ramp steps with a tracking of their own, which a Tailwind text size does not write. */
|
|
49
|
+
const TRACKED = new Set(Object.entries(spec.typeRamp)
|
|
50
|
+
.filter(([, v]) => Array.isArray(v) && v[2] !== 0)
|
|
51
|
+
.map(([k]) => k.replace(/-(regular|medium|semibold|bold)$/, '')));
|
|
52
|
+
/**
|
|
53
|
+
* Table mappings the parity corpus measured drawing otherwise, each with what
|
|
54
|
+
* differs. They stay JSX until the language or the table changes.
|
|
55
|
+
*/
|
|
56
|
+
function measuredDifference(utility, mods) {
|
|
57
|
+
const font = mods.find((m) => m.name === 'font');
|
|
58
|
+
if (font && /^text-/.test(utility) && TRACKED.has(font.args[0]))
|
|
59
|
+
return `.font('${font.args[0]}') brings the ramp’s letter spacing, which ${utility} does not write`;
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
/** A text's size in points from its `.font()` token, if the chain says one. */
|
|
63
|
+
function fontSizeOf(token) {
|
|
64
|
+
const size = spec.scales.fontSize?.[token];
|
|
65
|
+
if (typeof size === 'number')
|
|
66
|
+
return size;
|
|
67
|
+
const step = spec.typeRamp[`${token}-regular`];
|
|
68
|
+
return Array.isArray(step) ? step[0] : undefined;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* A leading or tracking that is a ratio of the size measures, in the language,
|
|
72
|
+
* from the element's own size (Bloom's `Text` default when it sets none); the
|
|
73
|
+
* class measures from the size it inherits. Where the element sets no size the
|
|
74
|
+
* class stays JSX.
|
|
75
|
+
*/
|
|
76
|
+
function unsized(mods) {
|
|
77
|
+
if (mods.some((m) => m.name === 'font' && fontSizeOf(m.args[0]) !== undefined))
|
|
78
|
+
return undefined;
|
|
79
|
+
for (const mod of mods) {
|
|
80
|
+
const token = mod.args[0];
|
|
81
|
+
const ratio = mod.name === 'leading'
|
|
82
|
+
? spec.scales.leading?.[token]
|
|
83
|
+
: mod.name === 'tracking'
|
|
84
|
+
? (spec.scales.tracking?.[token] ??
|
|
85
|
+
(/^-?[\d.]+em$/.test(token) ? Number(token.slice(0, -2)) : undefined))
|
|
86
|
+
: undefined;
|
|
87
|
+
if (typeof ratio === 'number')
|
|
88
|
+
return `.${mod.name}('${token}') measures from a size this element does not set`;
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
const BREAKPOINTS = ['sm', 'md', 'lg', 'xl', '2xl'];
|
|
93
|
+
/** Tailwind variants that are a `when()` condition, and the condition. */
|
|
94
|
+
const CONDITIONS = {
|
|
95
|
+
...Object.fromEntries(BREAKPOINTS.map((b) => [b, b])),
|
|
96
|
+
dark: 'dark',
|
|
97
|
+
web: 'web',
|
|
98
|
+
native: 'native',
|
|
99
|
+
ios: 'ios',
|
|
100
|
+
android: 'android',
|
|
101
|
+
hover: 'hovered',
|
|
102
|
+
active: 'pressed',
|
|
103
|
+
'focus-visible': 'focused',
|
|
104
|
+
};
|
|
105
|
+
/** The view's own states: NativeWind answers them on a Pressable only, so the language may too. */
|
|
106
|
+
const STATES = new Set(['hovered', 'pressed', 'focused']);
|
|
107
|
+
/** Utilities on one physical side the language has no physical form for yet: scroll snapping, a reversed child spacing. */
|
|
108
|
+
const PHYSICAL = /^-?(scroll-[mp][lr]|space-x-reverse)(-|$)/;
|
|
109
|
+
/** The sides an edge writes: `s`/`e` follow the reading direction, `l`/`r` are physical. */
|
|
110
|
+
const SIDES = {
|
|
111
|
+
all: 'tbse',
|
|
112
|
+
horizontal: 'se',
|
|
113
|
+
vertical: 'tb',
|
|
114
|
+
top: 't',
|
|
115
|
+
bottom: 'b',
|
|
116
|
+
leading: 's',
|
|
117
|
+
trailing: 'e',
|
|
118
|
+
left: 'l',
|
|
119
|
+
right: 'r',
|
|
120
|
+
};
|
|
121
|
+
/** The corners a `corner()` side rounds: `t`/`b` and `s`/`e` (reading direction) or `l`/`r` (physical). */
|
|
122
|
+
const CORNERS = {
|
|
123
|
+
top: ['ts', 'te'],
|
|
124
|
+
bottom: ['bs', 'be'],
|
|
125
|
+
leading: ['ts', 'bs'],
|
|
126
|
+
trailing: ['te', 'be'],
|
|
127
|
+
'top-leading': ['ts'],
|
|
128
|
+
'top-trailing': ['te'],
|
|
129
|
+
'bottom-leading': ['bs'],
|
|
130
|
+
'bottom-trailing': ['be'],
|
|
131
|
+
left: ['tl', 'bl'],
|
|
132
|
+
right: ['tr', 'br'],
|
|
133
|
+
'top-left': ['tl'],
|
|
134
|
+
'top-right': ['tr'],
|
|
135
|
+
'bottom-left': ['bl'],
|
|
136
|
+
'bottom-right': ['br'],
|
|
137
|
+
};
|
|
138
|
+
/** Whether two corner sides round one corner: a physical corner meets either reading-direction one of its row right to left. */
|
|
139
|
+
function cornersMeet(a, b) {
|
|
140
|
+
const x = CORNERS[a] ?? [];
|
|
141
|
+
const y = CORNERS[b] ?? [];
|
|
142
|
+
return x.some((p) => y.some((q) => p === q || (p[0] === q[0] && /[se]/.test(p[1]) !== /[se]/.test(q[1]))));
|
|
143
|
+
}
|
|
144
|
+
/** Whether two edges write one side: a physical side meets either reading-direction side right to left. */
|
|
145
|
+
function edgesMeet(a, b) {
|
|
146
|
+
const x = SIDES[a] ?? '';
|
|
147
|
+
const y = SIDES[b] ?? '';
|
|
148
|
+
if ([...x].some((side) => y.includes(side)))
|
|
149
|
+
return true;
|
|
150
|
+
const logical = (sides) => /[se]/.test(sides);
|
|
151
|
+
const physical = (sides) => /[lr]/.test(sides);
|
|
152
|
+
return (logical(x) && physical(y)) || (physical(x) && logical(y));
|
|
153
|
+
}
|
|
154
|
+
/** Splits `md:hover:p-4` into its variants and utility; brackets keep their colons. */
|
|
155
|
+
function splitVariants(token) {
|
|
156
|
+
const parts = [];
|
|
157
|
+
let depth = 0;
|
|
158
|
+
let start = 0;
|
|
159
|
+
for (let i = 0; i < token.length; i++) {
|
|
160
|
+
const c = token[i];
|
|
161
|
+
if (c === '[' || c === '(')
|
|
162
|
+
depth++;
|
|
163
|
+
else if (c === ']' || c === ')')
|
|
164
|
+
depth--;
|
|
165
|
+
else if (c === ':' && depth === 0) {
|
|
166
|
+
parts.push(token.slice(start, i));
|
|
167
|
+
start = i + 1;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return { variants: parts, utility: token.slice(start) };
|
|
171
|
+
}
|
|
172
|
+
const newGroup = () => ({
|
|
173
|
+
mods: [],
|
|
174
|
+
widths: [],
|
|
175
|
+
colors: [],
|
|
176
|
+
absolute: null,
|
|
177
|
+
impliedLeading: null,
|
|
178
|
+
subjects: new Map(),
|
|
179
|
+
});
|
|
180
|
+
/** Resolves `className` on a host of `kind`. */
|
|
181
|
+
function resolveClassName(className, kind, table, options = {}) {
|
|
182
|
+
const reasons = [];
|
|
183
|
+
const fail = (subject, code, why) => reasons.push({ subject, code, why });
|
|
184
|
+
let axis = 'column';
|
|
185
|
+
const groups = new Map();
|
|
186
|
+
const group = (conditions) => {
|
|
187
|
+
const key = conditions.join(':');
|
|
188
|
+
const found = groups.get(key) ?? newGroup();
|
|
189
|
+
groups.set(key, found);
|
|
190
|
+
return found;
|
|
191
|
+
};
|
|
192
|
+
for (const token of className.split(/\s+/).filter(Boolean)) {
|
|
193
|
+
const { variants, utility: rawUtility } = splitVariants(token);
|
|
194
|
+
if (rawUtility.startsWith('!') || rawUtility.endsWith('!')) {
|
|
195
|
+
fail(token, 'important', 'an !important utility outranks every other rule');
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
// NativeWind draws these on the web only (on native their rule sets a variable alone).
|
|
199
|
+
const conditions = WEB_ONLY.has(rawUtility) ? ['web'] : [];
|
|
200
|
+
let ok = true;
|
|
201
|
+
for (const variant of variants) {
|
|
202
|
+
const condition = CONDITIONS[variant];
|
|
203
|
+
if (!condition) {
|
|
204
|
+
fail(token, `variant:${variant}`, `the variant \`${variant}:\` is no when() condition`);
|
|
205
|
+
ok = false;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
if (STATES.has(condition) && kind !== 'pressable') {
|
|
209
|
+
fail(token, 'state-off-pressable', `\`${variant}:\` on a non-pressable host: NativeWind answers it on a Pressable only`);
|
|
210
|
+
ok = false;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
conditions.push(condition);
|
|
214
|
+
}
|
|
215
|
+
if (!ok)
|
|
216
|
+
continue;
|
|
217
|
+
if (options.overridden?.has(token)) {
|
|
218
|
+
fail(token, 'app-theme', 'the app’s stylesheet draws this utility differently from Bloom’s (an app theme override)');
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
const utility = rawUtility;
|
|
222
|
+
if (PHYSICAL.test(utility)) {
|
|
223
|
+
fail(token, 'physical-edge', 'a physical side: the language’s edges follow the reading direction');
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
const entry = table.utilities.get(utility);
|
|
227
|
+
if (!entry) {
|
|
228
|
+
fail(token, 'unknown', 'not in the className table');
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
const different = measuredDifference(utility, entry.mods ?? []);
|
|
232
|
+
if (different) {
|
|
233
|
+
fail(token, 'measured-difference', different);
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
// Under a condition that holds as the view renders, what no variant changes is the
|
|
237
|
+
// chain's own, but a box layer or a placement (render.tsx, heldAsOwn).
|
|
238
|
+
const held = conditions.every((c) => HELD.has(c));
|
|
239
|
+
const fixed = conditions.length > 0
|
|
240
|
+
? entry.mods?.find((m) => !VARIES.has(m.name) && (!held || LAYER.has(m.name) || PLACEMENT.has(m.name)))
|
|
241
|
+
: undefined;
|
|
242
|
+
if (fixed) {
|
|
243
|
+
fail(token, 'not-varied', `a when() variant cannot change .${fixed.name}() (spec \`variants\`)`);
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
const into = group(conditions);
|
|
247
|
+
switch (entry.status) {
|
|
248
|
+
case 'mapped': {
|
|
249
|
+
if (entry.mods) {
|
|
250
|
+
for (const mod of entry.mods) {
|
|
251
|
+
into.mods.push(mod);
|
|
252
|
+
into.subjects.set(mod, token);
|
|
253
|
+
}
|
|
254
|
+
if (/^text-/.test(utility) &&
|
|
255
|
+
entry.mods.length === 2 &&
|
|
256
|
+
entry.mods[1]?.name === 'leading')
|
|
257
|
+
into.impliedLeading = entry.mods[1];
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
const role = /^\.border\(role(?:, ('[^']*'))?(?:, ('[^']*'))?\)$/.exec(entry.language);
|
|
261
|
+
if (role) {
|
|
262
|
+
const args = [];
|
|
263
|
+
if (role[1])
|
|
264
|
+
args.push(role[1].slice(1, -1));
|
|
265
|
+
if (role[2])
|
|
266
|
+
args.push(role[2].slice(1, -1));
|
|
267
|
+
into.widths.push({ subject: token, args });
|
|
268
|
+
}
|
|
269
|
+
else if (utility === 'absolute')
|
|
270
|
+
into.absolute = token;
|
|
271
|
+
else
|
|
272
|
+
fail(token, 'annotated', entry.language);
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
case 'structure':
|
|
276
|
+
if (kind === 'text' || conditions.length > 0) {
|
|
277
|
+
fail(token, 'structure', entry.language);
|
|
278
|
+
}
|
|
279
|
+
else if (utility === 'flex-row')
|
|
280
|
+
axis = 'row';
|
|
281
|
+
else if (utility === 'flex-col')
|
|
282
|
+
axis = 'column';
|
|
283
|
+
else if (utility !== 'flex')
|
|
284
|
+
fail(token, 'structure', entry.language);
|
|
285
|
+
break;
|
|
286
|
+
case 'drop':
|
|
287
|
+
// A pressable takes the pointer; anything else would lose it on the web.
|
|
288
|
+
if (utility === 'cursor-pointer' && kind !== 'pressable')
|
|
289
|
+
fail(token, 'cursor', 'cursor-pointer on a host that is not pressable');
|
|
290
|
+
break;
|
|
291
|
+
default:
|
|
292
|
+
fail(token, entry.status, entry.language);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
for (const { subject, mod } of options.styleMods ?? []) {
|
|
296
|
+
const into = group([]);
|
|
297
|
+
into.mods.push(mod);
|
|
298
|
+
into.subjects.set(mod, subject);
|
|
299
|
+
}
|
|
300
|
+
if (reasons.length > 0)
|
|
301
|
+
return { ok: false, reasons };
|
|
302
|
+
const resolved = [];
|
|
303
|
+
for (const [key, g] of groups) {
|
|
304
|
+
const mods = finish(g, kind, fail);
|
|
305
|
+
resolved.push({ conditions: key ? key.split(':') : [], mods });
|
|
306
|
+
}
|
|
307
|
+
// A style is inline, so a class's variant never overrides it, where the language's would.
|
|
308
|
+
const inline = new Set([
|
|
309
|
+
...(options.styleClasses ?? []),
|
|
310
|
+
...(options.styleMods ?? []).map((s) => s.subject),
|
|
311
|
+
]);
|
|
312
|
+
const unconditional = groups.get('');
|
|
313
|
+
for (const mod of unconditional?.mods ?? []) {
|
|
314
|
+
const subject = unconditional?.subjects.get(mod) ?? '';
|
|
315
|
+
if (!inline.has(subject))
|
|
316
|
+
continue;
|
|
317
|
+
for (const variant of resolved)
|
|
318
|
+
if (variant.conditions.length > 0 && variant.mods.some((m) => m.name === mod.name))
|
|
319
|
+
fail(subject, 'style-variant', `a style a variant of the className sets too (.${mod.name}()): inline, the style wins`);
|
|
320
|
+
}
|
|
321
|
+
if (reasons.length > 0)
|
|
322
|
+
return { ok: false, reasons };
|
|
323
|
+
const base = resolved.find((v) => v.conditions.length === 0)?.mods ?? [];
|
|
324
|
+
const variants = resolved
|
|
325
|
+
.filter((v) => v.conditions.length > 0)
|
|
326
|
+
.sort((a, b) => rank(a.conditions) - rank(b.conditions));
|
|
327
|
+
return { ok: true, value: { axis, mods: base, variants } };
|
|
328
|
+
}
|
|
329
|
+
function rank(conditions) {
|
|
330
|
+
const first = conditions[0] ?? '';
|
|
331
|
+
const breakpoint = BREAKPOINTS.indexOf(first);
|
|
332
|
+
return breakpoint >= 0 ? breakpoint : BREAKPOINTS.length;
|
|
333
|
+
}
|
|
334
|
+
function finish(g, kind, fail) {
|
|
335
|
+
const subject = (mod) => g.subjects.get(mod) ?? mod.name;
|
|
336
|
+
let mods = g.mods;
|
|
337
|
+
// A text size's own leading gives way to an explicit leading class.
|
|
338
|
+
if (g.impliedLeading && mods.filter((m) => m.name === 'leading').length > 1)
|
|
339
|
+
mods = mods.filter((m) => m !== g.impliedLeading);
|
|
340
|
+
// Border: a width class and a colour class are one modifier.
|
|
341
|
+
const colors = mods.filter((m) => m.name === 'border');
|
|
342
|
+
mods = mods.filter((m) => m.name !== 'border');
|
|
343
|
+
if (g.widths.length > 0 || colors.length > 0) {
|
|
344
|
+
if (colors.length !== 1 || g.widths.length === 0) {
|
|
345
|
+
const what = colors.length === 0
|
|
346
|
+
? 'a border width with no colour class (Tailwind paints currentColor)'
|
|
347
|
+
: g.widths.length === 0
|
|
348
|
+
? 'a border colour with no width class (no border is drawn)'
|
|
349
|
+
: 'two border colours';
|
|
350
|
+
fail([...g.widths.map((w) => w.subject), ...colors.map(subject)].join(' '), 'border', what);
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
const color = colors[0];
|
|
354
|
+
for (const width of g.widths)
|
|
355
|
+
mods.push({ name: 'border', args: [color.args[0], ...width.args] });
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
// Position: `absolute` and its edges are one modifier.
|
|
359
|
+
const edges = mods.filter((m) => m.name === 'position');
|
|
360
|
+
mods = mods.filter((m) => m.name !== 'position');
|
|
361
|
+
if (g.absolute || edges.length > 0) {
|
|
362
|
+
if (!g.absolute)
|
|
363
|
+
fail(edges.map(subject).join(' '), 'offset-without-absolute', 'an inset without `absolute` offsets the view in its flow');
|
|
364
|
+
else if (edges.length === 0)
|
|
365
|
+
fail(g.absolute, 'absolute-without-edges', '`absolute` with no inset class');
|
|
366
|
+
else {
|
|
367
|
+
const merged = mergeObjects(edges, 'position', subject, fail);
|
|
368
|
+
if (merged)
|
|
369
|
+
mods.push(merged);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
// Frames join one object.
|
|
373
|
+
const frames = mods.filter((m) => m.name === 'frame');
|
|
374
|
+
if (frames.length > 1) {
|
|
375
|
+
const merged = mergeObjects(frames, 'frame', subject, fail);
|
|
376
|
+
mods = mods.filter((m) => m.name !== 'frame');
|
|
377
|
+
if (merged)
|
|
378
|
+
mods.push(merged);
|
|
379
|
+
}
|
|
380
|
+
// Two paddings (or margins) of one side: the language adds paddings and keeps
|
|
381
|
+
// the later margin, CSS keeps the rule Tailwind writes later. Either way, a conflict.
|
|
382
|
+
for (const name of ['padding', 'margin']) {
|
|
383
|
+
const seen = [];
|
|
384
|
+
for (const mod of mods.filter((m) => m.name === name)) {
|
|
385
|
+
const edge = mod.args.length === 2 ? mod.args[0] : 'all';
|
|
386
|
+
const prior = seen.find((p) => edgesMeet(p.args.length === 2 ? p.args[0] : 'all', edge));
|
|
387
|
+
if (prior)
|
|
388
|
+
fail(`${subject(prior)} ${subject(mod)}`, `${name}-overlap`, `two ${name}s of one side`);
|
|
389
|
+
seen.push(mod);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
// Two corners of one corner: which wins is Tailwind's rule order on the web and
|
|
393
|
+
// React Native's own precedence on native. (A corner of all sides is `borderRadius`,
|
|
394
|
+
// which a corner of some sides overrides everywhere.)
|
|
395
|
+
const sided = mods.filter((m) => m.name === 'corner' && m.args.length === 2);
|
|
396
|
+
sided.forEach((mod, i) => {
|
|
397
|
+
const prior = sided
|
|
398
|
+
.slice(0, i)
|
|
399
|
+
.find((p) => cornersMeet(p.args[1], mod.args[1]));
|
|
400
|
+
if (prior)
|
|
401
|
+
fail(`${subject(prior)} ${subject(mod)}`, 'corner-overlap', 'two corners of one corner');
|
|
402
|
+
});
|
|
403
|
+
// Any other modifier twice is decided by Tailwind's rule order.
|
|
404
|
+
const seen = new Map();
|
|
405
|
+
for (const mod of mods) {
|
|
406
|
+
if (['padding', 'margin', 'border', 'corner', 'when'].includes(mod.name))
|
|
407
|
+
continue;
|
|
408
|
+
const prior = seen.get(mod.name);
|
|
409
|
+
if (prior)
|
|
410
|
+
fail(`${subject(prior)} ${subject(mod)}`, 'conflict', `two classes set .${mod.name}()`);
|
|
411
|
+
seen.set(mod.name, mod);
|
|
412
|
+
}
|
|
413
|
+
const measuredFrom = kind === 'text' ? unsized(mods) : undefined;
|
|
414
|
+
if (measuredFrom)
|
|
415
|
+
fail(mods
|
|
416
|
+
.filter((m) => m.name === 'leading' || m.name === 'tracking')
|
|
417
|
+
.map(subject)
|
|
418
|
+
.join(' '), 'unsized-ratio', measuredFrom);
|
|
419
|
+
for (const mod of mods) {
|
|
420
|
+
if (kind !== 'text' && (TRAITS.has(mod.name) || TEXT_ONLY.has(mod.name)))
|
|
421
|
+
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');
|
|
422
|
+
if (kind === 'text' && STACK_ONLY.has(mod.name))
|
|
423
|
+
fail(subject(mod), 'stack-on-text', 'a stack’s layout on a Text');
|
|
424
|
+
}
|
|
425
|
+
// The layer order keeps one view: padding, frame, fill, border, corner, opacity, shadow.
|
|
426
|
+
const layered = mods.filter((m) => LAYER.has(m.name));
|
|
427
|
+
const rest = mods.filter((m) => !LAYER.has(m.name));
|
|
428
|
+
layered.sort((a, b) => LAYER.get(a.name) - LAYER.get(b.name));
|
|
429
|
+
return [...layered, ...rest];
|
|
430
|
+
}
|
|
431
|
+
function mergeObjects(list, name, subject, fail) {
|
|
432
|
+
const out = {};
|
|
433
|
+
for (const mod of list) {
|
|
434
|
+
const value = mod.args[0];
|
|
435
|
+
if (typeof value !== 'object' || mod.args.length !== 1) {
|
|
436
|
+
fail(subject(mod), name, `an unexpected .${name}() form`);
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
439
|
+
for (const [key, v] of Object.entries(value)) {
|
|
440
|
+
if (key in out) {
|
|
441
|
+
fail(list.map(subject).join(' '), 'conflict', `two classes set .${name}({ ${key} })`);
|
|
442
|
+
return null;
|
|
443
|
+
}
|
|
444
|
+
out[key] = v;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return { name, args: [out] };
|
|
448
|
+
}
|
package/lib/style.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Mod } from './table';
|
|
2
|
+
type AnyNode = any;
|
|
3
|
+
/** A declaration the caller reads as a modifier (a theme colour as its role), or undefined. */
|
|
4
|
+
export type StyleRead = (key: string, value: AnyNode) => Mod | undefined;
|
|
5
|
+
export type StyleResult = {
|
|
6
|
+
readonly ok: true;
|
|
7
|
+
readonly classes: readonly string[];
|
|
8
|
+
/** What no utility writes, by its style key (`flex: 1` is `.grow()`). */
|
|
9
|
+
readonly mods: readonly {
|
|
10
|
+
readonly subject: string;
|
|
11
|
+
readonly mod: Mod;
|
|
12
|
+
}[];
|
|
13
|
+
readonly literal: string;
|
|
14
|
+
} | {
|
|
15
|
+
readonly ok: false;
|
|
16
|
+
readonly subject: string;
|
|
17
|
+
readonly why: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* An object literal of a style as utilities, and the literal itself (for the
|
|
21
|
+
* parity's before side, where `styles.x` is not in scope).
|
|
22
|
+
*/
|
|
23
|
+
export declare function styleUtilities(object: AnyNode, read?: StyleRead): StyleResult;
|
|
24
|
+
export {};
|
package/lib/style.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.styleUtilities = styleUtilities;
|
|
37
|
+
/**
|
|
38
|
+
* A static `style` as the Tailwind utilities that write the same declarations,
|
|
39
|
+
* so it lands through the className table like any class (and conflicts with
|
|
40
|
+
* a class that sets the same thing, as two classes would). Only layout values
|
|
41
|
+
* Tailwind writes exactly are taken: a length that is a step of the spacing
|
|
42
|
+
* scale (`gap: 12` is `gap-3`, 0.25rem steps at the preset's 16px rem), `100%`,
|
|
43
|
+
* and the flex keywords. Anything else is a reason.
|
|
44
|
+
*/
|
|
45
|
+
const recast = __importStar(require("recast"));
|
|
46
|
+
const n = recast.types.namedTypes;
|
|
47
|
+
/** A style key whose numeric value is a spacing-scale step: its utility prefix. */
|
|
48
|
+
const LENGTH = {
|
|
49
|
+
gap: 'gap',
|
|
50
|
+
rowGap: 'gap-y',
|
|
51
|
+
columnGap: 'gap-x',
|
|
52
|
+
padding: 'p',
|
|
53
|
+
paddingHorizontal: 'px',
|
|
54
|
+
paddingVertical: 'py',
|
|
55
|
+
paddingTop: 'pt',
|
|
56
|
+
paddingBottom: 'pb',
|
|
57
|
+
paddingLeft: 'pl',
|
|
58
|
+
paddingRight: 'pr',
|
|
59
|
+
paddingStart: 'ps',
|
|
60
|
+
paddingEnd: 'pe',
|
|
61
|
+
margin: 'm',
|
|
62
|
+
marginHorizontal: 'mx',
|
|
63
|
+
marginVertical: 'my',
|
|
64
|
+
marginTop: 'mt',
|
|
65
|
+
marginBottom: 'mb',
|
|
66
|
+
marginLeft: 'ml',
|
|
67
|
+
marginRight: 'mr',
|
|
68
|
+
width: 'w',
|
|
69
|
+
height: 'h',
|
|
70
|
+
minWidth: 'min-w',
|
|
71
|
+
minHeight: 'min-h',
|
|
72
|
+
maxWidth: 'max-w',
|
|
73
|
+
maxHeight: 'max-h',
|
|
74
|
+
top: 'top',
|
|
75
|
+
right: 'right',
|
|
76
|
+
bottom: 'bottom',
|
|
77
|
+
left: 'left',
|
|
78
|
+
};
|
|
79
|
+
/** A style key and keyword value as its utility. */
|
|
80
|
+
const KEYWORD = {
|
|
81
|
+
flexDirection: { row: 'flex-row', column: 'flex-col' },
|
|
82
|
+
flexWrap: { wrap: 'flex-wrap', nowrap: 'flex-nowrap' },
|
|
83
|
+
alignItems: {
|
|
84
|
+
center: 'items-center',
|
|
85
|
+
'flex-start': 'items-start',
|
|
86
|
+
'flex-end': 'items-end',
|
|
87
|
+
stretch: 'items-stretch',
|
|
88
|
+
baseline: 'items-baseline',
|
|
89
|
+
},
|
|
90
|
+
alignSelf: {
|
|
91
|
+
center: 'self-center',
|
|
92
|
+
'flex-start': 'self-start',
|
|
93
|
+
'flex-end': 'self-end',
|
|
94
|
+
stretch: 'self-stretch',
|
|
95
|
+
},
|
|
96
|
+
justifyContent: {
|
|
97
|
+
center: 'justify-center',
|
|
98
|
+
'flex-start': 'justify-start',
|
|
99
|
+
'flex-end': 'justify-end',
|
|
100
|
+
'space-between': 'justify-between',
|
|
101
|
+
'space-around': 'justify-around',
|
|
102
|
+
'space-evenly': 'justify-evenly',
|
|
103
|
+
},
|
|
104
|
+
position: { absolute: 'absolute', relative: 'relative' },
|
|
105
|
+
overflow: { hidden: 'overflow-hidden' },
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* A numeric key whose value is a utility of its own. (React Native's `flex: 1`
|
|
109
|
+
* is no utility: Tailwind's `flex-1` is grow 1, shrink 1, basis 0%, which
|
|
110
|
+
* React Native draws differently; measured on the corpus. It is the
|
|
111
|
+
* language's `.grow()`, which writes `flex: 1` itself: `DIRECT`.)
|
|
112
|
+
*/
|
|
113
|
+
const NUMERIC = {
|
|
114
|
+
flexGrow: { 1: 'grow', 0: 'grow-0' },
|
|
115
|
+
flexShrink: { 1: 'shrink', 0: 'shrink-0' },
|
|
116
|
+
};
|
|
117
|
+
/** A style key and value the language writes itself, as its modifier: React Native's `flex: 1` is `.grow()`. */
|
|
118
|
+
function direct(key, value) {
|
|
119
|
+
return key === 'flex' && n.NumericLiteral.check(value) && value.value === 1
|
|
120
|
+
? { name: 'grow', args: [] }
|
|
121
|
+
: undefined;
|
|
122
|
+
}
|
|
123
|
+
/** One declaration as its utility, or undefined. */
|
|
124
|
+
function utility(key, value) {
|
|
125
|
+
if (n.NumericLiteral.check(value)) {
|
|
126
|
+
const prefix = LENGTH[key];
|
|
127
|
+
// A whole number of points is a step of Tailwind's spacing scale: 0.25rem, 4 points (`gap-3` is 12).
|
|
128
|
+
if (prefix)
|
|
129
|
+
return Number.isInteger(value.value) && value.value >= 0
|
|
130
|
+
? `${prefix}-${value.value / 4}`
|
|
131
|
+
: undefined;
|
|
132
|
+
return NUMERIC[key]?.[String(value.value)];
|
|
133
|
+
}
|
|
134
|
+
if (n.StringLiteral.check(value)) {
|
|
135
|
+
if (value.value === '100%' &&
|
|
136
|
+
LENGTH[key] &&
|
|
137
|
+
/^(w|h|min-w|min-h|max-w|max-h)$/.test(LENGTH[key]))
|
|
138
|
+
return `${LENGTH[key]}-full`;
|
|
139
|
+
return KEYWORD[key]?.[value.value];
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* An object literal of a style as utilities, and the literal itself (for the
|
|
145
|
+
* parity's before side, where `styles.x` is not in scope).
|
|
146
|
+
*/
|
|
147
|
+
function styleUtilities(object, read) {
|
|
148
|
+
if (!n.ObjectExpression.check(object))
|
|
149
|
+
return { ok: false, subject: 'style', why: 'a style that is no object literal' };
|
|
150
|
+
const classes = [];
|
|
151
|
+
const mods = [];
|
|
152
|
+
for (const property of object.properties) {
|
|
153
|
+
if (!(n.ObjectProperty.check(property) || n.Property.check(property)) ||
|
|
154
|
+
property.computed ||
|
|
155
|
+
!n.Identifier.check(property.key))
|
|
156
|
+
return { ok: false, subject: 'style', why: 'a style built at runtime' };
|
|
157
|
+
const key = property.key.name;
|
|
158
|
+
const mod = direct(key, property.value) ?? read?.(key, property.value);
|
|
159
|
+
if (mod) {
|
|
160
|
+
mods.push({ subject: key, mod });
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const found = utility(key, property.value);
|
|
164
|
+
if (!found)
|
|
165
|
+
return {
|
|
166
|
+
ok: false,
|
|
167
|
+
subject: key,
|
|
168
|
+
why: `a style \`${key}\` whose value no Tailwind utility writes exactly`,
|
|
169
|
+
};
|
|
170
|
+
classes.push(found);
|
|
171
|
+
}
|
|
172
|
+
return { ok: true, classes, mods, literal: recast.print(object).code };
|
|
173
|
+
}
|
package/lib/table.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type Arg = string | number | boolean | Readonly<Record<string, string>>;
|
|
2
|
+
/** One modifier call, as the codemod writes it. */
|
|
3
|
+
export interface Mod {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly args: readonly Arg[];
|
|
6
|
+
}
|
|
7
|
+
export type Status = 'mapped' | 'structure' | 'drop' | 'token' | 'appToken' | 'modifier' | 'motion' | 'app';
|
|
8
|
+
export interface Entry {
|
|
9
|
+
readonly status: Status;
|
|
10
|
+
/** The table's words for the utility. */
|
|
11
|
+
readonly language: string;
|
|
12
|
+
/** The chain, when the table maps the utility to plain literal modifiers. */
|
|
13
|
+
readonly mods: readonly Mod[] | null;
|
|
14
|
+
readonly scale?: string;
|
|
15
|
+
readonly value?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface Table {
|
|
18
|
+
readonly utilities: ReadonlyMap<string, Entry>;
|
|
19
|
+
/** What a bare Bloom icon element (no fill) becomes: its primary paint. */
|
|
20
|
+
readonly bareIcon: readonly Mod[];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The Bloom language package the codemod writes for: the one installed beside
|
|
24
|
+
* it (its peer), whose spec, exports and className table it reads.
|
|
25
|
+
*/
|
|
26
|
+
export declare const LANGUAGE_PACKAGE: string;
|
|
27
|
+
/** `.a('x').b({ k: 'v' })` as mods, or null when it is anything but literal calls. */
|
|
28
|
+
export declare function chainOf(language: string): Mod[] | null;
|
|
29
|
+
export declare function loadTable(path?: string): Table;
|