@unframework.dev/target-angular 0.3.0 → 0.4.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/dist/members.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { keyOf } from "./composition.js";
2
2
  import { arrow, lifecycleCode, watchEffectCode, watcherCode } from "./effects.js";
3
+ import { ATTRIBUTES_INPUT, ELEMENT_MEMBER, ELEMENT_REF, LISTENERS_INPUT } from "./fallthrough.js";
3
4
  import { outputName } from "./outputs.js";
4
5
  import { bindingById, formOf, slotQuery, unreachable } from "./plan.js";
5
6
  import { outputType, tidied } from "./rules.js";
@@ -30,7 +31,10 @@ function classMembers(context) {
30
31
  const exposed = new Set((component.exposes?.functions ?? []).map((id) => bindingById(plan, id).name));
31
32
  const visibility = (name) => exposed.has(name) || context.external.has(name) ? void 0 : context.templateMembers.has(name) ? "protected" : "private";
32
33
  const members = component.props.map((prop) => inputMember(prop, component, core("input"), placeholders, context.declarations));
33
- if (context.fallthrough) members.push(js.propertyDefinition("class", js.callExpression(js.identifier(core("input")), [], [placeholders.type("string")]), { readonly: true }), js.propertyDefinition("style", js.callExpression(js.identifier(core("input")), [], [placeholders.type("Record<string, string | null | undefined>")]), { readonly: true }));
34
+ if (context.fallthrough !== void 0) {
35
+ members.push(js.propertyDefinition("class", js.callExpression(js.identifier(core("input")), [], [placeholders.type("string")]), { readonly: true }), js.propertyDefinition("style", js.callExpression(js.identifier(core("input")), [], [placeholders.type("Record<string, string | null | undefined>")]), { readonly: true }));
36
+ if (context.fallthrough.passes) members.push(js.propertyDefinition(ATTRIBUTES_INPUT, js.callExpression(js.identifier(core("input")), [], [placeholders.type("Record<string, unknown>")]), { readonly: true }), js.propertyDefinition(LISTENERS_INPUT, js.callExpression(js.identifier(core("input")), [], [placeholders.type(context.fallthrough.listeners)]), { readonly: true }));
37
+ }
34
38
  for (const item of component.setup) {
35
39
  if (item.kind !== "Model") continue;
36
40
  const { name } = bindingById(plan, item.binding);
@@ -62,11 +66,31 @@ function classMembers(context) {
62
66
  continue;
63
67
  }
64
68
  const element = `${core("ElementRef")}<${item.type?.code ?? "unknown"}>`;
69
+ const child = plan.elementRefs.get(item.binding);
70
+ if (child !== void 0) {
71
+ const instance = plan.members.claim(`${name}Component`);
72
+ members.push(js.propertyDefinition(instance, placeholders.expression(`${core("viewChild")}(${JSON.stringify(name)}, { read: ${child} })`), {
73
+ accessibility: "private",
74
+ readonly: true
75
+ }), js.propertyDefinition(name, placeholders.expression(`${core("computed")}<${element} | undefined>(() => this.${instance}()?.${ELEMENT_MEMBER}())`), {
76
+ accessibility: "private",
77
+ readonly: true
78
+ }));
79
+ continue;
80
+ }
65
81
  members.push(js.propertyDefinition(name, js.callExpression(js.identifier(core("viewChild")), [js.stringLiteral(name)], [placeholders.type(element)]), {
66
82
  accessibility: "private",
67
83
  readonly: true
68
84
  }));
69
85
  }
86
+ const { rootElement } = context;
87
+ if (rootElement?.component) {
88
+ const { className, query } = rootElement.component;
89
+ members.push(js.propertyDefinition(query, placeholders.expression(`${core("viewChild")}(${JSON.stringify(ELEMENT_REF)}, { read: ${className} })`), {
90
+ accessibility: "private",
91
+ readonly: true
92
+ }), js.propertyDefinition(ELEMENT_MEMBER, placeholders.expression(`${core("computed")}(() => this.${query}()?.${ELEMENT_MEMBER}())`), { readonly: true }));
93
+ } else if (rootElement?.type !== void 0) members.push(js.propertyDefinition(ELEMENT_MEMBER, js.callExpression(js.identifier(core("viewChild")), [js.stringLiteral(ELEMENT_REF)], [placeholders.type(`${core("ElementRef")}<${rootElement.type}>`)]), { readonly: true }));
70
94
  const injected = /* @__PURE__ */ new Map();
71
95
  const service = (name) => {
72
96
  let entry = injected.get(name);
@@ -339,6 +363,10 @@ function classMembers(context) {
339
363
  }
340
364
  break;
341
365
  }
366
+ case "DevCheck":
367
+ initCode.push(pure(context, item.code));
368
+ pins = void 0;
369
+ break;
342
370
  default: unreachable(item);
343
371
  }
