@unframework.dev/target-angular 0.4.0 → 0.4.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.
@@ -1,3 +1,4 @@
1
+ import { hoistedDefaultSlot } from "./default-slot.js";
1
2
  import { atConstraints } from "./types.js";
2
3
  import { childImports, kebabCase, keyImports, keyOwner, pascalCase } from "@unframework.dev/codegen";
3
4
  import { walk } from "@unframework.dev/ir";
@@ -95,6 +96,7 @@ function planComposition(plan) {
95
96
  }
96
97
  if (node.kind === "SlotOutlet" && usesDirective(node.slot, node.props !== void 0)) outlet = true;
97
98
  } });
99
+ if (hoistedDefaultSlot(component.render)) outlet = true;
98
100
  if (outlet) classes.push(imports.add("@angular/common", "NgTemplateOutlet"));
99
101
  return {
100
102
  tags,
@@ -0,0 +1,65 @@
1
+ import { unreachable } from "./plan.js";
2
+ /**
3
+ * The default slot the template declares once (ADR-0124), when the component renders it in
4
+ * exactly one place and that place is inside a `<component is>` of two or more candidates, with
5
+ * nothing between them that declares a name or narrows one: a loop, a conditional, a teleport, a
6
+ * transition, a scoped fill or another slot's fallback. Otherwise the template is as before:
7
+ * Angular projects into one `<ng-content>` alone, so a component that renders the default slot
8
+ * twice still renders it once.
9
+ */
10
+ function hoistedDefaultSlot(render) {
11
+ const projections = [];
12
+ const visit = (nodes, around) => {
13
+ for (const node of nodes) visitNode(node, around);
14
+ };
15
+ const visitFills = (fills, around) => {
16
+ for (const fill of fills) {
17
+ if (fill.forward !== void 0) continue;
18
+ visit(fill.children, fill.parameter === void 0 ? around : {
19
+ ...around,
20
+ direct: false
21
+ });
22
+ }
23
+ };
24
+ const indirect = (around) => ({
25
+ ...around,
26
+ direct: false
27
+ });
28
+ const visitNode = (node, around) => {
29
+ switch (node.kind) {
30
+ case "Text":
31
+ case "Interpolation": return;
32
+ case "Element": return visit(node.children, around);
33
+ case "Component": return visitFills(node.fills, around);
34
+ case "Dynamic": {
35
+ const inner = around.dynamic === void 0 && node.candidates.length > 1 ? {
36
+ dynamic: node,
37
+ direct: true
38
+ } : around;
39
+ visit(node.children, inner);
40
+ return visitFills(node.fills ?? [], inner);
41
+ }
42
+ case "SlotOutlet":
43
+ if (node.slot === "default" && node.props === void 0) projections.push({
44
+ outlet: node,
45
+ ...around
46
+ });
47
+ return visit(node.fallback, indirect(around));
48
+ case "If":
49
+ for (const branch of node.branches) visit(branch.children, indirect(around));
50
+ return;
51
+ case "For": return visitNode(node.body, indirect(around));
52
+ case "Teleport":
53
+ case "Transition": return visit(node.children, indirect(around));
54
+ default: return unreachable(node);
55
+ }
56
+ };
57
+ visit(render.kind === "Fragment" ? render.children : [render], { direct: true });
58
+ const [only, ...more] = projections;
59
+ if (only?.dynamic === void 0 || !only.direct || more.length) return;
60
+ return {
61
+ dynamic: only.dynamic,
62
+ outlet: only.outlet
63
+ };
64
+ }
65
+ export { hoistedDefaultSlot };
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { emitComposable } from "./composable.js";
2
2
  import { firstUse, keyOf, planComposition, refObjectCode, slotDirectiveCode, tokenType } from "./composition.js";
3
+ import { hoistedDefaultSlot } from "./default-slot.js";
3
4
  import { QUEUE_HELPER, RUNNER_HELPER, directiveClassCode, directiveClassName, directiveFile, emitDirective, runnerCode, runnerNames } from "./directive.js";
4
5
  import { directiveCode, scopeDirectiveCode, spaced, teleportDirectiveCode, textCaretDirectiveCode, transitionDirectiveCode, transitionRegionCode, trustedHtmlPipeCode } from "./directives.js";
5
6
  import { ATTRIBUTES_INPUT, ELEMENT_MEMBER, LISTENERS_INPUT, childApi, fallthroughDirectiveCode, planFallthrough, planRootElement } from "./fallthrough.js";
@@ -293,6 +294,7 @@ function emitComponent(component, context) {
293
294
  regions: holdsTransition(component, true),
294
295
  ref: plan.members.claim("transition")
295
296
  } : void 0;
297
+ const defaultSlot = hoistedDefaultSlot(component.render) ? { ref: plan.members.claim("defaultSlot") } : void 0;
296
298
  const appliedImports = importedDirectives(component, module).map(({ specifier, imported }) => imports.add(directiveSpecifier(specifier, directiveFile(imported)), directiveClassName(imported), { exact: true }));
297
299
  const ownApplied = ownDirectives(component, module);
298
300
  const runner = ownApplied.length ? runnerNames((base) => base === "queueHook" ? plan.helper(base) : imports.claim(base)) : void 0;
@@ -361,7 +363,7 @@ function emitComponent(component, context) {
361
363
  }, scope && {
362
364
  roots,
363
365
  host: scope.host
364
- }, teleportHost, transition)}\n `)],
366
+ }, teleportHost, transition, defaultSlot)}\n `)],
365
367
  ...component.styles ? [["styleUrl", js.stringLiteral(`./${module.file.slice(module.file.lastIndexOf("/") + 1)}.css`)], ["encapsulation", js.memberExpression(js.identifier(imports.add("@angular/core", "ViewEncapsulation")), "None")]] : []
