view-contracts 0.5.11 → 0.5.12
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/cli-contract.yaml +1 -1
- package/dist/preview.mjs +140 -132
- package/dist/preview.mjs.map +4 -4
- package/dist/types/renderers/compose/lowering/lowerToCompose.d.ts +2 -1
- package/dist/types/renderers/swiftui/lowering/lowerToSwiftUI.d.ts +2 -1
- package/dist/types/src/compiler/jsxToIr.d.ts +2 -1
- package/dist/types/src/compiler/viewPartial.d.ts +2 -0
- package/dist/types/src/ir/types.d.ts +8 -0
- package/dist/types/src/lib/rendererPaths.d.ts +2 -0
- package/dist/types/src/renderer/native/optionalGate.d.ts +33 -0
- package/dist/view-contracts.bundle.mjs +202 -194
- package/dist/view-contracts.bundle.mjs.map +4 -4
- package/docs/cli-reference.md +1 -1
- package/package.json +1 -1
- package/renderers/compose/README.md +1 -0
- package/renderers/compose/lowering/lowerToCompose.ts +54 -25
- package/renderers/compose/renderComponents.ts +6 -18
- package/renderers/react/renderComponents.ts +2 -8
- package/renderers/swiftui/README.md +1 -0
- package/renderers/swiftui/lowering/lowerToSwiftUI.ts +34 -13
- package/renderers/swiftui/renderComponents.ts +6 -18
- package/spec/ui-dsl.schema.json +17 -0
package/docs/cli-reference.md
CHANGED
package/package.json
CHANGED
|
@@ -7,6 +7,7 @@ Jetpack Compose renderer implemented as a SwiftUI-parity lowering path:
|
|
|
7
7
|
- Modifier synthesis in `lowering/modifiers.ts`.
|
|
8
8
|
- Component emitter in `renderComponents.ts`.
|
|
9
9
|
- Runtime emitter (screens, partials, theme, sample JSON, dispatch stub) in `renderRuntime.ts`.
|
|
10
|
+
- Partial props become function parameters; an optional prop (`x?: T`) is `x: T? = null`, and a node that renders it is emitted inside `if (x != null) { ... }`.
|
|
10
11
|
|
|
11
12
|
- Maps: `elements.yaml`, `properties.yaml` (hand-maintained under this directory)
|
|
12
13
|
- Routes: `routes.yaml`
|
|
@@ -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,
|
|
@@ -39,7 +42,7 @@ import {
|
|
|
39
42
|
visualPropSetToModifiers,
|
|
40
43
|
} from './modifiers.js';
|
|
41
44
|
|
|
42
|
-
export interface ComposeRenderContext {
|
|
45
|
+
export interface ComposeRenderContext extends OptionalGateState {
|
|
43
46
|
map: ComposeElementMap;
|
|
44
47
|
theme: ThemeIR;
|
|
45
48
|
tokens: TokenIR;
|
|
@@ -223,7 +226,7 @@ function renderGridEqualColumns(
|
|
|
223
226
|
const mapNode = node.children[0] as IrMapNode;
|
|
224
227
|
const item = mapNode.item;
|
|
225
228
|
const source = bindingExpr(mapNode.source, ctx, itemScope);
|
|
226
|
-
const body = renderNested(mapNode.body, level + 3, item);
|
|
229
|
+
const body = renderUngated(ctx, [item], () => renderNested(mapNode.body, level + 3, item));
|
|
227
230
|
return [
|
|
228
231
|
`${indent(level)}Row(modifier = ${modifierExpr}, horizontalArrangement = Arrangement.spacedBy(${gap}), verticalAlignment = Alignment.Top) {`,
|
|
229
232
|
`${indent(level + 1)}for (${item} in ${source}) {`,
|
|
@@ -238,7 +241,7 @@ function renderGridEqualColumns(
|
|
|
238
241
|
let content = '';
|
|
239
242
|
if (child.kind === 'map') content = renderMap(child, level + 2, renderNested, ctx, 'none');
|
|
240
243
|
else if (child.kind === 'text') content = `${indent(level + 2)}Text(text = ${JSON.stringify((child as IrTextNode).value)})`;
|
|
241
|
-
else if (child.kind === 'binding') content =
|
|
244
|
+
else if (child.kind === 'binding') content = renderBindingText(child, level + 2, ctx, itemScope);
|
|
242
245
|
else content = renderNested(child, level + 2, itemScope);
|
|
243
246
|
if (!content) return '';
|
|
244
247
|
return `${indent(level + 1)}Box(modifier = Modifier.weight(1f).fillMaxSize(), propagateMinConstraints = true, contentAlignment = Alignment.TopStart) {\n${content}\n${indent(level + 1)}}`;
|
|
@@ -263,7 +266,7 @@ function renderGridChildBlock(
|
|
|
263
266
|
}
|
|
264
267
|
let rendered = '';
|
|
265
268
|
if (child.kind === 'text') rendered = `${indent(level + 1)}Text(text = ${JSON.stringify((child as IrTextNode).value)}, modifier = Modifier.fillMaxWidth())`;
|
|
266
|
-
else if (child.kind === 'binding') rendered =
|
|
269
|
+
else if (child.kind === 'binding') rendered = renderBindingText(child, level + 1, ctx, itemScope, ', modifier = Modifier.fillMaxWidth()');
|
|
267
270
|
else rendered = renderNested(child, level + 1, itemScope);
|
|
268
271
|
if (!rendered) return '';
|
|
269
272
|
if (child.kind !== 'text' && child.kind !== 'binding') {
|
|
@@ -295,7 +298,7 @@ function renderGridFixedColumns(
|
|
|
295
298
|
let rendered = '';
|
|
296
299
|
if (child.kind === 'map') rendered = renderMap(child, level + 2, renderNested, ctx, 'none');
|
|
297
300
|
else if (child.kind === 'text') rendered = `${indent(level + 2)}Text(text = ${JSON.stringify((child as IrTextNode).value)})`;
|
|
298
|
-
else if (child.kind === 'binding') rendered =
|
|
301
|
+
else if (child.kind === 'binding') rendered = renderBindingText(child, level + 2, ctx, itemScope);
|
|
299
302
|
else rendered = renderNested(child, level + 2, itemScope);
|
|
300
303
|
if (!rendered) continue;
|
|
301
304
|
const bgColor = resolveBackgroundColor(child, ctx);
|
|
@@ -311,7 +314,7 @@ function renderGridFixedColumns(
|
|
|
311
314
|
let rendered = '';
|
|
312
315
|
if (child.kind === 'map') rendered = renderMap(child, level + 2, renderNested, ctx, 'none');
|
|
313
316
|
else if (child.kind === 'text') rendered = `${indent(level + 2)}Text(text = ${JSON.stringify((child as IrTextNode).value)})`;
|
|
314
|
-
else if (child.kind === 'binding') rendered =
|
|
317
|
+
else if (child.kind === 'binding') rendered = renderBindingText(child, level + 2, ctx, itemScope);
|
|
315
318
|
else rendered = renderNested(child, level + 2, itemScope);
|
|
316
319
|
ctx.inVerticalScroll = prevScroll;
|
|
317
320
|
if (!rendered) continue;
|
|
@@ -326,7 +329,7 @@ function renderGridFixedColumns(
|
|
|
326
329
|
let rendered = '';
|
|
327
330
|
if (child.kind === 'map') rendered = renderMap(child, level + 2, renderNested, ctx, 'none');
|
|
328
331
|
else if (child.kind === 'text') rendered = `${indent(level + 2)}Text(text = ${JSON.stringify((child as IrTextNode).value)})`;
|
|
329
|
-
else if (child.kind === 'binding') rendered =
|
|
332
|
+
else if (child.kind === 'binding') rendered = renderBindingText(child, level + 2, ctx, itemScope);
|
|
330
333
|
else rendered = renderNested(child, level + 2, itemScope);
|
|
331
334
|
if (!rendered) continue;
|
|
332
335
|
parts.push(rendered);
|
|
@@ -340,18 +343,40 @@ function renderGridFixedColumns(
|
|
|
340
343
|
}
|
|
341
344
|
|
|
342
345
|
function bindingExpr(binding: IrBinding, ctx: ComposeRenderContext, itemScope?: string): string {
|
|
346
|
+
recordEmittedScope(ctx, binding.scope);
|
|
343
347
|
const root = binding.scope ?? itemScope ?? ctx.propsRoot ?? 'vm';
|
|
344
348
|
return binding.path ? `${root}.${binding.path}` : root;
|
|
345
349
|
}
|
|
346
350
|
|
|
347
|
-
|
|
351
|
+
/** Makes `inner` render only when `names` are non-null; Kotlin smart-casts them inside. */
|
|
352
|
+
function unwrapIn(level: number): (names: string[], inner: string) => string {
|
|
353
|
+
return (names, inner) => {
|
|
354
|
+
const condition = names.map((name) => `${name} != null`).join(' && ');
|
|
355
|
+
return `${indent(level)}if (${condition}) {\n${indentBlock(inner, indent(1))}\n${indent(level)}}`;
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function renderBindingText(
|
|
360
|
+
binding: IrBinding,
|
|
361
|
+
level: number,
|
|
362
|
+
ctx: ComposeRenderContext,
|
|
363
|
+
itemScope?: string,
|
|
364
|
+
extraArgs = '',
|
|
365
|
+
): string {
|
|
366
|
+
return gateOptional(
|
|
367
|
+
ctx,
|
|
368
|
+
bindingScopes([binding]),
|
|
369
|
+
() => `${indent(level)}Text(text = ${bindingExpr(binding, ctx, itemScope)}${extraArgs})`,
|
|
370
|
+
unwrapIn(level),
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function resolveComposeValue(props: Record<string, IrValue>, ctx: ComposeRenderContext, itemScope?: string): string {
|
|
348
375
|
const valueRaw = props.value;
|
|
349
376
|
if (!valueRaw) return '""';
|
|
350
377
|
if (typeof valueRaw === 'string') return JSON.stringify(valueRaw);
|
|
351
378
|
if (typeof valueRaw === 'object' && valueRaw !== null && 'kind' in valueRaw && valueRaw.kind === 'binding') {
|
|
352
|
-
|
|
353
|
-
const root = binding.scope ?? (itemScope ?? 'vm');
|
|
354
|
-
return binding.path ? `${root}.${binding.path}` : root;
|
|
379
|
+
return bindingExpr(valueRaw, ctx, itemScope);
|
|
355
380
|
}
|
|
356
381
|
return '""';
|
|
357
382
|
}
|
|
@@ -603,7 +628,7 @@ function renderChildren(
|
|
|
603
628
|
const parts = children.map((child) => {
|
|
604
629
|
if (child.kind === 'map') return renderMap(child, level, renderNested, ctx, listScope);
|
|
605
630
|
if (child.kind === 'text') return `${indent(level)}Text(text = ${JSON.stringify((child as IrTextNode).value)})`;
|
|
606
|
-
if (child.kind === 'binding') return
|
|
631
|
+
if (child.kind === 'binding') return renderBindingText(child, level, ctx, itemScope);
|
|
607
632
|
return renderNested(child, level, itemScope);
|
|
608
633
|
}).filter(Boolean);
|
|
609
634
|
return parts.join('\n');
|
|
@@ -622,7 +647,7 @@ function renderLazyGridChildren(
|
|
|
622
647
|
return renderMap(child, level, renderNested, ctx, 'lazyGrid');
|
|
623
648
|
}
|
|
624
649
|
if (child.kind === 'text') content = `${indent(level + 1)}Text(text = ${JSON.stringify((child as IrTextNode).value)})`;
|
|
625
|
-
else if (child.kind === 'binding') content =
|
|
650
|
+
else if (child.kind === 'binding') content = renderBindingText(child, level + 1, ctx, itemScope);
|
|
626
651
|
else content = renderNested(child, level + 1, itemScope);
|
|
627
652
|
if (!content) return '';
|
|
628
653
|
return `${indent(level)}item {\n${content}\n${indent(level)}}`;
|
|
@@ -639,7 +664,7 @@ function renderMap(
|
|
|
639
664
|
): string {
|
|
640
665
|
const source = bindingExpr(node.source, ctx);
|
|
641
666
|
const item = node.item;
|
|
642
|
-
const body = renderNested(node.body, level + 2, item);
|
|
667
|
+
const body = renderUngated(ctx, [item], () => renderNested(node.body, level + 2, item));
|
|
643
668
|
if (listScope === 'lazyGrid') {
|
|
644
669
|
return `${indent(level)}gridItems(${source}, key = { it.id }) { ${item} ->\n${indent(level + 1)}Box(modifier = Modifier.fillMaxWidth()) {\n${body}\n${indent(level + 1)}}\n${indent(level)}}`;
|
|
645
670
|
}
|
|
@@ -650,13 +675,14 @@ function renderMap(
|
|
|
650
675
|
}
|
|
651
676
|
|
|
652
677
|
function renderViewRef(node: IrViewRefNode, level: number, itemScope: string | undefined, ctx: ComposeRenderContext): string {
|
|
653
|
-
|
|
678
|
+
// A view() partial declares its own optional props, so optional values pass through ungated.
|
|
679
|
+
const props = renderUngated(ctx, [], () => Object.entries(node.props).map(([key, value]) => {
|
|
654
680
|
if (typeof value === 'object' && value !== null && 'kind' in value && value.kind === 'binding') {
|
|
655
681
|
return `${key} = ${bindingExpr(value as IrBinding, ctx, itemScope)}`;
|
|
656
682
|
}
|
|
657
683
|
return `${key} = ${JSON.stringify(value)}`;
|
|
658
|
-
});
|
|
659
|
-
return `${indent(level)}${node.name}(${props
|
|
684
|
+
}).join(', '));
|
|
685
|
+
return `${indent(level)}${node.name}(${props})`;
|
|
660
686
|
}
|
|
661
687
|
|
|
662
688
|
function renderVcButton(
|
|
@@ -728,7 +754,7 @@ function renderLeaf(
|
|
|
728
754
|
const textStyleLine = textStyleParts.length > 0
|
|
729
755
|
? `${pad} textStyle = TextStyle(${textStyleParts.join(', ')}),`
|
|
730
756
|
: `${pad} textStyle = TextStyle(color = ${textColor}, fontSize = 14.sp),`;
|
|
731
|
-
const composeVal = resolveComposeValue(node.props, itemScope);
|
|
757
|
+
const composeVal = resolveComposeValue(node.props, ctx, itemScope);
|
|
732
758
|
return [
|
|
733
759
|
`${pad}BasicTextField(`,
|
|
734
760
|
`${pad} value = ${composeVal},`,
|
|
@@ -759,7 +785,7 @@ function renderLeaf(
|
|
|
759
785
|
const containerExpr = applyModifiers('Modifier', dedupeModifiers(containerMods));
|
|
760
786
|
const textStyleParts = extractTextStyleModifiers(textMods);
|
|
761
787
|
const textStyleSuffix = textStyleParts.length > 0 ? `, textStyle = TextStyle(${textStyleParts.join(', ')})` : '';
|
|
762
|
-
const composeVal = resolveComposeValue(node.props, itemScope);
|
|
788
|
+
const composeVal = resolveComposeValue(node.props, ctx, itemScope);
|
|
763
789
|
return [
|
|
764
790
|
`${pad}Box(modifier = ${containerExpr}.heightIn(min = ${minHeight}.dp)) {`,
|
|
765
791
|
`${pad} BasicTextField(`,
|
|
@@ -970,7 +996,7 @@ function renderContainer(
|
|
|
970
996
|
const mapNode = child as IrMapNode;
|
|
971
997
|
const source = bindingExpr(mapNode.source, ctx);
|
|
972
998
|
const item = mapNode.item;
|
|
973
|
-
const mapBody = renderNested(mapNode.body, level + 3, item);
|
|
999
|
+
const mapBody = renderUngated(ctx, [item], () => renderNested(mapNode.body, level + 3, item));
|
|
974
1000
|
return [
|
|
975
1001
|
`${indent(level + 1)}for (${item} in ${source}) {`,
|
|
976
1002
|
`${indent(level + 2)}${divLine.trim()}`,
|
|
@@ -1030,14 +1056,14 @@ function renderContainer(
|
|
|
1030
1056
|
);
|
|
1031
1057
|
}
|
|
1032
1058
|
if (child.kind === 'binding') {
|
|
1033
|
-
return formatTextComposable(
|
|
1059
|
+
return gateOptional(ctx, bindingScopes([child]), () => formatTextComposable(
|
|
1034
1060
|
indent(level + 1),
|
|
1035
|
-
bindingExpr(child
|
|
1061
|
+
bindingExpr(child, ctx, itemScope),
|
|
1036
1062
|
dedupeModifiers([...cellTextBehaviorMods(), `@textAlign(${textAlign})`, ...paddingMods, ...inheritedMods.map((m) => m)]),
|
|
1037
1063
|
ctx,
|
|
1038
1064
|
undefined,
|
|
1039
1065
|
itemScope,
|
|
1040
|
-
);
|
|
1066
|
+
), unwrapIn(level + 1));
|
|
1041
1067
|
}
|
|
1042
1068
|
return renderNested(child, level + 1, itemScope);
|
|
1043
1069
|
}).filter(Boolean).join('\n');
|
|
@@ -1101,10 +1127,13 @@ export function createComposeNodeRenderer(ctx: ComposeRenderContext): NodeRender
|
|
|
1101
1127
|
const renderNested: NodeRenderer = (node, level, itemScope) => {
|
|
1102
1128
|
if (node.kind === 'viewRef') return renderViewRef(node, level, itemScope, ctx);
|
|
1103
1129
|
if (node.kind !== 'component') return '';
|
|
1104
|
-
|
|
1130
|
+
const entry = ctx.map.elements[node.name];
|
|
1131
|
+
if (!entry) {
|
|
1105
1132
|
throw new Error(`Element "${node.name}" is not in Compose element map`);
|
|
1106
1133
|
}
|
|
1107
|
-
|
|
1134
|
+
const inlinedChildren = entry.leaf ? node.children.filter((child): child is IrBinding => child.kind === 'binding') : [];
|
|
1135
|
+
const scopes = bindingScopes([...Object.values(node.props), ...inlinedChildren]);
|
|
1136
|
+
return gateOptional(ctx, scopes, () => renderComponent(node, entry, level, itemScope, renderNested, ctx), unwrapIn(level));
|
|
1108
1137
|
};
|
|
1109
1138
|
return renderNested;
|
|
1110
1139
|
}
|
|
@@ -21,26 +21,13 @@ function tsTypeToKotlin(type: string): string {
|
|
|
21
21
|
|
|
22
22
|
function partialPropsSignature(doc: ViewIrDocument): string {
|
|
23
23
|
if (!doc.propsType) return '';
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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 = tsTypeToKotlin(rawType);
|
|
32
|
-
return `${name}: ${type}`;
|
|
24
|
+
if (!doc.propDecls) return `props: ${doc.propsType}`;
|
|
25
|
+
return doc.propDecls.map((decl) => {
|
|
26
|
+
const type = tsTypeToKotlin(decl.type);
|
|
27
|
+
return decl.optional ? `${decl.name}: ${type}? = null` : `${decl.name}: ${type}`;
|
|
33
28
|
}).join(', ');
|
|
34
29
|
}
|
|
35
30
|
|
|
36
|
-
function propsRootFromDoc(doc: ViewIrDocument): string | undefined {
|
|
37
|
-
if (!doc.propsType) return undefined;
|
|
38
|
-
const match = doc.propsType.match(/^\{\s*([^}]+)\s*\}$/);
|
|
39
|
-
if (!match) return undefined;
|
|
40
|
-
const first = match[1].split(',')[0]!.split(':')[0]!.trim();
|
|
41
|
-
return first;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
31
|
export async function renderComposeComponent(
|
|
45
32
|
doc: ViewIrDocument,
|
|
46
33
|
options: RenderComposeOptions,
|
|
@@ -54,7 +41,8 @@ export async function renderComposeComponent(
|
|
|
54
41
|
theme: options.theme,
|
|
55
42
|
tokens: options.tokens,
|
|
56
43
|
allowlistExtra,
|
|
57
|
-
propsRoot:
|
|
44
|
+
propsRoot: doc.propDecls?.[0]?.name,
|
|
45
|
+
optionalProps: new Set(doc.propDecls?.filter((decl) => decl.optional).map((decl) => decl.name)),
|
|
58
46
|
};
|
|
59
47
|
|
|
60
48
|
const body = lowerIrToComposeBody(doc.root, ctx, 2);
|
|
@@ -51,14 +51,8 @@ function extractContractTypeNames(propsType: string | undefined): string[] {
|
|
|
51
51
|
|
|
52
52
|
function partialFunctionParams(doc: ViewIrDocument): string {
|
|
53
53
|
if (!doc.propsType) return '';
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const inner = match[1].trim();
|
|
57
|
-
const propNames = inner
|
|
58
|
-
.split(/[,;]/)
|
|
59
|
-
.map((part) => part.split(':')[0]!.trim())
|
|
60
|
-
.filter(Boolean);
|
|
61
|
-
return `{ ${propNames.join(', ')} }: ${doc.propsType}`;
|
|
54
|
+
if (!doc.propDecls) return `props: ${doc.propsType}`;
|
|
55
|
+
return `{ ${doc.propDecls.map((decl) => decl.name).join(', ')} }: ${doc.propsType}`;
|
|
62
56
|
}
|
|
63
57
|
|
|
64
58
|
/** Root-level `{cond ? ...}` from visibilityGuard is invalid inside `return (...)` — wrap in fragment. */
|
|
@@ -11,6 +11,7 @@ Compiles View IR into native SwiftUI code at build time.
|
|
|
11
11
|
- **Dynamic variants** — `IrConcat` expressions (e.g. `` `badge-${row.statusTone}` ``) are lowered to runtime `VcTheme.resolve()` calls with a pre-built theme dictionary
|
|
12
12
|
- **Table layout** — `Table` / `TableHeader` / `TableRow` / `TableColumn` / `TableCell` with fixed widths, flex columns, and configurable `separator` / `separatorColor`
|
|
13
13
|
- **justify="between"** — `Row` elements with `justify="between"` emit `Spacer()` between children
|
|
14
|
+
- **Partial props** — each prop of a `view<{ ... }>()` partial becomes a stored property; an optional prop (`x?: T`) is `var x: T? = nil`. A node that renders an optional prop is emitted inside `if let x { ... }`, so it appears only when the prop is present
|
|
14
15
|
- **Mock data SSoT** — `mock.yaml` is converted to `SampleData.json` and bundled into the Xcode project; Swift ViewModels decode via `JSONDecoder` (no hand-written sample data)
|
|
15
16
|
|
|
16
17
|
## Files
|
|
@@ -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 =
|
|
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
|
|
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
|
|
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
|
|
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 =
|
|
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
|
-
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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:
|
|
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);
|
package/spec/ui-dsl.schema.json
CHANGED
|
@@ -358,6 +358,11 @@
|
|
|
358
358
|
"x-category": "identity",
|
|
359
359
|
"description": "Form field name (for FormData) or icon token in the renderer icon set"
|
|
360
360
|
},
|
|
361
|
+
"action": {
|
|
362
|
+
"type": "action",
|
|
363
|
+
"x-category": "interaction",
|
|
364
|
+
"description": "Action descriptor { name, params? }; the element's click/submit action (takes precedence over onClick/onSubmit)"
|
|
365
|
+
},
|
|
361
366
|
"onClick": {
|
|
362
367
|
"type": "action",
|
|
363
368
|
"x-category": "interaction",
|
|
@@ -1022,6 +1027,9 @@
|
|
|
1022
1027
|
"visible": {
|
|
1023
1028
|
"$ref": "#/properties/visible"
|
|
1024
1029
|
},
|
|
1030
|
+
"action": {
|
|
1031
|
+
"$ref": "#/properties/action"
|
|
1032
|
+
},
|
|
1025
1033
|
"onClick": {
|
|
1026
1034
|
"$ref": "#/properties/onClick"
|
|
1027
1035
|
},
|
|
@@ -1127,6 +1135,9 @@
|
|
|
1127
1135
|
"enabled": {
|
|
1128
1136
|
"$ref": "#/properties/enabled"
|
|
1129
1137
|
},
|
|
1138
|
+
"action": {
|
|
1139
|
+
"$ref": "#/properties/action"
|
|
1140
|
+
},
|
|
1130
1141
|
"onClick": {
|
|
1131
1142
|
"$ref": "#/properties/onClick"
|
|
1132
1143
|
},
|
|
@@ -1190,6 +1201,9 @@
|
|
|
1190
1201
|
"enabled": {
|
|
1191
1202
|
"$ref": "#/properties/enabled"
|
|
1192
1203
|
},
|
|
1204
|
+
"action": {
|
|
1205
|
+
"$ref": "#/properties/action"
|
|
1206
|
+
},
|
|
1193
1207
|
"onClick": {
|
|
1194
1208
|
"$ref": "#/properties/onClick"
|
|
1195
1209
|
},
|
|
@@ -1591,6 +1605,9 @@
|
|
|
1591
1605
|
"gap": {
|
|
1592
1606
|
"$ref": "#/properties/gap"
|
|
1593
1607
|
},
|
|
1608
|
+
"action": {
|
|
1609
|
+
"$ref": "#/properties/action"
|
|
1610
|
+
},
|
|
1594
1611
|
"onSubmit": {
|
|
1595
1612
|
"$ref": "#/properties/onSubmit"
|
|
1596
1613
|
},
|