344
372
  for (const guard of context.guards) fields.push(js.propertyDefinition(guard, expression("new WeakSet<EventTarget>()"), {
package/dist/models.js CHANGED
@@ -1,6 +1,13 @@
1
1
  import { bindingById } from "./plan.js";
2
2
  import { templateRules } from "./rules.js";
3
3
  import { angularCode, bindingForms, mergesWholeStyle, rewriteExpression, typeNames } from "@unframework.dev/codegen";
4
+ /** The model helpers shared in `_unframework/` (ADR-0121); the others stay inline (R4). */
5
+ const SHARED_MODEL_HELPERS = /* @__PURE__ */ new Set([
6
+ "styleObject",
7
+ "modelText",
8
+ "writeModelText",
9
+ "looseToNumber"
10
+ ]);
4
11
  /** The helpers' code, by their base names. */
5
12
  const HELPERS = {
6
13
  classNames: (name) => `/**
@@ -37,7 +44,7 @@ function ${name}(style: object | string | null | undefined): Record<string, stri
37
44
  }
38
45
  return properties;
39
46
  }`,
40
- modelText: (name) => `/**
47
+ modelText: (name, loose = "looseToNumber") => `/**
41
48
  * Vue's \`vModelText\` (ADR-0054): the text a field shows for its model's value. It keeps the
42
49
  * field's own text where that text already means the value (\`12.5kg\` for \`12.5\` with a number
43
50
  * cast), or where the person is editing a trimmed field whose trimmed text is the value.
@@ -48,21 +55,21 @@ function ${name}(
48
55
  modifiers: { trim?: boolean; number?: boolean },
49
56
  ): string {
50
57
  const text = field.value;
51
- const current = modifiers.number && !/^0\\d/.test(text) ? looseToNumber(text) : text;
58
+ const current = modifiers.number && !/^0\\d/.test(text) ? ${loose}(text) : text;
52
59
  const next = value ?? "";
53
60
  if (current === next) return text;
54
61
  const editing = field.ownerDocument.activeElement === field && field.type !== "range";
55
62
  if (editing && modifiers.trim && text.trim() === next) return text;
56
63
  return String(next);
57
64
  }`,
58
- writeModelText: (name) => `/** What a field's text writes to its model, as Vue's \`vModelText\` reads it (ADR-0054). */
65
+ writeModelText: (name, loose = "looseToNumber") => `/** What a field's text writes to its model, as Vue's \`vModelText\` reads it (ADR-0054). */
59
66
  function ${name}<T>(
60
67
  model: WritableSignal<T>,
61
68
  field: HTMLInputElement | HTMLTextAreaElement,
62
69
  modifiers: { trim?: boolean; number?: boolean },
63
70
  ): void {
64
71
  const text = modifiers.trim ? field.value.trim() : field.value;
65
- model.set((modifiers.number ? looseToNumber(text) : text) as T);
72
+ model.set((modifiers.number ? ${loose}(text) : text) as T);
66
73
  }`,
67
74
  looseToNumber: (name) => `/** Vue's \`looseToNumber\`: the text's leading number, or the text when it has none. */
68
75
  function ${name}(text: string): string | number {
@@ -91,6 +98,10 @@ function ${name}(select: HTMLSelectElement): string[] {
91
98
  return Array.from(select.selectedOptions, (option) => option.value);
92
99
  }`
93
100
  };
101
+ /** A model helper's code under `name`, calling `looseToNumber` by `loose` where it calls it. */
102
+ function modelHelperCode(base, name, loose) {
103
+ return base === "modelText" || base === "writeModelText" ? HELPERS[base](name, loose) : HELPERS[base](name);
104
+ }
94
105
  /** Plans every native control's `v-model` of the component's template. */
95
106
  function planModels(plan) {
96
107
  const attributes = /* @__PURE__ */ new Map();
@@ -100,16 +111,18 @@ function planModels(plan) {
100
111
  const helper = (base) => {
101
112
  let found = helpers.get(base);
102
113
  if (!found) {
103
- let name = plan.imports.claim(base);
114
+ const shared = plan.shared !== void 0 && SHARED_MODEL_HELPERS.has(base);
115
+ let name = shared ? plan.helper(base) : plan.imports.claim(base);
104
116
  while (plan.members.has(name)) name = plan.imports.claim(base);
105
117
  plan.members.reserve(name);
106
- if ((base === "modelText" || base === "writeModelText") && !helpers.has("looseToNumber")) helper("looseToNumber");
118
+ if ((base === "modelText" || base === "writeModelText") && !shared && !helpers.has("looseToNumber")) helper("looseToNumber");
107
119
  found = {
108
120
  name,
109
- code: ""
121
+ code: "",
122
+ ...shared ? { shared: base } : {}
110
123
  };
111
124
  helpers.set(base, found);
112
- found.code = HELPERS[base](name);
125
+ if (!shared) found.code = HELPERS[base](name);
113
126
  }
114
127
  return found.name;
115
128
  };
@@ -212,7 +225,7 @@ function planModels(plan) {
212
225
  children: element.children.map((child) => mapElements(child, withSelected))
213
226
  };
214
227
  }
215
- if (model.control === "textarea") textCaret ??= plan.imports.claim("TextCaret");
228
+ if (model.control === "textarea") textCaret ??= plan.helper("TextCaret");
216
229
  if (variable !== void 0) printed.unshift({
217
230
  name: `#${variable}`,
218
231
  text: `#${variable}`
@@ -260,7 +273,7 @@ function optionValue(plan, option, rules, optionText) {
260
273
  if (value?.kind === "Bound") return rewriteExpression(value.value, plan.component, rules, "render");
261
274
  if (value?.kind === "Static") return JSON.stringify(value.value === true ? "" : value.value);
262
275
  const [only] = option.children;
263
- if (option.children.length === 1 && only?.kind === "Text") return JSON.stringify(only.value.replace(ASCII_EDGES, "").replace(ASCII_WHITESPACE, " "));
276
+ if (option.children.length === 1 && only?.kind === "Text") return JSON.stringify(only.value.replaceAll(ASCII_EDGES, "").replaceAll(ASCII_WHITESPACE, " "));
264
277
  const parts = option.children.flatMap((child) => {
265
278
  if (child.kind === "Text") return [JSON.stringify(child.value)];
266
279
  if (child.kind === "Interpolation") return [`((${rewriteExpression(child.value, plan.component, rules, "render")}) ?? "")`];
@@ -308,4 +321,4 @@ const CHILD_KEYS = /* @__PURE__ */ new Set([
308
321
  "fills",
309
322
  "fallback"
310
323
  ]);
311
- export { planModels };
324
+ export { modelHelperCode, planModels };
package/dist/narrowing.js CHANGED
@@ -89,6 +89,10 @@ function assertedSetup(component) {
89
89
  case "Model":
90
90
  case "Provide":
91
91
  case "Inject": return item;
92
+ case "DevCheck": return {
93
+ ...item,
94
+ code: code(item.code)
95
+ };
92
96
  case "Use": return {
93
97
  ...item,
94
98
  arguments: item.arguments.map((argument) => {
@@ -144,7 +148,8 @@ function movedReference(reference, moved) {
144
148
  case "Import":
145
149
  case "Event":
146
150
  case "Api":
147
- case "Slot": return {
151
+ case "Slot":
152
+ case "Dev": return {
148
153
  ...reference,
149
154
  span: moved(reference.span)
150
155
  };
package/dist/plan.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { FALLTHROUGH_MEMBERS, childApi } from "./fallthrough.js";
1
2
  import { assertedSetup } from "./narrowing.js";
2
3
  import { outputName } from "./outputs.js";
3
4
  import { ImportSet, NameScope, addPassThroughImports, codeKind, codeNames, passThroughImports, sourceNames } from "@unframework.dev/codegen";
@@ -52,7 +53,7 @@ const ANGULAR_IMPORTS = [
52
53
  * component is the source's, with each setup binding an event shares its name with renamed
53
54
  * (`withOutputNames`) and each narrowed member path of the setup asserted (./narrowing.ts).
54
55
  */
55
- function planComponent(declared, module) {
56
+ function planComponent(declared, module, shares = false) {
56
57
  const component = assertedSetup(withOutputNames(declared, module));
57
58
  const source = sourceNames(component, module);
58
59
  const imports = new ImportSet(new NameScope([...source].filter((name) => name !== component.name)));
@@ -68,8 +69,7 @@ function planComponent(declared, module) {
68
69
  const loopNames = new Set(component.bindings.filter(({ kind }) => isLoopVariable(kind)).map(({ name }) => name));
69
70
  const composition = new NameScope([
70
71
  ...memberNames,
71
- "class",
72
- "style",
72
+ ...FALLTHROUGH_MEMBERS,
73
73
  ...component.slots?.slots.map(({ name }) => slotQuery(name)) ?? []
74
74
  ]);
75
75
  const recipes = /* @__PURE__ */ new Map();
@@ -87,13 +87,13 @@ function planComponent(declared, module) {
87
87
  };
88
88
  const declaredMembers = /* @__PURE__ */ new Set([
89
89
  ...[...events.keys()].map(outputName),
90
- "class",
91
- "style",
90
+ ...FALLTHROUGH_MEMBERS,
92
91
  ...component.slots?.slots.map(({ name }) => slotQuery(name)) ?? []
93
92
  ]);
94
93
  const moduleMembers = new Map(component.bindings.filter(({ kind, name }) => isModuleBinding(kind) && declaredMembers.has(name)).map(({ id, name, kind }) => [id, claimMember(outputSafeName(name, kind))]));
95
94
  const bindingNames = new Set(component.bindings.map(({ name }) => name));
96
- const { forms, seeded, placements, late } = formsOf(component);
95
+ const elementRefs = rootElementRefs(component, module);
96
+ const { forms, seeded, placements, late } = formsOf(component, elementRefs);
97
97
  const calls = component.setup.flatMap((item) => {
98
98
  if (item.kind !== "Use") return [];
99
99
  const holder = claimMember(holderName(item.composable));
@@ -107,17 +107,31 @@ function planComponent(declared, module) {
107
107
  });
108
108
  const resolved = component.setup.some((item) => item.kind === "Await") ? claimMember("resolved") : void 0;
109
109
  const results = new Map(calls.flatMap(({ item, holder }) => item.members.map(({ member, binding }) => [binding, `${holder}.${member}`])));
110
+ const locals = new NameScope([
111
+ ...[...source].filter((name) => !bindingNames.has(name)),
112
+ ...component.bindings.filter(({ kind }) => isModuleBinding(kind)).map(({ name }) => name),
113
+ ...ANGULAR_IMPORTS,
114
+ "Object"
115
+ ]);
116
+ const shared = shares ? /* @__PURE__ */ new Map() : void 0;
117
+ let declaredInSource;
118
+ const helper = (base) => {
119
+ if (!shared) return imports.claim(base);
120
+ const known = shared.get(base);
121
+ if (known !== void 0) return known;
122
+ declaredInSource ??= [...module.components.flatMap((each) => [...sourceNames(each, module)]), ...(module.imports ?? []).flatMap((entry) => entry.names.map(({ local }) => local))];
123
+ const own = new RegExp(`^${base}(?:_\\d+)*$`);
124
+ imports.reserve(...declaredInSource.filter((name) => own.test(name)));
125
+ const local = imports.claim(base);
126
+ shared.set(base, local);
127
+ return local;
128
+ };
110
129
  return {
111
130
  component,
112
131
  module,
113
132
  imports,
114
133
  members,
115
- locals: new NameScope([
116
- ...[...source].filter((name) => !bindingNames.has(name)),
117
- ...component.bindings.filter(({ kind }) => isModuleBinding(kind)).map(({ name }) => name),
118
- ...ANGULAR_IMPORTS,
119
- "Object"
120
- ]),
134
+ locals,
121
135
  reserved: (fn) => [
122
136
  ...codeNames(fn.body.code, codeKind(fn.body, component)),
123
137
  ...fn.parameters.flatMap((parameter) => parameter.name === void 0 ? parameter.pattern?.names ?? [] : [parameter.name]),
@@ -133,7 +147,10 @@ function planComponent(declared, module) {
133
147
  results,
134
148
  moduleMembers,
135
149
  late,
136
- ...resolved === void 0 ? {} : { resolved }
150
+ elementRefs,
151
+ ...resolved === void 0 ? {} : { resolved },
152
+ helper,
153
+ ...shared === void 0 ? {} : { shared }
137
154
  };
138
155
  }
139
156
  /** A module's constant or a value imported from a `.ts` module (ADR-0070). */
@@ -161,11 +178,7 @@ function holderName(composable) {
161
178
  */
162
179
  function withOutputNames(component, module) {
163
180
  const events = new Set(component.emits?.events.map(({ name }) => outputName(name)) ?? []);
164
- const declared = /* @__PURE__ */ new Set([
165
- "class",
166
- "style",
167
- ...component.slots?.slots.map(({ name }) => slotQuery(name)) ?? []
168
- ]);
181
+ const declared = /* @__PURE__ */ new Set([...FALLTHROUGH_MEMBERS, ...component.slots?.slots.map(({ name }) => slotQuery(name)) ?? []]);
169
182
  const clashing = component.bindings.filter(({ name, kind }) => (events.has(name) || declared.has(name)) && kind !== "prop" && kind !== "emit" && !isModuleBinding(kind) && !isLoopVariable(kind));
170
183
  if (clashing.length === 0) return component;
171
184
  const taken = new NameScope([
@@ -224,7 +237,7 @@ function outputSafeName(name, kind) {
224
237
  * Each binding's form, and the setup `let`s seeded once the inputs are set: setup items in source
225
238
  * order, as a later item may read an earlier one.
226
239
  */
227
- function formsOf(component) {
240
+ function formsOf(component, elementRefs) {
228
241
  const forms = /* @__PURE__ */ new Map();
229
242
  const seeded = /* @__PURE__ */ new Set();
230
243
  const placements = /* @__PURE__ */ new Map();
@@ -274,7 +287,7 @@ function formsOf(component) {
274
287
  case "argument": break;
275
288
  default: unreachable(binding.kind);
276
289
  }
277
- const instances = componentRefs(component);
290
+ const instances = componentRefs(component, elementRefs);
278
291
  const escaping = escapingFunctions(component);
279
292
  let constructs = false;
280
293
  let watches = false;
@@ -333,7 +346,8 @@ function formsOf(component) {
333
346
  watches = true;
334
347
  break;
335
348
  case "Provide":
336
- case "Inject": break;
349
+ case "Inject":
350
+ case "DevCheck": break;
337
351
  case "Use": {
338
352
  const placement = item.arguments.flatMap((argument) => {
339
353
  switch (argument.kind) {
@@ -362,12 +376,29 @@ function formsOf(component) {
362
376
  late
363
377
  };
364
378
  }
365
- /** The template refs a `ref` on a component holds, which hold its instance (ADR-0054). */
366
- function componentRefs(component) {
379
+ /**
380
+ * The template refs a `ref` on a component holds, which hold its instance (ADR-0054): all but
381
+ * those on a child that exposes nothing, which hold its root element (ADR-0118).
382
+ */
383
+ function componentRefs(component, elementRefs) {
367
384
  const found = /* @__PURE__ */ new Set();
368
385
  walk(component.render, { enter(node) {
369
386
  if (node.kind !== "Component" && node.kind !== "Dynamic") return;
370
- for (const attribute of node.attributes) if (attribute.kind === "Ref") found.add(attribute.binding);
387
+ for (const attribute of node.attributes) if (attribute.kind === "Ref" && !elementRefs.has(attribute.binding)) found.add(attribute.binding);
388
+ } });
389
+ return found;
390
+ }
391
+ /**
392
+ * The template refs on a child that exposes nothing, by binding, with the child's local name: each
393
+ * holds the child's root element, which the child declares as `ufElement` (ADR-0118).
394
+ */
395
+ function rootElementRefs(component, module) {
396
+ const found = /* @__PURE__ */ new Map();
397
+ walk(component.render, { enter(node) {
398
+ if (node.kind !== "Component") return;
399
+ const child = childApi(module, node.component);
400
+ if (!child || child.exposes || child.refTag === void 0) return;
401
+ for (const attribute of node.attributes) if (attribute.kind === "Ref") found.set(attribute.binding, node.component);
371
402
  } });
372
403
  return found;
373
404
  }
@@ -413,6 +444,9 @@ function escapingFunctions(component) {
413
444
  for (const argument of item.arguments) if (argument.kind === "Value") visit(argument.value);
414
445
  else visit((argument.kind === "Getter" ? argument.getter : argument.callback).body);
415
446
  break;
447
+ case "DevCheck":
448
+ visit(item.code);
449
+ break;
416
450
  case "TemplateRef":
417
451
  case "Id":
418
452
  case "Model": break;
package/dist/rules.js CHANGED
@@ -46,7 +46,8 @@ function classRules(plan, nextTick) {
46
46
  api: () => {
47
47
  if (nextTick === void 0) throw new Error("`nextTick` is called, and no helper declared.");
48
48
  return `this.${nextTick}`;
49
- }
49
+ },
50
+ dev: () => "typeof ngDevMode === \"undefined\" || ngDevMode"
50
51
  };
51
52
  }
52
53
  /**
@@ -84,7 +85,7 @@ function closed(value, parent, key = "") {
84
85
  */
85
86
  function tidied(code, kind) {
86
87
  if (!code.includes("?.nativeElement")) return code;
87
- return withNullElements(code.includes("?.nativeElement?.") ? mapCode(code, { other: (text) => text.replace(/(\(\)\?\.nativeElement)\?\./g, "$1.") }, kind) : code, kind);
88
+ return withNullElements(code.includes("?.nativeElement?.") ? mapCode(code, { other: (text) => text.replaceAll(/(\(\)\?\.nativeElement)\?\./g, "$1.") }, kind) : code, kind);
88
89
  }
89
90
  /** Class code with each template ref's read used as a value ending `?? null` (see `tidied`). */
90
91
  function withNullElements(code, kind) {
package/dist/seeded.js CHANGED
@@ -159,7 +159,7 @@ function valueType(code, plan, declarations, seen = /* @__PURE__ */ new Set()) {
159
159
  case "LogicalExpression": {
160
160
  const left = typeOf(node.left);
161
161
  const right = typeOf(node.right);
162
- if (left === void 0 || right === void 0) return void 0;
162
+ if (left === void 0 || right === void 0) return;
163
163
  const present = withoutNullish(left, declarations);
164
164
  switch (node.operator) {
165
165
  case "??": return union(present, right);
@@ -198,9 +198,9 @@ function valueType(code, plan, declarations, seen = /* @__PURE__ */ new Set()) {
198
198
  const end = code.span.start + node.end;
199
199
  const reference = code.refs.find((each) => each.span.start === start && each.span.end === end);
200
200
  if (reference?.kind === "Binding") return bindingType(reference.binding, plan, declarations, seen);
201
- if (node.type !== "MemberExpression" || node.optional) return void 0;
201
+ if (node.type !== "MemberExpression" || node.optional) return;
202
202
  const receiver = typeOf(node.object);
203
- if (receiver === void 0) return void 0;
203
+ if (receiver === void 0) return;
204
204
  const property = node.property;
205
205
  if (!node.computed) return property.name === "length" && (receiver === "string" || arrayElement(receiver) !== void 0) ? "number" : void 0;
206
206
  const element = arrayElement(receiver);
@@ -211,20 +211,20 @@ function valueType(code, plan, declarations, seen = /* @__PURE__ */ new Set()) {
211
211
  };
212
212
  /** A call's type: a method of a string, a number or an array, or a pure global's. */
213
213
  const callType = (node) => {
214
- if (node.optional) return void 0;
214
+ if (node.optional) return;
215
215
  const callee = node.callee;
216
216
  const global = GLOBAL_CALLS.get(text(callee));
217
217
  if (global !== void 0) {
218
218
  const start = code.span.start + callee.start;
219
219
  return code.refs.some((reference) => reference.kind === "Global" && reference.span.start === start) ? global : void 0;
220
220
  }
221
- if (callee.type !== "MemberExpression" || callee.computed || callee.optional) return void 0;
221
+ if (callee.type !== "MemberExpression" || callee.computed || callee.optional) return;
222
222
  const receiver = typeOf(callee.object);
223
223
  const method = callee.property.name;
224
224
  if (receiver === "string") return STRING_METHODS.get(method);
225
225
  if (receiver === "number") return NUMBER_METHODS.has(method) ? "string" : void 0;
226
226
  const element = receiver === void 0 ? void 0 : arrayElement(receiver);
227
- if (element === void 0) return void 0;
227
+ if (element === void 0) return;
228
228
  const result = ARRAY_METHODS.get(method);
229
229
  return result === ARRAY ? `${grouped(element)}[]` : result;
230
230
  };
@@ -244,7 +244,7 @@ function union(first, second) {
244
244
  }
245
245
  /** Whether two types are written alike, whitespace apart. */
246
246
  function same(first, second) {
247
- return first.replace(/\s+/g, "") === second.replace(/\s+/g, "");
247
+ return first.replaceAll(/\s+/g, "") === second.replaceAll(/\s+/g, "");
248
248
  }
249
249
  /** A type in parentheses where `[]` would bind into it (a union, a function or a conditional). */
250
250
  function grouped(type) {
@@ -252,13 +252,13 @@ function grouped(type) {
252
252
  }
253
253
  /** The type of a binding's value, when its declaration makes it certain. */
254
254
  function bindingType(id, plan, declarations, seen) {
255
- if (seen.has(id)) return void 0;
255
+ if (seen.has(id)) return;
256
256
  const next = /* @__PURE__ */ new Set([...seen, id]);
257
257
  const binding = bindingById(plan, id);
258
258
  const { component } = plan;
259
259
  if (binding.kind === "prop") {
260
260
  const prop = component.props.find((each) => each.binding === id);
261
- if (prop === void 0) return void 0;
261
+ if (prop === void 0) return;
262
262
  const type = propType(prop, component);
263
263
  if (!prop.optional) return type;
264
264
  return prop.default === void 0 ? `${parenthesised(type)} | undefined` : defaultedType(prop, component, declarations);
package/dist/shared.js ADDED
@@ -0,0 +1,89 @@
1
+ import { directiveCode, scopeDirectiveCode, spaced, textCaretDirectiveCode, trustedHtmlPipeCode } from "./directives.js";
2
+ import { fallthroughClassCode, listenersTypeCode } from "./fallthrough.js";
3
+ import { modelHelperCode } from "./models.js";
4
+ import { ImportSet, Placeholders, hookQueueCode, js, printProgram } from "@unframework.dev/codegen";
5
+ /** Exports the declaration of `name`: `function styleObject(` → `export function styleObject(`. */
6
+ function exported(code, name) {
7
+ return code.replace(new RegExp(`^(function|class) ${name}\\b`, "m"), "export $1 " + name);
8
+ }
9
+ /** Imports a sibling helper module, `./looseToNumber`, as a helper module imports another (A1). */
10
+ function sibling(imports, name) {
11
+ return imports.add(`./${name}`, name, { exact: true });
12
+ }
13
+ /** The helpers each shared helper's module imports. */
14
+ const DEPENDENCIES = {
15
+ modelText: ["looseToNumber"],
16
+ writeModelText: ["looseToNumber"],
17
+ Fallthrough: ["Listeners"]
18
+ };
19
+ /** The module of a helper that another module exports, by the helper's export name. */
20
+ const MODULE_OF = { SvgListeners: "Listeners" };
21
+ /**
22
+ * Every helper Angular shares by a fixed name, the module's export name. The event directives
23
+ * are shared too, one module per event and option (`ClickCapture.ts`), named by
24
+ * {@link directiveModule}.
25
+ */
26
+ const SHARED_HELPERS = {
27
+ ScopeAttributes: (imports) => scopeDirectiveCode({
28
+ className: "ScopeAttributes",
29
+ scopes: "scopes"
30
+ }, imports),
31
+ TextCaret: (imports) => textCaretDirectiveCode("TextCaret", imports),
32
+ TrustedHtmlPipe: (imports) => exported(trustedHtmlPipeCode("TrustedHtmlPipe", imports), "TrustedHtmlPipe"),
33
+ queueHook: () => exported(hookQueueCode("queueHook"), "queueHook"),
34
+ styleObject: () => exported(modelHelperCode("styleObject", "styleObject"), "styleObject"),
35
+ looseToNumber: () => exported(modelHelperCode("looseToNumber", "looseToNumber"), "looseToNumber"),
36
+ modelText: (imports) => exported(modelHelperCode("modelText", "modelText", sibling(imports, "looseToNumber")), "modelText"),
37
+ Listeners: () => `${listenersTypeCode("Listeners", "HTMLElementEventMap", true)}\n${listenersTypeCode("SvgListeners", "SVGElementEventMap", true)}`,
38
+ Fallthrough: (imports) => fallthroughClassCode("Fallthrough", ["Listeners", "SvgListeners"].map((name) => imports.add("./Listeners", name, {
39
+ type: true,
40
+ exact: true
41
+ })).join(" | "), (name, type = false) => imports.add("@angular/core", name, { type }), (name) => imports.add("@angular/common", name)),
42
+ writeModelText: (imports) => {
43
+ imports.add("@angular/core", "WritableSignal", { type: true });
44
+ return exported(modelHelperCode("writeModelText", "writeModelText", sibling(imports, "looseToNumber")), "writeModelText");
45
+ }
46
+ };
47
+ Object.keys(SHARED_HELPERS);
48
+ /** The text of a module: its imports, then its code. */
49
+ function moduleText(code) {
50
+ const imports = new ImportSet();
51
+ const placeholders = new Placeholders();
52
+ const built = code(imports, placeholders);
53
+ return placeholders.print(() => {
54
+ const body = (typeof built === "string" ? built : built()).trim();
55
+ const declarations = imports.toDeclarations();
56
+ return `${declarations.length ? `${printProgram(js.program(declarations)).trim()}\n\n` : ""}${body}\n`;
57
+ });
58
+ }
59
+ /** The text of a shared helper's module, by its export name. */
60
+ function sharedModule(name) {
61
+ return moduleText(SHARED_HELPERS[name]);
62
+ }
63
+ /** The module of the directive that listens to one event with one option, `ClickCapture.ts`. */
64
+ function directiveModule(spec) {
65
+ return moduleText((imports, placeholders) => {
66
+ const { decorator, statements } = directiveCode({
67
+ ...spec,
68
+ className: spec.name
69
+ }, imports, placeholders);
70
+ return () => `${decorator}\n${spaced(printProgram(js.program(statements)))}`;
71
+ });
72
+ }
73
+ /**
74
+ * Puts the shared helpers an output file uses in their modules, through `helper`, and imports
75
+ * each into `imports` under its local name, the one the plan claimed.
76
+ */
77
+ function importShared(helper, from, imports, used) {
78
+ const put = (name, contents) => {
79
+ const module = MODULE_OF[name] ?? name;
80
+ for (const dependency of DEPENDENCIES[module] ?? []) put(dependency);
81
+ return helper(`${module}.ts`, contents ?? sharedModule(module), from).replace(/\.ts$/, "");
82
+ };
83
+ for (const { name, local, contents, type = false } of used) imports.add(put(name, contents), name, {
84
+ local,
85
+ exact: true,
86
+ type
87
+ });
88
+ }
89
+ export { directiveModule, importShared, sharedModule };
@@ -200,7 +200,7 @@ function isRead(node, members) {
200
200
  }
201
201
  /** `this.count` as `count`, `this.toggle_1.on` as `toggle_1.on`, or `undefined`. */
202
202
  function memberPath(node) {
203
- if (node.type !== "MemberExpression" || node.computed) return void 0;
203
+ if (node.type !== "MemberExpression" || node.computed) return;
204
204
  const object = node.object;
205
205
  const name = node.property.name;
206
206
  if (object.type === "ThisExpression") return name;