view-contracts 0.5.11 → 0.5.13

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.
Files changed (36) hide show
  1. package/cli-contract.yaml +1 -1
  2. package/dist/preview.mjs +148 -140
  3. package/dist/preview.mjs.map +4 -4
  4. package/dist/types/renderers/compose/lowering/lowerToCompose.d.ts +2 -1
  5. package/dist/types/renderers/react/dataAttributes.d.ts +2 -0
  6. package/dist/types/renderers/swiftui/lowering/lowerToSwiftUI.d.ts +2 -1
  7. package/dist/types/src/compiler/jsxToIr.d.ts +2 -1
  8. package/dist/types/src/compiler/viewPartial.d.ts +3 -1
  9. package/dist/types/src/config.d.ts +1 -3
  10. package/dist/types/src/dsl/types.d.ts +2 -3
  11. package/dist/types/src/ir/types.d.ts +8 -0
  12. package/dist/types/src/lib/projectPath.d.ts +2 -0
  13. package/dist/types/src/lib/rendererPaths.d.ts +2 -0
  14. package/dist/types/src/renderer/native/optionalGate.d.ts +33 -0
  15. package/dist/types/src/scaffold/standalone.d.ts +2 -2
  16. package/dist/types/src/schema/dsl-catalog.d.ts +1 -0
  17. package/dist/types/src/schema/loadSchema.d.ts +7 -0
  18. package/dist/types/src/schema/types.d.ts +31 -0
  19. package/dist/types/src/validate/validateViewProps.d.ts +0 -3
  20. package/dist/view-contracts.bundle.mjs +214 -200
  21. package/dist/view-contracts.bundle.mjs.map +4 -4
  22. package/docs/cli-reference.md +1 -1
  23. package/package.json +1 -1
  24. package/renderers/compose/README.md +1 -0
  25. package/renderers/compose/lowering/lowerToCompose.ts +54 -25
  26. package/renderers/compose/renderComponents.ts +6 -18
  27. package/renderers/react/dataAttributes.ts +4 -0
  28. package/renderers/react/lowering/lowerToJsx.ts +45 -11
  29. package/renderers/react/preview/vocabulary.tsx +7 -3
  30. package/renderers/react/renderComponents.ts +2 -8
  31. package/renderers/swiftui/README.md +1 -0
  32. package/renderers/swiftui/lowering/lowerToSwiftUI.ts +34 -13
  33. package/renderers/swiftui/renderComponents.ts +6 -18
  34. package/spec/ui-dsl.schema.json +263 -0
  35. package/src/dsl/types.ts +2 -3
  36. package/renderers/react/templates/handlers.ts.hbs +0 -4
@@ -1,6 +1,9 @@
1
1
  import { LOWERING_STYLE_PROPERTIES } from '../../../src/schema/dsl-catalog.js';
2
2
  import type { ThemeIR, TokenIR } from '../../../src/design/types.js';
3
3
  import { mergeVisualProps, type VisualPropSet } from '../../../src/renderer/style/mergeVisualProps.js';
