@oxy.so/bloom-language-codemod 0.3.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/lib/style.js CHANGED
@@ -68,9 +68,71 @@ function byValue(scale) {
68
68
  return out;
69
69
  }
70
70
  const CORNER = byValue(scales.corner);
71
+ const SHRINK = byValue(scales.shrink);
71
72
  const Z_INDEX = byValue(scales.zIndex);
72
73
  const FONT_SIZE = byValue(scales.fontSize);
73
74
  const LEADING_PX = byValue(scales.leadingPx);
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
+ }
106
+ /** React Native's inline and block insets, as the edge of `.position()` that sets both sides. */
107
+ const INSET_AXIS = {
108
+ insetInline: 'horizontal',
109
+ insetBlock: 'vertical',
110
+ };
111
+ const TEXT_SHADOW_KEYS = ['textShadowColor', 'textShadowOffset', 'textShadowRadius'];
112
+ /**
113
+ * A text shadow written out (`textShadowColor: 'rgba(0, 0, 0, 0.8)'`,
114
+ * `textShadowOffset: { width: 0, height: 1 }`, `textShadowRadius: 3`) as the
115
+ * text shadow token of exactly those three values, or undefined.
116
+ */
117
+ function textShadowToken(object) {
118
+ const [color, offset, radius] = TEXT_SHADOW_KEYS.map((key) => declared(object, key));
119
+ if (!n.StringLiteral.check(color) || !n.ObjectExpression.check(offset))
120
+ return undefined;
121
+ if (!n.NumericLiteral.check(radius))
122
+ return undefined;
123
+ const width = declared(offset, 'width');
124
+ const height = declared(offset, 'height');
125
+ if (!n.NumericLiteral.check(width) || !n.NumericLiteral.check(height))
126
+ return undefined;
127
+ if (offset.properties.length !== 2)
128
+ return undefined;
129
+ const shadows = scales.textShadow;
130
+ return Object.entries(shadows).find(([name, shadow]) => name !== '$comment' &&
131
+ shadow.color === color.value &&
132
+ shadow.offset[0] === width.value &&
133
+ shadow.offset[1] === height.value &&
134
+ shadow.radius === radius.value)?.[0];
135
+ }
74
136
  /**
75
137
  * A text's own `fontSize` and `lineHeight` written out, as the language's
76
138
  * size and line: `fontSize: 15` is `.font('md')` (a named rung over a numeric
@@ -199,6 +261,9 @@ const CORNER_SIDE = {
199
261
  * both round any box under 1998pt fully, so they draw alike.
200
262
  */
201
263
  function direct(key, value) {
264
+ // The platform's own monospaced face, by its generic name.
265
+ if (key === 'fontFamily' && n.StringLiteral.check(value) && value.value === 'monospace')
266
+ return { name: 'font', args: ['system-mono'] };
202
267
  const number = n.NumericLiteral.check(value)
203
268
  ? value.value
204
269
  : n.UnaryExpression.check(value) &&
@@ -223,8 +288,50 @@ function direct(key, value) {
223
288
  }
224
289
  if (key === 'aspectRatio' && number > 0)
225
290
  return { name: 'aspectRatio', args: [number] };
291
+ // A shrink rank Tailwind has no utility for (`flexShrink: 20`): the spec's rank of that value.
292
+ if (key === 'flexShrink' && number > 1) {
293
+ const rank = SHRINK.get(number);
294
+ return rank === undefined ? undefined : { name: 'shrink', args: [rank] };
295
+ }
296
+ const axis = INSET_AXIS[key];
297
+ if (axis !== undefined) {
298
+ const token = SPACE.get(Math.abs(number));
299
+ return token === undefined
300
+ ? undefined
301
+ : { name: 'position', args: [{ [axis]: number < 0 ? `-${token}` : token }] };
302
+ }
226
303
  return undefined;
227
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
+ }
228
335
  /** One declaration as its utility, or undefined. */