366
368
  ]);
367
369
  const decorator = () => `@${printExpression(js.callExpression(js.identifier(decoratorName), [metadata]))}`;
package/dist/template.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { selectorOf } from "./composition.js";
2
+ import { hoistedDefaultSlot } from "./default-slot.js";
2
3
  import { TELEPORT_ATTRIBUTE, TELEPORT_HOST_INPUT } from "./directives.js";
3
4
  import { ATTRIBUTES_INPUT, ELEMENT_REF, FALLTHROUGH_ATTRIBUTE, LISTENERS_INPUT, childApi } from "./fallthrough.js";
4
5
  import { outputName } from "./outputs.js";
@@ -31,7 +32,7 @@ const LEVEL = 2;
31
32
  * (NG8009), so a signal a list's key reads is read there from the class, `this.label()`, and
32
33
  * counts as no use of its variable (an unread `@let` is NG8112).
33
34
  */
34
- function template(plan, listeners, reads, composition, models, fallthrough, scope, teleportHost, transition) {
35
+ function template(plan, listeners, reads, composition, models, fallthrough, scope, teleportHost, transition, defaultSlot) {
35
36
  const { component } = plan;
36
37
  const level = plan.resolved === void 0 ? LEVEL : 3;
37
38
  const pad = INDENT.repeat(level);
@@ -46,8 +47,37 @@ function template(plan, listeners, reads, composition, models, fallthrough, scop
46
47
  if ("binding" in item) declare(item.binding);
47
48
  if (item.kind === "Use") for (const member of item.members) declare(member.binding);
48
49
  }
50
+ const render = fallthrough.plan ? withFallthrough(models.render, fallthrough.plan) : models.render;
51
+ const found = defaultSlot && hoistedDefaultSlot(render);
52
+ const hoisted = found && {
53
+ ...found,
54
+ ref: defaultSlot.ref
55
+ };
56
+ /** Where a case renders the slot: the template, through `ngTemplateOutlet`. */
57
+ const renderSlot = (ref) => ({
58
+ kind: "tag",
59
+ tag: "ng-container",
60
+ attributes: [{
61
+ name: "[ngTemplateOutlet]",
62
+ text: `[ngTemplateOutlet]="${ref}"`
63
+ }]
64
+ });
49
65
  const dialect = {
50
66
  ...angularDialect,
67
+ dynamic: (printed) => {
68
+ const pieces = angularDialect.dynamic(printed);
69
+ if (printed.node !== hoisted?.dynamic) return pieces;
70
+ return [{
71
+ kind: "tag",
72
+ tag: "ng-template",
73
+ attributes: [{
74
+ name: `#${hoisted.ref}`,
75
+ text: `#${hoisted.ref}`
76
+ }],
77
+ content: angularDialect.slotOutlet({ node: hoisted.outlet })
78
+ }, ...pieces];
79
+ },
80
+ slotOutlet: (outlet) => hoisted && outlet.node === hoisted.outlet ? [renderSlot(hoisted.ref)] : angularDialect.slotOutlet(outlet),
51
81
  teleport: ({ node }) => [{
52
82
  kind: "wrapper",
53
83
  tag: "ng-template",
@@ -151,7 +181,6 @@ function template(plan, listeners, reads, composition, models, fallthrough, scop
151
181
  return angularDialect.styleAttribute(parts, context);
152
182
  }
153
183
  };
154
- const render = fallthrough.plan ? withFallthrough(models.render, fallthrough.plan) : models.render;
155
184
  const markup = printMarkup(render, dialect, {
156
185
  indent: INDENT,
157
186
  level,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unframework.dev/target-angular",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "The Angular 22 target of the Unframework compiler: emits idiomatic Angular components from the portable IR, declares their capabilities, and provides the toolchain the tests use.",
5
5
  "keywords": [
6
6
  "angular",