4
+ import {
5
+ bindingScopes, gateOptional, indentBlock, recordEmittedScope, renderUngated, type OptionalGateState,
6
+ } from '../../../src/renderer/native/optionalGate.js';
4
7
  import type {
5
8
  IrBinding,
6
9
  IrComponentNode,
@@ -15,7 +18,7 @@ import type {
15
18
  import type { SwiftUIElementConfig, SwiftUIElementMap } from '../elementMap.js';
16
19
  import { applyModifiers, visualPropSetToModifiers } from './modifiers.js';
17
20
 
18
- export interface SwiftUIRenderContext {
21
+ export interface SwiftUIRenderContext extends OptionalGateState {
19
22
  map: SwiftUIElementMap;
20
23
  theme: ThemeIR;
21
24
  tokens: TokenIR;
@@ -52,14 +55,14 @@ function renderGridChildBlock(
52
55
  if (child.kind === 'map') {
53
56
  const mapNode = child as IrMapNode;
54
57
  const item = mapNode.item;
55
- let body = renderNested(mapNode.body, level + 2, item);
58
+ let body = renderUngated(ctx, [item], () => renderNested(mapNode.body, level + 2, item));
56
59
  body += `\n${childFrameMod}`;
57
60
  const source = bindingExpr(mapNode.source, ctx, itemScope);
58
61
  return `${indent(level + 2)}ForEach(${source}, id: \\.id) { ${item} in\n${body}\n${indent(level + 2)}}`;
59
62
  }
60
63
  let rendered = '';
61
64
  if (child.kind === 'text') rendered = `${indent(level + 2)}Text(${JSON.stringify((child as IrTextNode).value)})`;
62
- else if (child.kind === 'binding') rendered = `${indent(level + 2)}Text(${bindingExpr(child as IrBinding, ctx, itemScope)})`;
65
+ else if (child.kind === 'binding') rendered = renderBindingText(child, level + 2, ctx, itemScope);
63
66
  else rendered = renderNested(child, level + 2, itemScope);
64
67
  if (!rendered) return '';
65
68
  return `${rendered}\n${childFrameMod}`;
@@ -241,10 +244,24 @@ function emitSelectLabelText(
241
244
  }
242
245
 
243
246
  function bindingExpr(binding: IrBinding, ctx: SwiftUIRenderContext, itemScope?: string): string {
247
+ recordEmittedScope(ctx, binding.scope);
244
248
  const root = binding.scope ?? itemScope ?? ctx.propsRoot ?? 'vm';
245
249
  return binding.path ? `${root}.${binding.path}` : root;
246
250
  }
247
251
 
252
+ /** Makes `inner` render only when `names` are present, with them unwrapped (`if let`). */
253
+ function unwrapIn(level: number): (names: string[], inner: string) => string {
254
+ return (names, inner) => {
255
+ const condition = names.map((name) => `let ${name}`).join(', ');
256
+ // Group keeps the gated block a single view so callers can append modifiers.
257
+ return `${indent(level)}Group {\n${indent(level + 1)}if ${condition} {\n${indentBlock(inner, indent(2))}\n${indent(level + 1)}}\n${indent(level)}}`;
258
+ };
259
+ }
260
+
261
+ function renderBindingText(binding: IrBinding, level: number, ctx: SwiftUIRenderContext, itemScope?: string): string {
262
+ return gateOptional(ctx, bindingScopes([binding]), () => `${indent(level)}Text(${bindingExpr(binding, ctx, itemScope)})`, unwrapIn(level));
263
+ }
264
+
248
265
  function resolveValueBinding(props: Record<string, IrValue>, ctx: SwiftUIRenderContext, itemScope?: string): { valueExpr: string; hasOnChange: boolean } {
249
266
  const vmRef = itemScope ?? ctx.propsRoot ?? 'vm';
250
267
  const valueRaw = props.value;
@@ -284,7 +301,7 @@ function renderChildren(
284
301
  const parts = children.map((child) => {
285
302
  if (child.kind === 'map') return renderMap(child, level, renderNested, ctx);
286
303
  if (child.kind === 'text') return `${indent(level)}Text(${JSON.stringify((child as IrTextNode).value)})`;
287
- if (child.kind === 'binding') return `${indent(level)}Text(${bindingExpr(child as IrBinding, ctx, itemScope)})`;
304
+ if (child.kind === 'binding') return renderBindingText(child, level, ctx, itemScope);
288
305
  return renderNested(child, level, itemScope);
289
306
  }).filter(Boolean);
290
307
  return parts.join('\n');
@@ -292,7 +309,7 @@ function renderChildren(
292
309
 
293
310
  function renderMap(node: IrMapNode, level: number, renderNested: NodeRenderer, ctx: SwiftUIRenderContext): string {
294
311
  const item = node.item;
295
- const body = renderNested(node.body, level + 2, item);
312
+ const body = renderUngated(ctx, [item], () => renderNested(node.body, level + 2, item));
296
313
  const source = bindingExpr(node.source, ctx, item);
297
314
  return `${indent(level)}ForEach(${source}, id: \\.id) { ${item} in\n${body}\n${indent(level)}}`;
298
315
  }
@@ -486,7 +503,7 @@ function renderContainer(
486
503
  const childLines = node.children.map((child) => {
487
504
  if (child.kind === 'map') return renderMap(child, level + 2, renderNested, ctx);
488
505
  if (child.kind === 'text') return `${indent(level + 2)}Text(${JSON.stringify((child as IrTextNode).value)})`;
489
- if (child.kind === 'binding') return `${indent(level + 2)}Text(${bindingExpr(child as IrBinding, ctx, itemScope)})`;
506
+ if (child.kind === 'binding') return renderBindingText(child, level + 2, ctx, itemScope);
490
507
  return renderNested(child, level + 2, itemScope);
491
508
  }).filter(Boolean);
492
509
  if (childLines.length >= 2) {
@@ -498,7 +515,7 @@ function renderContainer(
498
515
  block = node.children.map((child) => {
499
516
  if (child.kind === 'map') return renderMap(child, level + 2, renderNested, ctx);
500
517
  if (child.kind === 'text') return `${indent(level + 2)}Text(${JSON.stringify((child as IrTextNode).value)})`;
501
- if (child.kind === 'binding') return `${indent(level + 2)}Text(${bindingExpr(child as IrBinding, ctx, itemScope)})`;
518
+ if (child.kind === 'binding') return renderBindingText(child, level + 2, ctx, itemScope);
502
519
  const rendered = renderNested(child, level + 2, itemScope);
503
520
  if (!rendered) return '';
504
521
  return rendered;
@@ -730,7 +747,7 @@ function renderGridFixedColumns(
730
747
  let rendered = '';
731
748
  if (child.kind === 'map') rendered = renderMap(child, level + 2, renderNested, ctx);
732
749
  else if (child.kind === 'text') rendered = `${indent(level + 2)}Text(${JSON.stringify((child as IrTextNode).value)})`;
733
- else if (child.kind === 'binding') rendered = `${indent(level + 2)}Text(${bindingExpr(child as IrBinding, ctx, itemScope)})`;
750
+ else if (child.kind === 'binding') rendered = renderBindingText(child, level + 2, ctx, itemScope);
734
751
  else rendered = renderNested(child, level + 2, itemScope);
735
752
  if (!rendered) continue;
736
753
  const w = widths[i];
@@ -754,13 +771,14 @@ function renderGridFixedColumns(
754
771
  }
755
772
 
756
773
  function renderViewRef(node: IrViewRefNode, level: number, itemScope: string | undefined, ctx: SwiftUIRenderContext): string {
757
- const props = Object.entries(node.props).map(([key, value]) => {
774
+ // A view() partial declares its own optional props, so optional values pass through ungated.
775
+ const props = renderUngated(ctx, [], () => Object.entries(node.props).map(([key, value]) => {
758
776
  if (typeof value === 'object' && value !== null && 'kind' in value && value.kind === 'binding') {
759
777
  return `${key}: ${bindingExpr(value as IrBinding, ctx, itemScope)}`;
760
778
  }
761
779
  return `${key}: ${JSON.stringify(value)}`;
762
- });
763
- const argStr = props.length > 0 ? `(${props.join(', ')})` : '()';
780
+ }).join(', '));
781
+ const argStr = `(${props})`;
764
782
  return `${indent(level)}${node.name}${argStr}`;
765
783
  }
766
784
 
@@ -772,10 +790,13 @@ export function createSwiftUINodeRenderer(ctx: SwiftUIRenderContext): NodeRender
772
790
  if (node.kind !== 'component') {
773
791
  return '';
774
792
  }
775
- if (!ctx.map.elements[node.name]) {
793
+ const entry = ctx.map.elements[node.name];
794
+ if (!entry) {
776
795
  throw new Error(`Element "${node.name}" is not in SwiftUI element map (prototype subset)`);
777
796
  }
778
- return renderComponent(node, ctx.map.elements[node.name]!, level, itemScope, renderNested, ctx);
797
+ const inlinedChildren = entry.leaf ? node.children.filter((child): child is IrBinding => child.kind === 'binding') : [];
798
+ const scopes = bindingScopes([...Object.values(node.props), ...inlinedChildren]);
799
+ return gateOptional(ctx, scopes, () => renderComponent(node, entry, level, itemScope, renderNested, ctx), unwrapIn(level));
779
800
  };
780
801
  return renderNested;
781
802
  }
@@ -21,27 +21,14 @@ function tsTypeToSwift(type: string): string {
21
21
 
22
22
  function partialPropsLines(doc: ViewIrDocument): string {
23
23
  if (!doc.propsType) return '';
24
- const match = doc.propsType.match(/^\{\s*([^}]+)\s*\}$/);
25
- if (!match) return ` let props: ${doc.propsType}\n`;
26
- const inner = match[1].trim();
27
- const lines = inner.split(',').map((part) => {
28
- const colon = part.indexOf(':');
29
- const name = colon >= 0 ? part.slice(0, colon).trim() : part.trim();
30
- const rawType = colon >= 0 ? part.slice(colon + 1).trim() : 'Any';
31
- const type = tsTypeToSwift(rawType);
32
- return ` let ${name}: ${type}`;
24
+ if (!doc.propDecls) return ` let props: ${doc.propsType}\n`;
25
+ const lines = doc.propDecls.map((decl) => {
26
+ const type = tsTypeToSwift(decl.type);
27
+ return decl.optional ? ` var ${decl.name}: ${type}? = nil` : ` let ${decl.name}: ${type}`;
33
28
  });
34
29
  return `${lines.join('\n')}\n`;
35
30
  }
36
31
 
37
- function propsRootFromDoc(doc: ViewIrDocument): string | undefined {
38
- if (!doc.propsType) return undefined;
39
- const match = doc.propsType.match(/^\{\s*([^}]+)\s*\}$/);
40
- if (!match) return undefined;
41
- const first = match[1].split(',')[0]!.split(':')[0]!.trim();
42
- return first;
43
- }
44
-
45
32
  export async function renderSwiftComponent(
46
33
  doc: ViewIrDocument,
47
34
  options: RenderSwiftOptions,
@@ -55,7 +42,8 @@ export async function renderSwiftComponent(
55
42
  theme: options.theme,
56
43
  tokens: options.tokens,
57
44
  allowlistExtra,
58
- propsRoot: propsRootFromDoc(doc),
45
+ propsRoot: doc.propDecls?.[0]?.name,
46
+ optionalProps: new Set(doc.propDecls?.filter((decl) => decl.optional).map((decl) => decl.name)),
59
47
  };
60
48
 
61
49
  const body = lowerIrToSwiftBody(doc.root, ctx, 4);