@oxy.so/bloom-language-codemod 0.1.0 → 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 +12 -0
- package/lib/components.d.ts +27 -2
- package/lib/components.js +62 -8
- package/lib/corpus.js +24 -17
- package/lib/resolve.js +43 -8
- package/lib/style.d.ts +49 -5
- package/lib/style.js +314 -12
- package/lib/table.d.ts +13 -1
- package/lib/table.js +8 -1
- package/lib/transform.d.ts +2 -0
- package/lib/transform.js +1868 -97
- package/package.json +3 -3
package/lib/transform.js
CHANGED
|
@@ -61,14 +61,113 @@ const n = recast.types.namedTypes;
|
|
|
61
61
|
const spec = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(table_1.LANGUAGE_PACKAGE, 'spec/language.json'), 'utf8'));
|
|
62
62
|
/** The roles the universal `.role()` takes, each React Native's `accessibilityRole` of its name. */
|
|
63
63
|
const ROLES = new Set(spec.enums.role);
|
|
64
|
+
/** A React Native `fontWeight` as the spec's weight of that value (`'700'` is `bold`). */
|
|
65
|
+
const WEIGHT_BY_VALUE = {
|
|
66
|
+
...Object.fromEntries(Object.entries(spec.scales.weight).map(([name, value]) => [value, name])),
|
|
67
|
+
bold: 'bold',
|
|
68
|
+
normal: 'regular',
|
|
69
|
+
};
|
|
64
70
|
/** What a scroll view's content view takes: the layout of one view. */
|
|
65
71
|
const CONTENT_VIEW = new Set(spec.contentView.modifiers);
|
|
72
|
+
/** The modifiers each constructor's node takes: its own, and the universal ones unless it opts out. */
|
|
73
|
+
const NODE_MODIFIERS = new Map();
|
|
74
|
+
for (const node of Object.values(spec.nodes)) {
|
|
75
|
+
if (typeof node === 'string')
|
|
76
|
+
continue;
|
|
77
|
+
const taken = new Set([
|
|
78
|
+
...(node.modifiers ?? []),
|
|
79
|
+
...(node.universal === false ? [] : spec.universal),
|
|
80
|
+
]);
|
|
81
|
+
for (const constructor of node.constructors ?? [])
|
|
82
|
+
NODE_MODIFIERS.set(constructor, taken);
|
|
83
|
+
}
|
|
84
|
+
/** React Native's `accessibilityLiveRegion` values, each the language's `.liveRegion()` of its name. */
|
|
85
|
+
const LIVE_REGIONS = new Set(spec.enums.liveRegion);
|
|
86
|
+
/** An opacity (`0.7`) as the opacity token of its value (`'70'`): a `TouchableOpacity`'s `activeOpacity`. */
|
|
87
|
+
const OPACITY_TOKEN = new Map(Object.entries(spec.scales.opacity)
|
|
88
|
+
.filter(([name]) => name !== '$comment')
|
|
89
|
+
.map(([name, value]) => [value, name]));
|
|
90
|
+
/** A hit slop in points (`8`) as the hit slop token of its value (`'md'`); a name of Bloom's (`tokens.hitSlop.md`) as itself. */
|
|
91
|
+
const HIT_SLOP = Object.fromEntries(Object.entries(spec.scales.hitSlop).filter(([name]) => name !== '$comment'));
|
|
92
|
+
const HIT_SLOP_TOKEN = new Map(Object.entries(HIT_SLOP).map(([name, value]) => [value, name]));
|
|
93
|
+
/** One side's slop: a static number, or Bloom's `tokens.hitSlop.<name>`, as its token. */
|
|
94
|
+
function hitSlopToken(value) {
|
|
95
|
+
const number = staticNumber(value);
|
|
96
|
+
if (number !== undefined)
|
|
97
|
+
return HIT_SLOP_TOKEN.get(number);
|
|
98
|
+
const expression = n.JSXExpressionContainer.check(value) ? value.expression : value;
|
|
99
|
+
if (n.MemberExpression.check(expression) &&
|
|
100
|
+
!expression.computed &&
|
|
101
|
+
n.Identifier.check(expression.property) &&
|
|
102
|
+
n.MemberExpression.check(expression.object) &&
|
|
103
|
+
n.Identifier.check(expression.object.property) &&
|
|
104
|
+
expression.object.property.name === 'hitSlop' &&
|
|
105
|
+
expression.property.name in HIT_SLOP)
|
|
106
|
+
return expression.property.name;
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* React Native's `hitSlop` as `.hitSlop()` calls: one slop is one call; an
|
|
111
|
+
* object of static sides is a call per pair of equal opposite sides (or per
|
|
112
|
+
* side), only the sides it names. Undefined when any side is no token.
|
|
113
|
+
*/
|
|
114
|
+
function hitSlopCalls(value) {
|
|
115
|
+
const one = hitSlopToken(value);
|
|
116
|
+
if (one !== undefined)
|
|
117
|
+
return [[one]];
|
|
118
|
+
const object = n.JSXExpressionContainer.check(value) ? value.expression : value;
|
|
119
|
+
if (!n.ObjectExpression.check(object))
|
|
120
|
+
return undefined;
|
|
121
|
+
const sides = {};
|
|
122
|
+
for (const property of object.properties) {
|
|
123
|
+
if (!(n.ObjectProperty.check(property) || n.Property.check(property)) ||
|
|
124
|
+
property.computed ||
|
|
125
|
+
!n.Identifier.check(property.key) ||
|
|
126
|
+
!['top', 'bottom', 'left', 'right'].includes(property.key.name))
|
|
127
|
+
return undefined;
|
|
128
|
+
const token = hitSlopToken(property.value);
|
|
129
|
+
if (token === undefined)
|
|
130
|
+
return undefined;
|
|
131
|
+
sides[property.key.name] = token;
|
|
132
|
+
}
|
|
133
|
+
const { top, bottom, left, right } = sides;
|
|
134
|
+
if (top !== undefined && top === bottom && bottom === left && left === right)
|
|
135
|
+
return [[top]];
|
|
136
|
+
const calls = [];
|
|
137
|
+
const pair = (edge, a, b, sideA, sideB) => {
|
|
138
|
+
if (a !== undefined && a === b)
|
|
139
|
+
calls.push([edge, a]);
|
|
140
|
+
else {
|
|
141
|
+
if (a !== undefined)
|
|
142
|
+
calls.push([sideA, a]);
|
|
143
|
+
if (b !== undefined)
|
|
144
|
+
calls.push([sideB, b]);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
pair('vertical', top, bottom, 'top', 'bottom');
|
|
148
|
+
pair('horizontal', left, right, 'left', 'right');
|
|
149
|
+
return calls;
|
|
150
|
+
}
|
|
151
|
+
/** The roles the theme's palette paints (the surface fill is the surface's: `useSurfaceFill()`). */
|
|
152
|
+
const PALETTE = Object.entries(spec.colors).filter((entry) => Array.isArray(entry[1]) && entry[1][1] !== null);
|
|
66
153
|
/** A Bloom theme colour (`theme.colors.textSecondary`) as the role painted from it (`muted`). */
|
|
67
154
|
const ROLE_OF_THEME_COLOR = new Map();
|
|
68
|
-
for (const [role, entry] of
|
|
69
|
-
if (
|
|
155
|
+
for (const [role, entry] of PALETTE)
|
|
156
|
+
if (!ROLE_OF_THEME_COLOR.has(entry[0]))
|
|
70
157
|
ROLE_OF_THEME_COLOR.set(entry[0], role);
|
|
158
|
+
/** Each canonical colour token, as `useColorTokens()` names it (`mutedForeground`), as the role painted from it. */
|
|
159
|
+
const ROLE_OF_COLOR_TOKEN = new Map();
|
|
160
|
+
for (const [role, entry] of PALETTE) {
|
|
161
|
+
const token = entry[1].replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
162
|
+
if (!ROLE_OF_COLOR_TOKEN.has(token))
|
|
163
|
+
ROLE_OF_COLOR_TOKEN.set(token, role);
|
|
164
|
+
}
|
|
71
165
|
const ICON_SIZES = Object.entries(spec.scales.iconSize);
|
|
166
|
+
/** A glyph size in points (`28`) as the icon size token of its value (`'xl'`), a named rung over a numeric one. */
|
|
167
|
+
const ICON_SIZE_TOKEN = new Map();
|
|
168
|
+
for (const [name, size] of ICON_SIZES)
|
|
169
|
+
if (typeof size === 'number' && (!/^\d/.test(name) || !ICON_SIZE_TOKEN.has(size)))
|
|
170
|
+
ICON_SIZE_TOKEN.set(size, name);
|
|
72
171
|
const RAMP = new Set(Object.keys(spec.typeRamp).filter((k) => !k.startsWith('$')));
|
|
73
172
|
/** A content container's padding key as the edge of its content margin. */
|
|
74
173
|
const CONTENT_EDGE = {
|
|
@@ -127,6 +226,10 @@ function hasJsx(expression) {
|
|
|
127
226
|
return found;
|
|
128
227
|
}
|
|
129
228
|
const LANGUAGE_MODULE = '@oxy.so/bloom-language';
|
|
229
|
+
/** Bloom's floating action button. */
|
|
230
|
+
const FAB_MODULE = '@oxy.so/bloom/fab';
|
|
231
|
+
/** Bloom's dropdown menu and its parts. */
|
|
232
|
+
const DROPDOWN = '@oxy.so/bloom/dropdown-menu';
|
|
130
233
|
const SCREEN_KEY = '@oxy.so/bloom/screen#Screen';
|
|
131
234
|
/**
|
|
132
235
|
* Bloom's `PressableScale`: a Pressable that eases to a smaller scale under a
|
|
@@ -145,6 +248,9 @@ function kindOf(source) {
|
|
|
145
248
|
return 'view';
|
|
146
249
|
if (source.imported === 'Pressable')
|
|
147
250
|
return 'pressable';
|
|
251
|
+
// A TouchableOpacity is a pressable whose press fades it: `.pressFade()`.
|
|
252
|
+
if (source.imported === 'TouchableOpacity')
|
|
253
|
+
return 'pressable';
|
|
148
254
|
if (source.imported === 'Text')
|
|
149
255
|
return 'rnText';
|
|
150
256
|
if (source.imported === 'Image')
|
|
@@ -258,15 +364,24 @@ function attributeExpression(value) {
|
|
|
258
364
|
/** Code without its block comments: a name in a comment is no binding. */
|
|
259
365
|
const uncommented = (code) => code.replace(/\/\*[\s\S]*?\*\//g, '');
|
|
260
366
|
const quote = (text) => `'${text.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\n/g, '\\n')}'`;
|
|
261
|
-
|
|
367
|
+
/** An argument as code; points computed at render are their carried value (`carry`). */
|
|
368
|
+
function printArg(arg, carry) {
|
|
262
369
|
if (typeof arg === 'string')
|
|
263
370
|
return quote(arg);
|
|
264
371
|
if (typeof arg === 'number' || typeof arg === 'boolean')
|
|
265
372
|
return String(arg);
|
|
266
|
-
|
|
373
|
+
if ((0, table_1.isPoints)(arg))
|
|
374
|
+
return carry(arg);
|
|
375
|
+
const entries = Object.entries(arg).map(([k, v]) => `${/^[A-Za-z_$][\w$]*$/.test(k) ? k : quote(k)}: ${(0, table_1.isPoints)(v) ? carry(v) : quote(v)}`);
|
|
267
376
|
return `{ ${entries.join(', ')} }`;
|
|
268
377
|
}
|
|
269
|
-
const printMod = (mod) => `.${mod.name}(${mod.args.map(printArg).join(', ')})`;
|
|
378
|
+
const printMod = (mod, carry) => `.${mod.name}(${mod.args.map((arg) => printArg(arg, carry)).join(', ')})`;
|
|
379
|
+
/** The points an argument carries: itself, or an object's values (`{ width: itemSize }`). */
|
|
380
|
+
const pointsOf = (arg) => (0, table_1.isPoints)(arg)
|
|
381
|
+
? [arg]
|
|
382
|
+
: typeof arg === 'object' && arg !== null && !('$' in arg) && !('hole' in arg)
|
|
383
|
+
? Object.values(arg).filter(table_1.isPoints)
|
|
384
|
+
: [];
|
|
270
385
|
/** Transforms one file's source. */
|
|
271
386
|
function transformSource(source, options) {
|
|
272
387
|
const ast = recast.parse(source, { parser: babelTs });
|
|
@@ -291,9 +406,17 @@ function transformSource(source, options) {
|
|
|
291
406
|
this.traverse(path);
|
|
292
407
|
},
|
|
293
408
|
});
|
|
294
|
-
/**
|
|
409
|
+
/** The language names the file already imports, by their local name (`Embed`, `toElement`). */
|
|
410
|
+
const importedLanguage = new Map();
|
|
411
|
+
for (const [local, source] of sources)
|
|
412
|
+
if (source.module === LANGUAGE_MODULE || source.module === `${LANGUAGE_MODULE}/router`)
|
|
413
|
+
importedLanguage.set(source.imported, local);
|
|
414
|
+
/** A language name: the file's own import of it, else one added, aliased when the file already uses the name. */
|
|
295
415
|
const usedLanguage = new Map();
|
|
296
416
|
const lang = (name) => {
|
|
417
|
+
const imported = importedLanguage.get(name);
|
|
418
|
+
if (imported)
|
|
419
|
+
return imported;
|
|
297
420
|
let local = usedLanguage.get(name);
|
|
298
421
|
if (!local) {
|
|
299
422
|
local = declared.has(name) ? `L${name}` : name;
|
|
@@ -336,6 +459,11 @@ function transformSource(source, options) {
|
|
|
336
459
|
* theme colour, resolved through the scope it is read in.
|
|
337
460
|
*/
|
|
338
461
|
const themeRoles = new Map();
|
|
462
|
+
const useColorTokensLocals = new Set([...sources]
|
|
463
|
+
.filter(([, s]) => s.module === '@oxy.so/bloom/theme' && s.imported === 'useColorTokens')
|
|
464
|
+
.map(([local]) => local));
|
|
465
|
+
/** The theme reads that are `useColorTokens()`'s (`colorTokens.border`), not `useTheme()`'s. */
|
|
466
|
+
const colorTokenReads = new Set();
|
|
339
467
|
const useThemeLocals = new Set([...sources]
|
|
340
468
|
.filter(([, s]) => s.module === '@oxy.so/bloom/theme' && s.imported === 'useTheme')
|
|
341
469
|
.map(([local]) => local));
|
|
@@ -365,9 +493,13 @@ function transformSource(source, options) {
|
|
|
365
493
|
if (!init ||
|
|
366
494
|
!n.CallExpression.check(init) ||
|
|
367
495
|
!n.Identifier.check(init.callee) ||
|
|
368
|
-
!useThemeLocals.has(init.callee.name) ||
|
|
369
496
|
at.parent?.node.kind !== 'const')
|
|
370
497
|
return undefined;
|
|
498
|
+
// `const colorTokens = useColorTokens()`: the theme's canonical colours by token.
|
|
499
|
+
if (useColorTokensLocals.has(init.callee.name))
|
|
500
|
+
return n.Identifier.check(at.node.id) && at.node.id.name === name ? 'tokens' : undefined;
|
|
501
|
+
if (!useThemeLocals.has(init.callee.name))
|
|
502
|
+
return undefined;
|
|
371
503
|
if (n.Identifier.check(at.node.id) && at.node.id.name === name)
|
|
372
504
|
return 'theme';
|
|
373
505
|
if (n.ObjectPattern.check(at.node.id) &&
|
|
@@ -382,7 +514,7 @@ function transformSource(source, options) {
|
|
|
382
514
|
};
|
|
383
515
|
/** Each `styles.x` read where `styles` is the module's StyleSheet (not a name that shadows it). */
|
|
384
516
|
const moduleStyleReads = new Set();
|
|
385
|
-
if (useThemeLocals.size > 0 || styleSheets.size > 0)
|
|
517
|
+
if (useThemeLocals.size > 0 || useColorTokensLocals.size > 0 || styleSheets.size > 0)
|
|
386
518
|
recast.visit(ast, {
|
|
387
519
|
visitMemberExpression(path) {
|
|
388
520
|
const { object, property, computed } = path.node;
|
|
@@ -401,13 +533,254 @@ function transformSource(source, options) {
|
|
|
401
533
|
const colors = n.Identifier.check(object) && boundTo(path, object.name) === 'colors';
|
|
402
534
|
if (role && (colorsOf || colors))
|
|
403
535
|
themeRoles.set(path.node, role);
|
|
536
|
+
const token = ROLE_OF_COLOR_TOKEN.get(property.name);
|
|
537
|
+
if (token && n.Identifier.check(object) && boundTo(path, object.name) === 'tokens') {
|
|
538
|
+
themeRoles.set(path.node, token);
|
|
539
|
+
colorTokenReads.add(path.node);
|
|
540
|
+
}
|
|
404
541
|
}
|
|
405
542
|
this.traverse(path);
|
|
406
543
|
},
|
|
407
544
|
});
|
|
408
545
|
/** A className chosen at runtime, by its expression: both branches' classes, which the parity's stylesheet holds. */
|
|
409
546
|
const choiceClasses = new Map();
|
|
410
|
-
/**
|
|
547
|
+
/**
|
|
548
|
+
* Each `insets.top` (`bottom`, `left`, `right`) where `insets` is a `const`
|
|
549
|
+
* holding `useSafeAreaInsets()` (react-native-safe-area-context): the device's
|
|
550
|
+
* inset on that side, which `.safeAreaPadding(side)` pads by.
|
|
551
|
+
*/
|
|
552
|
+
const insetReads = new Map();
|
|
553
|
+
recast.visit(ast, {
|
|
554
|
+
visitMemberExpression(path) {
|
|
555
|
+
const { object, property } = path.node;
|
|
556
|
+
const side = !path.node.computed && n.Identifier.check(property) ? property.name : '';
|
|
557
|
+
if (n.Identifier.check(object) && ['top', 'bottom', 'left', 'right'].includes(side)) {
|
|
558
|
+
const bindings = scopeOf(path, object.name)?.getBindings()[object.name];
|
|
559
|
+
let at = bindings?.length === 1 ? bindings[0] : undefined;
|
|
560
|
+
while (at && !n.VariableDeclarator.check(at.node))
|
|
561
|
+
at = at.parent;
|
|
562
|
+
const init = at?.node.init;
|
|
563
|
+
const callee = n.CallExpression.check(init) ? init.callee : undefined;
|
|
564
|
+
const source = n.Identifier.check(callee) ? sources.get(callee.name) : undefined;
|
|
565
|
+
if (at?.parent?.node.kind === 'const' &&
|
|
566
|
+
n.Identifier.check(at.node.id) &&
|
|
567
|
+
init.arguments.length === 0 &&
|
|
568
|
+
source?.module === 'react-native-safe-area-context' &&
|
|
569
|
+
source.imported === 'useSafeAreaInsets')
|
|
570
|
+
insetReads.set(path.node, side);
|
|
571
|
+
}
|
|
572
|
+
this.traverse(path);
|
|
573
|
+
},
|
|
574
|
+
});
|
|
575
|
+
/**
|
|
576
|
+
* Each identifier read whose binding is inside a function (a prop, a local, a
|
|
577
|
+
* hook's result): what the view works out as it renders, never a module's constant.
|
|
578
|
+
*/
|
|
579
|
+
const renderReads = new Set();
|
|
580
|
+
recast.visit(ast, {
|
|
581
|
+
visitIdentifier(path) {
|
|
582
|
+
const parent = path.parent?.node;
|
|
583
|
+
const named = (n.MemberExpression.check(parent) && parent.property === path.node && !parent.computed) ||
|
|
584
|
+
((n.ObjectProperty.check(parent) || n.Property.check(parent)) &&
|
|
585
|
+
parent.key === path.node &&
|
|
586
|
+
!parent.computed);
|
|
587
|
+
if (!named && scopeOf(path, path.node.name)?.isGlobal === false)
|
|
588
|
+
renderReads.add(path.node);
|
|
589
|
+
this.traverse(path);
|
|
590
|
+
},
|
|
591
|
+
});
|
|
592
|
+
/** An expression of numbers alone: reads, arithmetic, `Math`'s functions, a plain choice between two. */
|
|
593
|
+
const numberForm = (node) => {
|
|
594
|
+
if (n.NumericLiteral.check(node))
|
|
595
|
+
return true;
|
|
596
|
+
if (n.Identifier.check(node))
|
|
597
|
+
return node.name !== 'undefined';
|
|
598
|
+
if (n.MemberExpression.check(node))
|
|
599
|
+
return ((n.Identifier.check(node.object) || n.MemberExpression.check(node.object)) &&
|
|
600
|
+
numberForm(node.object) &&
|
|
601
|
+
(!node.computed || numberForm(node.property)));
|
|
602
|
+
if (n.BinaryExpression.check(node))
|
|
603
|
+
return (['+', '-', '*', '/'].includes(node.operator) &&
|
|
604
|
+
numberForm(node.left) &&
|
|
605
|
+
numberForm(node.right));
|
|
606
|
+
if (n.UnaryExpression.check(node))
|
|
607
|
+
return node.operator === '-' && numberForm(node.argument);
|
|
608
|
+
if (n.TSNonNullExpression.check(node))
|
|
609
|
+
return numberForm(node.expression);
|
|
610
|
+
if (n.ConditionalExpression.check(node))
|
|
611
|
+
return ((isPlainRead(node.test) || isBooleanForm(node.test)) &&
|
|
612
|
+
numberForm(node.consequent) &&
|
|
613
|
+
numberForm(node.alternate));
|
|
614
|
+
return (n.CallExpression.check(node) &&
|
|
615
|
+
n.MemberExpression.check(node.callee) &&
|
|
616
|
+
n.Identifier.check(node.callee.object) &&
|
|
617
|
+
node.callee.object.name === 'Math' &&
|
|
618
|
+
!renderReads.has(node.callee.object) &&
|
|
619
|
+
node.arguments.every(numberForm));
|
|
620
|
+
};
|
|
621
|
+
/**
|
|
622
|
+
* A number the view computes as it renders (`itemSize`, `insets.top + 8`,
|
|
623
|
+
* `SIZES[size] / 2`): a number form that reads something bound in the
|
|
624
|
+
* component. A number written out, or one of module constants alone, is a token.
|
|
625
|
+
*/
|
|
626
|
+
const computedAtRender = (node) => {
|
|
627
|
+
if (n.NumericLiteral.check(node) || !numberForm(node))
|
|
628
|
+
return false;
|
|
629
|
+
let reads = false;
|
|
630
|
+
recast.visit(node, {
|
|
631
|
+
visitIdentifier(path) {
|
|
632
|
+
if (renderReads.has(path.node))
|
|
633
|
+
reads = true;
|
|
634
|
+
return false;
|
|
635
|
+
},
|
|
636
|
+
});
|
|
637
|
+
return reads;
|
|
638
|
+
};
|
|
639
|
+
/**
|
|
640
|
+
* A padding by the device's inset, as `.safeAreaPadding(side)`: `paddingTop:
|
|
641
|
+
* insets.top`, or `insets.top + 24` with a padding of the space token beyond it
|
|
642
|
+
* on the same view (a top or bottom one: the language adds them).
|
|
643
|
+
*/
|
|
644
|
+
const insetPadding = (key, value) => {
|
|
645
|
+
const side = {
|
|
646
|
+
paddingTop: 'top',
|
|
647
|
+
paddingBottom: 'bottom',
|
|
648
|
+
paddingLeft: 'left',
|
|
649
|
+
paddingRight: 'right',
|
|
650
|
+
}[key];
|
|
651
|
+
if (!side)
|
|
652
|
+
return undefined;
|
|
653
|
+
const sum = n.BinaryExpression.check(value) && value.operator === '+' ? value : undefined;
|
|
654
|
+
const inset = sum ? sum.left : value;
|
|
655
|
+
if (insetReads.get(inset) !== side)
|
|
656
|
+
return undefined;
|
|
657
|
+
if (!sum)
|
|
658
|
+
return [{ name: 'safeAreaPadding', args: [side] }];
|
|
659
|
+
const beyond = n.NumericLiteral.check(sum.right) ? components_1.SPACE[String(sum.right.value)] : undefined;
|
|
660
|
+
if (beyond === undefined || (side !== 'top' && side !== 'bottom'))
|
|
661
|
+
return undefined;
|
|
662
|
+
return [
|
|
663
|
+
{ name: 'safeAreaPadding', args: [side] },
|
|
664
|
+
{ name: 'padding', args: [side, beyond] },
|
|
665
|
+
];
|
|
666
|
+
};
|
|
667
|
+
/**
|
|
668
|
+
* Each read of a `const` holding Bloom's `useSurfaceFill()`: the fill of the
|
|
669
|
+
* surface around the component, the language's `surfaceFill` role.
|
|
670
|
+
*/
|
|
671
|
+
const surfaceReads = new Set();
|
|
672
|
+
recast.visit(ast, {
|
|
673
|
+
visitIdentifier(path) {
|
|
674
|
+
const bindings = renderReads.has(path.node)
|
|
675
|
+
? scopeOf(path, path.node.name)?.getBindings()[path.node.name]
|
|
676
|
+
: undefined;
|
|
677
|
+
let at = bindings?.length === 1 ? bindings[0] : undefined;
|
|
678
|
+
while (at && !n.VariableDeclarator.check(at.node))
|
|
679
|
+
at = at.parent;
|
|
680
|
+
const init = at?.node.init;
|
|
681
|
+
const callee = n.CallExpression.check(init) ? init.callee : undefined;
|
|
682
|
+
const source = n.Identifier.check(callee) ? sources.get(callee.name) : undefined;
|
|
683
|
+
if (at?.parent?.node.kind === 'const' &&
|
|
684
|
+
n.Identifier.check(at.node.id) &&
|
|
685
|
+
at.node.id !== path.node &&
|
|
686
|
+
source?.module === '@oxy.so/bloom/styles' &&
|
|
687
|
+
source.imported === 'useSurfaceFill' &&
|
|
688
|
+
init.arguments.length === 0)
|
|
689
|
+
surfaceReads.add(path.node);
|
|
690
|
+
this.traverse(path);
|
|
691
|
+
},
|
|
692
|
+
});
|
|
693
|
+
/** Each `{name}` in a JSX attribute where `name` is a `const`: its initialiser (a Button's ink held in a variable). */
|
|
694
|
+
const constInits = new Map();
|
|
695
|
+
recast.visit(ast, {
|
|
696
|
+
visitJSXAttribute(path) {
|
|
697
|
+
const expression = n.JSXExpressionContainer.check(path.node.value)
|
|
698
|
+
? path.node.value.expression
|
|
699
|
+
: undefined;
|
|
700
|
+
if (n.Identifier.check(expression)) {
|
|
701
|
+
const bindings = scopeOf(path, expression.name)?.getBindings()[expression.name];
|
|
702
|
+
let at = bindings?.length === 1 ? bindings[0] : undefined;
|
|
703
|
+
while (at && !n.VariableDeclarator.check(at.node))
|
|
704
|
+
at = at.parent;
|
|
705
|
+
if (at?.parent?.node.kind === 'const' && n.Identifier.check(at.node.id) && at.node.init)
|
|
706
|
+
constInits.set(expression, at.node.init);
|
|
707
|
+
}
|
|
708
|
+
this.traverse(path);
|
|
709
|
+
},
|
|
710
|
+
});
|
|
711
|
+
/**
|
|
712
|
+
* A colour as the role it is painted from: a theme colour read from
|
|
713
|
+
* `useTheme()`, a choice of them by a plain condition (`undefined` for none),
|
|
714
|
+
* or a `const` holding one.
|
|
715
|
+
*/
|
|
716
|
+
/** The `const` reads `roleOf` followed: the before side writes their initialiser in their place. */
|
|
717
|
+
const roleConstReads = new Set();
|
|
718
|
+
const roleOf = (expression) => {
|
|
719
|
+
const role = themeRoles.get(expression);
|
|
720
|
+
if (role)
|
|
721
|
+
return role;
|
|
722
|
+
if (n.Identifier.check(expression) && expression.name === 'undefined')
|
|
723
|
+
return { $: 'absent' };
|
|
724
|
+
if (n.Identifier.check(expression) && constInits.has(expression)) {
|
|
725
|
+
const held = roleOf(constInits.get(expression));
|
|
726
|
+
if (held !== undefined)
|
|
727
|
+
roleConstReads.add(expression);
|
|
728
|
+
return held;
|
|
729
|
+
}
|
|
730
|
+
if (n.ConditionalExpression.check(expression) &&
|
|
731
|
+
(isPlainRead(expression.test) || isBooleanForm(expression.test))) {
|
|
732
|
+
const then = roleOf(expression.consequent);
|
|
733
|
+
const otherwise = roleOf(expression.alternate);
|
|
734
|
+
if (then !== undefined && otherwise !== undefined)
|
|
735
|
+
return { $: 'choose', test: { node: expression.test, hole: 'condition' }, then, otherwise };
|
|
736
|
+
}
|
|
737
|
+
// Bloom's `withAlpha(colors.primary, 0.1)` or `tokenAlpha(tokens.primary, 0.1)`: the role at
|
|
738
|
+
// that opacity token, `'primary/10'` (the web paints it from the language's ink sheet).
|
|
739
|
+
const callee = n.CallExpression.check(expression) ? expression.callee : undefined;
|
|
740
|
+
const alpha = n.Identifier.check(callee) ? sources.get(callee.name) : undefined;
|
|
741
|
+
if (alpha?.module === '@oxy.so/bloom/theme' &&
|
|
742
|
+
(alpha.imported === 'withAlpha' || alpha.imported === 'tokenAlpha') &&
|
|
743
|
+
expression.arguments.length === 2) {
|
|
744
|
+
const role = roleOf(expression.arguments[0]);
|
|
745
|
+
const opacity = OPACITY_TOKEN.get(staticNumber(expression.arguments[1]));
|
|
746
|
+
if (typeof role === 'string' && !role.includes('/') && opacity !== undefined)
|
|
747
|
+
return `${role}/${opacity}`;
|
|
748
|
+
}
|
|
749
|
+
return undefined;
|
|
750
|
+
};
|
|
751
|
+
/**
|
|
752
|
+
* A colour as a modifier's value: a theme colour as its role (outside a
|
|
753
|
+
* colour scope, where the role would paint the scope's scheme), a colour
|
|
754
|
+
* written out as an app colour, or a plain choice between them
|
|
755
|
+
* (`empty ? theme.colors.background : '#000000'`).
|
|
756
|
+
*/
|
|
757
|
+
const inkOf = (node, expression) => {
|
|
758
|
+
if (n.ConditionalExpression.check(expression) &&
|
|
759
|
+
(isPlainRead(expression.test) || isBooleanForm(expression.test))) {
|
|
760
|
+
const then = inkOf(node, expression.consequent);
|
|
761
|
+
const otherwise = inkOf(node, expression.alternate);
|
|
762
|
+
return then === undefined || otherwise === undefined
|
|
763
|
+
? undefined
|
|
764
|
+
: { $: 'choose', test: { node: expression.test, hole: 'condition' }, then, otherwise };
|
|
765
|
+
}
|
|
766
|
+
const role = roleOf(expression);
|
|
767
|
+
if (typeof role === 'string')
|
|
768
|
+
return insideOf(node, [COLOR_SCOPE]) ? undefined : role;
|
|
769
|
+
const value = staticString(expression);
|
|
770
|
+
if (value === undefined || !isAppColorValue(value) || colorConstants.has(appColorName(value)))
|
|
771
|
+
return undefined;
|
|
772
|
+
return { $: 'appColor', color: { name: appColorName(value), value } };
|
|
773
|
+
};
|
|
774
|
+
/** A language view placed as an element: `toElement(view)`, as a file partly in the language writes it. */
|
|
775
|
+
const isToElement = (expression) => {
|
|
776
|
+
const callee = n.CallExpression.check(expression) ? expression.callee : undefined;
|
|
777
|
+
const source = n.Identifier.check(callee) ? sources.get(callee.name) : undefined;
|
|
778
|
+
return source?.module === LANGUAGE_MODULE && source.imported === 'toElement';
|
|
779
|
+
};
|
|
780
|
+
/**
|
|
781
|
+
* Each `{name}` in JSX where `name` is a `const` holding an element
|
|
782
|
+
* (`const header = <PageHeader … />`, or `toElement(Header(…))`).
|
|
783
|
+
*/
|
|
411
784
|
const elementReads = new Set();
|
|
412
785
|
recast.visit(ast, {
|
|
413
786
|
visitJSXExpressionContainer(path) {
|
|
@@ -420,7 +793,9 @@ function transformSource(source, options) {
|
|
|
420
793
|
at = at.parent;
|
|
421
794
|
if (at?.parent?.node.kind === 'const' &&
|
|
422
795
|
n.Identifier.check(at.node.id) &&
|
|
423
|
-
(n.JSXElement.check(at.node.init) ||
|
|
796
|
+
(n.JSXElement.check(at.node.init) ||
|
|
797
|
+
n.JSXFragment.check(at.node.init) ||
|
|
798
|
+
isToElement(at.node.init)))
|
|
424
799
|
elementReads.add(expression);
|
|
425
800
|
this.traverse(path);
|
|
426
801
|
},
|
|
@@ -463,17 +838,197 @@ function transformSource(source, options) {
|
|
|
463
838
|
const appColors = new Map();
|
|
464
839
|
/** A `style` whose objects are the parity's before side, where `styles.x` is not in scope. */
|
|
465
840
|
const styleLiterals = new Map();
|
|
841
|
+
/** Each shorthand property's value (`{ width }`), which shares its key's place in the source. */
|
|
842
|
+
const shorthandValues = new Set();
|
|
843
|
+
recast.visit(ast, {
|
|
844
|
+
visitObjectProperty(path) {
|
|
845
|
+
if (path.node.shorthand)
|
|
846
|
+
shorthandValues.add(path.node.value);
|
|
847
|
+
this.traverse(path);
|
|
848
|
+
},
|
|
849
|
+
});
|
|
850
|
+
/** Points a style's chain draws without carrying them (a half-square radius's side): holes on the before side. */
|
|
851
|
+
const heldPoints = new Set();
|
|
852
|
+
/**
|
|
853
|
+
* Module-level `const NAME = { … }` (a style held in a constant, `satisfies`
|
|
854
|
+
* or `as` a type left out), by name: read as a StyleSheet's style is.
|
|
855
|
+
*/
|
|
856
|
+
const styleConstants = new Map();
|
|
857
|
+
for (const statement of ast.program.body) {
|
|
858
|
+
const declaration = n.ExportNamedDeclaration.check(statement)
|
|
859
|
+
? statement.declaration
|
|
860
|
+
: statement;
|
|
861
|
+
if (!n.VariableDeclaration.check(declaration) || declaration.kind !== 'const')
|
|
862
|
+
continue;
|
|
863
|
+
for (const declarator of declaration.declarations) {
|
|
864
|
+
let init = declarator.init;
|
|
865
|
+
while (init && (init.type === 'TSSatisfiesExpression' || init.type === 'TSAsExpression'))
|
|
866
|
+
init = init.expression;
|
|
867
|
+
if (n.Identifier.check(declarator.id) && n.ObjectExpression.check(init))
|
|
868
|
+
styleConstants.set(declarator.id.name, init);
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
/** Each name that reads a module style constant (not a name that shadows it): the constant's object. */
|
|
872
|
+
const styleConstantReads = new Map();
|
|
873
|
+
if (styleConstants.size > 0)
|
|
874
|
+
recast.visit(ast, {
|
|
875
|
+
visitIdentifier(path) {
|
|
876
|
+
const parent = path.parent?.node;
|
|
877
|
+
const declares = n.VariableDeclarator.check(parent) && parent.id === path.node;
|
|
878
|
+
if (!declares &&
|
|
879
|
+
styleConstants.has(path.node.name) &&
|
|
880
|
+
scopeOf(path, path.node.name)?.isGlobal)
|
|
881
|
+
styleConstantReads.set(path.node, styleConstants.get(path.node.name));
|
|
882
|
+
this.traverse(path);
|
|
883
|
+
},
|
|
884
|
+
});
|
|
885
|
+
/** Bloom's `tokens` (`@oxy.so/bloom/styles`), by its local name in the file. */
|
|
886
|
+
const bloomTokens = [...sources].find(([, s]) => s.module === '@oxy.so/bloom/styles' && s.imported === 'tokens')?.[0];
|
|
887
|
+
/** Bloom's scales the language holds, by their name in `tokens`. */
|
|
888
|
+
const TOKEN_SCALES = {
|
|
889
|
+
space: spec.scales.space,
|
|
890
|
+
borderRadius: spec.scales.corner,
|
|
891
|
+
fontSize: spec.scales.fontSize,
|
|
892
|
+
};
|
|
893
|
+
/** `tokens.space.sm` (`tokens.borderRadius.xs`, `tokens.fontSize._2xl`): the rung's points, the spec's own. */
|
|
894
|
+
const bloomTokenNumber = (node) => {
|
|
895
|
+
const scale = node?.object;
|
|
896
|
+
if (!bloomTokens ||
|
|
897
|
+
!n.MemberExpression.check(node) ||
|
|
898
|
+
node.computed ||
|
|
899
|
+
!n.MemberExpression.check(scale) ||
|
|
900
|
+
scale.computed ||
|
|
901
|
+
!n.Identifier.check(scale.object) ||
|
|
902
|
+
scale.object.name !== bloomTokens ||
|
|
903
|
+
!n.Identifier.check(scale.property) ||
|
|
904
|
+
!n.Identifier.check(node.property))
|
|
905
|
+
return undefined;
|
|
906
|
+
const value = TOKEN_SCALES[scale.property.name]?.[node.property.name.replace(/^_/, '')];
|
|
907
|
+
return typeof value === 'number' ? value : undefined;
|
|
908
|
+
};
|
|
909
|
+
/** React Native's `StyleSheet.absoluteFill`, read from the module's `StyleSheet`. */
|
|
910
|
+
const isAbsoluteFill = (node) => !!styleSheetLocal &&
|
|
911
|
+
n.MemberExpression.check(node) &&
|
|
912
|
+
!node.computed &&
|
|
913
|
+
n.Identifier.check(node.object) &&
|
|
914
|
+
node.object.name === styleSheetLocal &&
|
|
915
|
+
n.Identifier.check(node.property) &&
|
|
916
|
+
node.property.name === 'absoluteFill';
|
|
917
|
+
/** Bloom's `fontFamilyStyle` (`@oxy.so/bloom/typography`), by its local name in the file. */
|
|
918
|
+
const fontFamilyStyleLocal = [...sources].find(([, s]) => s.module === '@oxy.so/bloom/typography' && s.imported === 'fontFamilyStyle')?.[0];
|
|
919
|
+
/**
|
|
920
|
+
* A spread a style reads: `StyleSheet.absoluteFill`'s edges, and Bloom's
|
|
921
|
+
* `fontFamilyStyle('mono' | 'display')`, the face the language's
|
|
922
|
+
* `.font('mono')` and `.font('display')` set (the same family on every platform).
|
|
923
|
+
*/
|
|
924
|
+
const spreadOf = (node) => {
|
|
925
|
+
if (isAbsoluteFill(node))
|
|
926
|
+
return { classes: style_1.ABSOLUTE_FILL.classes, mods: [], literal: style_1.ABSOLUTE_FILL.literal };
|
|
927
|
+
if (fontFamilyStyleLocal &&
|
|
928
|
+
n.CallExpression.check(node) &&
|
|
929
|
+
n.Identifier.check(node.callee) &&
|
|
930
|
+
node.callee.name === fontFamilyStyleLocal &&
|
|
931
|
+
node.arguments.length === 1 &&
|
|
932
|
+
n.StringLiteral.check(node.arguments[0]) &&
|
|
933
|
+
spec.enums.fontFamily.includes(node.arguments[0].value))
|
|
934
|
+
return { classes: [], mods: [{ name: 'font', args: [node.arguments[0].value] }] };
|
|
935
|
+
return undefined;
|
|
936
|
+
};
|
|
937
|
+
/** No style: a branch of a choice that adds none. */
|
|
938
|
+
const noStyle = (node) => n.NullLiteral.check(node) ||
|
|
939
|
+
(n.BooleanLiteral.check(node) && !node.value) ||
|
|
940
|
+
(n.Identifier.check(node) && node.name === 'undefined');
|
|
941
|
+
/**
|
|
942
|
+
* A list part chosen by a plain condition: `test && style` or `test ? a : b`,
|
|
943
|
+
* its branches (null where it adds no style).
|
|
944
|
+
*/
|
|
945
|
+
function styleBranches(part) {
|
|
946
|
+
const plain = (test) => isPlainRead(test) || isBooleanForm(test);
|
|
947
|
+
if (n.LogicalExpression.check(part) && part.operator === '&&' && plain(part.left))
|
|
948
|
+
return { test: part.left, branches: [part.right, null] };
|
|
949
|
+
if (n.ConditionalExpression.check(part) && plain(part.test))
|
|
950
|
+
return {
|
|
951
|
+
test: part.test,
|
|
952
|
+
branches: [
|
|
953
|
+
noStyle(part.consequent) ? null : part.consequent,
|
|
954
|
+
noStyle(part.alternate) ? null : part.alternate,
|
|
955
|
+
],
|
|
956
|
+
};
|
|
957
|
+
return undefined;
|
|
958
|
+
}
|
|
466
959
|
/** A static style (an object literal, a module StyleSheet's style, or a list of them) as utilities. */
|
|
467
|
-
function staticStyle(expression, read
|
|
960
|
+
function staticStyle(expression, read,
|
|
961
|
+
/** A Pressable's style function's `pressed`: what it adds is the pressed variant (`active:`). */
|
|
962
|
+
pressed) {
|
|
468
963
|
const parts = n.ArrayExpression.check(expression)
|
|
469
964
|
? expression.elements
|
|
470
965
|
: [expression];
|
|
471
966
|
const classes = [];
|
|
472
967
|
const mods = [];
|
|
968
|
+
let choice;
|
|
473
969
|
// A StyleSheet's style is its literal on the before side; an inline object stays as written.
|
|
474
970
|
const literals = new Map();
|
|
475
971
|
for (const part of parts) {
|
|
972
|
+
// A style chosen by a plain condition (`!focused && styles.dim`, `compact ? A : B`): each
|
|
973
|
+
// branch's utilities, chosen as a className is (`dynamic-className`).
|
|
974
|
+
const chosen = styleBranches(part);
|
|
975
|
+
if (chosen && pressed && n.Identifier.check(chosen.test) && chosen.test.name === pressed) {
|
|
976
|
+
const [whenPressed, otherwise] = chosen.branches;
|
|
977
|
+
const style = whenPressed && !otherwise ? staticStyle(whenPressed, read) : undefined;
|
|
978
|
+
if (!style?.ok || style.mods.length || style.choice)
|
|
979
|
+
return {
|
|
980
|
+
ok: false,
|
|
981
|
+
subject: 'style',
|
|
982
|
+
why: 'a pressed style that is more than utilities added while pressed',
|
|
983
|
+
};
|
|
984
|
+
classes.push(...style.classes.map((name) => `active:${name}`));
|
|
985
|
+
continue;
|
|
986
|
+
}
|
|
987
|
+
if (chosen) {
|
|
988
|
+
// Inside a style function a choice reads its parameter, which the chain has no name for.
|
|
989
|
+
if (pressed)
|
|
990
|
+
return {
|
|
991
|
+
ok: false,
|
|
992
|
+
subject: 'style',
|
|
993
|
+
why: 'a pressed style chosen with another condition (`pressed || locked`)',
|
|
994
|
+
};
|
|
995
|
+
if (choice)
|
|
996
|
+
return { ok: false, subject: 'style', why: 'a style with two choices' };
|
|
997
|
+
const [whenTrue, whenFalse] = chosen.branches.map((branch) => branch ? staticStyle(branch, read) : { ok: true, classes: [], mods: [] });
|
|
998
|
+
if (!whenTrue.ok)
|
|
999
|
+
return whenTrue;
|
|
1000
|
+
if (!whenFalse.ok)
|
|
1001
|
+
return whenFalse;
|
|
1002
|
+
if (whenTrue.mods.length || whenFalse.mods.length || whenTrue.choice || whenFalse.choice)
|
|
1003
|
+
return {
|
|
1004
|
+
ok: false,
|
|
1005
|
+
subject: 'style',
|
|
1006
|
+
why: 'a style chosen at runtime whose branches write more than utilities',
|
|
1007
|
+
};
|
|
1008
|
+
choice = { test: chosen.test, whenTrue: whenTrue.classes, whenFalse: whenFalse.classes };
|
|
1009
|
+
continue;
|
|
1010
|
+
}
|
|
1011
|
+
// React Native's own fill, pinned to every edge: its declarations, written out on the before side.
|
|
1012
|
+
if (isAbsoluteFill(part)) {
|
|
1013
|
+
classes.push(...style_1.ABSOLUTE_FILL.classes);
|
|
1014
|
+
literals.set(part, `{ ${style_1.ABSOLUTE_FILL.literal} }`);
|
|
1015
|
+
continue;
|
|
1016
|
+
}
|
|
476
1017
|
let object = part;
|
|
1018
|
+
// A module constant holding a style is its object, written out on the before side.
|
|
1019
|
+
if (styleConstantReads.has(part)) {
|
|
1020
|
+
const style = (0, style_1.styleUtilities)(styleConstantReads.get(part), {
|
|
1021
|
+
read,
|
|
1022
|
+
spreadOf,
|
|
1023
|
+
numberOf: bloomTokenNumber,
|
|
1024
|
+
});
|
|
1025
|
+
if (!style.ok)
|
|
1026
|
+
return style;
|
|
1027
|
+
classes.push(...style.classes);
|
|
1028
|
+
mods.push(...style.mods);
|
|
1029
|
+
literals.set(part, style.literal);
|
|
1030
|
+
continue;
|
|
1031
|
+
}
|
|
477
1032
|
const referenced = n.MemberExpression.check(part) &&
|
|
478
1033
|
!part.computed &&
|
|
479
1034
|
n.Identifier.check(part.object) &&
|
|
@@ -484,19 +1039,61 @@ function transformSource(source, options) {
|
|
|
484
1039
|
? styleSheets.get(sheet.name)?.get(property.name)
|
|
485
1040
|
: undefined;
|
|
486
1041
|
}
|
|
487
|
-
const style = object
|
|
1042
|
+
const style = object
|
|
1043
|
+
? (0, style_1.styleUtilities)(object, {
|
|
1044
|
+
read,
|
|
1045
|
+
spreadOf,
|
|
1046
|
+
numberOf: bloomTokenNumber,
|
|
1047
|
+
computed: computedAtRender,
|
|
1048
|
+
})
|
|
1049
|
+
: undefined;
|
|
488
1050
|
if (!style)
|
|
489
1051
|
return { ok: false, subject: 'style', why: 'a style the codemod cannot read' };
|
|
490
1052
|
if (!style.ok)
|
|
491
1053
|
return style;
|
|
492
1054
|
classes.push(...style.classes);
|
|
493
1055
|
mods.push(...style.mods);
|
|
1056
|
+
for (const side of style.held)
|
|
1057
|
+
heldPoints.add(side);
|
|
494
1058
|
if (referenced)
|
|
495
1059
|
literals.set(part, style.literal);
|
|
496
1060
|
}
|
|
497
1061
|
for (const [part, literal] of literals)
|
|
498
1062
|
styleLiterals.set(part, literal);
|
|
499
|
-
return { ok: true, classes, mods };
|
|
1063
|
+
return { ok: true, classes, mods, choice };
|
|
1064
|
+
}
|
|
1065
|
+
/**
|
|
1066
|
+
* A Bloom component's static `style` as its node's box, for a component
|
|
1067
|
+
* that draws its `style` on its outer view, where the language draws a
|
|
1068
|
+
* node's box: each declaration through the className table as a View's
|
|
1069
|
+
* (`padding: 16` is `.padding('lg')`, `gap: 12` `.spacing('md')`,
|
|
1070
|
+
* `flexDirection: 'row'` `.axis('horizontal')`), every modifier one the node
|
|
1071
|
+
* takes. A colour, a variant or a value no token writes is a reason.
|
|
1072
|
+
*/
|
|
1073
|
+
function componentStyle(expression, node) {
|
|
1074
|
+
const reason = (subject, why) => ({
|
|
1075
|
+
reasons: [{ subject, code: `${node}:style`, why }],
|
|
1076
|
+
});
|
|
1077
|
+
const style = staticStyle(expression, () => undefined);
|
|
1078
|
+
if (!style.ok)
|
|
1079
|
+
return reason(style.subject, style.why);
|
|
1080
|
+
const resolution = (0, resolve_1.resolveClassName)(style.classes.join(' '), 'view', options.table, {
|
|
1081
|
+
overridden: options.overridden,
|
|
1082
|
+
});
|
|
1083
|
+
if (!resolution.ok)
|
|
1084
|
+
return { reasons: [...resolution.reasons] };
|
|
1085
|
+
if (resolution.value.variants.length)
|
|
1086
|
+
return reason('style', 'a style with a variant');
|
|
1087
|
+
const mods = [
|
|
1088
|
+
...(resolution.value.axis === 'row' ? [{ name: 'axis', args: ['horizontal'] }] : []),
|
|
1089
|
+
...resolution.value.mods,
|
|
1090
|
+
...style.mods.map(({ mod }) => mod),
|
|
1091
|
+
];
|
|
1092
|
+
const taken = NODE_MODIFIERS.get(node);
|
|
1093
|
+
const outside = mods.find((mod) => !taken?.has(mod.name));
|
|
1094
|
+
if (outside)
|
|
1095
|
+
return reason('style', `a style that is \`.${outside.name}()\`, which ${node} does not take`);
|
|
1096
|
+
return { mods: mods.map((mod) => ({ name: mod.name, args: [...mod.args] })) };
|
|
500
1097
|
}
|
|
501
1098
|
/** Each element's nearest enclosing element in the file. */
|
|
502
1099
|
const enclosing = new Map();
|
|
@@ -510,6 +1107,15 @@ function transformSource(source, options) {
|
|
|
510
1107
|
this.traverse(path);
|
|
511
1108
|
},
|
|
512
1109
|
});
|
|
1110
|
+
/** Whether a Bloom component's element is around `node` in the file. */
|
|
1111
|
+
function insideBloomComponent(node) {
|
|
1112
|
+
for (let at = enclosing.get(node); at; at = enclosing.get(at)) {
|
|
1113
|
+
const local = jsxName(at);
|
|
1114
|
+
if (local && sources.get(local)?.module.startsWith('@oxy.so/bloom/'))
|
|
1115
|
+
return true;
|
|
1116
|
+
}
|
|
1117
|
+
return false;
|
|
1118
|
+
}
|
|
513
1119
|
/** The first element around `node` (in the file) drawn by one of `components` (`module#export`). */
|
|
514
1120
|
function insideOf(node, components) {
|
|
515
1121
|
for (let at = enclosing.get(node); at; at = enclosing.get(at)) {
|
|
@@ -521,43 +1127,248 @@ function transformSource(source, options) {
|
|
|
521
1127
|
return undefined;
|
|
522
1128
|
}
|
|
523
1129
|
/**
|
|
524
|
-
* Each
|
|
525
|
-
* async, no return type, its name read nowhere else) whose every return is a
|
|
1130
|
+
* Each screen: a module-level function component whose every return is a
|
|
526
1131
|
* Bloom Screen becomes `screen(function …)`, each Screen its body's root.
|
|
1132
|
+
* `screen()` wraps the whole component, so its branches (a loading, an
|
|
1133
|
+
* error, the content) each return their content and chrome. A component is
|
|
1134
|
+
* a function declaration (exported or not) or a `const` holding an arrow or
|
|
1135
|
+
* a function expression, named with a capital, not async, with no return
|
|
1136
|
+
* type; a `const` or a declaration whose name is read becomes
|
|
1137
|
+
* `const Name = screen(function Name …)`, so its name is never read before
|
|
1138
|
+
* the module has defined it.
|
|
527
1139
|
*/
|
|
528
1140
|
const screenRoutes = new Map();
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
const
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
1141
|
+
/** Where each screen's function is declared: the statement that holds it, and its name. */
|
|
1142
|
+
const screenDeclarations = new Map();
|
|
1143
|
+
/** The Screens of a component whose name is read before it is declared. */
|
|
1144
|
+
const earlyScreens = new Map();
|
|
1145
|
+
const isScreenElement = (node) => {
|
|
1146
|
+
const local = n.JSXElement.check(node) ? jsxName(node) : undefined;
|
|
1147
|
+
const source = local ? sources.get(local) : undefined;
|
|
1148
|
+
return source?.module === '@oxy.so/bloom/screen' && source.imported === 'Screen';
|
|
1149
|
+
};
|
|
1150
|
+
const peeled = new Map();
|
|
1151
|
+
function peelScreen(node) {
|
|
1152
|
+
if (peeled.has(node))
|
|
1153
|
+
return peeled.get(node);
|
|
1154
|
+
let result;
|
|
1155
|
+
if (isScreenElement(node))
|
|
1156
|
+
result = { screen: node, mods: [] };
|
|
1157
|
+
else if (n.JSXElement.check(node)) {
|
|
1158
|
+
const children = node.children.filter((child) => !(n.JSXText.check(child) && !child.value.trim()));
|
|
1159
|
+
const inner = children.length === 1 ? peelScreen(children[0]) : undefined;
|
|
1160
|
+
if (inner && 'mods' in inner) {
|
|
1161
|
+
const wrapper = screenWrapper(node);
|
|
1162
|
+
result =
|
|
1163
|
+
'mod' in wrapper
|
|
1164
|
+
? { screen: inner.screen, mods: [...inner.mods, wrapper.mod] }
|
|
1165
|
+
: { screen: inner.screen, reason: wrapper.reason };
|
|
1166
|
+
}
|
|
1167
|
+
else
|
|
1168
|
+
result = inner;
|
|
1169
|
+
}
|
|
1170
|
+
peeled.set(node, result);
|
|
1171
|
+
return result;
|
|
1172
|
+
}
|
|
1173
|
+
/** The one element a wrapper around a route's Screen holds. */
|
|
1174
|
+
const peelChild = (node) => node.children.find((child) => n.JSXElement.check(child));
|
|
1175
|
+
/** One element around a route's Screen as its screen modifier, or why it is none. */
|
|
1176
|
+
function screenWrapper(node) {
|
|
1177
|
+
const local = jsxName(node) ?? 'an element';
|
|
1178
|
+
const source = sources.get(local);
|
|
1179
|
+
const reason = (why) => ({
|
|
1180
|
+
reason: {
|
|
1181
|
+
subject: 'Screen',
|
|
1182
|
+
code: 'screen:wrapped',
|
|
1183
|
+
why: `inside \`${local}\`: ${why}`,
|
|
541
1184
|
},
|
|
542
1185
|
});
|
|
543
|
-
const
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
1186
|
+
const attributes = node.openingElement.attributes.filter((a) => n.JSXAttribute.check(a));
|
|
1187
|
+
if (attributes.length !== node.openingElement.attributes.length)
|
|
1188
|
+
return reason('spread props: the codemod cannot see what they set');
|
|
1189
|
+
const named = new Map(attributes.map((a) => [a.name.name, a.value]));
|
|
1190
|
+
const only = (...names) => [...named.keys()].every((name) => names.includes(name));
|
|
1191
|
+
if (source?.module === '@oxy.so/bloom/theme' && source.imported === 'BloomColorScope') {
|
|
1192
|
+
const asChild = named.get('asChild');
|
|
1193
|
+
const mode = named.get('mode');
|
|
1194
|
+
if (!only('asChild', 'mode') || !named.has('asChild') || !named.has('mode'))
|
|
1195
|
+
return reason('a colour scope is `.colorScope(mode)` when it takes only `mode` and `asChild` (a preset, a seed or its own view are its own)');
|
|
1196
|
+
const bare = asChild === null ||
|
|
1197
|
+
(n.JSXExpressionContainer.check(asChild) &&
|
|
1198
|
+
n.BooleanLiteral.check(asChild.expression) &&
|
|
1199
|
+
asChild.expression.value === true);
|
|
1200
|
+
if (!bare)
|
|
1201
|
+
return reason('a colour scope that is no `asChild` draws a view of its own');
|
|
1202
|
+
const scheme = staticString(mode);
|
|
1203
|
+
if (scheme !== undefined && scheme !== 'light' && scheme !== 'dark')
|
|
1204
|
+
return reason(`a scheme \`${scheme}\` that is neither light nor dark`);
|
|
1205
|
+
return {
|
|
1206
|
+
mod: {
|
|
1207
|
+
name: 'colorScope',
|
|
1208
|
+
args: [scheme ?? { node: attributeExpression(mode), hole: 'string' }],
|
|
1209
|
+
},
|
|
1210
|
+
};
|
|
1211
|
+
}
|
|
1212
|
+
if (source?.module === 'react-native' && source.imported === 'KeyboardAvoidingView') {
|
|
1213
|
+
if (!only('style', 'behavior'))
|
|
1214
|
+
return reason('a keyboard avoiding view with props `.keyboardAvoiding()` does not draw');
|
|
1215
|
+
const style = named.get('style');
|
|
1216
|
+
const fill = style ? staticStyle(attributeExpression(style), () => undefined) : undefined;
|
|
1217
|
+
const filled = fill?.ok && fill.classes.length === 0 && fill.mods.length === 1
|
|
1218
|
+
? fill.mods[0]?.mod
|
|
1219
|
+
: undefined;
|
|
1220
|
+
if (filled?.name !== 'grow' && filled?.name !== 'flex')
|
|
1221
|
+
return reason('a style other than a fill `.keyboardAvoiding()` draws (React Native’s `flex: 1`, or a flex token)');
|
|
1222
|
+
const behavior = attributeExpression(named.get('behavior'));
|
|
1223
|
+
if (!isKeyboardBehavior(behavior))
|
|
1224
|
+
return reason("a behavior other than React Native’s (`Platform.OS === 'ios' ? 'padding' : 'height'`)");
|
|
1225
|
+
keyboardBehaviors.add(behavior);
|
|
1226
|
+
return {
|
|
1227
|
+
mod: { name: 'keyboardAvoiding', args: filled.name === 'flex' ? [...filled.args] : [] },
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
if (source && /Provider$/.test(local) && !source.module.startsWith('react-native')) {
|
|
1231
|
+
if (named.size > 0)
|
|
1232
|
+
return reason('a provider with props: `.provider()` draws one with none');
|
|
1233
|
+
return { mod: { name: 'provider', args: [{ $: 'component', name: local }] } };
|
|
1234
|
+
}
|
|
1235
|
+
return reason('`screen()` wraps the whole component, so what is around the Screen would draw inside it (a colour scope, a keyboard avoiding view and an app’s provider are screen modifiers)');
|
|
1236
|
+
}
|
|
1237
|
+
/** `Platform.OS === 'ios' ? 'padding' : 'height'`, React Native's behavior, as a route writes it. */
|
|
1238
|
+
const isKeyboardBehavior = (expression) => {
|
|
1239
|
+
if (!n.ConditionalExpression.check(expression))
|
|
1240
|
+
return false;
|
|
1241
|
+
const { test, consequent, alternate } = expression;
|
|
1242
|
+
const platform = n.BinaryExpression.check(test) && test.operator === '===' ? test.left : undefined;
|
|
1243
|
+
return (n.MemberExpression.check(platform) &&
|
|
1244
|
+
n.Identifier.check(platform.object) &&
|
|
1245
|
+
sources.get(platform.object.name)?.module === 'react-native' &&
|
|
1246
|
+
sources.get(platform.object.name)?.imported === 'Platform' &&
|
|
1247
|
+
n.Identifier.check(platform.property) &&
|
|
1248
|
+
platform.property.name === 'OS' &&
|
|
1249
|
+
staticString(test.right) === 'ios' &&
|
|
1250
|
+
staticString(consequent) === 'padding' &&
|
|
1251
|
+
staticString(alternate) === 'height');
|
|
1252
|
+
};
|
|
1253
|
+
/** The behaviors a converted keyboard avoiding view wrote: the parity's before side writes React Native's own `Platform`. */
|
|
1254
|
+
const keyboardBehaviors = new Set();
|
|
1255
|
+
/** Each route's Screen: the return around it, and the screen modifiers what is around it draws. */
|
|
1256
|
+
const screenOuter = new Map();
|
|
1257
|
+
/** Where each module-level name is read outside a function, before its own statement. */
|
|
1258
|
+
const readsBefore = (name, statement) => {
|
|
1259
|
+
let early = false;
|
|
550
1260
|
recast.visit(ast, {
|
|
1261
|
+
visitFunction: () => false,
|
|
551
1262
|
visitIdentifier(path) {
|
|
552
|
-
if (path.node.name === name)
|
|
553
|
-
|
|
1263
|
+
if (path.node.name === name && path.node.start < statement.start)
|
|
1264
|
+
early = true;
|
|
554
1265
|
this.traverse(path);
|
|
555
1266
|
},
|
|
556
1267
|
});
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
1268
|
+
return early;
|
|
1269
|
+
};
|
|
1270
|
+
for (const statement of ast.program.body) {
|
|
1271
|
+
const declaration = n.ExportDefaultDeclaration.check(statement) || n.ExportNamedDeclaration.check(statement)
|
|
1272
|
+
? statement.declaration
|
|
1273
|
+
: statement;
|
|
1274
|
+
let fn;
|
|
1275
|
+
let name;
|
|
1276
|
+
if (n.FunctionDeclaration.check(declaration)) {
|
|
1277
|
+
fn = declaration;
|
|
1278
|
+
name = declaration.id?.name;
|
|
1279
|
+
}
|
|
1280
|
+
else if (n.VariableDeclaration.check(declaration) &&
|
|
1281
|
+
declaration.kind === 'const' &&
|
|
1282
|
+
declaration.declarations.length === 1) {
|
|
1283
|
+
const declarator = declaration.declarations[0];
|
|
1284
|
+
if (n.Identifier.check(declarator.id) &&
|
|
1285
|
+
!declarator.id.typeAnnotation &&
|
|
1286
|
+
(n.ArrowFunctionExpression.check(declarator.init) ||
|
|
1287
|
+
(n.FunctionExpression.check(declarator.init) && !declarator.init.id))) {
|
|
1288
|
+
fn = declarator.init;
|
|
1289
|
+
name = declarator.id.name;
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
if (!fn || !name || !/^[A-Z]/.test(name) || fn.async || fn.generator || fn.returnType)
|
|
1293
|
+
continue;
|
|
1294
|
+
const returned = [];
|
|
1295
|
+
if (!n.BlockStatement.check(fn.body))
|
|
1296
|
+
returned.push(fn.body);
|
|
1297
|
+
else
|
|
1298
|
+
recast.visit(fn.body, {
|
|
1299
|
+
visitFunction: () => false,
|
|
1300
|
+
visitReturnStatement(path) {
|
|
1301
|
+
returned.push(path.node.argument);
|
|
1302
|
+
return false;
|
|
1303
|
+
},
|
|
1304
|
+
});
|
|
1305
|
+
const screens = returned.map(peelScreen);
|
|
1306
|
+
if (!returned.length || !screens.every((p) => p !== undefined))
|
|
1307
|
+
continue;
|
|
1308
|
+
// A Screen inside an element that is no screen modifier stays JSX, with what keeps it.
|
|
1309
|
+
const kept = screens.find((p) => 'reason' in p);
|
|
1310
|
+
if (kept) {
|
|
1311
|
+
for (const p of screens)
|
|
1312
|
+
earlyScreens.set(p.screen, 'reason' in p ? p.reason : kept.reason);
|
|
1313
|
+
continue;
|
|
1314
|
+
}
|
|
1315
|
+
if (readsBefore(name, statement)) {
|
|
1316
|
+
for (const { screen: node } of screens)
|
|
1317
|
+
earlyScreens.set(node, {
|
|
1318
|
+
subject: 'Screen',
|
|
1319
|
+
code: 'screen:read-early',
|
|
1320
|
+
why: `\`${name}\` is read before its declaration, where \`const ${name} = screen(…)\` is not yet defined`,
|
|
1321
|
+
});
|
|
1322
|
+
continue;
|
|
1323
|
+
}
|
|
1324
|
+
returned.forEach((outer, i) => {
|
|
1325
|
+
const { screen, mods } = screens[i];
|
|
1326
|
+
screenRoutes.set(screen, fn);
|
|
1327
|
+
if (outer !== screen)
|
|
1328
|
+
screenOuter.set(screen, { outer, mods });
|
|
1329
|
+
});
|
|
1330
|
+
screenDeclarations.set(fn, { statement, name });
|
|
560
1331
|
}
|
|
1332
|
+
/**
|
|
1333
|
+
* Why a Screen that is no screen's return stays JSX: what is around it, or
|
|
1334
|
+
* the component's other returns. `screen()` wraps the whole component, so a
|
|
1335
|
+
* view beside the Screen or around it would draw inside it.
|
|
1336
|
+
*/
|
|
1337
|
+
const screenPlaces = new Map(earlyScreens);
|
|
1338
|
+
recast.visit(ast, {
|
|
1339
|
+
visitJSXElement(path) {
|
|
1340
|
+
const node = path.node;
|
|
1341
|
+
if (isScreenElement(node) && !screenRoutes.has(node) && !earlyScreens.has(node)) {
|
|
1342
|
+
// Past what its screen modifiers draw (a colour scope, a keyboard avoiding view, a provider).
|
|
1343
|
+
let at = path;
|
|
1344
|
+
while (n.JSXElement.check(at.parent?.node) &&
|
|
1345
|
+
peelScreen(at.parent.node)?.screen === node &&
|
|
1346
|
+
'mods' in peelScreen(at.parent.node))
|
|
1347
|
+
at = at.parent;
|
|
1348
|
+
const outer = at.node;
|
|
1349
|
+
const parent = at.parent?.node;
|
|
1350
|
+
const name = (element) => `\`${jsxName(element) ?? 'an element'}\``;
|
|
1351
|
+
const beside = n.JSXFragment.check(parent)
|
|
1352
|
+
? parent.children.filter((child) => child !== outer && n.JSXElement.check(child))
|
|
1353
|
+
: [];
|
|
1354
|
+
const wrapper = n.JSXElement.check(parent) ? screenWrapper(parent) : undefined;
|
|
1355
|
+
screenPlaces.set(node, wrapper && 'reason' in wrapper
|
|
1356
|
+
? wrapper.reason
|
|
1357
|
+
: beside.length
|
|
1358
|
+
? {
|
|
1359
|
+
subject: 'Screen',
|
|
1360
|
+
code: 'screen:beside',
|
|
1361
|
+
why: `beside ${beside.map(name).join(', ')}: \`screen()\` wraps the whole component, so a view beside the Screen would draw inside it (one that draws nothing, a head element, moves into the Screen by hand)`,
|
|
1362
|
+
}
|
|
1363
|
+
: {
|
|
1364
|
+
subject: 'Screen',
|
|
1365
|
+
code: 'screen:returns',
|
|
1366
|
+
why: 'a return of its component that is no Screen (`null`, another view, a choice): `screen()` would draw a Screen around it',
|
|
1367
|
+
});
|
|
1368
|
+
}
|
|
1369
|
+
this.traverse(path);
|
|
1370
|
+
},
|
|
1371
|
+
});
|
|
561
1372
|
/**
|
|
562
1373
|
* An icon's fill as the chain paints it: a Bloom theme colour read from
|
|
563
1374
|
* `useTheme()` is its role, and a static colour (a literal, or a module
|
|
@@ -656,6 +1467,10 @@ function transformSource(source, options) {
|
|
|
656
1467
|
return convertButton(node);
|
|
657
1468
|
if (kind === 'kept' && screenRoutes.has(node))
|
|
658
1469
|
return convertScreen(node);
|
|
1470
|
+
if (kind === 'kept' && sources.get(local)?.module === FAB_MODULE)
|
|
1471
|
+
return convertFab(node);
|
|
1472
|
+
if (kind === 'kept' && sources.get(local)?.module === DROPDOWN)
|
|
1473
|
+
return convertDropdownMenu(node);
|
|
659
1474
|
if (kind === 'kept') {
|
|
660
1475
|
const source = sources.get(local);
|
|
661
1476
|
const removed = components_1.REMOVED[`${source.module}#${source.imported}`];
|
|
@@ -667,11 +1482,11 @@ function transformSource(source, options) {
|
|
|
667
1482
|
code: `removed:${source.imported}`,
|
|
668
1483
|
why: `removed in Bloom 22: use ${removed}`,
|
|
669
1484
|
}
|
|
670
|
-
: {
|
|
1485
|
+
: (screenPlaces.get(node) ?? {
|
|
671
1486
|
subject: local,
|
|
672
1487
|
code: `kept:${source.imported}`,
|
|
673
1488
|
why: components_1.KEPT[`${source.module}#${source.imported}`],
|
|
674
|
-
},
|
|
1489
|
+
}),
|
|
675
1490
|
],
|
|
676
1491
|
};
|
|
677
1492
|
}
|
|
@@ -704,8 +1519,13 @@ function transformSource(source, options) {
|
|
|
704
1519
|
/** The utilities a static `style` writes, read with the className. */
|
|
705
1520
|
const styleClasses = [];
|
|
706
1521
|
const styleMods = [];
|
|
1522
|
+
let styleChoice;
|
|
707
1523
|
/** An icon's `width`, `height` and `fill`. */
|
|
708
1524
|
const iconProps = new Map();
|
|
1525
|
+
/** A TouchableOpacity: its press fades it (`.pressFade()`), at its `activeOpacity` token. */
|
|
1526
|
+
const fades = kind === 'pressable' &&
|
|
1527
|
+
sources.get(local)?.imported === 'TouchableOpacity';
|
|
1528
|
+
let fade;
|
|
709
1529
|
for (const attribute of node.openingElement.attributes) {
|
|
710
1530
|
if (!n.JSXAttribute.check(attribute) || !n.JSXIdentifier.check(attribute.name)) {
|
|
711
1531
|
fail('{...spread}', 'spread', 'spread props: the codemod cannot see what they set');
|
|
@@ -768,6 +1588,44 @@ function transformSource(source, options) {
|
|
|
768
1588
|
props.push({ name: 'disabled', args: [{ node: expression, hole: 'boolean' }] });
|
|
769
1589
|
continue;
|
|
770
1590
|
}
|
|
1591
|
+
if (fades && name === 'activeOpacity') {
|
|
1592
|
+
const token = OPACITY_TOKEN.get(staticNumber(value));
|
|
1593
|
+
if (token === undefined)
|
|
1594
|
+
fail(name, 'prop:activeOpacity', 'an activeOpacity that is no opacity token, or set at runtime');
|
|
1595
|
+
else
|
|
1596
|
+
fade = token;
|
|
1597
|
+
continue;
|
|
1598
|
+
}
|
|
1599
|
+
if (kind !== 'icon' && name === 'accessibilityLiveRegion') {
|
|
1600
|
+
const region = staticString(value);
|
|
1601
|
+
if (region !== undefined && LIVE_REGIONS.has(region))
|
|
1602
|
+
props.push({ name: 'liveRegion', args: [region] });
|
|
1603
|
+
else
|
|
1604
|
+
fail(name, 'prop:accessibilityLiveRegion', 'a live region set at runtime');
|
|
1605
|
+
continue;
|
|
1606
|
+
}
|
|
1607
|
+
if (kind !== 'icon' && name === 'accessibilityState') {
|
|
1608
|
+
props.push({ name: 'accessibilityState', args: [{ node: expression, hole: 'state' }] });
|
|
1609
|
+
continue;
|
|
1610
|
+
}
|
|
1611
|
+
if (kind !== 'icon' && name === 'accessibilityValue') {
|
|
1612
|
+
props.push({ name: 'accessibilityValue', args: [{ node: expression, hole: 'state' }] });
|
|
1613
|
+
continue;
|
|
1614
|
+
}
|
|
1615
|
+
// The escape hatches, on the view's outermost box as React Native's own.
|
|
1616
|
+
if (kind !== 'icon' && (name === 'ref' || name === 'onLayout')) {
|
|
1617
|
+
props.push({ name, args: [{ node: expression, hole: 'handler' }] });
|
|
1618
|
+
continue;
|
|
1619
|
+
}
|
|
1620
|
+
if (kind !== 'icon' && name === 'hitSlop') {
|
|
1621
|
+
const calls = hitSlopCalls(value);
|
|
1622
|
+
if (calls === undefined)
|
|
1623
|
+
fail(name, 'prop:hitSlop', 'a hit slop whose sides are no hit slop tokens, or set at runtime');
|
|
1624
|
+
else
|
|
1625
|
+
for (const args of calls)
|
|
1626
|
+
props.push({ name: 'hitSlop', args });
|
|
1627
|
+
continue;
|
|
1628
|
+
}
|
|
771
1629
|
if ((kind === 'bloomText' || kind === 'rnText') && name === 'numberOfLines') {
|
|
772
1630
|
numberOfLines = staticNumber(value) ?? { node: expression, hole: 'number' };
|
|
773
1631
|
continue;
|
|
@@ -783,8 +1641,25 @@ function transformSource(source, options) {
|
|
|
783
1641
|
if (name === 'style' && kind !== 'icon') {
|
|
784
1642
|
// A theme colour read from `useTheme()` here is its role, as an icon's fill is.
|
|
785
1643
|
const read = (key, read) => {
|
|
786
|
-
const
|
|
787
|
-
if (
|
|
1644
|
+
const inset = insetPadding(key, read);
|
|
1645
|
+
if (inset)
|
|
1646
|
+
return inset;
|
|
1647
|
+
// The surface fill, read where the component is: the same surface the view sits on unless
|
|
1648
|
+
// a Bloom component between them (a Card, a Surface) publishes one of its own.
|
|
1649
|
+
if (surfaceReads.has(read) && !insideBloomComponent(node)) {
|
|
1650
|
+
if (key === 'backgroundColor')
|
|
1651
|
+
return { name: 'background', args: ['surfaceFill'] };
|
|
1652
|
+
if (key === 'color' && (kind === 'bloomText' || kind === 'rnText'))
|
|
1653
|
+
return { name: 'foreground', args: ['surfaceFill'] };
|
|
1654
|
+
}
|
|
1655
|
+
if (kind === 'bloomText' || kind === 'rnText') {
|
|
1656
|
+
const text = (0, style_1.textDeclaration)(key, read);
|
|
1657
|
+
if (text)
|
|
1658
|
+
return text;
|
|
1659
|
+
}
|
|
1660
|
+
// A role, or a role at an opacity (`withAlpha(colors.primary, 0.1)`).
|
|
1661
|
+
const role = roleOf(read);
|
|
1662
|
+
if (typeof role !== 'string' || insideOf(node, [COLOR_SCOPE]))
|
|
788
1663
|
return undefined;
|
|
789
1664
|
if (key === 'backgroundColor')
|
|
790
1665
|
return { name: 'background', args: [role] };
|
|
@@ -792,10 +1667,25 @@ function transformSource(source, options) {
|
|
|
792
1667
|
return { name: 'foreground', args: [role] };
|
|
793
1668
|
return undefined;
|
|
794
1669
|
};
|
|
795
|
-
|
|
1670
|
+
// A Pressable's style of its press state (`({ pressed }) => [base, pressed && dim]`): `.when('pressed')`.
|
|
1671
|
+
const fn = kind === 'pressable' &&
|
|
1672
|
+
n.ArrowFunctionExpression.check(expression) &&
|
|
1673
|
+
expression.params.length === 1 &&
|
|
1674
|
+
n.ObjectPattern.check(expression.params[0]) &&
|
|
1675
|
+
!n.BlockStatement.check(expression.body)
|
|
1676
|
+
? expression
|
|
1677
|
+
: undefined;
|
|
1678
|
+
const pressedProperty = fn?.params[0].properties.find((p) => (n.ObjectProperty.check(p) || n.Property.check(p)) &&
|
|
1679
|
+
n.Identifier.check(p.key) &&
|
|
1680
|
+
p.key.name === 'pressed' &&
|
|
1681
|
+
n.Identifier.check(p.value));
|
|
1682
|
+
const style = fn && pressedProperty && fn.params[0].properties.length === 1
|
|
1683
|
+
? staticStyle(fn.body, read, pressedProperty.value.name)
|
|
1684
|
+
: staticStyle(expression, read);
|
|
796
1685
|
if (style.ok) {
|
|
797
1686
|
styleClasses.push(...style.classes);
|
|
798
1687
|
styleMods.push(...style.mods);
|
|
1688
|
+
styleChoice = style.choice;
|
|
799
1689
|
}
|
|
800
1690
|
else
|
|
801
1691
|
fail(style.subject, 'style', style.why);
|
|
@@ -803,7 +1693,7 @@ function transformSource(source, options) {
|
|
|
803
1693
|
}
|
|
804
1694
|
// What takes touches: `none` nothing (SwiftUI's `allowsHitTesting(false)`), `box-none` what is inside only, `box-only` the view only.
|
|
805
1695
|
if (name === 'pointerEvents' && (kind === 'view' || kind === 'pressable')) {
|
|
806
|
-
const hitTesting =
|
|
1696
|
+
const hitTesting = hitTestingOf(attributeExpression(value));
|
|
807
1697
|
if (hitTesting !== undefined)
|
|
808
1698
|
props.push({ name: 'allowsHitTesting', args: [hitTesting] });
|
|
809
1699
|
else
|
|
@@ -828,6 +1718,18 @@ function transformSource(source, options) {
|
|
|
828
1718
|
}
|
|
829
1719
|
fail(name, `prop:${name}`, `the prop \`${name}\` has no mapping`);
|
|
830
1720
|
}
|
|
1721
|
+
// A style chosen by a condition is the className choice it writes: what each branch adds.
|
|
1722
|
+
if (styleChoice) {
|
|
1723
|
+
if (other)
|
|
1724
|
+
fail('style', 'dynamic-className', 'a className and a style both chosen at runtime');
|
|
1725
|
+
else {
|
|
1726
|
+
other = {
|
|
1727
|
+
test: { node: styleChoice.test, hole: 'condition' },
|
|
1728
|
+
className: [className, ...styleChoice.whenFalse].filter(Boolean).join(' '),
|
|
1729
|
+
};
|
|
1730
|
+
className = [className, ...styleChoice.whenTrue].filter(Boolean).join(' ');
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
831
1733
|
for (const token of `${className} ${other?.className ?? ''}`.split(/\s+/).filter(Boolean))
|
|
832
1734
|
classes.add(token);
|
|
833
1735
|
if (styleClasses.length) {
|
|
@@ -1012,6 +1914,8 @@ function transformSource(source, options) {
|
|
|
1012
1914
|
return { reasons };
|
|
1013
1915
|
if (isPressableScale(sources.get(local)))
|
|
1014
1916
|
props.push({ name: 'pressScale', args: [] });
|
|
1917
|
+
if (fades)
|
|
1918
|
+
props.push({ name: 'pressFade', args: fade === undefined ? [] : [fade] });
|
|
1015
1919
|
return { ir: { type: 'stack', resolved: resolution.value, choice, props, key, children } };
|
|
1016
1920
|
}
|
|
1017
1921
|
/**
|
|
@@ -1043,6 +1947,7 @@ function transformSource(source, options) {
|
|
|
1043
1947
|
const props = inner.ir.props;
|
|
1044
1948
|
if (!props.some((prop) => prop.name === 'header'))
|
|
1045
1949
|
props.unshift({ name: 'header', args: [false] });
|
|
1950
|
+
props.push(...(screenOuter.get(node)?.mods ?? []));
|
|
1046
1951
|
return { ir: { type: 'screen', content: root, props } };
|
|
1047
1952
|
}
|
|
1048
1953
|
/**
|
|
@@ -1088,14 +1993,13 @@ function transformSource(source, options) {
|
|
|
1088
1993
|
* nothing the other modifiers set: the variant adds it, overriding nothing.
|
|
1089
1994
|
*/
|
|
1090
1995
|
function branchesOnly(chain, other) {
|
|
1091
|
-
if (chain.axis !== other.axis ||
|
|
1092
|
-
JSON.stringify(chain.variants) !== JSON.stringify(other.variants))
|
|
1996
|
+
if (chain.axis !== other.axis || (0, table_1.modKey)(chain.variants) !== (0, table_1.modKey)(other.variants))
|
|
1093
1997
|
return undefined;
|
|
1094
|
-
const rest = other.mods.map(
|
|
1998
|
+
const rest = other.mods.map(table_1.modKey);
|
|
1095
1999
|
const both = [];
|
|
1096
2000
|
const whenTrue = [];
|
|
1097
2001
|
for (const mod of chain.mods) {
|
|
1098
|
-
const at = rest.indexOf(
|
|
2002
|
+
const at = rest.indexOf((0, table_1.modKey)(mod));
|
|
1099
2003
|
if (at < 0)
|
|
1100
2004
|
whenTrue.push(mod);
|
|
1101
2005
|
else {
|
|
@@ -1114,7 +2018,7 @@ function transformSource(source, options) {
|
|
|
1114
2018
|
*/
|
|
1115
2019
|
function sameShape(chain, other) {
|
|
1116
2020
|
const same = chain.axis === other.axis &&
|
|
1117
|
-
|
|
2021
|
+
(0, table_1.modKey)(chain.variants) === (0, table_1.modKey)(other.variants) &&
|
|
1118
2022
|
chain.mods.length === other.mods.length &&
|
|
1119
2023
|
chain.mods.every((mod, i) => {
|
|
1120
2024
|
const at = other.mods[i];
|
|
@@ -1300,9 +2204,25 @@ function transformSource(source, options) {
|
|
|
1300
2204
|
const { name } = value[0];
|
|
1301
2205
|
return { $: 'view', view: { type: 'icon', component: name, mods: [], key: null } };
|
|
1302
2206
|
};
|
|
1303
|
-
|
|
2207
|
+
/** A view in the glyph's slot: one of the element props naming it, and no glyph beside it. */
|
|
2208
|
+
const slotView = (props = [], icon) => {
|
|
2209
|
+
const named = props.filter((prop) => given.has(prop));
|
|
2210
|
+
for (const prop of named)
|
|
2211
|
+
consumed.add(prop);
|
|
2212
|
+
if (named.length === 0)
|
|
2213
|
+
return icon;
|
|
2214
|
+
const value = named.length === 1 && !icon
|
|
2215
|
+
? valuesOf('view', given.get(named[0]))
|
|
2216
|
+
: undefined;
|
|
2217
|
+
if (value?.length !== 1) {
|
|
2218
|
+
unmapped(named[0]);
|
|
2219
|
+
return icon;
|
|
2220
|
+
}
|
|
2221
|
+
return value[0];
|
|
2222
|
+
};
|
|
2223
|
+
const leading = slotView(param.label.leadingViews, glyph(param.label.leading));
|
|
1304
2224
|
const text = textOf(node.children);
|
|
1305
|
-
const trailing = glyph(param.label.trailing);
|
|
2225
|
+
const trailing = slotView(param.label.trailingViews, glyph(param.label.trailing));
|
|
1306
2226
|
if (leading)
|
|
1307
2227
|
args.push(leading);
|
|
1308
2228
|
if (text !== undefined)
|
|
@@ -1370,6 +2290,8 @@ function transformSource(source, options) {
|
|
|
1370
2290
|
if (given.has(option))
|
|
1371
2291
|
consumed.add(option);
|
|
1372
2292
|
}
|
|
2293
|
+
/** Its `style`, the node's box: chained after its own modifiers. */
|
|
2294
|
+
const styled = [];
|
|
1373
2295
|
for (const [name, value] of given) {
|
|
1374
2296
|
if (consumed.has(name))
|
|
1375
2297
|
continue;
|
|
@@ -1390,6 +2312,40 @@ function transformSource(source, options) {
|
|
|
1390
2312
|
props.push({ name: prop.modifier, args: [view] });
|
|
1391
2313
|
continue;
|
|
1392
2314
|
}
|
|
2315
|
+
if (prop?.form === 'style') {
|
|
2316
|
+
const box = value ? componentStyle(attributeExpression(value), rule.node) : undefined;
|
|
2317
|
+
if (!box)
|
|
2318
|
+
unmapped(name);
|
|
2319
|
+
else if ('reasons' in box)
|
|
2320
|
+
reasons.push(...box.reasons);
|
|
2321
|
+
else
|
|
2322
|
+
styled.push(...box.mods);
|
|
2323
|
+
continue;
|
|
2324
|
+
}
|
|
2325
|
+
if (prop?.form === 'fill') {
|
|
2326
|
+
const ink = value ? fillOf(node, attributeExpression(value)) : undefined;
|
|
2327
|
+
if (ink === undefined)
|
|
2328
|
+
unmapped(name);
|
|
2329
|
+
else
|
|
2330
|
+
props.push({ name: prop.modifier, args: [ink] });
|
|
2331
|
+
continue;
|
|
2332
|
+
}
|
|
2333
|
+
if (prop?.form === 'reselect') {
|
|
2334
|
+
const handlers = value ? reselectOf(attributeExpression(value)) : undefined;
|
|
2335
|
+
if (!handlers)
|
|
2336
|
+
unmapped(name);
|
|
2337
|
+
else
|
|
2338
|
+
props.push(...handlers);
|
|
2339
|
+
continue;
|
|
2340
|
+
}
|
|
2341
|
+
if (prop?.form === 'textStyle') {
|
|
2342
|
+
const text = value ? partTextStyleOf(attributeExpression(value)) : undefined;
|
|
2343
|
+
if (text)
|
|
2344
|
+
props.push(...text);
|
|
2345
|
+
else
|
|
2346
|
+
unmapped(name);
|
|
2347
|
+
continue;
|
|
2348
|
+
}
|
|
1393
2349
|
if (prop?.form === 'contentStyle') {
|
|
1394
2350
|
const margins = value ? contentStyleOf(attributeExpression(value)) : undefined;
|
|
1395
2351
|
if (margins)
|
|
@@ -1408,7 +2364,7 @@ function transformSource(source, options) {
|
|
|
1408
2364
|
props.push({ name: prop.modifier, args: [] });
|
|
1409
2365
|
continue;
|
|
1410
2366
|
}
|
|
1411
|
-
const values = prop ? valuesOf(prop.form, value, prop.map) : undefined;
|
|
2367
|
+
const values = prop ? valuesOf(prop.form, value, prop.map, prop.unset) : undefined;
|
|
1412
2368
|
if (!prop || !values) {
|
|
1413
2369
|
unmapped(name);
|
|
1414
2370
|
continue;
|
|
@@ -1505,6 +2461,7 @@ function transformSource(source, options) {
|
|
|
1505
2461
|
unmapped('children');
|
|
1506
2462
|
for (const name of rule.always ?? [])
|
|
1507
2463
|
props.push({ name, args: [] });
|
|
2464
|
+
props.push(...styled);
|
|
1508
2465
|
// An optional argument left out at the end (children included) is no argument.
|
|
1509
2466
|
while (args.length > 0 && isAbsent(args[args.length - 1]))
|
|
1510
2467
|
args.pop();
|
|
@@ -1617,7 +2574,7 @@ function transformSource(source, options) {
|
|
|
1617
2574
|
* A prop's value in a rule's form, as the modifier's arguments (none for a
|
|
1618
2575
|
* bare flag), or undefined when the value is in a form the rule does not take.
|
|
1619
2576
|
*/
|
|
1620
|
-
function valuesOf(form, value, map) {
|
|
2577
|
+
function valuesOf(form, value, map, unset) {
|
|
1621
2578
|
if (value === null)
|
|
1622
2579
|
// A bare prop is `true`.
|
|
1623
2580
|
return form === 'flag' || form === 'mark'
|
|
@@ -1638,6 +2595,26 @@ function transformSource(source, options) {
|
|
|
1638
2595
|
return hasJsx(expression) ? undefined : [{ node: expression, hole: 'string' }];
|
|
1639
2596
|
}
|
|
1640
2597
|
case 'token': {
|
|
2598
|
+
// Tokens chosen by a plain condition (`sensitive ? 'error' : 'default'`) are that choice;
|
|
2599
|
+
// none is a branch only where the modifier takes `undefined` (`unset`).
|
|
2600
|
+
if (n.ConditionalExpression.check(expression) &&
|
|
2601
|
+
(isPlainRead(expression.test) || isBooleanForm(expression.test))) {
|
|
2602
|
+
const branch = (node) => unset && noStyle(node) && !n.BooleanLiteral.check(node)
|
|
2603
|
+
? { $: 'absent' }
|
|
2604
|
+
: valuesOf('token', node, map)?.[0];
|
|
2605
|
+
const then = branch(expression.consequent);
|
|
2606
|
+
const otherwise = branch(expression.alternate);
|
|
2607
|
+
return then === undefined || otherwise === undefined
|
|
2608
|
+
? undefined
|
|
2609
|
+
: [
|
|
2610
|
+
{
|
|
2611
|
+
$: 'choose',
|
|
2612
|
+
test: { node: expression.test, hole: 'condition' },
|
|
2613
|
+
then,
|
|
2614
|
+
otherwise,
|
|
2615
|
+
},
|
|
2616
|
+
];
|
|
2617
|
+
}
|
|
1641
2618
|
// A token is a static string, or a static number of points (`spacing={12}`).
|
|
1642
2619
|
const number = staticNumber(value);
|
|
1643
2620
|
const text = staticString(value) ?? (number === undefined ? undefined : String(number));
|
|
@@ -1676,6 +2653,10 @@ function transformSource(source, options) {
|
|
|
1676
2653
|
n.CallExpression.check(expression)
|
|
1677
2654
|
? [{ node: expression, hole: 'handler' }]
|
|
1678
2655
|
: undefined;
|
|
2656
|
+
case 'content':
|
|
2657
|
+
return n.JSXElement.check(expression) || n.JSXFragment.check(expression)
|
|
2658
|
+
? valuesOf('view', value, map)
|
|
2659
|
+
: valuesOf('string', value, map);
|
|
1679
2660
|
case 'view': {
|
|
1680
2661
|
if (n.StringLiteral.check(expression) || n.TemplateLiteral.check(expression))
|
|
1681
2662
|
return undefined;
|
|
@@ -1708,12 +2689,7 @@ function transformSource(source, options) {
|
|
|
1708
2689
|
? undefined
|
|
1709
2690
|
: [{ node: expression, hole: 'items' }];
|
|
1710
2691
|
case 'element':
|
|
1711
|
-
|
|
1712
|
-
return [{ $: 'view', view: elementView(expression) }];
|
|
1713
|
-
// A fragment, or a `const` holding an element, is drawn as it is.
|
|
1714
|
-
return n.JSXFragment.check(expression) || elementReads.has(expression)
|
|
1715
|
-
? [{ $: 'view', view: { type: 'embed', content: { node: expression, hole: 'element' } } }]
|
|
1716
|
-
: undefined;
|
|
2692
|
+
return elementPropOf(expression);
|
|
1717
2693
|
case 'row': {
|
|
1718
2694
|
const row = rowOf(expression);
|
|
1719
2695
|
return row === undefined ? undefined : [row];
|
|
@@ -1722,6 +2698,35 @@ function transformSource(source, options) {
|
|
|
1722
2698
|
return placementOf(value);
|
|
1723
2699
|
}
|
|
1724
2700
|
}
|
|
2701
|
+
/**
|
|
2702
|
+
* An element prop as a view: an element converted; a fragment, a language
|
|
2703
|
+
* view's element, a `const` holding an element, or what a render function
|
|
2704
|
+
* returns (`renderEmpty()`, a lowercase callee: a component is never called)
|
|
2705
|
+
* drawn as it is; an element or none chosen by a plain condition (`loading ?
|
|
2706
|
+
* <Spinner /> : null`) as that choice.
|
|
2707
|
+
*/
|
|
2708
|
+
function elementPropOf(expression) {
|
|
2709
|
+
if (n.JSXElement.check(expression))
|
|
2710
|
+
return [{ $: 'view', view: elementView(expression) }];
|
|
2711
|
+
if (n.ConditionalExpression.check(expression) &&
|
|
2712
|
+
(isPlainRead(expression.test) || isBooleanForm(expression.test))) {
|
|
2713
|
+
const branch = (node) => noStyle(node) ? { $: 'null' } : elementPropOf(node)?.[0];
|
|
2714
|
+
const then = branch(expression.consequent);
|
|
2715
|
+
const otherwise = branch(expression.alternate);
|
|
2716
|
+
if (then === undefined || otherwise === undefined)
|
|
2717
|
+
return undefined;
|
|
2718
|
+
return [{ $: 'choose', test: { node: expression.test, hole: 'condition' }, then, otherwise }];
|
|
2719
|
+
}
|
|
2720
|
+
const rendered = n.CallExpression.check(expression) &&
|
|
2721
|
+
n.Identifier.check(expression.callee) &&
|
|
2722
|
+
/^[a-z]/.test(expression.callee.name);
|
|
2723
|
+
return n.JSXFragment.check(expression) ||
|
|
2724
|
+
isToElement(expression) ||
|
|
2725
|
+
elementReads.has(expression) ||
|
|
2726
|
+
rendered
|
|
2727
|
+
? [{ $: 'view', view: { type: 'embed', content: { node: expression, hole: 'element' } } }]
|
|
2728
|
+
: undefined;
|
|
2729
|
+
}
|
|
1725
2730
|
/**
|
|
1726
2731
|
* Bloom's responsive placement as `.presentAt(base, tiers)`: a static token,
|
|
1727
2732
|
* or an object literal of static tokens with its `base` and any of the
|
|
@@ -1769,6 +2774,50 @@ function transformSource(source, options) {
|
|
|
1769
2774
|
return { node: expression, hole: 'action' };
|
|
1770
2775
|
return undefined;
|
|
1771
2776
|
}
|
|
2777
|
+
/**
|
|
2778
|
+
* A part's label style (a tab's `textStyle`), an object literal of a
|
|
2779
|
+
* `fontSize` and `lineHeight` of the spec's tokens and a `fontWeight`, as
|
|
2780
|
+
* `.font()`, `.leading()` and `.weight()`; a weight chosen by a plain
|
|
2781
|
+
* condition (`open ? '700' : '500'`) is the weight it chooses.
|
|
2782
|
+
*/
|
|
2783
|
+
function partTextStyleOf(expression) {
|
|
2784
|
+
if (!n.ObjectExpression.check(expression))
|
|
2785
|
+
return undefined;
|
|
2786
|
+
const weightOf = (node) => {
|
|
2787
|
+
const text = staticString(node) ?? (n.NumericLiteral.check(node) ? String(node.value) : undefined);
|
|
2788
|
+
if (text !== undefined)
|
|
2789
|
+
return WEIGHT_BY_VALUE[text];
|
|
2790
|
+
if (n.ConditionalExpression.check(node) &&
|
|
2791
|
+
(isPlainRead(node.test) || isBooleanForm(node.test))) {
|
|
2792
|
+
const then = weightOf(node.consequent);
|
|
2793
|
+
const otherwise = weightOf(node.alternate);
|
|
2794
|
+
return then === undefined || otherwise === undefined
|
|
2795
|
+
? undefined
|
|
2796
|
+
: { $: 'choose', test: { node: node.test, hole: 'condition' }, then, otherwise };
|
|
2797
|
+
}
|
|
2798
|
+
return undefined;
|
|
2799
|
+
};
|
|
2800
|
+
const mods = [];
|
|
2801
|
+
for (const property of expression.properties) {
|
|
2802
|
+
if (!(n.ObjectProperty.check(property) || n.Property.check(property)) ||
|
|
2803
|
+
property.computed ||
|
|
2804
|
+
!n.Identifier.check(property.key))
|
|
2805
|
+
return undefined;
|
|
2806
|
+
const key = property.key.name;
|
|
2807
|
+
if (key === 'fontWeight') {
|
|
2808
|
+
const weight = weightOf(property.value);
|
|
2809
|
+
if (weight === undefined)
|
|
2810
|
+
return undefined;
|
|
2811
|
+
mods.push({ name: 'weight', args: [weight] });
|
|
2812
|
+
continue;
|
|
2813
|
+
}
|
|
2814
|
+
const mod = (0, style_1.textDeclaration)(key, property.value);
|
|
2815
|
+
if (!mod)
|
|
2816
|
+
return undefined;
|
|
2817
|
+
mods.push({ name: mod.name, args: [...mod.args] });
|
|
2818
|
+
}
|
|
2819
|
+
return mods;
|
|
2820
|
+
}
|
|
1772
2821
|
/**
|
|
1773
2822
|
* A static content container style (an object literal or a module
|
|
1774
2823
|
* StyleSheet's style) of paddings and a gap, each a space token, as the
|
|
@@ -1776,9 +2825,19 @@ function transformSource(source, options) {
|
|
|
1776
2825
|
* container style holds the same keys.
|
|
1777
2826
|
*/
|
|
1778
2827
|
function contentStyleOf(expression) {
|
|
2828
|
+
// Content that grows while a condition holds (`empty ? { flexGrow: 1 } : undefined`):
|
|
2829
|
+
// `.contentGrows(condition)`, the style itself nothing but the grow.
|
|
2830
|
+
if (n.ConditionalExpression.check(expression) &&
|
|
2831
|
+
noStyle(expression.alternate) &&
|
|
2832
|
+
(isPlainRead(expression.test) || isBooleanForm(expression.test))) {
|
|
2833
|
+
const grown = contentStyleOf(expression.consequent);
|
|
2834
|
+
return grown?.length === 1 && grown[0]?.name === 'contentGrows'
|
|
2835
|
+
? [{ name: 'contentGrows', args: [{ node: expression.test, hole: 'condition' }] }]
|
|
2836
|
+
: undefined;
|
|
2837
|
+
}
|
|
1779
2838
|
const object = moduleStyleReads.has(expression)
|
|
1780
2839
|
? styleSheets.get(expression.object.name)?.get(expression.property.name)
|
|
1781
|
-
: expression;
|
|
2840
|
+
: (styleConstantReads.get(expression) ?? expression);
|
|
1782
2841
|
if (!n.ObjectExpression.check(object))
|
|
1783
2842
|
return undefined;
|
|
1784
2843
|
const mods = [];
|
|
@@ -1787,6 +2846,11 @@ function transformSource(source, options) {
|
|
|
1787
2846
|
property.computed ||
|
|
1788
2847
|
!n.Identifier.check(property.key))
|
|
1789
2848
|
return undefined;
|
|
2849
|
+
// A content container that grows to the scroll: `.contentGrows()`.
|
|
2850
|
+
if (property.key.name === 'flexGrow' && staticNumber(property.value) === 1) {
|
|
2851
|
+
mods.push({ name: 'contentGrows', args: [] });
|
|
2852
|
+
continue;
|
|
2853
|
+
}
|
|
1790
2854
|
const token = components_1.SPACE[String(staticNumber(property.value))];
|
|
1791
2855
|
const key = property.key.name;
|
|
1792
2856
|
const edge = key === 'gap' ? undefined : CONTENT_EDGE[key];
|
|
@@ -1800,6 +2864,45 @@ function transformSource(source, options) {
|
|
|
1800
2864
|
styleLiterals.set(expression, recast.print(object).code);
|
|
1801
2865
|
return mods;
|
|
1802
2866
|
}
|
|
2867
|
+
/** A style that is a fill alone (`{ backgroundColor: '#000' }`) as its colour (`inkOf`). */
|
|
2868
|
+
function fillOf(node, expression) {
|
|
2869
|
+
const [property, ...rest] = n.ObjectExpression.check(expression) ? expression.properties : [];
|
|
2870
|
+
if (rest.length ||
|
|
2871
|
+
!(n.ObjectProperty.check(property) || n.Property.check(property)) ||
|
|
2872
|
+
property.computed ||
|
|
2873
|
+
!n.Identifier.check(property.key) ||
|
|
2874
|
+
property.key.name !== 'backgroundColor')
|
|
2875
|
+
return undefined;
|
|
2876
|
+
return inkOf(node, property.value);
|
|
2877
|
+
}
|
|
2878
|
+
/**
|
|
2879
|
+
* Bloom Screen's `reselect` (`{ refresh, isAtTop, scrollToTop }`, each as
|
|
2880
|
+
* written) as `.onReselect(refresh)` and `.reselectTop(isAtTop, scrollToTop)`:
|
|
2881
|
+
* where the top is comes with how to go there, both or neither.
|
|
2882
|
+
*/
|
|
2883
|
+
function reselectOf(expression) {
|
|
2884
|
+
if (!n.ObjectExpression.check(expression))
|
|
2885
|
+
return undefined;
|
|
2886
|
+
const handlers = new Map();
|
|
2887
|
+
for (const property of expression.properties) {
|
|
2888
|
+
if (!(n.ObjectProperty.check(property) || n.Property.check(property)) ||
|
|
2889
|
+
property.computed ||
|
|
2890
|
+
!n.Identifier.check(property.key) ||
|
|
2891
|
+
!['refresh', 'isAtTop', 'scrollToTop'].includes(property.key.name) ||
|
|
2892
|
+
hasJsx(property.value))
|
|
2893
|
+
return undefined;
|
|
2894
|
+
handlers.set(property.key.name, { node: property.value, hole: 'handler' });
|
|
2895
|
+
}
|
|
2896
|
+
const [refresh, isAtTop, scrollToTop] = ['refresh', 'isAtTop', 'scrollToTop'].map((key) => handlers.get(key));
|
|
2897
|
+
if (Boolean(isAtTop) !== Boolean(scrollToTop))
|
|
2898
|
+
return undefined;
|
|
2899
|
+
const mods = [];
|
|
2900
|
+
if (refresh)
|
|
2901
|
+
mods.push({ name: 'onReselect', args: [refresh] });
|
|
2902
|
+
if (isAtTop && scrollToTop)
|
|
2903
|
+
mods.push({ name: 'reselectTop', args: [isAtTop, scrollToTop] });
|
|
2904
|
+
return mods.length ? mods : undefined;
|
|
2905
|
+
}
|
|
1803
2906
|
/**
|
|
1804
2907
|
* A scroll view's content class as its content view's build function: each
|
|
1805
2908
|
* class through the className table, as a View's, and every modifier one the
|
|
@@ -1843,8 +2946,8 @@ function transformSource(source, options) {
|
|
|
1843
2946
|
* React Native's `RefreshControl` as a pull to refresh: its `refreshing` and
|
|
1844
2947
|
* the binding a pull writes `true` to, which calls `onRefresh` with no
|
|
1845
2948
|
* arguments as React Native does; its `tintColor` and `colors` as the tint
|
|
1846
|
-
* when both are one theme colour, since the tint paints both
|
|
1847
|
-
*
|
|
2949
|
+
* when both are one theme colour, since the tint paints both; each alone, or
|
|
2950
|
+
* two colours, the tint of its platform (`{ ios: 'primary' }`).
|
|
1848
2951
|
*/
|
|
1849
2952
|
function refreshOf(expression) {
|
|
1850
2953
|
const local = n.JSXElement.check(expression) ? jsxName(expression) : undefined;
|
|
@@ -1864,23 +2967,34 @@ function transformSource(source, options) {
|
|
|
1864
2967
|
const { refreshing, onRefresh, tintColor, colors, ...rest } = Object.fromEntries(given);
|
|
1865
2968
|
if (Object.keys(rest).length || !refreshing || !onRefresh || hasJsx(refreshing))
|
|
1866
2969
|
return undefined;
|
|
1867
|
-
//
|
|
1868
|
-
|
|
1869
|
-
if (!action)
|
|
2970
|
+
// The refreshing value and the action a pull runs, as written: `{ refreshing, onRefresh }`.
|
|
2971
|
+
if (hasJsx(onRefresh))
|
|
1870
2972
|
return undefined;
|
|
1871
2973
|
const binding = {
|
|
1872
|
-
$: '
|
|
1873
|
-
|
|
2974
|
+
$: 'options',
|
|
2975
|
+
entries: [
|
|
2976
|
+
['refreshing', { node: refreshing, hole: 'boolean' }],
|
|
2977
|
+
['onRefresh', { node: onRefresh, hole: 'handler' }],
|
|
2978
|
+
],
|
|
1874
2979
|
};
|
|
2980
|
+
// iOS's `tintColor` and Android's `colors` (one theme colour), each the tint of its platform.
|
|
2981
|
+
const ios = tintColor ? themeRoles.get(tintColor) : undefined;
|
|
2982
|
+
const [only, ...others] = n.ArrayExpression.check(colors) ? colors.elements : [];
|
|
2983
|
+
const android = colors && only && !others.length ? themeRoles.get(only) : undefined;
|
|
2984
|
+
if ((tintColor && !ios) || (colors && !android))
|
|
2985
|
+
return undefined;
|
|
1875
2986
|
// Its import goes with the element when nothing else draws one.
|
|
1876
2987
|
convertedLocals.add(local);
|
|
1877
|
-
if (!
|
|
2988
|
+
if (!ios && !android)
|
|
1878
2989
|
return [binding];
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
2990
|
+
if (ios === android)
|
|
2991
|
+
return [binding, ios];
|
|
2992
|
+
const entries = [];
|
|
2993
|
+
if (ios)
|
|
2994
|
+
entries.push(['ios', ios]);
|
|
2995
|
+
if (android)
|
|
2996
|
+
entries.push(['android', android]);
|
|
2997
|
+
return [binding, { $: 'options', entries }];
|
|
1884
2998
|
}
|
|
1885
2999
|
/**
|
|
1886
3000
|
* FlashList's `renderItem` as the node's row, which Bloom hands the same
|
|
@@ -1976,11 +3090,358 @@ function transformSource(source, options) {
|
|
|
1976
3090
|
return text;
|
|
1977
3091
|
return hasJsx(run.expression) ? undefined : { node: run.expression, hole: 'string' };
|
|
1978
3092
|
}
|
|
3093
|
+
/**
|
|
3094
|
+
* Bloom's `DropdownMenu` tree as `Menu(trigger, ...rows).menuStyle('dropdown')`,
|
|
3095
|
+
* which draws Bloom's DropdownMenu on every platform (parity/context-menu.cases.tsx):
|
|
3096
|
+
* a `DropdownMenuTrigger asChild` around one view the codemod converts, and a
|
|
3097
|
+
* `DropdownMenuContent` of `DropdownMenuItem`s and `DropdownMenuSeparator`s
|
|
3098
|
+
* (`Divider()`), with the trigger's label (`.label()`). The menu holds its
|
|
3099
|
+
* own open state, so a menu the app opens itself (`open`) stays JSX. An
|
|
3100
|
+
* item's `leading` is a language glyph, `toElement(Icon(icon).size(s).foreground(f))`:
|
|
3101
|
+
* the row's `.icon()`, `.iconSize()` and `.iconForeground()`.
|
|
3102
|
+
*/
|
|
3103
|
+
function convertDropdownMenu(node) {
|
|
3104
|
+
const reasons = [];
|
|
3105
|
+
const fail = (subject, code, why) => {
|
|
3106
|
+
reasons.push({ subject, code: `DropdownMenu:${code}`, why });
|
|
3107
|
+
return { reasons };
|
|
3108
|
+
};
|
|
3109
|
+
const part = (element) => {
|
|
3110
|
+
const local = n.JSXElement.check(element) ? jsxName(element) : undefined;
|
|
3111
|
+
const source = local ? sources.get(local) : undefined;
|
|
3112
|
+
return source?.module === DROPDOWN ? source.imported : undefined;
|
|
3113
|
+
};
|
|
3114
|
+
const elements = (parent) => {
|
|
3115
|
+
const out = [];
|
|
3116
|
+
for (const child of parent.children) {
|
|
3117
|
+
if (n.JSXText.check(child) && cleanJsxText(child.value) === '')
|
|
3118
|
+
continue;
|
|
3119
|
+
if (!n.JSXElement.check(child))
|
|
3120
|
+
return undefined;
|
|
3121
|
+
out.push(child);
|
|
3122
|
+
}
|
|
3123
|
+
return out;
|
|
3124
|
+
};
|
|
3125
|
+
const attributes = (element) => {
|
|
3126
|
+
const out = new Map();
|
|
3127
|
+
for (const attribute of element.openingElement.attributes) {
|
|
3128
|
+
if (!n.JSXAttribute.check(attribute) || !n.JSXIdentifier.check(attribute.name))
|
|
3129
|
+
return undefined;
|
|
3130
|
+
out.set(attribute.name.name, attribute.value);
|
|
3131
|
+
}
|
|
3132
|
+
return out;
|
|
3133
|
+
};
|
|
3134
|
+
// A menu the app opens itself (`open` and `onOpenChange`) is `.presented([open, onOpenChange])`.
|
|
3135
|
+
const menuProps = attributes(node);
|
|
3136
|
+
const opened = menuProps?.get('open');
|
|
3137
|
+
const onOpenChange = menuProps?.get('onOpenChange');
|
|
3138
|
+
const presented = opened && onOpenChange && menuProps?.size === 2;
|
|
3139
|
+
if (menuProps?.size !== 0 && !presented)
|
|
3140
|
+
return fail('props', 'props', 'a menu with props besides `open` and `onOpenChange`, its `.presented()` binding');
|
|
3141
|
+
const [trigger, content, ...rest] = elements(node) ?? [];
|
|
3142
|
+
if (part(trigger) !== 'DropdownMenuTrigger' ||
|
|
3143
|
+
part(content) !== 'DropdownMenuContent' ||
|
|
3144
|
+
rest.length)
|
|
3145
|
+
return fail('children', 'children', 'children that are no trigger and content');
|
|
3146
|
+
const triggerProps = attributes(trigger);
|
|
3147
|
+
const contentProps = attributes(content);
|
|
3148
|
+
const label = triggerProps?.get('label');
|
|
3149
|
+
if (!triggerProps?.has('asChild') ||
|
|
3150
|
+
[...triggerProps.keys()].some((key) => key !== 'asChild' && key !== 'label'))
|
|
3151
|
+
return fail('trigger', 'trigger', 'a trigger that is not `asChild`, or with props besides its label');
|
|
3152
|
+
const labelText = (value) => value === undefined ? undefined : source.slice(value.start, value.end);
|
|
3153
|
+
if (!contentProps ||
|
|
3154
|
+
labelText(contentProps.get('label')) !== labelText(label) ||
|
|
3155
|
+
[...contentProps.keys()].some((key) => key !== 'label' && key !== 'testID'))
|
|
3156
|
+
return fail('content', 'content', 'a content whose label is not its trigger’s, or with other props');
|
|
3157
|
+
const [view, ...others] = elements(trigger) ?? [];
|
|
3158
|
+
const triggerView = view ? convert(view) : undefined;
|
|
3159
|
+
if (!triggerView || others.length || !('ir' in triggerView))
|
|
3160
|
+
return fail('trigger', 'trigger', 'a trigger that is no one view the codemod converts');
|
|
3161
|
+
/** One text child as a row's title: a literal, or the string it carries. */
|
|
3162
|
+
const titleOf = (element) => {
|
|
3163
|
+
const texts = element?.children?.filter((child) => !(n.JSXText.check(child) && cleanJsxText(child.value) === ''));
|
|
3164
|
+
const text = texts?.length === 1 ? texts[0] : undefined;
|
|
3165
|
+
return n.JSXText.check(text)
|
|
3166
|
+
? cleanJsxText(text.value)
|
|
3167
|
+
: n.JSXExpressionContainer.check(text) && !n.JSXEmptyExpression.check(text.expression)
|
|
3168
|
+
? (staticString(text) ?? { node: text.expression, hole: 'string' })
|
|
3169
|
+
: undefined;
|
|
3170
|
+
};
|
|
3171
|
+
const rowView = (node, args, props) => ({
|
|
3172
|
+
$: 'view',
|
|
3173
|
+
view: { type: 'component', node, within: undefined, args, props, key: null },
|
|
3174
|
+
});
|
|
3175
|
+
/**
|
|
3176
|
+
* An item, or a radio item (`radio`): its value is the row's tag, the
|
|
3177
|
+
* choice it is in its section of choices.
|
|
3178
|
+
*/
|
|
3179
|
+
const itemRow = (row, radio) => {
|
|
3180
|
+
const props = row && part(row) === (radio ? 'DropdownMenuRadioItem' : 'DropdownMenuItem')
|
|
3181
|
+
? attributes(row)
|
|
3182
|
+
: undefined;
|
|
3183
|
+
const title = titleOf(row);
|
|
3184
|
+
if (!props || title === undefined)
|
|
3185
|
+
return fail('row', 'row', 'a row that is no item with one text, or a separator');
|
|
3186
|
+
const mods = [];
|
|
3187
|
+
for (const [name, value] of props) {
|
|
3188
|
+
const expression = value ? attributeExpression(value) : undefined;
|
|
3189
|
+
if (name === 'onPress' && expression && !radio)
|
|
3190
|
+
mods.push({ name, args: [{ node: expression, hole: 'handler' }] });
|
|
3191
|
+
else if (name === 'value' && expression && radio)
|
|
3192
|
+
mods.push({
|
|
3193
|
+
name: 'tag',
|
|
3194
|
+
args: [staticString(value) ?? { node: expression, hole: 'string' }],
|
|
3195
|
+
});
|
|
3196
|
+
else if (name === 'variant' && staticString(value) === 'destructive' && !radio)
|
|
3197
|
+
mods.push({ name: 'destructive', args: [] });
|
|
3198
|
+
else if (name === 'variant' && staticString(value) === 'default' && !radio)
|
|
3199
|
+
continue;
|
|
3200
|
+
else if ((name === 'disabled' || name === 'keepOpen') && value === null)
|
|
3201
|
+
mods.push({ name, args: [] });
|
|
3202
|
+
else if (name === 'testID' && staticString(value) !== undefined)
|
|
3203
|
+
mods.push({ name, args: [staticString(value)] });
|
|
3204
|
+
else if (name === 'leading' && expression) {
|
|
3205
|
+
const glyph = menuGlyph(expression);
|
|
3206
|
+
if (!glyph)
|
|
3207
|
+
return fail(name, 'leading', 'a leading view that is no `toElement(Icon(icon).size(s).foreground(f))`');
|
|
3208
|
+
mods.unshift(...glyph);
|
|
3209
|
+
}
|
|
3210
|
+
else
|
|
3211
|
+
return fail(name, `item:${name}`, `an item's \`${name}\` (or its form here) has no mapping`);
|
|
3212
|
+
}
|
|
3213
|
+
// The tag first, as a choice is written: `Item(title).tag(value)`.
|
|
3214
|
+
mods.sort((a, b) => Number(b.name === 'tag') - Number(a.name === 'tag'));
|
|
3215
|
+
return rowView('Item', [title], mods);
|
|
3216
|
+
};
|
|
3217
|
+
const rows = [];
|
|
3218
|
+
/** Elements the menu holds below its content's rows (a radio group's items). */
|
|
3219
|
+
const nested = [];
|
|
3220
|
+
const contentRows = elements(content) ?? [undefined];
|
|
3221
|
+
for (let index = 0; index < contentRows.length; index++) {
|
|
3222
|
+
const row = contentRows[index];
|
|
3223
|
+
if (part(row) === 'DropdownMenuSeparator' &&
|
|
3224
|
+
attributes(row)?.size === 0 &&
|
|
3225
|
+
!elements(row)?.length) {
|
|
3226
|
+
rows.push(rowView('Divider', [], []));
|
|
3227
|
+
continue;
|
|
3228
|
+
}
|
|
3229
|
+
// A label over a radio group, or a radio group alone: a Section of choices,
|
|
3230
|
+
// `Section(...items).title(label).selection([value, onValueChange])`.
|
|
3231
|
+
const labelled = part(row) === 'DropdownMenuLabel' &&
|
|
3232
|
+
part(contentRows[index + 1]) === 'DropdownMenuRadioGroup' &&
|
|
3233
|
+
attributes(row)?.size === 0;
|
|
3234
|
+
const group = labelled ? contentRows[index + 1] : row;
|
|
3235
|
+
if (part(group) === 'DropdownMenuRadioGroup') {
|
|
3236
|
+
const title = labelled ? titleOf(row) : undefined;
|
|
3237
|
+
const groupProps = attributes(group);
|
|
3238
|
+
const value = groupProps?.get('value');
|
|
3239
|
+
const set = groupProps?.get('onValueChange');
|
|
3240
|
+
const items = elements(group);
|
|
3241
|
+
if ((labelled && title === undefined) ||
|
|
3242
|
+
!value ||
|
|
3243
|
+
!set ||
|
|
3244
|
+
groupProps?.size !== 2 ||
|
|
3245
|
+
!items?.length)
|
|
3246
|
+
return fail('radio-group', 'radio-group', 'a radio group that is no `value` and `onValueChange` over radio items, under one label');
|
|
3247
|
+
const choices = [];
|
|
3248
|
+
for (const item of items) {
|
|
3249
|
+
const choice = itemRow(item, true);
|
|
3250
|
+
if ('reasons' in choice)
|
|
3251
|
+
return choice;
|
|
3252
|
+
choices.push(choice);
|
|
3253
|
+
}
|
|
3254
|
+
nested.push(...items);
|
|
3255
|
+
const sectionProps = [];
|
|
3256
|
+
if (title !== undefined)
|
|
3257
|
+
sectionProps.push({ name: 'title', args: [title] });
|
|
3258
|
+
sectionProps.push({
|
|
3259
|
+
name: 'selection',
|
|
3260
|
+
args: [
|
|
3261
|
+
{
|
|
3262
|
+
$: 'binding',
|
|
3263
|
+
binding: [
|
|
3264
|
+
{ node: attributeExpression(value), hole: 'string' },
|
|
3265
|
+
{ node: attributeExpression(set), hole: 'handler' },
|
|
3266
|
+
],
|
|
3267
|
+
},
|
|
3268
|
+
],
|
|
3269
|
+
});
|
|
3270
|
+
rows.push(rowView('Section', choices, sectionProps));
|
|
3271
|
+
if (labelled)
|
|
3272
|
+
index++;
|
|
3273
|
+
continue;
|
|
3274
|
+
}
|
|
3275
|
+
const item = itemRow(row, false);
|
|
3276
|
+
if ('reasons' in item)
|
|
3277
|
+
return item;
|
|
3278
|
+
rows.push(item);
|
|
3279
|
+
}
|
|
3280
|
+
const props = [];
|
|
3281
|
+
if (label) {
|
|
3282
|
+
const expression = attributeExpression(label);
|
|
3283
|
+
props.push({
|
|
3284
|
+
name: 'label',
|
|
3285
|
+
args: [staticString(label) ?? { node: expression, hole: 'string' }],
|
|
3286
|
+
});
|
|
3287
|
+
}
|
|
3288
|
+
const testID = contentProps.get('testID');
|
|
3289
|
+
if (testID) {
|
|
3290
|
+
const text = staticString(testID);
|
|
3291
|
+
if (text === undefined)
|
|
3292
|
+
return fail('testID', 'testID', 'a testID set at runtime');
|
|
3293
|
+
props.push({ name: 'testID', args: [text] });
|
|
3294
|
+
}
|
|
3295
|
+
props.push({ name: 'menuStyle', args: ['dropdown'] });
|
|
3296
|
+
if (presented)
|
|
3297
|
+
props.push({
|
|
3298
|
+
name: 'presented',
|
|
3299
|
+
args: [
|
|
3300
|
+
{
|
|
3301
|
+
$: 'binding',
|
|
3302
|
+
binding: [
|
|
3303
|
+
{ node: attributeExpression(opened), hole: 'boolean' },
|
|
3304
|
+
{ node: attributeExpression(onOpenChange), hole: 'handler' },
|
|
3305
|
+
],
|
|
3306
|
+
},
|
|
3307
|
+
],
|
|
3308
|
+
});
|
|
3309
|
+
for (const element of [trigger, content, ...(elements(content) ?? []), ...nested])
|
|
3310
|
+
results.set(element, {
|
|
3311
|
+
ir: { type: 'component', node: 'Menu', within: undefined, args: [], props: [], key: null },
|
|
3312
|
+
});
|
|
3313
|
+
return {
|
|
3314
|
+
ir: {
|
|
3315
|
+
type: 'component',
|
|
3316
|
+
node: 'Menu',
|
|
3317
|
+
within: undefined,
|
|
3318
|
+
args: [{ $: 'view', view: triggerView.ir }, ...rows],
|
|
3319
|
+
props,
|
|
3320
|
+
key: null,
|
|
3321
|
+
},
|
|
3322
|
+
};
|
|
3323
|
+
}
|
|
3324
|
+
/**
|
|
3325
|
+
* Bloom's `Fab` as `FAB(Icon(icon), Text(label)).glass(identity)`
|
|
3326
|
+
* (parity/button.cases.tsx): the language's FAB is on glass, so an app's
|
|
3327
|
+
* solid Fab names no glass. Its `icon` is the glyph, its `label` the
|
|
3328
|
+
* extended label, `accessibilityLabel` its `.label()`, and `size`,
|
|
3329
|
+
* `appearance`, `tone`, `disabled`, `collapsed`, `onPress` and `testID` the
|
|
3330
|
+
* modifiers of the same meaning.
|
|
3331
|
+
*/
|
|
3332
|
+
function convertFab(node) {
|
|
3333
|
+
const reasons = [];
|
|
3334
|
+
const fail = (subject, why) => {
|
|
3335
|
+
reasons.push({ subject, code: `Fab:${subject}`, why });
|
|
3336
|
+
return { reasons };
|
|
3337
|
+
};
|
|
3338
|
+
if (node.children.some((c) => !(n.JSXText.check(c) && cleanJsxText(c.value) === '')))
|
|
3339
|
+
return fail('children', 'children: a Fab draws its icon and label props');
|
|
3340
|
+
const views = [];
|
|
3341
|
+
const props = [];
|
|
3342
|
+
const TOKENS = {
|
|
3343
|
+
size: ['size', 'controlSize'],
|
|
3344
|
+
appearance: ['style', 'appearance'],
|
|
3345
|
+
tone: ['tint', 'tone'],
|
|
3346
|
+
};
|
|
3347
|
+
let icon;
|
|
3348
|
+
let label;
|
|
3349
|
+
for (const attribute of node.openingElement.attributes) {
|
|
3350
|
+
if (!n.JSXAttribute.check(attribute) || !n.JSXIdentifier.check(attribute.name))
|
|
3351
|
+
return fail('spread', 'spread props: the codemod cannot see what they set');
|
|
3352
|
+
const name = attribute.name.name;
|
|
3353
|
+
const { value } = attribute;
|
|
3354
|
+
const expression = value ? attributeExpression(value) : undefined;
|
|
3355
|
+
const text = staticString(value);
|
|
3356
|
+
if (name === 'icon' &&
|
|
3357
|
+
n.Identifier.check(expression) &&
|
|
3358
|
+
kindOf(sources.get(expression.name)) === 'icon')
|
|
3359
|
+
icon = {
|
|
3360
|
+
$: 'view',
|
|
3361
|
+
view: { type: 'icon', component: expression.name, mods: [], key: null },
|
|
3362
|
+
};
|
|
3363
|
+
else if (name === 'label' && expression && !hasJsx(expression))
|
|
3364
|
+
label = text ?? { node: expression, hole: 'string' };
|
|
3365
|
+
else if (name === 'accessibilityLabel' && expression && !hasJsx(expression))
|
|
3366
|
+
props.push({ name: 'label', args: [text ?? { node: expression, hole: 'string' }] });
|
|
3367
|
+
else if (TOKENS[name] && text !== undefined && spec.enums[TOKENS[name][1]]?.includes(text))
|
|
3368
|
+
props.push({ name: TOKENS[name][0], args: [text] });
|
|
3369
|
+
else if ((name === 'disabled' || name === 'collapsed') && (value === null || expression))
|
|
3370
|
+
props.push({
|
|
3371
|
+
name,
|
|
3372
|
+
args: [value === null ? true : { node: expression, hole: 'boolean' }],
|
|
3373
|
+
});
|
|
3374
|
+
else if (name === 'onPress' && expression)
|
|
3375
|
+
props.push({ name, args: [{ node: expression, hole: 'handler' }] });
|
|
3376
|
+
else if (name === 'testID' && text !== undefined)
|
|
3377
|
+
props.push({ name, args: [text] });
|
|
3378
|
+
else
|
|
3379
|
+
return fail(name, `a Fab's \`${name}\` (or its form here) has no mapping`);
|
|
3380
|
+
}
|
|
3381
|
+
if (icon === undefined)
|
|
3382
|
+
return fail('icon', 'a Fab with no icon the codemod reads');
|
|
3383
|
+
views.push(icon);
|
|
3384
|
+
if (label !== undefined)
|
|
3385
|
+
views.push({
|
|
3386
|
+
$: 'view',
|
|
3387
|
+
view: {
|
|
3388
|
+
type: 'text',
|
|
3389
|
+
raw: false,
|
|
3390
|
+
resolved: { axis: 'column', mods: [], variants: [] },
|
|
3391
|
+
choice: null,
|
|
3392
|
+
props: [],
|
|
3393
|
+
key: null,
|
|
3394
|
+
runs: [
|
|
3395
|
+
typeof label === 'string'
|
|
3396
|
+
? { type: 'string', value: label }
|
|
3397
|
+
: { type: 'expr', value: label },
|
|
3398
|
+
],
|
|
3399
|
+
},
|
|
3400
|
+
});
|
|
3401
|
+
props.push({ name: 'glass', args: [{ $: 'language', name: 'identity' }] });
|
|
3402
|
+
return {
|
|
3403
|
+
ir: { type: 'component', node: 'FAB', within: undefined, args: views, props, key: null },
|
|
3404
|
+
};
|
|
3405
|
+
}
|
|
3406
|
+
/** A row's glyph as the language writes it, `toElement(Icon(icon).size(s).foreground(f))`, as the row's modifiers. */
|
|
3407
|
+
function menuGlyph(expression) {
|
|
3408
|
+
if (!isToElement(expression) || expression.arguments.length !== 1)
|
|
3409
|
+
return undefined;
|
|
3410
|
+
const chain = [];
|
|
3411
|
+
let at = expression.arguments[0];
|
|
3412
|
+
while (n.CallExpression.check(at) && n.MemberExpression.check(at.callee)) {
|
|
3413
|
+
if (at.arguments.length !== 1 || !n.Identifier.check(at.callee.property))
|
|
3414
|
+
return undefined;
|
|
3415
|
+
chain.unshift([at.callee.property.name, at.arguments[0]]);
|
|
3416
|
+
at = at.callee.object;
|
|
3417
|
+
}
|
|
3418
|
+
const callee = n.CallExpression.check(at) ? at.callee : undefined;
|
|
3419
|
+
const base = n.Identifier.check(callee) ? sources.get(callee.name) : undefined;
|
|
3420
|
+
const icon = at?.arguments?.[0];
|
|
3421
|
+
if (base?.module !== LANGUAGE_MODULE ||
|
|
3422
|
+
base.imported !== 'Icon' ||
|
|
3423
|
+
at.arguments.length !== 1 ||
|
|
3424
|
+
!n.Identifier.check(icon) ||
|
|
3425
|
+
kindOf(sources.get(icon.name)) !== 'icon')
|
|
3426
|
+
return undefined;
|
|
3427
|
+
const size = chain.find(([name]) => name === 'size')?.[1];
|
|
3428
|
+
const ink = chain.find(([name]) => name === 'foreground')?.[1];
|
|
3429
|
+
// Both named: the menu's own glyph (`md`, quiet) is drawn otherwise.
|
|
3430
|
+
if (chain.length !== 2 || staticString(size) === undefined || staticString(ink) === undefined)
|
|
3431
|
+
return undefined;
|
|
3432
|
+
return [
|
|
3433
|
+
{ name: 'icon', args: [{ $: 'icon', name: icon.name }] },
|
|
3434
|
+
{ name: 'iconSize', args: [staticString(size)] },
|
|
3435
|
+
{ name: 'iconForeground', args: [staticString(ink)] },
|
|
3436
|
+
];
|
|
3437
|
+
}
|
|
1979
3438
|
/** Bloom's `Button`, as the language's Button parity cases draw it (`packages/bloom-language/parity/button.cases.tsx`). */
|
|
1980
3439
|
function convertButton(node) {
|
|
1981
3440
|
const reasons = [];
|
|
1982
3441
|
const fail = (subject, code, why) => reasons.push({ subject, code, why });
|
|
1983
3442
|
const props = [];
|
|
3443
|
+
/** Its `style`, the button's box: chained after its own modifiers. */
|
|
3444
|
+
const styled = [];
|
|
1984
3445
|
let key = null;
|
|
1985
3446
|
let leading = null;
|
|
1986
3447
|
let trailing = null;
|
|
@@ -2026,6 +3487,38 @@ function transformSource(source, options) {
|
|
|
2026
3487
|
else if (['iconOnly', 'wide', 'grow'].includes(name) &&
|
|
2027
3488
|
(value === null || value === undefined))
|
|
2028
3489
|
flags.add(name);
|
|
3490
|
+
// An appearance or tone set at runtime is the modifier's own expression.
|
|
3491
|
+
else if (ENUM[name] && expression && !hasJsx(expression))
|
|
3492
|
+
props.push({ name: ENUM[name], args: [{ node: expression, hole: 'string' }] });
|
|
3493
|
+
else if (name === 'stopPropagation' && (value === null || value === undefined))
|
|
3494
|
+
props.push({ name: 'stopPropagation', args: [] });
|
|
3495
|
+
else if (name === 'iconSize' && ICON_SIZE_TOKEN.has(staticNumber(value)))
|
|
3496
|
+
props.push({
|
|
3497
|
+
name: 'iconSize',
|
|
3498
|
+
args: [ICON_SIZE_TOKEN.get(staticNumber(value))],
|
|
3499
|
+
});
|
|
3500
|
+
// No spinner while loading.
|
|
3501
|
+
else if (name === 'loadingIndicator' && n.NullLiteral.check(expression))
|
|
3502
|
+
props.push({ name: 'loadingIndicator', args: [{ node: expression, hole: 'handler' }] });
|
|
3503
|
+
// A menu trigger's open state: Bloom's aria-expanded is the language's accessibility state.
|
|
3504
|
+
else if (name === 'aria-expanded' && expression && !hasJsx(expression))
|
|
3505
|
+
props.push({
|
|
3506
|
+
name: 'accessibilityState',
|
|
3507
|
+
args: [
|
|
3508
|
+
{
|
|
3509
|
+
node: recast.types.builders.objectExpression([
|
|
3510
|
+
recast.types.builders.objectProperty(recast.types.builders.identifier('expanded'), expression),
|
|
3511
|
+
]),
|
|
3512
|
+
hole: 'state',
|
|
3513
|
+
},
|
|
3514
|
+
],
|
|
3515
|
+
});
|
|
3516
|
+
// Its inks, each a theme colour read as its role.
|
|
3517
|
+
else if ((name === 'color' || name === 'hoverColor') && expression && roleOf(expression))
|
|
3518
|
+
props.push({
|
|
3519
|
+
name: name === 'color' ? 'foreground' : 'hoverForeground',
|
|
3520
|
+
args: [roleOf(expression)],
|
|
3521
|
+
});
|
|
2029
3522
|
else if (name === 'leadingIcon' && icon(value))
|
|
2030
3523
|
leading = icon(value);
|
|
2031
3524
|
else if (name === 'trailingIcon' && icon(value))
|
|
@@ -2042,9 +3535,17 @@ function transformSource(source, options) {
|
|
|
2042
3535
|
name: 'testID',
|
|
2043
3536
|
args: [staticString(value) ?? { node: expression, hole: 'string' }],
|
|
2044
3537
|
});
|
|
3538
|
+
else if (name === 'style' && expression) {
|
|
3539
|
+
const box = componentStyle(expression, 'Button');
|
|
3540
|
+
if ('reasons' in box)
|
|
3541
|
+
reasons.push(...box.reasons);
|
|
3542
|
+
else
|
|
3543
|
+
styled.push(...box.mods);
|
|
3544
|
+
}
|
|
2045
3545
|
else
|
|
2046
3546
|
fail(name, `button:${name}`, `Button's \`${name}\` (or its form here) has no mapping`);
|
|
2047
3547
|
}
|
|
3548
|
+
props.push(...styled);
|
|
2048
3549
|
const shape = flags.has('iconOnly')
|
|
2049
3550
|
? flags.has('wide')
|
|
2050
3551
|
? 'pill'
|
|
@@ -2115,12 +3616,106 @@ function transformSource(source, options) {
|
|
|
2115
3616
|
}
|
|
2116
3617
|
return { type: 'embed', content: { node: expression, hole: 'element' } };
|
|
2117
3618
|
}
|
|
3619
|
+
/**
|
|
3620
|
+
* React Native's `pointerEvents` as the language's hit testing: a static
|
|
3621
|
+
* value, or values (and none, `undefined`) chosen by a plain condition
|
|
3622
|
+
* (`split ? 'box-none' : undefined`). `auto` alone stays JSX, the default.
|
|
3623
|
+
*/
|
|
3624
|
+
function hitTestingOf(expression) {
|
|
3625
|
+
if (n.ConditionalExpression.check(expression) &&
|
|
3626
|
+
(isPlainRead(expression.test) || isBooleanForm(expression.test))) {
|
|
3627
|
+
const branch = (node) => n.Identifier.check(node) && node.name === 'undefined'
|
|
3628
|
+
? { $: 'absent' }
|
|
3629
|
+
: staticString(node) === 'auto'
|
|
3630
|
+
? true
|
|
3631
|
+
: hitTestingOf(node);
|
|
3632
|
+
const then = branch(expression.consequent);
|
|
3633
|
+
const otherwise = branch(expression.alternate);
|
|
3634
|
+
return then === undefined || otherwise === undefined
|
|
3635
|
+
? undefined
|
|
3636
|
+
: { $: 'choose', test: { node: expression.test, hole: 'condition' }, then, otherwise };
|
|
3637
|
+
}
|
|
3638
|
+
return HIT_TESTING[staticString(expression) ?? ''];
|
|
3639
|
+
}
|
|
3640
|
+
// ── styles left unread ───────────────────────────────────────────────
|
|
3641
|
+
/**
|
|
3642
|
+
* How often each module style is read: a StyleSheet's style by
|
|
3643
|
+
* `sheet.key`, a module style constant by its name (its declaration not counted).
|
|
3644
|
+
*/
|
|
3645
|
+
function styleReadsIn(root) {
|
|
3646
|
+
const reads = new Map();
|
|
3647
|
+
const count = (name) => reads.set(name, (reads.get(name) ?? 0) + 1);
|
|
3648
|
+
recast.visit(root, {
|
|
3649
|
+
visitMemberExpression(path) {
|
|
3650
|
+
const { object, property } = path.node;
|
|
3651
|
+
if (!path.node.computed &&
|
|
3652
|
+
n.Identifier.check(object) &&
|
|
3653
|
+
n.Identifier.check(property) &&
|
|
3654
|
+
styleSheets.get(object.name)?.has(property.name))
|
|
3655
|
+
count(`${object.name}.${property.name}`);
|
|
3656
|
+
this.traverse(path);
|
|
3657
|
+
},
|
|
3658
|
+
visitIdentifier(path) {
|
|
3659
|
+
const parent = path.parent?.node;
|
|
3660
|
+
const declares = n.VariableDeclarator.check(parent) && parent.id === path.node;
|
|
3661
|
+
const member = (n.MemberExpression.check(parent) && parent.property === path.node && !parent.computed) ||
|
|
3662
|
+
(n.ObjectProperty.check(parent) && parent.key === path.node && !parent.computed);
|
|
3663
|
+
if (!declares && !member && styleConstants.has(path.node.name))
|
|
3664
|
+
count(path.node.name);
|
|
3665
|
+
this.traverse(path);
|
|
3666
|
+
},
|
|
3667
|
+
});
|
|
3668
|
+
return reads;
|
|
3669
|
+
}
|
|
3670
|
+
/**
|
|
3671
|
+
* The module styles the conversion leaves unread: a StyleSheet's style, or a
|
|
3672
|
+
* module style constant, that an element read before the walk and nothing
|
|
3673
|
+
* reads now is removed (a style never read before stays: not the
|
|
3674
|
+
* conversion's). A StyleSheet left empty goes with its declaration, its
|
|
3675
|
+
* `StyleSheet` import then pruned as a converted component's is. An
|
|
3676
|
+
* exported style is the module's API and stays.
|
|
3677
|
+
*/
|
|
3678
|
+
function removeUnreadStyles(before) {
|
|
3679
|
+
const after = styleReadsIn(ast);
|
|
3680
|
+
const unread = (name) => (before.get(name) ?? 0) > 0 && !after.get(name);
|
|
3681
|
+
const body = ast.program.body;
|
|
3682
|
+
for (const statement of [...body]) {
|
|
3683
|
+
if (!n.VariableDeclaration.check(statement) || statement.kind !== 'const')
|
|
3684
|
+
continue;
|
|
3685
|
+
statement.declarations = statement.declarations.filter((declarator) => {
|
|
3686
|
+
if (!n.Identifier.check(declarator.id))
|
|
3687
|
+
return true;
|
|
3688
|
+
const name = declarator.id.name;
|
|
3689
|
+
if (styleConstants.has(name))
|
|
3690
|
+
return !unread(name);
|
|
3691
|
+
const sheet = styleSheets.get(name);
|
|
3692
|
+
const object = sheet ? declarator.init.arguments[0] : undefined;
|
|
3693
|
+
if (!object)
|
|
3694
|
+
return true;
|
|
3695
|
+
object.properties = object.properties.filter((property) => !(n.Identifier.check(property.key) && unread(`${name}.${property.key.name}`)));
|
|
3696
|
+
if (object.properties.length > 0)
|
|
3697
|
+
return true;
|
|
3698
|
+
if (styleSheetLocal)
|
|
3699
|
+
convertedLocals.add(styleSheetLocal);
|
|
3700
|
+
return false;
|
|
3701
|
+
});
|
|
3702
|
+
if (statement.declarations.length === 0)
|
|
3703
|
+
body.splice(body.indexOf(statement), 1);
|
|
3704
|
+
}
|
|
3705
|
+
}
|
|
2118
3706
|
// ── printing ─────────────────────────────────────────────────────────
|
|
2119
3707
|
/** Prints the chain; with `holes`, each carried value is its hole. */
|
|
2120
3708
|
function print(ir, carry, root = false) {
|
|
2121
3709
|
switch (ir.type) {
|
|
2122
|
-
case 'embed':
|
|
2123
|
-
|
|
3710
|
+
case 'embed': {
|
|
3711
|
+
const text = carry(ir.content);
|
|
3712
|
+
const shown = ir.content.node.__carried ?? ir.content.node;
|
|
3713
|
+
// A language view the file placed with toElement() is that view: `Embed(toElement(v))` is
|
|
3714
|
+
// `v`. (A fixture's hole stands for the element, so there it stays embedded.)
|
|
3715
|
+
if (isToElement(shown) && shown.arguments.length === 1 && text === recast.print(shown).code)
|
|
3716
|
+
return recast.print(shown.arguments[0]).code;
|
|
3717
|
+
return `${lang('Embed')}(${text})`;
|
|
3718
|
+
}
|
|
2124
3719
|
case 'comment':
|
|
2125
3720
|
return ir.text;
|
|
2126
3721
|
case 'screen':
|
|
@@ -2130,7 +3725,7 @@ function transformSource(source, options) {
|
|
|
2130
3725
|
case 'map':
|
|
2131
3726
|
return `${carry(ir.items)}.map((${ir.params.map((param) => recast.print(param).code).join(', ')}) => ${print(ir.body, carry)})`;
|
|
2132
3727
|
case 'icon':
|
|
2133
|
-
return `${lang('Icon')}(${ir.component})${ir.mods.map(printMod).join('')}${ir.color ? `.foreground(${appColorTable}.color.${ir.color.name})` : ''}${ir.key && !root ? `.id(${carry(ir.key)})` : ''}`;
|
|
3728
|
+
return `${lang('Icon')}(${ir.component})${ir.mods.map((mod) => printMod(mod, carry)).join('')}${ir.color ? `.foreground(${appColorTable}.color.${ir.color.name})` : ''}${ir.key && !root ? `.id(${carry(ir.key)})` : ''}`;
|
|
2134
3729
|
case 'component': {
|
|
2135
3730
|
// Its children (the trailing views) with their comments; the arguments before them as values.
|
|
2136
3731
|
const first = ir.args.findIndex((a) => isTagged(a) && a.$ === 'view');
|
|
@@ -2177,12 +3772,12 @@ function transformSource(source, options) {
|
|
|
2177
3772
|
.map((mod, i) => {
|
|
2178
3773
|
const other = choice && 'mods' in choice ? choice.mods[i] : undefined;
|
|
2179
3774
|
if (!choice || !other)
|
|
2180
|
-
return printMod(mod);
|
|
3775
|
+
return printMod(mod, carry);
|
|
2181
3776
|
const args = mod.args.map((arg, j) => {
|
|
2182
3777
|
const alternative = other.args[j];
|
|
2183
3778
|
return arg === alternative
|
|
2184
|
-
? printArg(arg)
|
|
2185
|
-
: `${carry(choice.test)} ? ${printArg(arg)} : ${printArg(alternative)}`;
|
|
3779
|
+
? printArg(arg, carry)
|
|
3780
|
+
: `${carry(choice.test)} ? ${printArg(arg, carry)} : ${printArg(alternative, carry)}`;
|
|
2186
3781
|
});
|
|
2187
3782
|
return `.${mod.name}(${args.join(', ')})`;
|
|
2188
3783
|
})
|
|
@@ -2198,10 +3793,10 @@ function transformSource(source, options) {
|
|
|
2198
3793
|
[fails, choice.only.whenFalse],
|
|
2199
3794
|
])
|
|
2200
3795
|
if (mods.length)
|
|
2201
|
-
out += `.when(${condition}, (v) => v${mods.map(printMod).join('')})`;
|
|
3796
|
+
out += `.when(${condition}, (v) => v${mods.map((mod) => printMod(mod, carry)).join('')})`;
|
|
2202
3797
|
}
|
|
2203
3798
|
for (const variant of resolved.variants) {
|
|
2204
|
-
let inner = `v${variant.mods.map(printMod).join('')}`;
|
|
3799
|
+
let inner = `v${variant.mods.map((mod) => printMod(mod, carry)).join('')}`;
|
|
2205
3800
|
for (const condition of [...variant.conditions].reverse().slice(0, -1))
|
|
2206
3801
|
inner = `v.when(${quote(condition)}, (v) => ${inner})`;
|
|
2207
3802
|
out += `.when(${quote(variant.conditions[0])}, (v) => ${inner})`;
|
|
@@ -2230,22 +3825,32 @@ function transformSource(source, options) {
|
|
|
2230
3825
|
case 'row':
|
|
2231
3826
|
return `(${value.param}) => ${print(value.view, carry)}`;
|
|
2232
3827
|
case 'build':
|
|
2233
|
-
return `(v) => v${value.mods.map(printMod).join('')}`;
|
|
3828
|
+
return `(v) => v${value.mods.map((mod) => printMod(mod, carry)).join('')}`;
|
|
2234
3829
|
case 'renderer':
|
|
2235
3830
|
return carry(value.fn);
|
|
2236
3831
|
case 'absent':
|
|
2237
3832
|
return 'undefined';
|
|
3833
|
+
case 'component':
|
|
3834
|
+
return value.name;
|
|
3835
|
+
case 'language':
|
|
3836
|
+
return lang(value.name);
|
|
2238
3837
|
case 'null':
|
|
2239
3838
|
return 'null';
|
|
3839
|
+
case 'choose':
|
|
3840
|
+
return `${carry(value.test)} ? ${printValue(value.then, carry)} : ${printValue(value.otherwise, carry)}`;
|
|
3841
|
+
case 'appColor':
|
|
3842
|
+
return `${appColorTable}.color.${value.color.name}`;
|
|
2240
3843
|
}
|
|
2241
3844
|
}
|
|
2242
|
-
return printArg(value);
|
|
3845
|
+
return printArg(value, carry);
|
|
2243
3846
|
}
|
|
2244
3847
|
const printProp = (prop, carry) => `.${prop.name}(${prop.args.map((a) => printValue(a, carry)).join(', ')})`;
|
|
2245
3848
|
/** The carried values inside a value, in order. */
|
|
2246
3849
|
function carriedOfValue(value, out) {
|
|
2247
3850
|
if (isCarried(value))
|
|
2248
3851
|
out.push(value);
|
|
3852
|
+
else if (!isTagged(value))
|
|
3853
|
+
out.push(...pointsOf(value));
|
|
2249
3854
|
else if (isTagged(value)) {
|
|
2250
3855
|
if (value.$ === 'view' || value.$ === 'row')
|
|
2251
3856
|
carriedOf(value.view, out);
|
|
@@ -2257,6 +3862,11 @@ function transformSource(source, options) {
|
|
|
2257
3862
|
else if (value.$ === 'options')
|
|
2258
3863
|
for (const [, v] of value.entries)
|
|
2259
3864
|
carriedOfValue(v, out);
|
|
3865
|
+
else if (value.$ === 'choose') {
|
|
3866
|
+
out.push(value.test);
|
|
3867
|
+
carriedOfValue(value.then, out);
|
|
3868
|
+
carriedOfValue(value.otherwise, out);
|
|
3869
|
+
}
|
|
2260
3870
|
}
|
|
2261
3871
|
}
|
|
2262
3872
|
/** The views a value holds. */
|
|
@@ -2292,6 +3902,15 @@ function transformSource(source, options) {
|
|
|
2292
3902
|
return props;
|
|
2293
3903
|
}
|
|
2294
3904
|
}
|
|
3905
|
+
/** The points a view's modifiers carry (a style's `width: itemSize`), each once. */
|
|
3906
|
+
function resolvedPoints(resolved, choice) {
|
|
3907
|
+
const mods = [
|
|
3908
|
+
...resolved.mods,
|
|
3909
|
+
...resolved.variants.flatMap((variant) => variant.mods),
|
|
3910
|
+
...choiceMods(choice),
|
|
3911
|
+
];
|
|
3912
|
+
return [...new Set(mods.flatMap((mod) => mod.args.flatMap(pointsOf)))];
|
|
3913
|
+
}
|
|
2295
3914
|
/** Every carried value of a tree, in order. */
|
|
2296
3915
|
function carriedOf(ir, out = []) {
|
|
2297
3916
|
switch (ir.type) {
|
|
@@ -2344,6 +3963,7 @@ function transformSource(source, options) {
|
|
|
2344
3963
|
out.push(run.value);
|
|
2345
3964
|
else if (!('type' in run && run.type === 'string'))
|
|
2346
3965
|
carriedOf(run, out);
|
|
3966
|
+
out.push(...resolvedPoints(ir.resolved, ir.choice));
|
|
2347
3967
|
if (ir.choice)
|
|
2348
3968
|
out.push(ir.choice.test);
|
|
2349
3969
|
for (const prop of ir.props)
|
|
@@ -2355,6 +3975,7 @@ function transformSource(source, options) {
|
|
|
2355
3975
|
case 'stack':
|
|
2356
3976
|
for (const child of ir.children)
|
|
2357
3977
|
carriedOf(child, out);
|
|
3978
|
+
out.push(...resolvedPoints(ir.resolved, ir.choice));
|
|
2358
3979
|
if (ir.choice)
|
|
2359
3980
|
out.push(ir.choice.test);
|
|
2360
3981
|
for (const prop of ir.props)
|
|
@@ -2373,7 +3994,10 @@ function transformSource(source, options) {
|
|
|
2373
3994
|
string: (i) => `'⟨${i}⟩'`,
|
|
2374
3995
|
key: (i) => `'k${i}'`,
|
|
2375
3996
|
number: () => '2',
|
|
3997
|
+
// A length of its own for each, so a value carried to another key shows.
|
|
3998
|
+
points: (i) => String(37 + 6 * i),
|
|
2376
3999
|
boolean: () => 'true',
|
|
4000
|
+
state: () => '{ selected: true, busy: false }',
|
|
2377
4001
|
handler: () => '() => {}',
|
|
2378
4002
|
action: () => '(() => {})',
|
|
2379
4003
|
element: (i) => `<Hole n="${i}" />`,
|
|
@@ -2391,19 +4015,35 @@ function transformSource(source, options) {
|
|
|
2391
4015
|
if (conditions && carried.some((c) => c.hole === 'condition'))
|
|
2392
4016
|
fixture(node, ir, rootKey, false);
|
|
2393
4017
|
const index = new Map(carried.map((c, i) => [c, i]));
|
|
2394
|
-
|
|
4018
|
+
// Points are one hole per expression: `width: size, height: size` stays a square.
|
|
4019
|
+
const pointsTexts = [];
|
|
4020
|
+
const pointsHole = (node) => {
|
|
4021
|
+
const text = recast.print(node).code;
|
|
4022
|
+
if (!pointsTexts.includes(text))
|
|
4023
|
+
pointsTexts.push(text);
|
|
4024
|
+
return HOLE.points(pointsTexts.indexOf(text));
|
|
4025
|
+
};
|
|
4026
|
+
const hole = (c) => c.hole === 'condition'
|
|
4027
|
+
? String(conditions)
|
|
4028
|
+
: c.hole === 'points'
|
|
4029
|
+
? pointsHole(c.node)
|
|
4030
|
+
: HOLE[c.hole](index.get(c) ?? 0);
|
|
2395
4031
|
const removed = rootKey?.attribute;
|
|
2396
4032
|
const after = ir.type === 'screen' ? print(ir, hole, true) : `toElement(${print(ir, hole, true)})`;
|
|
2397
4033
|
// The source of the region, each carried value replaced by its hole.
|
|
2398
4034
|
let before = '';
|
|
2399
4035
|
let at = node.start;
|
|
4036
|
+
// A shorthand property's value is its key too (`{ width }`): the hole is written beside the key.
|
|
2400
4037
|
const spans = carried.map((c) => ({
|
|
2401
4038
|
start: c.node.start,
|
|
2402
4039
|
end: c.node.end,
|
|
2403
|
-
text: hole(c),
|
|
4040
|
+
text: shorthandValues.has(c.node) ? `${c.node.name}: ${hole(c)}` : hole(c),
|
|
2404
4041
|
}));
|
|
2405
4042
|
if (removed)
|
|
2406
4043
|
spans.push({ start: removed.start, end: removed.end, text: '' });
|
|
4044
|
+
for (const side of heldPoints)
|
|
4045
|
+
if (side.start >= node.start && side.end <= node.end)
|
|
4046
|
+
spans.push({ start: side.start, end: side.end, text: pointsHole(side) });
|
|
2407
4047
|
// An element's type arguments (`<SegmentedControl<Cadence>`) draw nothing, and name the app's types.
|
|
2408
4048
|
recast.visit(node, {
|
|
2409
4049
|
visitJSXOpeningElement(path) {
|
|
@@ -2418,6 +4058,24 @@ function transformSource(source, options) {
|
|
|
2418
4058
|
for (const [read, literal] of styleLiterals)
|
|
2419
4059
|
if (read.start >= node.start && read.end <= node.end)
|
|
2420
4060
|
spans.push({ start: read.start, end: read.end, text: literal });
|
|
4061
|
+
// React Native's keyboard behavior reads its `Platform`, the corpus's own on the before side.
|
|
4062
|
+
for (const behavior of keyboardBehaviors)
|
|
4063
|
+
if (behavior.start >= node.start && behavior.end <= node.end)
|
|
4064
|
+
spans.push({
|
|
4065
|
+
start: behavior.test.left.object.start,
|
|
4066
|
+
end: behavior.test.left.object.end,
|
|
4067
|
+
text: 'RN.Platform',
|
|
4068
|
+
});
|
|
4069
|
+
// An inset the chain pads by is the corpus's on the before side, both sides drawn under it.
|
|
4070
|
+
let insets = false;
|
|
4071
|
+
for (const [read, side] of insetReads)
|
|
4072
|
+
if (read.start >= node.start &&
|
|
4073
|
+
read.end <= node.end &&
|
|
4074
|
+
// An inset inside points the chain carries (`top: insets.top + 8`) is that value's hole.
|
|
4075
|
+
!carried.some((c) => c.node.start <= read.start && read.end <= c.node.end)) {
|
|
4076
|
+
insets = true;
|
|
4077
|
+
spans.push({ start: read.start, end: read.end, text: `PARITY_INSETS.${side}` });
|
|
4078
|
+
}
|
|
2421
4079
|
// A theme colour the chain names by its role is read from the theme the before side is drawn in.
|
|
2422
4080
|
let themed = false;
|
|
2423
4081
|
for (const [read] of themeRoles)
|
|
@@ -2426,9 +4084,50 @@ function transformSource(source, options) {
|
|
|
2426
4084
|
spans.push({
|
|
2427
4085
|
start: read.start,
|
|
2428
4086
|
end: read.end,
|
|
2429
|
-
text:
|
|
4087
|
+
text: colorTokenReads.has(read)
|
|
4088
|
+
? `parityTokens.${read.property.name}`
|
|
4089
|
+
: `parityTheme.colors.${read.property.name}`,
|
|
2430
4090
|
});
|
|
2431
4091
|
}
|
|
4092
|
+
// The surface fill is the before side's own read of it, where it is drawn.
|
|
4093
|
+
for (const read of surfaceReads)
|
|
4094
|
+
if (read.start >= node.start && read.end <= node.end) {
|
|
4095
|
+
themed = true;
|
|
4096
|
+
spans.push({ start: read.start, end: read.end, text: 'paritySurface' });
|
|
4097
|
+
}
|
|
4098
|
+
// A `const` holding the ink is its initialiser on the before side, its conditions the holes and its colours the theme's.
|
|
4099
|
+
for (const read of roleConstReads) {
|
|
4100
|
+
if (read.start < node.start || read.end > node.end)
|
|
4101
|
+
continue;
|
|
4102
|
+
const init = constInits.get(read);
|
|
4103
|
+
const inner = [];
|
|
4104
|
+
// A condition both inks read is one span.
|
|
4105
|
+
for (const c of carried)
|
|
4106
|
+
if (c.node.start >= init.start &&
|
|
4107
|
+
c.node.end <= init.end &&
|
|
4108
|
+
!inner.some((span) => span.start === c.node.start))
|
|
4109
|
+
inner.push({ start: c.node.start, end: c.node.end, text: hole(c) });
|
|
4110
|
+
for (const [themeRead] of themeRoles)
|
|
4111
|
+
if (themeRead.start >= init.start && themeRead.end <= init.end) {
|
|
4112
|
+
themed = true;
|
|
4113
|
+
inner.push({
|
|
4114
|
+
start: themeRead.start,
|
|
4115
|
+
end: themeRead.end,
|
|
4116
|
+
text: colorTokenReads.has(themeRead)
|
|
4117
|
+
? `parityTokens.${themeRead.property.name}`
|
|
4118
|
+
: `parityTheme.colors.${themeRead.property.name}`,
|
|
4119
|
+
});
|
|
4120
|
+
}
|
|
4121
|
+
inner.sort((a, b) => a.start - b.start);
|
|
4122
|
+
let text = '';
|
|
4123
|
+
let from = init.start;
|
|
4124
|
+
for (const span of inner) {
|
|
4125
|
+
text += source.slice(from, span.start) + span.text;
|
|
4126
|
+
from = span.end;
|
|
4127
|
+
}
|
|
4128
|
+
text += source.slice(from, init.end);
|
|
4129
|
+
spans.push({ start: read.start, end: read.end, text: `(${text})` });
|
|
4130
|
+
}
|
|
2432
4131
|
// A constant the chain names as an app colour is its colour on the before side, drawn without the module.
|
|
2433
4132
|
const colors = colorsOf(ir);
|
|
2434
4133
|
for (const color of colors)
|
|
@@ -2446,6 +4145,19 @@ function transformSource(source, options) {
|
|
|
2446
4145
|
for (const local of new Set([...uncommented(before).matchAll(/<\/?([A-Z][\w]*)/g)].map((m) => m[1])))
|
|
2447
4146
|
if (sources.has(local))
|
|
2448
4147
|
bindings[local] = sources.get(local);
|
|
4148
|
+
// A Bloom or React Native value the before side reads or calls (`tokens.hitSlop.md`, `fontFamilyStyle('display')`).
|
|
4149
|
+
for (const match of uncommented(before).matchAll(/(?:^|[^\w.]|\.\.\.)([a-z]\w*)[.(]/g)) {
|
|
4150
|
+
const local = match[1];
|
|
4151
|
+
const module = sources.get(local)?.module ?? '';
|
|
4152
|
+
if (module.startsWith('@oxy.so/bloom/') || module === 'react-native')
|
|
4153
|
+
bindings[local] = sources.get(local);
|
|
4154
|
+
}
|
|
4155
|
+
// A language view the before side already writes (a menu row's `toElement(Icon(…))`).
|
|
4156
|
+
for (const match of uncommented(before).matchAll(/\b([A-Z][\w]*)\(/g)) {
|
|
4157
|
+
const local = match[1];
|
|
4158
|
+
if (sources.get(local)?.module === LANGUAGE_MODULE)
|
|
4159
|
+
bindings[local] = sources.get(local);
|
|
4160
|
+
}
|
|
2449
4161
|
for (const match of uncommented(after).matchAll(/\b([A-Z][\w]*)\b/g)) {
|
|
2450
4162
|
const local = match[1];
|
|
2451
4163
|
if (sources.has(local))
|
|
@@ -2470,6 +4182,7 @@ function transformSource(source, options) {
|
|
|
2470
4182
|
// A route's Screen is the Screen its nodes are drawn in.
|
|
2471
4183
|
within: withinOf(ir).filter((key) => ir.type !== 'screen' || key !== SCREEN_KEY),
|
|
2472
4184
|
themed,
|
|
4185
|
+
insets,
|
|
2473
4186
|
screen: ir.type === 'screen',
|
|
2474
4187
|
appColors: colors.length
|
|
2475
4188
|
? {
|
|
@@ -2482,9 +4195,23 @@ function transformSource(source, options) {
|
|
|
2482
4195
|
/** The app colours a region's icons paint with. */
|
|
2483
4196
|
function colorsOf(ir) {
|
|
2484
4197
|
const found = [];
|
|
4198
|
+
const inValue = (value) => {
|
|
4199
|
+
if (!isTagged(value))
|
|
4200
|
+
return;
|
|
4201
|
+
if (value.$ === 'appColor')
|
|
4202
|
+
found.push(value.color);
|
|
4203
|
+
else if (value.$ === 'choose') {
|
|
4204
|
+
inValue(value.then);
|
|
4205
|
+
inValue(value.otherwise);
|
|
4206
|
+
}
|
|
4207
|
+
};
|
|
2485
4208
|
const visit = (at) => {
|
|
2486
4209
|
if (at.type === 'icon' && at.color)
|
|
2487
4210
|
found.push(at.color);
|
|
4211
|
+
if ('props' in at)
|
|
4212
|
+
for (const prop of at.props)
|
|
4213
|
+
for (const arg of prop.args)
|
|
4214
|
+
inValue(arg);
|
|
2488
4215
|
for (const child of childrenOf(at))
|
|
2489
4216
|
visit(child);
|
|
2490
4217
|
};
|
|
@@ -2544,8 +4271,18 @@ function transformSource(source, options) {
|
|
|
2544
4271
|
appColors.set(color.name, color);
|
|
2545
4272
|
lang('defineTokens');
|
|
2546
4273
|
}
|
|
4274
|
+
// A route's Screen inside what its screen modifiers draw is the return around it, whole.
|
|
4275
|
+
const outer = ir.type === 'screen' ? screenOuter.get(node)?.outer : undefined;
|
|
2547
4276
|
if (options.fixtures)
|
|
2548
|
-
fixture(node, ir, rootKey);
|
|
4277
|
+
fixture(outer ?? node, ir, rootKey);
|
|
4278
|
+
// What wrapped the Screen is its modifiers now: its imports go where nothing else reads them.
|
|
4279
|
+
if (outer)
|
|
4280
|
+
for (let at = outer; at !== node; at = peelChild(at)) {
|
|
4281
|
+
convertedLocals.add(jsxName(at));
|
|
4282
|
+
for (const behavior of keyboardBehaviors)
|
|
4283
|
+
if (behavior.start >= at.start && behavior.end <= at.end)
|
|
4284
|
+
convertedLocals.add(behavior.test.left.object.name);
|
|
4285
|
+
}
|
|
2549
4286
|
// Regions inside the values it carries (an embedded element's children, an array's elements).
|
|
2550
4287
|
for (const c of carriedOf(ir)) {
|
|
2551
4288
|
if (c.hole === 'element' || c.hole === 'items') {
|
|
@@ -2565,10 +4302,13 @@ function transformSource(source, options) {
|
|
|
2565
4302
|
: `${lang('toElement')}(${chain}${rootKey ? `, ${carry(rootKey)}` : ''})`;
|
|
2566
4303
|
// Parsed by recast, so the comments the chain carries are printed where they are.
|
|
2567
4304
|
const expression = recast.parse(`${code};`, { parser: babelTs }).program.body[0].expression;
|
|
2568
|
-
|
|
4305
|
+
let at = path;
|
|
4306
|
+
while (outer && at.node !== outer)
|
|
4307
|
+
at = at.parent;
|
|
4308
|
+
const parent = at.parent?.node;
|
|
2569
4309
|
const inJsx = parent &&
|
|
2570
4310
|
(n.JSXElement.check(parent) || n.JSXFragment.check(parent) || n.JSXAttribute.check(parent));
|
|
2571
|
-
|
|
4311
|
+
at.replace(inJsx ? recast.types.builders.jsxExpressionContainer(expression) : expression);
|
|
2572
4312
|
return false;
|
|
2573
4313
|
},
|
|
2574
4314
|
};
|
|
@@ -2606,17 +4346,48 @@ function transformSource(source, options) {
|
|
|
2606
4346
|
}
|
|
2607
4347
|
/** The routes whose Screens converted: each becomes `screen(function …)`. */
|
|
2608
4348
|
const routes = new Set();
|
|
4349
|
+
// The module styles elements read before the walk: what it leaves unread goes after it.
|
|
4350
|
+
const stylesReadBefore = styleReadsIn(ast);
|
|
2609
4351
|
recast.visit(ast, visitor);
|
|
2610
|
-
for (const
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
4352
|
+
for (const fn of routes) {
|
|
4353
|
+
const { statement, name } = screenDeclarations.get(fn);
|
|
4354
|
+
const b = recast.types.builders;
|
|
4355
|
+
const id = b.identifier(name);
|
|
4356
|
+
// An arrow's expression body is the one return of its function.
|
|
4357
|
+
const block = n.BlockStatement.check(fn.body)
|
|
4358
|
+
? fn.body
|
|
4359
|
+
: b.blockStatement([b.returnStatement(fn.body)]);
|
|
4360
|
+
const body = b.functionExpression(id, fn.params, block);
|
|
4361
|
+
body.typeParameters = fn.typeParameters;
|
|
4362
|
+
const call = b.callExpression(b.identifier(lang('screen')), [body]);
|
|
4363
|
+
const program = ast.program.body;
|
|
4364
|
+
const at = program.indexOf(statement);
|
|
4365
|
+
let named = 0;
|
|
4366
|
+
recast.visit(ast, {
|
|
4367
|
+
visitIdentifier(path) {
|
|
4368
|
+
if (path.node.name === name)
|
|
4369
|
+
named++;
|
|
4370
|
+
this.traverse(path);
|
|
4371
|
+
},
|
|
4372
|
+
});
|
|
4373
|
+
if (n.ExportDefaultDeclaration.check(statement) && named === 1) {
|
|
4374
|
+
// A default export read nowhere else is the call itself.
|
|
4375
|
+
statement.declaration = call;
|
|
4376
|
+
continue;
|
|
2617
4377
|
}
|
|
4378
|
+
const constant = b.variableDeclaration('const', [b.variableDeclarator(id, call)]);
|
|
4379
|
+
if (!n.ExportNamedDeclaration.check(statement))
|
|
4380
|
+
constant.comments = statement.comments;
|
|
4381
|
+
if (n.ExportDefaultDeclaration.check(statement))
|
|
4382
|
+
program.splice(at, 1, constant, b.exportDefaultDeclaration(b.identifier(name)));
|
|
4383
|
+
else if (n.ExportNamedDeclaration.check(statement))
|
|
4384
|
+
statement.declaration = constant;
|
|
4385
|
+
else
|
|
4386
|
+
program.splice(at, 1, constant);
|
|
4387
|
+
}
|
|
2618
4388
|
const converted = [...counted].filter((node) => convertedNodes.has(node)).length;
|
|
2619
4389
|
if (regions > 0) {
|
|
4390
|
+
removeUnreadStyles(stylesReadBefore);
|
|
2620
4391
|
// Imports: the language's names in, and the converted components' out when nothing else uses them.
|
|
2621
4392
|
const remaining = new Map();
|
|
2622
4393
|
recast.visit(ast, {
|