@oxy.so/bloom-language-codemod 0.4.0 → 0.5.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 CHANGED
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Compatibility
6
+
7
+ - Its peer is `@oxy.so/bloom-language` 1.7.0 or newer, the language it writes (an offset in points, the frame length `'auto'`, the stacking level `'1000'` and a border width with no colour).
8
+
9
+ ### Added
10
+
11
+ - Maps a transform of translations alone as `.offset()` (a number written out as its space token, one computed at render as points), an `aspectRatio` computed at render, `width: 'auto'` and `w-auto`/`h-auto`/`size-auto` as the frame length `'auto'` (no longer dropped: on a Bloom component it takes back the component's own length), and `zIndex: 1000`/`z-[1000]` as `.zIndex('1000')`.
12
+ - Maps `absolute` with no inset (a class or `position: 'absolute'` in a style) to `.position({})`, a view taken out of the flow where it sits, where it gave up (`absolute-without-edges`); an edge computed at render stays the position on its own.
13
+ - Maps a border width with no colour, a style's `borderWidth` (or an edge's, `borderBottomWidth`…) beside no border colour and a `border` / `border-b-2` class beside no colour class, to `.border(undefined, width, edge?)`: the line in the platform's default colour, where it was left JSX. A sample-app pill and rule join the corpus.
14
+ - Maps a `pointerEvents` in a style where it draws. A StyleSheet's style is compiled by react-native-web into the prop's classes, so it is `.allowsHitTesting()` on every platform (`none` `false`, `box-none` `'children'`, `box-only` `'self'`, `auto` `true`); in an inline object or a module style constant react-native-web writes `box-none` and `box-only` as CSS the browser drops, so it is `.when('native', (v) => v.allowsHitTesting('children'))` (or `'self'`), as Mention's attachment move row draws. An inline `none` or `auto` stays JSX with its own reason (the browser keeps it, and what is inside inherits it instead of taking the prop's rule). A style modifier can hold on one platform, a variant as a `native:` class's.
15
+ - Maps a Text's (and a span's) `onPress` and `onLongPress` to `.onPress()` and `.onLongPress()`.
16
+ - An element inside a Text that the codemod does not convert (a router's `Link`, an app's component) is an element run, `Embed(element)`, so the Text converts around it; an embedded `asChild` element around one text it converts is `Embed((render) => <Link … asChild>{render(Text(…))}</Link>)`.
17
+
18
+ ### Fixed
19
+
20
+ - The child of an `asChild` element is no longer made a `toElement` root outside a language view: the element hands its child its own props (expo-router's `Link asChild`: the href and the press), which a root took in place of its host and dropped. It stays JSX, with the reason `as-child`.
21
+
3
22
  ## 0.4.0
4
23
 
5
24
  ### Compatibility
package/lib/resolve.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type Mod, type Table } from './table';
1
+ import { type Mod, type StyleMod, type Table } from './table';
2
2
  /** What draws the className: a React Native View, Pressable or a Bloom Text. */
3
3
  export type HostKind = 'view' | 'pressable' | 'text';
4
4
  export interface Reason {
@@ -49,10 +49,7 @@ export interface ResolveOptions {
49
49
  * as `.grow()`, a theme colour as its role), by the style key: they join the
50
50
  * classes' modifiers, so a class setting the same thing is a conflict.
51
51
  */
52
- readonly styleMods?: readonly {
53
- readonly subject: string;
54
- readonly mod: Mod;
55
- }[];
52
+ readonly styleMods?: readonly StyleMod[];
56
53
  /** The classes a `style` was read as (`gap: 12` is `gap-3`): inline, so no variant overrides them. */
57
54
  readonly styleClasses?: ReadonlySet<string>;
58
55
  }
package/lib/resolve.js CHANGED
@@ -337,8 +337,8 @@ function resolveClassName(className, kind, table, options = {}) {
337
337
  fail(token, entry.status, entry.language);
338
338
  }
339
339
  }
