@mun-ui/compiler 0.2.0 → 0.2.1

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/dist/ui-ir.js CHANGED
@@ -7,7 +7,7 @@ import { parseMunBuilder, parseMunStructs, } from "./ast.js";
7
7
  import { assertCanonicalMunSource } from "./analysis.js";
8
8
  import { describeValueType, displayMunType, parseMunType, runtimeTypeName, validateCanonicalDeclarations, valueMatchesType } from "./value-types.js";
9
9
  import { resolveContractCall, swiftTypeName } from "./native-contract.js";
10
- import { lowerAnimationValue, lowerColor, lowerPaint, lowerTransitionValue, nativeValueImplementations, parseMemberChain } from "./native-values.js";
10
+ import { lowerAnimationValue, lowerColor, lowerPaint, lowerTransitionValue, nativeValueImplementations, parseMemberChain, lowerFontValue, lowerFontWeight } from "./native-values.js";
11
11
  import { semanticViewLookupCandidates, semanticViewsForStructs, } from "./semantic.js";
12
12
  import { splitStatements, splitTopLevel } from "./scanner.js";
13
13
  const emptyBindings = new Map();
@@ -679,6 +679,47 @@ function collectionAction(state, method, argumentSource, bindings) {
679
679
  throw new SyntaxError(`Unsupported native collection operation '${method}'`);
680
680
  }
681
681
  }
682
+ /** Operating-system service calls: owner → service, operation → arity, whether it yields a value. */
683
+ const hostOperations = {
684
+ FileManager: {
685
+ service: "files",
686
+ operations: {
687
+ homeDirectory: { arity: 0, result: true },
688
+ list: { arity: 1, result: true },
689
+ exists: { arity: 1, result: true },
690
+ readText: { arity: 1, result: true },
691
+ writeText: { arity: 2, result: false },
692
+ createDirectory: { arity: 1, result: false },
693
+ remove: { arity: 1, result: false },
694
+ parent: { arity: 1, result: true },
695
+ join: { arity: 2, result: true },
696
+ },
697
+ },
698
+ Workspace: { service: "workspace", operations: { open: { arity: 1, result: false }, reveal: { arity: 1, result: false } } },
699
+ };
700
+ function hostAction(owner, operation, argumentSource, state, bindings) {
701
+ const contract = hostOperations[owner]?.operations[operation];
702
+ if (!contract) {
703
+ const known = Object.keys(hostOperations[owner]?.operations ?? {}).join(", ");
704
+ throw new SyntaxError(`Unknown host operation ${owner}.${operation}; available: ${known}`);
705
+ }
706
+ const args = collectionArguments(argumentSource);
707
+ if (args.some(argument => argument.label !== undefined)) {
708
+ throw new SyntaxError(`${owner}.${operation} takes positional arguments only: ${owner}.${operation}(${argumentSource})`);
709
+ }
710
+ if (args.length !== contract.arity) {
711
+ throw new SyntaxError(`${owner}.${operation} takes ${contract.arity} argument(s), got ${args.length}: ${owner}.${operation}(${argumentSource})`);
712
+ }
713
+ if (state && !contract.result)
714
+ throw new SyntaxError(`${owner}.${operation} returns no value to assign`);
715
+ return {
716
+ kind: "host",
717
+ service: hostOperations[owner].service,
718
+ operation,
719
+ arguments: args.map(argument => lowerValueExpression(argument.source, bindings)),
720
+ ...(state ? { state } : {}),
721
+ };
722
+ }
682
723
  function actionFromClosure(source, bindings = emptyBindings) {
683
724
  const body = source.trim().replace(/;$/, "").trim();
684
725
  const statements = splitStatements(body);
@@ -743,6 +784,14 @@ function actionFromClosure(source, bindings = emptyBindings) {
743
784
  value: lowerValueExpression(assignment[2], bindings),
744
785
  };
745
786
  }
