@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/effects.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { bindingById, formOf, unreachable } from "./plan.js";
|
|
2
|
+
import { classRead, tidied } from "./rules.js";
|
|
3
|
+
import { bodyStatements, hasReturn } from "./statements.js";
|
|
4
|
+
import { NameScope, codeKind, codeNames, functionText, pascalCase, rewriteCode } from "@unframework.dev/codegen";
|
|
5
|
+
/** A watcher's code for `ngOnInit`. */
|
|
6
|
+
function watcherCode(item, plan, rules, names) {
|
|
7
|
+
const { locals } = plan;
|
|
8
|
+
const [valueParameter, previousParameter, cleanupParameter] = item.callback.parameters;
|
|
9
|
+
const lines = [];
|
|
10
|
+
let read;
|
|
11
|
+
let hint;
|
|
12
|
+
if (item.array) {
|
|
13
|
+
const reads = item.sources.map((source) => sourceRead(source, plan, rules));
|
|
14
|
+
const signal = locals.claim("currentValues");
|
|
15
|
+
const equal = `(next, last) => next.every((value, index) => Object.is(value, last[index]))`;
|
|
16
|
+
const tuple = item.sources.map(() => "unknown").join(", ");
|
|
17
|
+
lines.push(`const ${signal} = ${names.core("computed")}(() => [${reads.join(", ")}] satisfies [${tuple}], { equal: ${equal} });`);
|
|
18
|
+
read = `${signal}()`;
|
|
19
|
+
hint = "values";
|
|
20
|
+
} else {
|
|
21
|
+
const [source] = item.sources;
|
|
22
|
+
if (!source) throw new Error("A watcher has no source.");
|
|
23
|
+
const watched = watchedSignal(source, plan);
|
|
24
|
+
if (watched !== void 0) {
|
|
25
|
+
hint = watched.name;
|
|
26
|
+
read = classRead(plan, watched);
|
|
27
|
+
} else if (source.kind === "Ref") {
|
|
28
|
+
const state = bindingById(plan, source.binding);
|
|
29
|
+
hint = state.name.replace(/^current([A-Z])/, (_, first) => first.toLowerCase());
|
|
30
|
+
const signal = locals.claim(`current${pascalCase(hint)}`);
|
|
31
|
+
lines.push(`const ${signal} = ${names.core("computed")}(${arrow(classRead(plan, state))});`);
|
|
32
|
+
read = `${signal}()`;
|
|
33
|
+
} else {
|
|
34
|
+
hint = valueParameter?.name ?? "value";
|
|
35
|
+
const signal = locals.claim(`current${pascalCase(hint)}`);
|
|
36
|
+
const getter = functionText(source.getter, plan.component, rules, "pure");
|
|
37
|
+
lines.push(`const ${signal} = ${names.core("computed")}(${getter});`);
|
|
38
|
+
read = `${signal}()`;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const reads = codeNames(item.callback.body.code, codeKind(item.callback.body, plan.component));
|
|
42
|
+
const readsPrevious = previousParameter !== void 0 && (previousParameter.name === void 0 ? (previousParameter.pattern?.names ?? []).some((name) => reads.has(name)) : reads.has(previousParameter.name));
|
|
43
|
+
const last = !item.immediate || readsPrevious ? locals.claim(`last${pascalCase(hint)}`) : void 0;
|
|
44
|
+
if (last !== void 0) lines.push(`let ${last} = ${read};`);
|
|
45
|
+
const first = item.immediate && readsPrevious ? locals.claim("first") : void 0;
|
|
46
|
+
if (first !== void 0) lines.push(`let ${first} = true;`);
|
|
47
|
+
const inner = new NameScope([...plan.reserved(item.callback), ...lines.flatMap((line) => /^(?:const|let) (\S+)/.exec(line)?.[1] ?? [])]);
|
|
48
|
+
const own = item.array && valueParameter?.type ? void 0 : valueParameter?.name;
|
|
49
|
+
const value = own ?? inner.claim(item.array ? "current" : "value");
|
|
50
|
+
const body = [`const ${value}${own !== void 0 && !item.array ? annotation(valueParameter) : ""} = ${read};`];
|
|
51
|
+
if (!item.immediate) body.push(`if (Object.is(${value}, ${last})) return;`);
|
|
52
|
+
if (valueParameter && own === void 0) body.push(`const ${parameterBinding(valueParameter)}${annotation(valueParameter)} = ${value};`);
|
|
53
|
+
if (previousParameter && readsPrevious) {
|
|
54
|
+
const previous = first === void 0 ? last : item.array ? `(${first} ? [] : ${last}) as [${item.sources.map((_, index) => `(typeof ${last})[${index}] | undefined`).join(", ")}]` : `${first} ? undefined : ${last}`;
|
|
55
|
+
body.push(`const ${parameterBinding(previousParameter)}${annotation(previousParameter)} = ${defaulted(previousParameter, previous)};`);
|
|
56
|
+
}
|
|
57
|
+
if (first !== void 0) body.push(`${first} = false;`);
|
|
58
|
+
if (last !== void 0) body.push(`${last} = ${value};`);
|
|
59
|
+
if (item.immediate) body.push(`if (!${names.common("isPlatformBrowser")}(this.${names.member("platformId")})) return;`);
|
|
60
|
+
body.push(`${names.core("untracked")}(${callbackFunction(item.callback, plan, rules)});`);
|
|
61
|
+
const effect = names.core(item.post ? "afterRenderEffect" : "effect");
|
|
62
|
+
const cleanup = cleanupParameter?.name ?? "";
|
|
63
|
+
const ref = cleanupParameter ? {
|
|
64
|
+
name: plan.members.claim(`${hint}Watcher`),
|
|
65
|
+
type: names.type(item.post ? "AfterRenderRef" : "EffectRef"),
|
|
66
|
+
optional: true
|
|
67
|
+
} : void 0;
|
|
68
|
+
lines.push(`${ref ? `this.${ref.name} = ` : ""}${effect}((${cleanup}) => {\n${body.join("\n")}\n}, { injector: this.${names.member("injector")} });`);
|
|
69
|
+
return {
|
|
70
|
+
code: lines.join("\n"),
|
|
71
|
+
...ref ? {
|
|
72
|
+
ref,
|
|
73
|
+
destroy: `this.${ref.name}?.destroy();`
|
|
74
|
+
} : {}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The signal a source reads as it is, when it is one that changes only when its value differs:
|
|
79
|
+
* a `derived` ref, or a getter that reads one input or one `derived` value whole (`() => title`).
|
|
80
|
+
*/
|
|
81
|
+
function watchedSignal(source, plan) {
|
|
82
|
+
const binding = source.kind === "Ref" ? bindingById(plan, source.binding) : source.getter.expression && source.getter.body.refs.length === 1 && source.getter.body.refs[0].kind === "Binding" && source.getter.body.refs[0].span.start === source.getter.body.span.start && source.getter.body.refs[0].span.end === source.getter.body.span.end ? bindingById(plan, source.getter.body.refs[0].binding) : void 0;
|
|
83
|
+
if (binding === void 0) return void 0;
|
|
84
|
+
const form = formOf(plan, binding);
|
|
85
|
+
switch (form) {
|
|
86
|
+
case "input":
|
|
87
|
+
case "computed":
|
|
88
|
+
case "model":
|
|
89
|
+
case "result": return binding;
|
|
90
|
+
case "context":
|
|
91
|
+
case "instance":
|
|
92
|
+
case "component":
|
|
93
|
+
case "signal":
|
|
94
|
+
case "linked":
|
|
95
|
+
case "once":
|
|
96
|
+
case "value":
|
|
97
|
+
case "query":
|
|
98
|
+
case "method":
|
|
99
|
+
case "arrow":
|
|
100
|
+
case "field":
|
|
101
|
+
case "loop":
|
|
102
|
+
case "emit":
|
|
103
|
+
case "module":
|
|
104
|
+
case "resultValue": return;
|
|
105
|
+
default: return unreachable(form);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/** A watch source's value, read in the class. */
|
|
109
|
+
function sourceRead(source, plan, rules) {
|
|
110
|
+
switch (source.kind) {
|
|
111
|
+
case "Ref": return classRead(plan, bindingById(plan, source.binding));
|
|
112
|
+
case "Getter": {
|
|
113
|
+
const { getter } = source;
|
|
114
|
+
const body = rewriteCode(getter.body, plan.component, rules, "pure");
|
|
115
|
+
return getter.expression ? body : `(() => ${body})()`;
|
|
116
|
+
}
|
|
117
|
+
default: return unreachable(source);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/** `() => code`, an object literal or a sequence in parentheses. */
|
|
121
|
+
function arrow(code) {
|
|
122
|
+
return `() => ${/^\s*\{/.test(code) ? `(${code})` : code}`;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The callback as `untracked`'s function: `() => …` with its block or its expression, or, for an
|
|
126
|
+
* async callback, `async () => …`, whose promise nothing awaits, as Vue awaits none (its
|
|
127
|
+
* continuation runs outside any reactive context on every target).
|
|
128
|
+
*/
|
|
129
|
+
function callbackFunction(fn, plan, rules) {
|
|
130
|
+
const head = fn.async ? "async () => " : "() => ";
|
|
131
|
+
if (!fn.expression) return `${head}{\n${bodyStatements(fn, plan, rules)}\n}`;
|
|
132
|
+
const body = tidied(rewriteCode(fn.body, plan.component, rules, "client"), "expression");
|
|
133
|
+
return `${head}${/^\s*\{/.test(body) ? `(${body})` : body}`;
|
|
134
|
+
}
|
|
135
|
+
/** A parameter's binding as written: its name, or its pattern. */
|
|
136
|
+
function parameterBinding(parameter) {
|
|
137
|
+
return parameter.name ?? parameter.pattern?.code ?? "";
|
|
138
|
+
}
|
|
139
|
+
/** A parameter's type annotation as written, `: number`, or nothing. */
|
|
140
|
+
function annotation(parameter) {
|
|
141
|
+
return parameter?.type ? `: ${parameter.type.code}` : "";
|
|
142
|
+
}
|
|
143
|
+
/** A value with a parameter's default applied, as calling with `undefined` applies it. */
|
|
144
|
+
function defaulted(parameter, value) {
|
|
145
|
+
return parameter.default ? `${value} === undefined ? ${parameter.default.code} : ${value}` : value;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* `watchEffect(effect)`'s code for the constructor, or for `ngOnInit` (`late`), which is no
|
|
149
|
+
* injection context and names the component's injector.
|
|
150
|
+
*/
|
|
151
|
+
function watchEffectCode(item, plan, rules, names, late = false) {
|
|
152
|
+
const [cleanup] = item.effect.parameters;
|
|
153
|
+
const statements = bodyStatements(item.effect, plan, rules);
|
|
154
|
+
const body = item.effect.async ? `void (async () => {\n${statements}\n})();` : statements;
|
|
155
|
+
const effect = `${names.core("afterRenderEffect")}((${cleanup?.name ?? ""}) => {\n${body}\n}${injectorOption(names, late)});`;
|
|
156
|
+
if (!cleanup) return { code: effect };
|
|
157
|
+
const ref = {
|
|
158
|
+
name: plan.members.claim("renderEffect"),
|
|
159
|
+
type: names.type("AfterRenderRef"),
|
|
160
|
+
optional: late
|
|
161
|
+
};
|
|
162
|
+
return {
|
|
163
|
+
code: `this.${ref.name} = ${effect}`,
|
|
164
|
+
ref,
|
|
165
|
+
destroy: `this.${ref.name}${late ? "?." : "."}destroy();`
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* A lifecycle hook's code: `onMounted` for the constructor, or for `ngOnInit` (`late`), and
|
|
170
|
+
* `onUnmounted` for `ngOnDestroy`.
|
|
171
|
+
*/
|
|
172
|
+
function lifecycleCode(item, plan, rules, names, late = false) {
|
|
173
|
+
const body = bodyStatements(item.callback, plan, rules);
|
|
174
|
+
switch (item.hook) {
|
|
175
|
+
case "mounted": return { code: `${names.core("afterNextRender")}(${item.callback.async ? "async " : ""}() => {\n${body}\n}${injectorOption(names, late)});` };
|
|
176
|
+
case "unmounted": {
|
|
177
|
+
const run = item.callback.async ? `void (async () => {\n${body}\n})();` : hasReturn(item.callback) ? `(() => {\n${body}\n})();` : body;
|
|
178
|
+
return { destroy: `if (${names.common("isPlatformBrowser")}(this.${names.member("platformId")})) {\n${run}\n}` };
|
|
179
|
+
}
|
|
180
|
+
default: return unreachable(item.hook);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/** The options argument a hook registered in `ngOnInit` takes: the component's injector. */
|
|
184
|
+
function injectorOption(names, late) {
|
|
185
|
+
return late ? `, { injector: this.${names.member("injector")} }` : "";
|
|
186
|
+
}
|
|
187
|
+
export { annotation, arrow, defaulted, lifecycleCode, parameterBinding, watchEffectCode, watcherCode };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Target } from "@unframework.dev/codegen";
|
|
2
|
+
/**
|
|
3
|
+
* The Angular 22 target: standalone, zoneless components. The host element gets
|
|
4
|
+
* `display: contents`, so it adds no box of its own (decision D6, ADR-0010). `standalone` and
|
|
5
|
+
* OnPush change detection are Angular 22's defaults, so the metadata does not repeat them. It
|
|
6
|
+
* does repeat `preserveWhitespaces: false`, the default the template's layout is printed for
|
|
7
|
+
* (`angularDialect`): the component's own setting wins over a consumer's
|
|
8
|
+
* `angularCompilerOptions`, which would otherwise turn the indentation into text.
|
|
9
|
+
*
|
|
10
|
+
* Each prop is a public signal input (ADR-0034), each event an output of its name
|
|
11
|
+
* (ADR-0047); the setup's state, derived values and constants are signals and fields, its
|
|
12
|
+
* functions methods, its effects Angular's (see ./members.ts); the template reads signals
|
|
13
|
+
* through `@let` and each allowed global an expression reads as a protected member of the same
|
|
14
|
+
* name, since a template sees only its component's members.
|
|
15
|
+
*/
|
|
16
|
+
export declare const angular: Target;
|
|
17
|
+
export { angular as default };
|