229
336
  function utility(key, value) {
230
337
  if (n.NumericLiteral.check(value)) {
@@ -329,11 +436,24 @@ function sidesResolved(properties) {
329
436
  const FRAME_KEYS = new Set(['width', 'height', 'minWidth', 'minHeight', 'maxWidth', 'maxHeight']);
330
437
  /** A pinned view's physical edges, as React Native's keys (spec `objects.edges`). */
331
438
  const PINNED_EDGES = new Set(['top', 'bottom', 'left', 'right']);
439
+ /** React Native's padding keys as the language's edges. */
440
+ const PADDING_EDGES = {
441
+ padding: 'all',
442
+ paddingTop: 'top',
443
+ paddingBottom: 'bottom',
444
+ paddingLeft: 'left',
445
+ paddingRight: 'right',
446
+ paddingStart: 'leading',
447
+ paddingEnd: 'trailing',
448
+ paddingHorizontal: 'horizontal',
449
+ paddingVertical: 'vertical',
450
+ };
332
451
  /**
333
452
  * Geometry computed at render as the language's points (spec `points`):
334
453
  * `width: itemSize` is `.frame({ width: itemSize })`, `top: insets.top + 8`
335
454
  * `.position({ top: insets.top + 8 })` (which joins the view's `absolute`),
336
- * `zIndex: count - index` `.zIndex(count - index)`. Only a value `computed`
455
+ * `zIndex: count - index` `.zIndex(count - index)`, and a padding the view
456
+ * computes, `paddingLeft: depth * 12` `.padding('left', depth * 12)`. Only a value `computed`
337
457
  * says the view works out as it renders; a number written out is a token.
338
458
  */
339
459
  function pointsDeclaration(key, value) {
@@ -344,8 +464,59 @@ function pointsDeclaration(key, value) {
344
464
  return { name: 'position', args: [{ [key]: points }] };
345
465
  if (key === 'zIndex')
346
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] };
470
+ const edge = PADDING_EDGES[key];
471
+ if (edge === 'all')
472
+ return { name: 'padding', args: [points] };
473
+ if (edge !== undefined)
474
+ return { name: 'padding', args: [edge, points] };
347
475
  return undefined;
348
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
+ }
349
520
  /** A style's own declaration by its key, or undefined. */
350
521
  function declared(object, key) {
351
522
  return object.properties.find((property) => (n.ObjectProperty.check(property) || n.Property.check(property)) &&
@@ -397,7 +568,7 @@ function percentDeclaration(key, value) {
397
568
  * parity's before side, where `styles.x` is not in scope). A spread `spreadOf`
398
569
  * reads is what it writes; any other is a style built at runtime.
399
570
  */
400
- function styleUtilities(object, { read, spreadOf, numberOf, computed } = {}) {
571
+ function styleUtilities(object, { read, spreadOf, numberOf, computed, compiled = false } = {}) {
401
572
  if (!n.ObjectExpression.check(object))
402
573
  return { ok: false, subject: 'style', why: 'a style that is no object literal' };
403
574
  const classes = [];
@@ -428,6 +599,25 @@ function styleUtilities(object, { read, spreadOf, numberOf, computed } = {}) {
428
599
  !n.Identifier.check(property.key))
429
600
  return { ok: false, subject: 'style', why: 'a style built at runtime' };
430
601
  const key = property.key.name;
602
+ if (TEXT_SHADOW_KEYS.includes(key)) {
603
+ const token = textShadowToken(object);
604
+ if (token === undefined)
605
+ return {
606
+ ok: false,
607
+ subject: key,
608
+ why: 'a text shadow that is no text shadow token (its colour, offset and radius written out)',
609
+ };
610
+ if (key === 'textShadowColor')
611
+ mods.push({ subject: key, mod: { name: 'textShadow', args: [token] } });
612
+ continue;
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
+ }
431
621
  const known = numberOf?.(property.value);
432
622
  const value = known === undefined
433
623
  ? property.value
@@ -441,7 +631,9 @@ function styleUtilities(object, { read, spreadOf, numberOf, computed } = {}) {
441
631
  held.push(side);
442
632
  continue;
443
633
  }
444
- 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) ??
445
637
  read?.(key, value) ??
446
638
  (computed?.(value) ? pointsDeclaration(key, value) : undefined);
447
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;