787
+ const hostCall = /^(?:([A-Za-z_$][\w$]*)(?:\.value)?\s*=\s*)?(FileManager|Workspace)\.([A-Za-z]+)\s*\(([\s\S]*)\)$/.exec(body);
788
+ if (hostCall) {
789
+ const [, target, owner, operation, argumentSource] = hostCall;
790
+ if (target && bindings.get(target)?.kind !== "state" && !/\.value\s*=/.test(body)) {
791
+ throw new SyntaxError(`${owner}.${operation} results can only be assigned to @State: ${body}`);
792
+ }
793
+ return hostAction(owner, operation, argumentSource, target ? stateTarget(target, bindings) : undefined, bindings);
794
+ }
746
795
  const collectionCall = /^([A-Za-z_$][\w$]*)\.(insert|append|remove|move|update)\s*\(([\s\S]*)\)$/.exec(body);
747
796
  if (collectionCall) {
748
797
  return collectionAction(stateTarget(collectionCall[1], bindings), collectionCall[2], collectionCall[3], bindings);
@@ -1328,6 +1377,18 @@ function withChildren(node, map) {
1328
1377
  }
1329
1378
  }
1330
1379
  /** Lexical environment values written by modifiers and inherited by descendants. */
1380
+ /** Typography set by an outer modifier reaches every Text below that has not set the same field itself. */
1381
+ function inheritTypography(node, font) {
1382
+ let current = node;
1383
+ if (current.kind === "text") {
1384
+ const merged = { ...font };
1385
+ for (const [key, value] of Object.entries(current.font ?? {}))
1386
+ if (value !== undefined)
1387
+ merged[key] = value;
1388
+ current = { ...current, font: merged };
1389
+ }
1390
+ return withChildren(current, child => inheritTypography(child, font));
1391
+ }
1331
1392
  function inheritEnvironment(node, environment) {
1332
1393
  let current = node;
1333
1394
  if (environment.disabled && controlKinds.has(current.kind)) {
@@ -1549,25 +1610,33 @@ const nativeModifiers = {
1549
1610
  applyFrame(context, update, alignment(argumentSource(context, "alignment"), "frame alignment"));
1550
1611
  },
1551
1612
  "frame(minWidth:idealWidth:maxWidth:minHeight:idealHeight:maxHeight:alignment:)": context => {
1552
- for (const ideal of ["idealWidth", "idealHeight"]) {
1553
- if (context.args.has(ideal))
1554
- throw new SyntaxError(`.frame(${ideal}:) is not implemented in native Mün: layout has no ideal-size proposal. Use width/height or min/max bounds.`);
1555
- }
1556
1613
  const update = {};
1557
1614
  const minWidth = frameBound(argumentSource(context, "minWidth"), "minWidth", context.bindings, false);
1615
+ const idealWidth = frameBound(argumentSource(context, "idealWidth"), "idealWidth", context.bindings, false);
1558
1616
  const maxWidth = frameBound(argumentSource(context, "maxWidth"), "maxWidth", context.bindings, true);
1559
1617
  const minHeight = frameBound(argumentSource(context, "minHeight"), "minHeight", context.bindings, false);
1618
+ const idealHeight = frameBound(argumentSource(context, "idealHeight"), "idealHeight", context.bindings, false);
1560
1619
  const maxHeight = frameBound(argumentSource(context, "maxHeight"), "maxHeight", context.bindings, true);
1561
1620
  if (typeof minWidth === "number")
1562
1621
  update.minWidth = minWidth;
1622
+ if (typeof idealWidth === "number")
1623
+ update.idealWidth = idealWidth;
1563
1624
  if (maxWidth !== undefined)
1564
1625
  update.maxWidth = maxWidth;
1565
1626
  if (typeof minHeight === "number")
1566
1627
  update.minHeight = minHeight;
1628
+ if (typeof idealHeight === "number")
1629
+ update.idealHeight = idealHeight;
1567
1630
  if (maxHeight !== undefined)
1568
1631
  update.maxHeight = maxHeight;
1569
1632
  applyFrame(context, update, alignment(argumentSource(context, "alignment"), "frame alignment"));
1570
1633
  },
1634
+ "layoutPriority(_:)": context => {
1635
+ const priority = numberValue(argumentSource(context, "value"), undefined, context.bindings);
1636
+ if (priority === undefined || !Number.isFinite(priority))
1637
+ throw new SyntaxError(".layoutPriority(_:) needs a finite number");
1638
+ context.composer.layout({ layoutPriority: priority }, context.index);
1639
+ },
1571
1640
  "padding(_:)": context => padding(context, undefined, numberValue(argumentSource(context, "length"), undefined, context.bindings) ?? defaultPadding),
1572
1641
  "padding(_:_:)": context => padding(context, argumentSource(context, "edges"), numberValue(argumentSource(context, "length"), defaultPadding, context.bindings) ?? defaultPadding),
1573
1642
  "background(_:ignoresSafeAreaEdges:)": context => {
@@ -1585,6 +1654,45 @@ const nativeModifiers = {
1585
1654
  },
1586
1655
  "background(alignment:content:)": context => layered(context, "background"),
1587
1656
  "overlay(alignment:content:)": context => layered(context, "overlay"),
1657
+ "font(_:)": context => {
1658
+ const source = argumentSource(context, "font");
1659
+ if (!source || source === "nil")
1660
+ throw new SyntaxError(".font(_:) needs a Font value such as .title or .system(size: 13)");
1661
+ context.composer.transform(node => inheritTypography(node, lowerFontValue(source)));
1662
+ },
1663
+ "fontWeight(_:)": context => {
1664
+ const source = argumentSource(context, "weight");
1665
+ if (source === undefined || source === "nil")
1666
+ throw new SyntaxError(".fontWeight(_:) needs a Font.Weight such as .semibold");
1667
+ context.composer.transform(node => inheritTypography(node, { weight: lowerFontWeight(source) }));
1668
+ },
1669
+ "bold(_:)": context => {
1670
+ const active = argumentSource(context, "isActive");
1671
+ if (active !== undefined && active !== "true" && active !== "false")
1672
+ throw new SyntaxError(".bold(_:) takes a static Bool");
1673
+ context.composer.transform(node => inheritTypography(node, { weight: active === "false" ? 400 : 700 }));
1674
+ },
1675
+ "italic(_:)": context => {
1676
+ const active = argumentSource(context, "isActive");
1677
+ if (active !== undefined && active !== "true" && active !== "false")
1678
+ throw new SyntaxError(".italic(_:) takes a static Bool");
1679
+ context.composer.transform(node => inheritTypography(node, { italic: active !== "false" }));
1680
+ },
1681
+ "lineLimit(_:)": context => {
1682
+ const source = argumentSource(context, "number");
1683
+ if (source === undefined || source === "nil")
1684
+ throw new SyntaxError(".lineLimit(_:) needs a static positive Int");
1685
+ const limit = numberValue(source, undefined, context.bindings);
1686
+ if (limit === undefined || !Number.isInteger(limit) || limit < 1)
1687
+ throw new SyntaxError(`.lineLimit(_:) needs a static positive Int: ${source}`);
1688
+ context.composer.transform(node => inheritTypography(node, { lineLimit: limit }));
1689
+ },
1690
+ "multilineTextAlignment(_:)": context => {
1691
+ const value = argumentSource(context, "alignment")?.replace(/^(?:TextAlignment)?\./, "");
1692
+ if (value !== "leading" && value !== "center" && value !== "trailing")
1693
+ throw new SyntaxError(`.multilineTextAlignment(_:) must be .leading, .center or .trailing: ${argumentSource(context, "alignment")}`);
1694
+ context.composer.transform(node => inheritTypography(node, { alignment: value }));
1695
+ },
1588
1696
  "foregroundStyle(_:)": context => {
1589
1697
  const foreground = lowerPaint(argumentSource(context, "style"));
1590
1698
  context.composer.transform(node => inheritEnvironment(node, { foreground }));