@unframework.dev/target-angular 0.0.0-stage → 0.0.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/README.md +426 -2
- package/dist/composable.js +239 -0
- package/dist/composition.js +214 -0
- package/dist/directives.js +82 -0
- package/dist/effects.js +187 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +559 -0
- package/dist/listeners.js +547 -0
- package/dist/members.js +462 -0
- package/dist/models.js +308 -0
- package/dist/modules.js +54 -0
- package/dist/narrowing.js +157 -0
- package/dist/outputs.js +190 -0
- package/dist/plan.js +558 -0
- package/dist/rules.js +332 -0
- package/dist/seeded.js +284 -0
- package/dist/statements.js +213 -0
- package/dist/template.js +243 -0
- package/dist/toolchain/check.js +91 -0
- package/dist/toolchain/client.d.ts +10 -0
- package/dist/toolchain/client.js +79 -0
- package/dist/toolchain/component.js +20 -0
- package/dist/toolchain/index.d.ts +4 -0
- package/dist/toolchain/index.js +15 -0
- package/dist/toolchain/linker.js +57 -0
- package/dist/toolchain/ngtsc-virtual.js +238 -0
- package/dist/toolchain/ngtsc-worker.d.ts +1 -0
- package/dist/toolchain/ngtsc-worker.js +104 -0
- package/dist/toolchain/ngtsc.js +187 -0
- package/dist/toolchain/server.d.ts +8 -0
- package/dist/toolchain/server.js +31 -0
- package/dist/toolchain/tools.js +82 -0
- package/dist/toolchain/vite.js +103 -0
- package/dist/types.js +262 -0
- package/package.json +67 -3
package/dist/members.js
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
import { outputName } from "./outputs.js";
|
|
2
|
+
import { bindingById, formOf, slotQuery, unreachable } from "./plan.js";
|
|
3
|
+
import { outputType, tidied } from "./rules.js";
|
|
4
|
+
import { arrow, lifecycleCode, watchEffectCode, watcherCode } from "./effects.js";
|
|
5
|
+
import { defaultedType, propType } from "./types.js";
|
|
6
|
+
import { keyOf } from "./composition.js";
|
|
7
|
+
import { parenthesised, valueType } from "./seeded.js";
|
|
8
|
+
import { functionBodyText, functionText, js, kebabCase, parameterText, pascalCase, rewriteCode } from "@unframework.dev/codegen";
|
|
9
|
+
/** The services the class injects, in the order it declares them. */
|
|
10
|
+
const SERVICES = [
|
|
11
|
+
"injector",
|
|
12
|
+
"platformId",
|
|
13
|
+
"appRef"
|
|
14
|
+
];
|
|
15
|
+
/** Each service's token, by the member that holds it. */
|
|
16
|
+
const TOKENS = {
|
|
17
|
+
injector: "Injector",
|
|
18
|
+
platformId: "PLATFORM_ID",
|
|
19
|
+
appRef: "ApplicationRef"
|
|
20
|
+
};
|
|
21
|
+
/** The class's members, and the interfaces it implements (`OnInit`). */
|
|
22
|
+
function classMembers(context) {
|
|
23
|
+
const { plan, placeholders } = context;
|
|
24
|
+
const { component, imports } = plan;
|
|
25
|
+
const core = (name) => imports.add("@angular/core", name);
|
|
26
|
+
const exposed = new Set((component.exposes?.functions ?? []).map((id) => bindingById(plan, id).name));
|
|
27
|
+
const visibility = (name) => exposed.has(name) || context.external.has(name) ? void 0 : context.templateMembers.has(name) ? "protected" : "private";
|
|
28
|
+
const members = component.props.map((prop) => inputMember(prop, component, core("input"), placeholders, context.declarations));
|
|
29
|
+
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 }));
|
|
30
|
+
for (const item of component.setup) {
|
|
31
|
+
if (item.kind !== "Model") continue;
|
|
32
|
+
const { name } = bindingById(plan, item.binding);
|
|
33
|
+
const callee = js.identifier(core("model"));
|
|
34
|
+
const alias = name === item.name ? [] : [js.objectExpression([["alias", js.stringLiteral(item.name)]])];
|
|
35
|
+
const unset = !item.default && !item.required && alias.length > 0;
|
|
36
|
+
const written = item.type?.code;
|
|
37
|
+
const type = written === void 0 ? unset ? [placeholders.type("unknown")] : [] : [placeholders.type(unset ? `(${written}) | undefined` : written)];
|
|
38
|
+
const initial = item.default ? [placeholders.expression(item.default.code)] : unset ? [js.identifier("undefined")] : [];
|
|
39
|
+
members.push(js.propertyDefinition(name, item.required ? js.callExpression(js.memberExpression(callee, "required"), alias, type) : js.callExpression(callee, [...initial, ...alias], type), { readonly: true }));
|
|
40
|
+
}
|
|
41
|
+
const calls = new Map(plan.calls.map((call) => [call.item, call]));
|
|
42
|
+
for (const event of component.emits?.events ?? []) members.push(js.propertyDefinition(outputName(event.name), js.callExpression(js.identifier(core("output")), [], [placeholders.type(outputType(event))]), { readonly: true }));
|
|
43
|
+
for (const { slot, className } of context.slots) {
|
|
44
|
+
const name = slotQuery(slot.name);
|
|
45
|
+
members.push(js.propertyDefinition(name, placeholders.expression(`${core("contentChild")}(${className}, { read: ${core("TemplateRef")} })`), {
|
|
46
|
+
accessibility: visibility(name),
|
|
47
|
+
readonly: true
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
for (const item of component.setup) {
|
|
51
|
+
if (item.kind !== "TemplateRef") continue;
|
|
52
|
+
const name = bindingById(plan, item.binding).name;
|
|
53
|
+
if (formOf(plan, bindingById(plan, item.binding)) === "instance") {
|
|
54
|
+
members.push(js.propertyDefinition(name, js.callExpression(js.identifier(core("viewChild")), [js.stringLiteral(name)], [placeholders.type(item.type?.code ?? "unknown")]), {
|
|
55
|
+
accessibility: visibility(name),
|
|
56
|
+
readonly: true
|
|
57
|
+
}));
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const element = `${core("ElementRef")}<${item.type?.code ?? "unknown"}>`;
|
|
61
|
+
members.push(js.propertyDefinition(name, js.callExpression(js.identifier(core("viewChild")), [js.stringLiteral(name)], [placeholders.type(element)]), {
|
|
62
|
+
accessibility: "private",
|
|
63
|
+
readonly: true
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
const injected = /* @__PURE__ */ new Map();
|
|
67
|
+
const names = {
|
|
68
|
+
core,
|
|
69
|
+
common: (name) => imports.add("@angular/common", name),
|
|
70
|
+
type: (name) => imports.add("@angular/core", name, { type: true }),
|
|
71
|
+
member: (name) => {
|
|
72
|
+
let entry = injected.get(name);
|
|
73
|
+
if (!entry) {
|
|
74
|
+
entry = {
|
|
75
|
+
member: plan.members.claim(name),
|
|
76
|
+
token: TOKENS[name]
|
|
77
|
+
};
|
|
78
|
+
injected.set(name, entry);
|
|
79
|
+
}
|
|
80
|
+
return entry.member;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const fields = [];
|
|
84
|
+
const arrows = [];
|
|
85
|
+
const constructorCode = [];
|
|
86
|
+
const initCode = [];
|
|
87
|
+
const destroyCode = [];
|
|
88
|
+
const unmounted = [];
|
|
89
|
+
const refs = [];
|
|
90
|
+
/** Adds an effect's code: its statements, its member, what `ngOnDestroy` runs. */
|
|
91
|
+
const effect = (into, code, hook = false) => {
|
|
92
|
+
if (code.code !== void 0) into.push(code.code);
|
|
93
|
+
if (code.ref) {
|
|
94
|
+
const member = js.propertyDefinition(code.ref.name, null, {
|
|
95
|
+
accessibility: "private",
|
|
96
|
+
readonly: !code.ref.optional,
|
|
97
|
+
typeAnnotation: placeholders.type(code.ref.type)
|
|
98
|
+
});
|
|
99
|
+
refs.push(code.ref.optional ? {
|
|
100
|
+
...member,
|
|
101
|
+
optional: true
|
|
102
|
+
} : member);
|
|
103
|
+
}
|
|
104
|
+
if (code.destroy !== void 0) (hook ? unmounted : destroyCode).push(code.destroy);
|
|
105
|
+
};
|
|
106
|
+
let pins;
|
|
107
|
+
/**
|
|
108
|
+
* Reads a seeded value in `ngOnInit` (`this.amount();`), or assigns a seeded `let`, so that it
|
|
109
|
+
* keeps the inputs' first values.
|
|
110
|
+
*/
|
|
111
|
+
const pin = (statement) => {
|
|
112
|
+
if (pins === void 0) {
|
|
113
|
+
pins = ["// Read once the inputs are set: these keep the values they start with."];
|
|
114
|
+
initCode.push("");
|
|
115
|
+
}
|
|
116
|
+
pins.push(statement);
|
|
117
|
+
initCode[initCode.length - 1] = pins.join("\n");
|
|
118
|
+
};
|
|
119
|
+
const methods = [];
|
|
120
|
+
const expression = (code) => placeholders.expression(code);
|
|
121
|
+
const typed = (type) => type ? [placeholders.type(type)] : [];
|
|
122
|
+
for (const item of component.setup) switch (item.kind) {
|
|
123
|
+
case "State": {
|
|
124
|
+
const { name } = bindingById(plan, item.binding);
|
|
125
|
+
const form = formOf(plan, bindingById(plan, item.binding));
|
|
126
|
+
const type = item.type ? item.initial ? item.type.code : `${parenthesised(item.type.code)} | undefined` : void 0;
|
|
127
|
+
const initial = item.initial ? pure(context, item.initial) : "undefined";
|
|
128
|
+
let value;
|
|
129
|
+
switch (form) {
|
|
130
|
+
case "linked":
|
|
131
|
+
value = js.callExpression(js.identifier(core("linkedSignal")), [expression(arrow(`${core("untracked")}(${arrow(initial)})`))], typed(type));
|
|
132
|
+
pin(`this.${name}();`);
|
|
133
|
+
break;
|
|
134
|
+
case "signal":
|
|
135
|
+
value = js.callExpression(js.identifier(core("signal")), [expression(initial)], typed(type));
|
|
136
|
+
break;
|
|
137
|
+
default: throw new Error(`A state is declared as ${form}.`);
|
|
138
|
+
}
|
|
139
|
+
fields.push(js.propertyDefinition(name, value, {
|
|
140
|
+
accessibility: visibility(name),
|
|
141
|
+
readonly: true
|
|
142
|
+
}));
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
case "Derived": {
|
|
146
|
+
const { name } = bindingById(plan, item.binding);
|
|
147
|
+
const getter = functionText(item.getter, component, context.rules, "pure");
|
|
148
|
+
fields.push(js.propertyDefinition(name, js.callExpression(js.identifier(core("computed")), [expression(getter)], typed(item.type?.code)), {
|
|
149
|
+
accessibility: visibility(name),
|
|
150
|
+
readonly: true
|
|
151
|
+
}));
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case "TemplateRef": break;
|
|
155
|
+
case "Id": {
|
|
156
|
+
const { name } = bindingById(plan, item.binding);
|
|
157
|
+
if (context.nextId === void 0) throw new Error("An id is declared without a counter.");
|
|
158
|
+
const id = `\`uf-id-${kebabCase(component.name)}-\${${context.nextId}++}\``;
|
|
159
|
+
fields.push(js.propertyDefinition(name, expression(id), {
|
|
160
|
+
accessibility: visibility(name),
|
|
161
|
+
readonly: true
|
|
162
|
+
}));
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
case "Const": {
|
|
166
|
+
const binding = bindingById(plan, item.binding);
|
|
167
|
+
const value = pure(context, item.value);
|
|
168
|
+
const form = formOf(plan, binding);
|
|
169
|
+
switch (form) {
|
|
170
|
+
case "once":
|
|
171
|
+
fields.push(js.propertyDefinition(binding.name, js.callExpression(js.identifier(core("computed")), [expression(arrow(`${core("untracked")}(${arrow(value)})`))], typed(item.type?.code)), {
|
|
172
|
+
accessibility: visibility(binding.name),
|
|
173
|
+
readonly: true
|
|
174
|
+
}));
|
|
175
|
+
pin(`this.${binding.name}();`);
|
|
176
|
+
break;
|
|
177
|
+
case "value":
|
|
178
|
+
fields.push(js.propertyDefinition(binding.name, expression(value), {
|
|
179
|
+
accessibility: visibility(binding.name),
|
|
180
|
+
readonly: true,
|
|
181
|
+
...item.type ? { typeAnnotation: placeholders.type(item.type.code) } : {}
|
|
182
|
+
}));
|
|
183
|
+
break;
|
|
184
|
+
default: throw new Error(`A constant is declared as ${form}.`);
|
|
185
|
+
}
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
case "Variable": {
|
|
189
|
+
const { name } = bindingById(plan, item.binding);
|
|
190
|
+
if (plan.seeded.has(item.binding) && item.initial) {
|
|
191
|
+
let type = item.type?.code ?? valueType(item.initial, plan, context.declarations);
|
|
192
|
+
if (type === void 0) {
|
|
193
|
+
const initial = plan.members.claim(`initial${pascalCase(name)}`);
|
|
194
|
+
type = `ReturnType<typeof this.${initial}>`;
|
|
195
|
+
methods.push(js.methodDefinition(initial, [], statements(placeholders, `return ${pure(context, item.initial)};`), { accessibility: "private" }));
|
|
196
|
+
pin(`this.${name} = this.${initial}();`);
|
|
197
|
+
} else pin(`this.${name} = ${pure(context, item.initial)};`);
|
|
198
|
+
const field = js.propertyDefinition(name, null, {
|
|
199
|
+
accessibility: "private",
|
|
200
|
+
typeAnnotation: placeholders.type(type)
|
|
201
|
+
});
|
|
202
|
+
fields.push({
|
|
203
|
+
...field,
|
|
204
|
+
definite: true
|
|
205
|
+
});
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
fields.push(js.propertyDefinition(name, item.initial ? expression(pure(context, item.initial)) : null, {
|
|
209
|
+
accessibility: "private",
|
|
210
|
+
...item.type ? { typeAnnotation: placeholders.type(item.type.code) } : {}
|
|
211
|
+
}));
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
case "Function": {
|
|
215
|
+
const binding = bindingById(plan, item.binding);
|
|
216
|
+
const fn = item.function;
|
|
217
|
+
if (formOf(plan, binding) === "arrow") {
|
|
218
|
+
arrows.push(js.propertyDefinition(binding.name, expression(tidied(functionText(fn, component, context.rules, "client"), "expression")), {
|
|
219
|
+
accessibility: visibility(binding.name),
|
|
220
|
+
readonly: true
|
|
221
|
+
}));
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
methods.push(js.methodDefinition(binding.name, fn.parameters.map((parameter) => placeholders.parameter(parameterText(parameter))), statements(placeholders, tidied(functionBodyText(fn, component, context.rules, "client"), "statements")), {
|
|
225
|
+
accessibility: visibility(binding.name),
|
|
226
|
+
...fn.async ? { async: true } : {},
|
|
227
|
+
...fn.returnType ? { returnType: placeholders.type(fn.returnType.code) } : {}
|
|
228
|
+
}));
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
case "Watch":
|
|
232
|
+
effect(initCode, watcherCode(item, plan, context.rules, names));
|
|
233
|
+
pins = void 0;
|
|
234
|
+
break;
|
|
235
|
+
case "WatchEffect":
|
|
236
|
+
if (plan.late.has(item)) {
|
|
237
|
+
effect(initCode, watchEffectCode(item, plan, context.rules, names, true));
|
|
238
|
+
pins = void 0;
|
|
239
|
+
} else effect(constructorCode, watchEffectCode(item, plan, context.rules, names));
|
|
240
|
+
break;
|
|
241
|
+
case "Lifecycle":
|
|
242
|
+
if (plan.late.has(item)) {
|
|
243
|
+
effect(initCode, lifecycleCode(item, plan, context.rules, names, true), true);
|
|
244
|
+
pins = void 0;
|
|
245
|
+
} else effect(constructorCode, lifecycleCode(item, plan, context.rules, names), true);
|
|
246
|
+
break;
|
|
247
|
+
case "Inject": {
|
|
248
|
+
const { name } = bindingById(plan, item.binding);
|
|
249
|
+
const key = context.keyNames.get(item.key) ?? item.key;
|
|
250
|
+
const fallback = item.fallback ? pure(context, item.fallback) : "undefined";
|
|
251
|
+
const value = item.fallback && keyOf(plan, item.key).ref ? `${context.refObject}(${fallback})` : fallback;
|
|
252
|
+
const read = `this.${names.member("injector")}.get(${key}, null, { optional: true, skipSelf: true }) ?? ${value}`;
|
|
253
|
+
fields.push(js.propertyDefinition(name, js.callExpression(js.identifier(core("computed")), [expression(arrow(`${core("untracked")}(${arrow(read)})`))]), {
|
|
254
|
+
accessibility: visibility(name),
|
|
255
|
+
readonly: true
|
|
256
|
+
}));
|
|
257
|
+
pin(`this.${name}();`);
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
case "Model":
|
|
261
|
+
case "Provide": break;
|
|
262
|
+
case "Use": {
|
|
263
|
+
const { holder, placement, injector } = calls.get(item);
|
|
264
|
+
let invocation = callCode(context, item);
|
|
265
|
+
let type;
|
|
266
|
+
if (placement !== "field") {
|
|
267
|
+
const factory = plan.members.claim(`create${pascalCase(holder)}`);
|
|
268
|
+
methods.push(js.methodDefinition(factory, [], statements(placeholders, `return ${invocation};`), { accessibility: "private" }));
|
|
269
|
+
invocation = `this.${factory}()`;
|
|
270
|
+
type = `ReturnType<typeof this.${factory}>`;
|
|
271
|
+
}
|
|
272
|
+
const run = (into) => `${core("runInInjectionContext")}(this.${into}, () => ${invocation})`;
|
|
273
|
+
if (injector !== void 0) {
|
|
274
|
+
fields.push(js.propertyDefinition(injector, expression(`${core("Injector")}.create({ providers: [], parent: this.${names.member("injector")} })`), {
|
|
275
|
+
accessibility: "private",
|
|
276
|
+
readonly: true
|
|
277
|
+
}));
|
|
278
|
+
effect(initCode, { destroy: `this.${injector}.destroy();` }, true);
|
|
279
|
+
}
|
|
280
|
+
const call = injector === void 0 ? invocation : run(injector);
|
|
281
|
+
switch (placement) {
|
|
282
|
+
case "field":
|
|
283
|
+
fields.push(js.propertyDefinition(holder, expression(call), {
|
|
284
|
+
accessibility: visibility(holder),
|
|
285
|
+
readonly: true
|
|
286
|
+
}));
|
|
287
|
+
break;
|
|
288
|
+
case "constructor":
|
|
289
|
+
fields.push(js.propertyDefinition(holder, null, {
|
|
290
|
+
accessibility: visibility(holder),
|
|
291
|
+
readonly: true,
|
|
292
|
+
typeAnnotation: placeholders.type(type)
|
|
293
|
+
}));
|
|
294
|
+
constructorCode.push(`this.${holder} = ${call};`);
|
|
295
|
+
break;
|
|
296
|
+
case "init": {
|
|
297
|
+
const field = js.propertyDefinition(holder, null, {
|
|
298
|
+
accessibility: visibility(holder),
|
|
299
|
+
typeAnnotation: placeholders.type(type)
|
|
300
|
+
});
|
|
301
|
+
fields.push({
|
|
302
|
+
...field,
|
|
303
|
+
definite: true
|
|
304
|
+
});
|
|
305
|
+
initCode.push(`this.${holder} = ${injector === void 0 ? run(names.member("injector")) : call};`);
|
|
306
|
+
pins = void 0;
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
default: unreachable(placement);
|
|
310
|
+
}
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
default: unreachable(item);
|
|
314
|
+
}
|
|
315
|
+
for (const guard of context.guards) fields.push(js.propertyDefinition(guard, expression("new WeakSet<EventTarget>()"), {
|
|
316
|
+
accessibility: "protected",
|
|
317
|
+
readonly: true
|
|
318
|
+
}));
|
|
319
|
+
for (const handler of context.handlers) methods.push(js.methodDefinition(handler.name, handler.parameters.map((parameter) => placeholders.parameter(parameter)), statements(placeholders, handler.body), {
|
|
320
|
+
accessibility: "protected",
|
|
321
|
+
...handler.async ? { async: true } : {}
|
|
322
|
+
}));
|
|
323
|
+
if (context.once !== void 0) methods.push(js.methodDefinition(context.once, [placeholders.parameter("elements: WeakSet<EventTarget>"), placeholders.parameter("event: Event")], statements(placeholders, [
|
|
324
|
+
"const element = event.currentTarget;",
|
|
325
|
+
"if (element === null || elements.has(element)) return false;",
|
|
326
|
+
"elements.add(element);",
|
|
327
|
+
"return true;"
|
|
328
|
+
].join("\n")), {
|
|
329
|
+
accessibility: "protected",
|
|
330
|
+
returnType: placeholders.type("boolean")
|
|
331
|
+
}));
|
|
332
|
+
if (context.nextTick !== void 0) {
|
|
333
|
+
const appRef = names.member("appRef");
|
|
334
|
+
methods.push(js.methodDefinition(context.nextTick, [], statements(placeholders, `await Promise.resolve();\nthis.${appRef}.tick();`), {
|
|
335
|
+
accessibility: "private",
|
|
336
|
+
async: true,
|
|
337
|
+
returnType: placeholders.type("Promise<void>")
|
|
338
|
+
}));
|
|
339
|
+
}
|
|
340
|
+
const services = SERVICES.flatMap((name) => injected.get(name) ?? []).map(({ member, token }) => js.propertyDefinition(member, js.callExpression(js.identifier(core("inject")), [js.identifier(core(token))]), {
|
|
341
|
+
accessibility: "private",
|
|
342
|
+
readonly: true
|
|
343
|
+
}));
|
|
344
|
+
if (context.server !== void 0) fields.push(js.propertyDefinition(context.server, expression(`${imports.add("@angular/common", "isPlatformServer")}(${core("inject")}(${core("PLATFORM_ID")}))`), {
|
|
345
|
+
accessibility: "protected",
|
|
346
|
+
readonly: true
|
|
347
|
+
}));
|
|
348
|
+
const globals = [...[
|
|
349
|
+
...context.globals.map((name) => [name, name]),
|
|
350
|
+
...context.moduleMembers,
|
|
351
|
+
...plan.recipes
|
|
352
|
+
].map(([value, member]) => ({
|
|
353
|
+
value,
|
|
354
|
+
member
|
|
355
|
+
})).toSorted((a, b) => a.member < b.member ? -1 : a.member > b.member ? 1 : 0), ...context.helpers.map(({ name }) => ({
|
|
356
|
+
value: name,
|
|
357
|
+
member: name
|
|
358
|
+
}))].map(({ value, member }) => js.propertyDefinition(member, js.identifier(value), {
|
|
359
|
+
accessibility: "protected",
|
|
360
|
+
readonly: true
|
|
361
|
+
}));
|
|
362
|
+
const lifecycle = [];
|
|
363
|
+
const implementing = [];
|
|
364
|
+
if (constructorCode.length) lifecycle.push(js.methodDefinition("constructor", [], statements(placeholders, constructorCode.join("\n\n")), { kind: "constructor" }));
|
|
365
|
+
if (initCode.length) {
|
|
366
|
+
implementing.push(imports.add("@angular/core", "OnInit", { type: true }));
|
|
367
|
+
lifecycle.push(js.methodDefinition("ngOnInit", [], statements(placeholders, initCode.join("\n\n")), { returnType: js.keywordType("void") }));
|
|
368
|
+
}
|
|
369
|
+
if ([...destroyCode, ...unmounted].length) {
|
|
370
|
+
implementing.push(imports.add("@angular/core", "OnDestroy", { type: true }));
|
|
371
|
+
const why = destroyCode.length ? ["// Before Angular stops the outputs: a cleanup may still emit, as on every target."] : [];
|
|
372
|
+
lifecycle.push(js.methodDefinition("ngOnDestroy", [], statements(placeholders, [
|
|
373
|
+
...why,
|
|
374
|
+
...destroyCode,
|
|
375
|
+
...unmounted
|
|
376
|
+
].join("\n")), { returnType: js.keywordType("void") }));
|
|
377
|
+
}
|
|
378
|
+
return {
|
|
379
|
+
members: [
|
|
380
|
+
...members,
|
|
381
|
+
...services,
|
|
382
|
+
...arrows,
|
|
383
|
+
...fields,
|
|
384
|
+
...refs,
|
|
385
|
+
...globals,
|
|
386
|
+
...lifecycle,
|
|
387
|
+
...methods
|
|
388
|
+
],
|
|
389
|
+
implements: implementing
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* A composable's call, `useTotal(() => this.price(), () => this.quantity())`: a value argument
|
|
394
|
+
* as the setup evaluates it, a getter as a pure arrow, a callback as client code (ADR-0069).
|
|
395
|
+
*/
|
|
396
|
+
function callCode(context, item) {
|
|
397
|
+
const { component } = context.plan;
|
|
398
|
+
const parts = item.arguments.map((argument) => {
|
|
399
|
+
switch (argument.kind) {
|
|
400
|
+
case "Value": return pure(context, argument.value);
|
|
401
|
+
case "Getter": return functionText(argument.getter, component, context.rules, "pure");
|
|
402
|
+
case "Callback": return tidied(functionText(argument.callback, component, context.rules, "client"), "expression");
|
|
403
|
+
default: return unreachable(argument);
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
return `${composableName(context.plan, item.composable)}(${parts.join(", ")})`;
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* A called composable's name in the output, imported from its own module beside its `.uf.ts`
|
|
410
|
+
* (`./use-toggle`, ADR-0069) under the source's local name.
|
|
411
|
+
*/
|
|
412
|
+
function composableName(plan, local) {
|
|
413
|
+
for (const entry of plan.module.imports ?? []) {
|
|
414
|
+
const name = entry.names.find((each) => each.kind === "Composable" && each.local === local);
|
|
415
|
+
if (name === void 0) continue;
|
|
416
|
+
const directory = entry.specifier.slice(0, entry.specifier.lastIndexOf("/") + 1);
|
|
417
|
+
return plan.imports.add(`${directory}${kebabCase(name.imported)}`, name.imported, {
|
|
418
|
+
local,
|
|
419
|
+
exact: true
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
throw new Error(`\`${local}\` is called as a composable, and no import names it.`);
|
|
423
|
+
}
|
|
424
|
+
/** Code the setup evaluates (an initial value, a constant), spelled for the class. */
|
|
425
|
+
function pure(context, code) {
|
|
426
|
+
return rewriteCode(code, context.plan.component, context.rules, "pure");
|
|
427
|
+
}
|
|
428
|
+
/** A method's body as one statements placeholder, or none for an empty body. */
|
|
429
|
+
function statements(placeholders, code) {
|
|
430
|
+
return code.trim() === "" ? [] : [placeholders.statements(code)];
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* A prop as a signal input, typed by its member's type as written, or read from the props type
|
|
434
|
+
* where another declaration declares it (`propType`, ADR-0067):
|
|
435
|
+
* - required: `readonly label = input.required<string>();`
|
|
436
|
+
* - optional without a default: `readonly count = input<number>();`, whose value is
|
|
437
|
+
* `number | undefined` (Angular's own spelling of an optional input);
|
|
438
|
+
* - with a default: `readonly tone = input<Tone, Tone | undefined>("info", { transform })`, or
|
|
439
|
+
* `input<Exclude<ButtonProps["tone"], undefined>, ButtonProps["tone"]>` by indexed access, whose
|
|
440
|
+
* transform turns `undefined` into the default. A destructured default applies to an absent
|
|
441
|
+
* prop and to an explicit `undefined` alike, while Angular's initial value covers only the
|
|
442
|
+
* first (`setInput(name, undefined)` sets `undefined`). `null` is a value, as in JavaScript,
|
|
443
|
+
* so the test is `=== undefined`, never `??` (ADR-0034).
|
|
444
|
+
*/
|
|
445
|
+
function inputMember(prop, component, input, placeholders, declarations) {
|
|
446
|
+
const callee = js.identifier(input);
|
|
447
|
+
const { default: initial } = prop;
|
|
448
|
+
const type = propType(prop, component);
|
|
449
|
+
if (!prop.optional) {
|
|
450
|
+
const required = js.memberExpression(callee, "required");
|
|
451
|
+
return readonlyField(prop, js.callExpression(required, [], [placeholders.type(type)]));
|
|
452
|
+
}
|
|
453
|
+
if (initial === void 0) return readonlyField(prop, js.callExpression(callee, [], [placeholders.type(type)]));
|
|
454
|
+
const value = defaultedType(prop, component, declarations);
|
|
455
|
+
const transform = js.arrowFunction([js.bindingIdentifier("value")], js.conditionalExpression(js.binaryExpression("===", js.identifier("value"), js.identifier("undefined")), placeholders.expression(initial.code), js.identifier("value")));
|
|
456
|
+
return readonlyField(prop, js.callExpression(callee, [placeholders.expression(initial.code), js.objectExpression([["transform", transform]])], [placeholders.type(value), prop.declaredBy && type !== prop.type.code ? placeholders.type(type) : js.unionType([placeholders.type(value), js.keywordType("undefined")])]));
|
|
457
|
+
}
|
|
458
|
+
/** `readonly name = …;`: public, as `strictInputAccessModifiers` requires of an input. */
|
|
459
|
+
function readonlyField(prop, value) {
|
|
460
|
+
return js.propertyDefinition(prop.name, value, { readonly: true });
|
|
461
|
+
}
|
|
462
|
+
export { classMembers, composableName };
|