340
- for (const { subject, mod } of options.styleMods ?? []) {
341
- const into = group([]);
340
+ for (const { subject, mod, conditions } of options.styleMods ?? []) {
341
+ const into = group([...(conditions ?? [])]);
342
342
  into.mods.push(mod);
343
343
  into.subjects.set(mod, subject);
344
344
  }
@@ -401,16 +401,21 @@ function finish(g, kind, fail) {
401
401
  // A text size's own leading gives way to an explicit leading class.
402
402
  if (g.impliedLeading && mods.filter((m) => m.name === 'leading').length > 1)
403
403
  mods = mods.filter((m) => m !== g.impliedLeading);
404
- // Border: a width class and a colour class are one modifier.
405
- const colors = mods.filter((m) => m.name === 'border');
406
- mods = mods.filter((m) => m.name !== 'border');
404
+ // Border: a width class and a colour class are one modifier. A border a style
405
+ // wrote whole (its width in the platform's colour, `.border(undefined, width)`) is one already.
406
+ const colors = mods.filter((m) => m.name === 'border' && m.args.length === 1);
407
+ mods = mods.filter((m) => m.name !== 'border' || m.args.length > 1);
407
408
  if (g.widths.length > 0 || colors.length > 0) {
408
- if (colors.length !== 1 || g.widths.length === 0) {
409
- const what = colors.length === 0
410
- ? 'a border width with no colour class (Tailwind paints currentColor)'
411
- : g.widths.length === 0
412
- ? 'a border colour with no width class (no border is drawn)'
413
- : 'two border colours';
409
+ // A width with no colour class: the platform's default colour (React Native's black,
410
+ // react-native-web's own on the web, over the preflight's), `.border(undefined, width)`.
411
+ if (colors.length === 0) {
412
+ for (const width of g.widths)
413
+ mods.push({ name: 'border', args: [undefined, ...width.args] });
414
+ }
415
+ else if (colors.length !== 1 || g.widths.length === 0) {
416
+ const what = g.widths.length === 0
417
+ ? 'a border colour with no width class (no border is drawn)'
418
+ : 'two border colours';
414
419
  fail([...g.widths.map((w) => w.subject), ...colors.map(subject)].join(' '), 'border', what);
415
420
  }
416
421
  else {
@@ -425,8 +430,9 @@ function finish(g, kind, fail) {
425
430
  if (g.absolute || edges.length > 0) {
426
431
  if (!g.absolute)
427
432
  fail(edges.map(subject).join(' '), 'offset-without-absolute', 'an inset without `absolute` offsets the view in its flow');
433
+ // `absolute` alone takes the view out of the flow where it would sit: a position of no edge.
428
434
  else if (edges.length === 0)
429
- fail(g.absolute, 'absolute-without-edges', '`absolute` with no inset class');
435
+ mods.push({ name: 'position', args: [{}] });
430
436
  else {
431
437
  const merged = mergeObjects(edges, 'position', subject, fail);
432
438
  if (merged)
package/lib/style.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type Mod } from './table';
1
+ import { type Mod, type StyleMod } from './table';
2
2
  /**
3
3
  * A text's own `fontSize` and `lineHeight` written out, as the language's
4
4
  * size and line: `fontSize: 15` is `.font('md')` (a named rung over a numeric
@@ -13,10 +13,7 @@ export type StyleResult = {
13
13
  readonly ok: true;
14
14
  readonly classes: readonly string[];
15
15
  /** What no utility writes, by its style key (`flex: 1` is `.grow()`). */
16
- readonly mods: readonly {
17
- readonly subject: string;
18
- readonly mod: Mod;
19
- }[];
16
+ readonly mods: readonly StyleMod[];
20
17
  readonly literal: string;
21
18
  /**
22
19
  * Points the chain draws without carrying them (a half-square radius's
@@ -58,11 +55,13 @@ export interface StyleReaders {
58
55
  readonly numberOf?: (node: AnyNode) => number | undefined;
59
56
  /** Whether a value is a number the view computes as it renders (geometry's points). */
60
57
  readonly computed?: (node: AnyNode) => boolean;
58
+ /** Whether react-native-web compiles the style into classes (a StyleSheet's), not an inline object. */
59
+ readonly compiled?: boolean;
61
60
  }
62
61
  /**
63
62
  * An object literal of a style as utilities, and the literal itself (for the
64
63
  * parity's before side, where `styles.x` is not in scope). A spread `spreadOf`
65
64
  * reads is what it writes; any other is a style built at runtime.
66
65
  */
67
- export declare function styleUtilities(object: AnyNode, { read, spreadOf, numberOf, computed }?: StyleReaders): StyleResult;
66
+ export declare function styleUtilities(object: AnyNode, { read, spreadOf, numberOf, computed, compiled }?: StyleReaders): StyleResult;
68
67
  export {};
package/lib/style.js CHANGED
@@ -73,6 +73,36 @@ const Z_INDEX = byValue(scales.zIndex);
73
73
  const FONT_SIZE = byValue(scales.fontSize);
74
74
  const LEADING_PX = byValue(scales.leadingPx);
75
75
  const SPACE = byValue(scales.space);
76
+ const BORDER_WIDTH = byValue(scales.borderWidth);
77
+ /** A border width's key as the language's border edge. */
78
+ const BORDER_WIDTH_EDGE = {
79
+ borderWidth: 'all',
80
+ borderTopWidth: 'top',
81
+ borderBottomWidth: 'bottom',
82
+ borderStartWidth: 'leading',
83
+ borderEndWidth: 'trailing',
84
+ borderLeftWidth: 'left',
85
+ borderRightWidth: 'right',
86
+ };
87
+ /**
88
+ * A border width in a style that names no border colour: the language's
89
+ * `.border(undefined, width, edge)`, the line in the platform's default colour
90
+ * (React Native's black, react-native-web's own on the web), as the style draws it.
91
+ */
92
+ function defaultColorBorder(object, key, value) {
93
+ const edge = BORDER_WIDTH_EDGE[key];
94
+ if (edge === undefined || !n.NumericLiteral.check(value))
95
+ return undefined;
96
+ const colored = object.properties.some((property) => (n.ObjectProperty.check(property) || n.Property.check(property)) &&
97
+ n.Identifier.check(property.key) &&
98
+ /^border\w*Color$/.test(property.key.name));
99
+ if (colored)
100
+ return undefined;
101
+ const width = BORDER_WIDTH.get(value.value);
102
+ if (width === undefined)
103
+ return undefined;
104
+ return { name: 'border', args: edge === 'all' ? [undefined, width] : [undefined, width, edge] };
105
+ }
76
106
  /** React Native's inline and block insets, as the edge of `.position()` that sets both sides. */
77
107
  const INSET_AXIS = {
78
108
  insetInline: 'horizontal',
@@ -272,6 +302,36 @@ function direct(key, value) {
272
302
  }
273
303
  return undefined;
274
304
  }
305
+ /** React Native's `pointerEvents` as the language's hit testing. */
306
+ const HIT_TESTING = {
307
+ auto: true,
308
+ none: false,
309
+ 'box-none': 'children',
310
+ 'box-only': 'self',
311
+ };
312
+ /**
313
+ * A `pointerEvents` in a style as hit testing, drawn where the JSX's is. A
314
+ * StyleSheet's style is compiled by react-native-web into the classes its
315
+ * prop writes (`box-none` is `none` on the view and `auto` on what is inside
316
+ * it), the language's on every platform. An inline object it writes as it is:
317
+ * the CSS `pointer-events: box-none` (or `box-only`) is a value the browser
318
+ * drops, so the web view takes touches as any view does, and only native reads
319
+ * it (`.when('native', …)`). An inline `none` or `auto` is CSS a browser keeps,
320
+ * which reaches what is inside by inheritance, not as the prop's rule: a reason.
321
+ */
322
+ function hitTestingStyle(key, value, compiled) {
323
+ const hit = n.StringLiteral.check(value) ? HIT_TESTING[value.value] : undefined;
324
+ if (hit === undefined)
325
+ return { why: 'a style `pointerEvents` that is no static value' };
326
+ const mod = { name: 'allowsHitTesting', args: [hit] };
327
+ if (compiled)
328
+ return { subject: key, mod };
329
+ if (typeof hit === 'string')
330
+ return { subject: key, mod, conditions: ['native'] };
331
+ return {
332
+ why: `an inline style \`pointerEvents: '${value.value}'\`: react-native-web writes it as CSS that what is inside inherits, not as its prop's rule`,
333
+ };
334
+ }
275
335
  /** One declaration as its utility, or undefined. */
276
336
  function utility(key, value) {
277
337
  if (n.NumericLiteral.check(value)) {
@@ -404,6 +464,9 @@ function pointsDeclaration(key, value) {
404
464
  return { name: 'position', args: [{ [key]: points }] };
405
465
  if (key === 'zIndex')
406
466
  return { name: 'zIndex', args: [points] };
467
+ // A ratio is a number either way: one the view computes (a media's own) is carried as it is.
468
+ if (key === 'aspectRatio')
469
+ return { name: 'aspectRatio', args: [points] };
407
470
  const edge = PADDING_EDGES[key];
408
471
  if (edge === 'all')
409
472
  return { name: 'padding', args: [points] };
@@ -411,6 +474,49 @@ function pointsDeclaration(key, value) {
411
474
  return { name: 'padding', args: [edge, points] };
412
475
  return undefined;
413
476
  }
477
+ /** React Native's translations as the axes of `.offset()`. */
478
+ const TRANSLATE_AXIS = { translateX: 'x', translateY: 'y' };
479
+ /**
480
+ * A transform of translations alone (`transform: [{ translateY: nudge }]`) as
481
+ * `.offset()`: a number written out is the space token of its value (`-4` is
482
+ * `'-xs'`), one the view computes as it renders its points. A transform that
483
+ * scales, rotates or names an axis twice stays a reason.
484
+ */
485
+ function offsetDeclaration(value, numberOf, computed) {
486
+ if (!n.ArrayExpression.check(value) || value.elements.length === 0)
487
+ return undefined;
488
+ const offset = {};
489
+ for (const element of value.elements) {
490
+ const property = n.ObjectExpression.check(element) && element.properties.length === 1
491
+ ? element.properties[0]
492
+ : undefined;
493
+ if (!(n.ObjectProperty.check(property) || n.Property.check(property)) ||
494
+ property.computed ||
495
+ !n.Identifier.check(property.key))
496
+ return undefined;
497
+ const axis = TRANSLATE_AXIS[property.key.name];
498
+ if (axis === undefined || axis in offset)
499
+ return undefined;
500
+ const literal = n.NumericLiteral.check(property.value)
501
+ ? property.value.value
502
+ : n.UnaryExpression.check(property.value) &&
503
+ property.value.operator === '-' &&
504
+ n.NumericLiteral.check(property.value.argument)
505
+ ? -property.value.argument.value
506
+ : numberOf?.(property.value);
507
+ if (literal !== undefined) {
508
+ const token = SPACE.get(Math.abs(literal));
509
+ if (token === undefined)
510
+ return undefined;
511
+ offset[axis] = literal < 0 ? `-${token}` : token;
512
+ }
513
+ else if (computed?.(property.value))
514
+ offset[axis] = { node: property.value, hole: 'points' };
515
+ else
516
+ return undefined;
517
+ }
518
+ return { name: 'offset', args: [offset] };
519
+ }
414
520
  /** A style's own declaration by its key, or undefined. */
415
521
  function declared(object, key) {
416
522
  return object.properties.find((property) => (n.ObjectProperty.check(property) || n.Property.check(property)) &&
@@ -462,7 +568,7 @@ function percentDeclaration(key, value) {
462
568
  * parity's before side, where `styles.x` is not in scope). A spread `spreadOf`
463
569
  * reads is what it writes; any other is a style built at runtime.
464
570
  */
465
- function styleUtilities(object, { read, spreadOf, numberOf, computed } = {}) {
571
+ function styleUtilities(object, { read, spreadOf, numberOf, computed, compiled = false } = {}) {
466
572
  if (!n.ObjectExpression.check(object))
467
573
  return { ok: false, subject: 'style', why: 'a style that is no object literal' };
468
574
  const classes = [];
@@ -505,6 +611,13 @@ function styleUtilities(object, { read, spreadOf, numberOf, computed } = {}) {
505
611
  mods.push({ subject: key, mod: { name: 'textShadow', args: [token] } });
506
612
  continue;
507
613
  }
614
+ if (key === 'pointerEvents') {
615
+ const hit = hitTestingStyle(key, property.value, compiled);
616
+ if ('why' in hit)
617
+ return { ok: false, subject: key, why: hit.why };
618
+ mods.push(hit);
619
+ continue;
620
+ }
508
621
  const known = numberOf?.(property.value);
509
622
  const value = known === undefined
510
623
  ? property.value
@@ -518,7 +631,9 @@ function styleUtilities(object, { read, spreadOf, numberOf, computed } = {}) {
518
631
  held.push(side);
519
632
  continue;
520
633
  }
521
- const mod = direct(key, value) ??
634
+ const mod = (key === 'transform' ? offsetDeclaration(property.value, numberOf, computed) : undefined) ??
635
+ direct(key, value) ??
636
+ defaultColorBorder(object, key, value) ??
522
637
  read?.(key, value) ??
523
638
  (computed?.(value) ? pointsDeclaration(key, value) : undefined);
524
639
  if (mod) {
package/lib/table.d.ts CHANGED
@@ -7,7 +7,9 @@ export interface PointsArg {
7
7
  readonly node: unknown;
8
8
  readonly hole: 'points';
9
9
  }
10
- export type Arg = string | number | boolean | PointsArg | Readonly<Record<string, string | PointsArg>>;
10
+ export type Arg =
11
+ /** `undefined` written out: an argument the modifier takes as none (a border's colour). */
12
+ undefined | string | number | boolean | PointsArg | Readonly<Record<string, string | PointsArg>>;
11
13
  export declare const isPoints: (arg: unknown) => arg is PointsArg;
12
14
  /** Modifiers (or variants of them) as a comparable string, each points value by where it is written. */
13
15
  export declare const modKey: (mods: unknown) => string;
@@ -16,6 +18,15 @@ export interface Mod {
16
18
  readonly name: string;
17
19
  readonly args: readonly Arg[];
18
20
  }
21
+ /**
22
+ * A modifier a style writes, by its style key, and the platform it holds on
23
+ * when it holds on one only (`['native']`): a variant, as a `native:` class's.
24
+ */
25
+ export interface StyleMod {
26
+ readonly subject: string;
27
+ readonly mod: Mod;
28
+ readonly conditions?: readonly string[];
29
+ }
19
30
  export type Status = 'mapped' | 'structure' | 'drop' | 'token' | 'appToken' | 'modifier' | 'motion' | 'app';
20
31
  export interface Entry {
21
32
  readonly status: Status;
package/lib/transform.js CHANGED
@@ -359,6 +359,21 @@ function jsxName(node) {
359
359
  const name = node.openingElement.name;
360
360
  return n.JSXIdentifier.check(name) ? name.name : undefined;
361
361
  }
362
+ /** An element with `asChild` set (bare or `{true}`): it hands its props to the one element inside it. */
363
+ function isAsChild(element) {
364
+ return element.openingElement.attributes.some((attribute) => n.JSXAttribute.check(attribute) &&
365
+ n.JSXIdentifier.check(attribute.name) &&
366
+ attribute.name.name === 'asChild' &&
367
+ (attribute.value === null ||
368
+ (n.JSXExpressionContainer.check(attribute.value) &&
369
+ n.BooleanLiteral.check(attribute.value.expression) &&
370
+ attribute.value.expression.value === true)));
371
+ }
372
+ /** The one element among an element's children (blank text aside), if it holds nothing else. */
373
+ function onlyElementChild(element) {
374
+ const children = element.children.filter((child) => !(n.JSXText.check(child) && cleanJsxText(child.value) === ''));
375
+ return children.length === 1 && n.JSXElement.check(children[0]) ? children[0] : undefined;
376
+ }
362
377
  function staticString(value) {
363
378
  if (!value)
364
379
  return undefined;
@@ -402,6 +417,8 @@ const uncommented = (code) => code.replace(/\/\*[\s\S]*?\*\//g, '');
402
417
  const quote = (text) => `'${text.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\n/g, '\\n')}'`;
403
418
  /** An argument as code; points computed at render are their carried value (`carry`). */
404
419
  function printArg(arg, carry) {
420
+ if (arg === undefined)
421
+ return 'undefined';
405
422
  if (typeof arg === 'string')
406
423
  return quote(arg);
407
424
  if (typeof arg === 'number' || typeof arg === 'boolean')
@@ -409,7 +426,7 @@ function printArg(arg, carry) {
409
426
  if ((0, table_1.isPoints)(arg))
410
427
  return carry(arg);
411
428
  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)}`);
412
- return `{ ${entries.join(', ')} }`;
429
+ return entries.length === 0 ? '{}' : `{ ${entries.join(', ')} }`;
413
430
  }
414
431
  const printMod = (mod, carry) => `.${mod.name}(${mod.args.map((arg) => printArg(arg, carry)).join(', ')})`;
415
432
  /** The points an argument carries: itself, or an object's values (`{ width: itemSize }`). */
@@ -1164,6 +1181,7 @@ function transformSource(source, options) {
1164
1181
  spreadOf,
1165
1182
  numberOf: bloomTokenNumber,
1166
1183
  computed: computedAtRender,
1184
+ compiled: referenced,
1167
1185
  })
1168
1186
  : undefined;
1169
1187
  if (!style)
@@ -1174,8 +1192,13 @@ function transformSource(source, options) {
1174
1192
  mods.push(...style.mods);
1175
1193
  for (const side of style.held)
1176
1194
  heldPoints.add(side);
1195
+ // react-native-web compiles a StyleSheet's `pointerEvents` into its prop's classes, where an
1196
+ // inline object's is written as it is: on the before side that style is compiled again,
1197
+ // through the corpus's own React Native.
1177
1198
  if (referenced)
1178
- literals.set(part, style.literal);
1199
+ literals.set(part, style.mods.some((mod) => mod.subject === 'pointerEvents')
1200
+ ? `RN.StyleSheet.create({ style: ${style.literal} }).style`
1201
+ : style.literal);
1179
1202
  }
1180
1203
  for (const [part, literal] of literals)
1181
1204
  styleLiterals.set(part, literal);
@@ -1196,6 +1219,9 @@ function transformSource(source, options) {
1196
1219
  const style = staticStyle(expression, () => undefined);
1197
1220
  if (!style.ok)
1198
1221
  return reason(style.subject, style.why);
1222
+ const held = style.mods.find((mod) => mod.conditions !== undefined);
1223
+ if (held)
1224
+ return reason(held.subject, 'a style that holds on one platform only');
1199
1225
  const resolution = (0, resolve_1.resolveClassName)(style.classes.join(' '), 'view', options.table, {
1200
1226
  overridden: options.overridden,
1201
1227
  });
@@ -1710,7 +1736,9 @@ function transformSource(source, options) {
1710
1736
  });
1711
1737
  continue;
1712
1738
  }
1713
- if (kind === 'pressable' && (name === 'onPress' || name === 'onLongPress')) {
1739
+ // A Text takes a press as a Pressable does: React Native's Text with its `onPress`, a span's too.
1740
+ if ((kind === 'pressable' || kind === 'bloomText' || kind === 'rnText') &&
1741
+ (name === 'onPress' || name === 'onLongPress')) {
1714
1742
  props.push({ name, args: [{ node: expression, hole: 'handler' }] });
1715
1743
  continue;
1716
1744
  }
@@ -2046,6 +2074,10 @@ function transformSource(source, options) {
2046
2074
  const span = jsxName(child);
2047
2075
  if ('ir' in inner && innerKind === kind && !(span && textStyleOf(span)))
2048
2076
  runs.push(inner.ir);
2077
+ // An element the codemod does not convert (a router's Link, an app's own component) is
2078
+ // an element run, drawn as it is inside the text.
2079
+ else if (!('ir' in inner))
2080
+ runs.push({ type: 'embed', content: { node: child, hole: 'element' } });
2049
2081
  else
2050
2082
  fail(jsxName(child) ?? 'element', 'text-child', 'an element inside a Text that is no convertible Text');
2051
2083
  }
@@ -4106,6 +4138,9 @@ function transformSource(source, options) {
4106
4138
  // `v`. (A fixture's hole stands for the element, so there it stays embedded.)
4107
4139
  if (isToElement(shown) && shown.arguments.length === 1 && text === recast.print(shown).code)
4108
4140
  return recast.print(shown.arguments[0]).code;
4141
+ // An `asChild` element whose child is a text the codemod converted: it hands the text's host its props.
4142
+ if (renderedAsChild.has(ir.content.node))
4143
+ return `${lang('Embed')}((render) => ${text})`;
4109
4144
  return `${lang('Embed')}(${text})`;
4110
4145
  }
4111
4146
  case 'comment':
@@ -4637,6 +4672,38 @@ function transformSource(source, options) {
4637
4672
  }
4638
4673
  // ── the walk ─────────────────────────────────────────────────────────
4639
4674
  let regions = 0;
4675
+ /** Element runs whose `asChild` element is handed its text by `Embed`'s render function. */
4676
+ const renderedAsChild = new WeakSet();
4677
+ /**
4678
+ * An embedded `asChild` element (expo-router's `Link asChild`) around one
4679
+ * text the codemod converts: the text becomes `render(text)`, which draws
4680
+ * the text's host for the element to hand its props to, as its JSX child
4681
+ * was (`Embed((render) => <Link asChild>{render(Text(…))}</Link>)`). Inside
4682
+ * a text it is a span. A file that names `render` itself keeps the JSX.
4683
+ */
4684
+ function renderAsChild(element) {
4685
+ if (!n.JSXElement.check(element) || !isAsChild(element))
4686
+ return;
4687
+ const child = onlyElementChild(element);
4688
+ const kind = child ? elementOf(child)?.kind : undefined;
4689
+ if (!child || (kind !== 'bloomText' && kind !== 'rnText'))
4690
+ return;
4691
+ const { start, end } = element;
4692
+ if (/\brender\b/.test(source.slice(start, end)))
4693
+ return;
4694
+ const result = convert(child);
4695
+ if (!('ir' in result))
4696
+ return;
4697
+ markConverted(child);
4698
+ for (const color of colorsOf(result.ir)) {
4699
+ appColors.set(color.name, color);
4700
+ lang('defineTokens');
4701
+ }
4702
+ const code = `render(${print(result.ir, (c) => recast.print(c.node).code, true)})`;
4703
+ const call = recast.parse(`${code};`, { parser: babelTs }).program.body[0].expression;
4704
+ element.children = element.children.map((at) => at === child ? recast.types.builders.jsxExpressionContainer(call) : at);
4705
+ renderedAsChild.add(element);
4706
+ }
4640
4707
  const markConverted = (node) => {
4641
4708
  recast.visit(node, {
4642
4709
  visitJSXElement(path) {
@@ -4667,6 +4734,29 @@ function transformSource(source, options) {
4667
4734
  this.traverse(path);
4668
4735
  return undefined;
4669
4736
  }
4737
+ // An `asChild` element hands the one element inside it its own props (an href, a press):
4738
+ // a root there would take them in place of the element's host, and drop them. (A Screen
4739
+ // whose route draws its wrappers as screen modifiers, a colour scope among them, takes them.)
4740
+ const holder = path.parent?.node;
4741
+ if (n.JSXElement.check(holder) &&
4742
+ isAsChild(holder) &&
4743
+ onlyElementChild(holder) === node &&
4744
+ !screenOuter.has(node)) {
4745
+ if (elementOf(node))
4746
+ unconverted.push({
4747
+ line: node.loc?.start.line ?? 0,
4748
+ element: jsxName(node) ?? '',
4749
+ reasons: [
4750
+ {
4751
+ subject: jsxName(node) ?? 'element',
4752
+ code: 'as-child',
4753
+ why: 'the child of an `asChild` element, which hands it its own props: a root would drop them (a text inside a language view is `Embed((render) => …)`)',
4754
+ },
4755
+ ],
4756
+ });
4757
+ this.traverse(path);
4758
+ return undefined;
4759
+ }
4670
4760
  regions++;
4671
4761
  markConverted(node);
4672
4762
  const ir = result.ir;
@@ -4690,6 +4780,8 @@ function transformSource(source, options) {
4690
4780
  }
4691
4781
  // Regions inside the values it carries (an embedded element's children, an array's elements).
4692
4782
  for (const c of carriedOf(ir)) {
4783
+ if (c.hole === 'element')
4784
+ renderAsChild(c.node);
4693
4785
  if (c.hole === 'element' || c.hole === 'items') {
4694
4786
  // An element can sit at the root of what is carried; visit inside it.
4695
4787
  const holder = { type: 'ExpressionStatement', expression: c.node };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxy.so/bloom-language-codemod",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Moves an app's NativeWind className JSX and Bloom JSX to Bloom's UI language, only where the rendered result stays identical, and proves it with the language's parity harness.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "repository": {
@@ -51,10 +51,10 @@
51
51
  "recast": "0.23.11"
52
52
  },
53
53
  "peerDependencies": {
54
- "@oxy.so/bloom-language": ">=1.6.0"
54
+ "@oxy.so/bloom-language": ">=1.7.0"
55
55
  },
56
56
  "devDependencies": {
57
- "@oxy.so/bloom-language": "1.6.0",
57
+ "@oxy.so/bloom-language": "1.7.0",
58
58
  "bloom-22": "https://registry.npmjs.org/@oxy.so/bloom/-/bloom-22.1.11.tgz",
59
59
  "lightningcss": "1.30.1",
60
60
  "nativewind": "5.0.0-rc.0",