@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/index.js
ADDED
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
import { outputName } from "./outputs.js";
|
|
2
|
+
import { ANGULAR_MEMBERS, bindingById, formOf, memberOf, moduleMember, planComponent, slotQuery } from "./plan.js";
|
|
3
|
+
import { classRules } from "./rules.js";
|
|
4
|
+
import { moduleReads } from "./modules.js";
|
|
5
|
+
import { emitComposable } from "./composable.js";
|
|
6
|
+
import { declaredTypes } from "./types.js";
|
|
7
|
+
import { firstUse, keyOf, planComposition, refObjectCode, slotDirectiveCode, tokenType } from "./composition.js";
|
|
8
|
+
import { directiveCode, scopeDirectiveCode } from "./directives.js";
|
|
9
|
+
import { planListeners } from "./listeners.js";
|
|
10
|
+
import { classMembers } from "./members.js";
|
|
11
|
+
import { planModels } from "./models.js";
|
|
12
|
+
import { fallthroughRoot, template, templateReads } from "./template.js";
|
|
13
|
+
import { Placeholders, addTypeImports, angularDialect, codeKind, codeNames, componentTypes, defineTarget, exportDeclaration, exportsOf, js, kebabCase, moduleHelperName, printExpression, printProgram, rewriteCode, scopeRoots, sourceNames, typeDeclarationCode, typeExportsCode, typeNames } from "@unframework.dev/codegen";
|
|
14
|
+
import { codeOf, functionsOf, walk } from "@unframework.dev/ir";
|
|
15
|
+
/** The note the emulated cells share: why a listener's option needs a directive. */
|
|
16
|
+
const OPTION_NOTE = "A template's `(event)` binding takes no listener options: the file declares an attribute directive per event and option (`[ufClickCapture]`) that listens with `Renderer2.listen` and re-emits the event (ADR-0047).";
|
|
17
|
+
/**
|
|
18
|
+
* The Angular 22 target: standalone, zoneless components. The host element gets
|
|
19
|
+
* `display: contents`, so it adds no box of its own (decision D6, ADR-0010). `standalone` and
|
|
20
|
+
* OnPush change detection are Angular 22's defaults, so the metadata does not repeat them. It
|
|
21
|
+
* does repeat `preserveWhitespaces: false`, the default the template's layout is printed for
|
|
22
|
+
* (`angularDialect`): the component's own setting wins over a consumer's
|
|
23
|
+
* `angularCompilerOptions`, which would otherwise turn the indentation into text.
|
|
24
|
+
*
|
|
25
|
+
* Each prop is a public signal input (ADR-0034), each event an output of its name
|
|
26
|
+
* (ADR-0047); the setup's state, derived values and constants are signals and fields, its
|
|
27
|
+
* functions methods, its effects Angular's (see ./members.ts); the template reads signals
|
|
28
|
+
* through `@let` and each allowed global an expression reads as a protected member of the same
|
|
29
|
+
* name, since a template sees only its component's members.
|
|
30
|
+
*/
|
|
31
|
+
const angular = defineTarget({
|
|
32
|
+
name: "angular",
|
|
33
|
+
framework: {
|
|
34
|
+
package: "@angular/core",
|
|
35
|
+
range: ">=22 <23"
|
|
36
|
+
},
|
|
37
|
+
capabilities: {
|
|
38
|
+
element: { support: "native" },
|
|
39
|
+
text: { support: "native" },
|
|
40
|
+
"static-attribute": { support: "native" },
|
|
41
|
+
listbox: { support: "native" },
|
|
42
|
+
interactivity: { support: "native" },
|
|
43
|
+
"event-capture": {
|
|
44
|
+
support: "emulated",
|
|
45
|
+
helper: "uf<Event>Capture",
|
|
46
|
+
note: OPTION_NOTE
|
|
47
|
+
},
|
|
48
|
+
"event-once": {
|
|
49
|
+
support: "emulated",
|
|
50
|
+
helper: "uf<Event>Once",
|
|
51
|
+
note: `${OPTION_NOTE} Beside another listener of its event on its element, which runs in attribute order with it in one template listener, it runs under a guard instead, \`once(clickOnce, $event) && …\`, a method that remembers the elements it ran on.`
|
|
52
|
+
},
|
|
53
|
+
"event-passive": {
|
|
54
|
+
support: "emulated",
|
|
55
|
+
helper: "uf<Event>Passive",
|
|
56
|
+
note: OPTION_NOTE
|
|
57
|
+
},
|
|
58
|
+
"event-semantics": { support: "native" },
|
|
59
|
+
"conditional-event-control": { support: "native" },
|
|
60
|
+
"use-id": {
|
|
61
|
+
support: "emulated",
|
|
62
|
+
helper: "nextId",
|
|
63
|
+
note: "Angular has no id API: a module counter makes each instance's ids, `uf-id-<component>-<n>`, as Angular Material makes its own."
|
|
64
|
+
},
|
|
65
|
+
"next-tick": {
|
|
66
|
+
support: "emulated",
|
|
67
|
+
helper: "nextTick",
|
|
68
|
+
note: "Angular has no `nextTick`, and renders a change in a later task: a private method renders the pending change in a microtask with `ApplicationRef.tick()`, as Vue does, and resolves after it (ADR-0048)."
|
|
69
|
+
},
|
|
70
|
+
"late-prop": { support: "native" },
|
|
71
|
+
props: { support: "native" },
|
|
72
|
+
interpolation: { support: "native" },
|
|
73
|
+
conditional: { support: "native" },
|
|
74
|
+
list: { support: "native" },
|
|
75
|
+
fragment: { support: "native" },
|
|
76
|
+
"bound-attribute": { support: "native" },
|
|
77
|
+
"class-binding": {
|
|
78
|
+
support: "emulated",
|
|
79
|
+
helper: "classNames",
|
|
80
|
+
note: "Angular's `[class]` reads no nested array, and drops an entry that holds a space, so a class part that may be an array or an object goes through an inline helper, `classNames`, that reads it as Vue's `normalizeClass` does (ADR-0065)."
|
|
81
|
+
},
|
|
82
|
+
"style-binding": {
|
|
83
|
+
support: "emulated",
|
|
84
|
+
helper: "styleObject",
|
|
85
|
+
note: "A style bound to a value is Angular's own `[style]`, but where an object spread merges it, on the root that takes a consumer's style and on a component or a `<component is>`, an inline helper splits a declaration list into an object as Vue does (ADR-0065)."
|
|
86
|
+
},
|
|
87
|
+
"attribute-spread": { support: "native" },
|
|
88
|
+
svg: { support: "native" },
|
|
89
|
+
component: { support: "native" },
|
|
90
|
+
"component-event": { support: "native" },
|
|
91
|
+
"default-slot": { support: "native" },
|
|
92
|
+
"named-slot": {
|
|
93
|
+
support: "emulated",
|
|
94
|
+
helper: "uf<Component><Slot>",
|
|
95
|
+
note: "Each named slot is a directive the component's file exports, `ng-template[uf<Component><Slot>]`, read with `contentChild` and rendered with `ngTemplateOutlet` (ADR-0056)."
|
|
96
|
+
},
|
|
97
|
+
"scoped-slot": {
|
|
98
|
+
support: "emulated",
|
|
99
|
+
helper: "uf<Component><Slot>",
|
|
100
|
+
note: "A scoped slot is a slot directive with `ngTemplateContextGuard`, so strict templates type the consumer's `let-` variables (ADR-0056)."
|
|
101
|
+
},
|
|
102
|
+
"slot-fallback": { support: "native" },
|
|
103
|
+
"default-slot-presence": {
|
|
104
|
+
support: "unsupported",
|
|
105
|
+
code: "UF4001",
|
|
106
|
+
severity: "error",
|
|
107
|
+
reason: "Angular has no API for whether `<ng-content>` received content, and forwarding the default slot would re-project `<ng-content />`, which always counts as content (ADR-0056)."
|
|
108
|
+
},
|
|
109
|
+
"slot-forwarding": {
|
|
110
|
+
support: "emulated",
|
|
111
|
+
helper: "uf<Component><Slot>",
|
|
112
|
+
note: "A forwarded named slot re-declares the child's slot template under `@if`, with the parent's template as its content (ADR-0056)."
|
|
113
|
+
},
|
|
114
|
+
model: { support: "native" },
|
|
115
|
+
"two-way-binding": { support: "native" },
|
|
116
|
+
"model-array": {
|
|
117
|
+
support: "emulated",
|
|
118
|
+
helper: "toggle",
|
|
119
|
+
note: "A checkbox group's array goes through an inline helper, `toggle`, and a multiple select's through `selectedValues`, as Vue's `vModelCheckbox` and `vModelSelect` do (ADR-0054)."
|
|
120
|
+
},
|
|
121
|
+
"model-modifiers": {
|
|
122
|
+
support: "emulated",
|
|
123
|
+
helper: "modelText",
|
|
124
|
+
note: "A `v-model`'s modifiers and a number control's cast follow Vue's `vModelText` through an inline helper, `modelText` (ADR-0054)."
|
|
125
|
+
},
|
|
126
|
+
fallthrough: {
|
|
127
|
+
support: "emulated",
|
|
128
|
+
helper: "fallthrough",
|
|
129
|
+
note: "A component that inherits `class` and `style` declares them as inputs, merges them into its root element and clears them from its host, which keeps only `display: contents` (ADR-0056)."
|
|
130
|
+
},
|
|
131
|
+
"contextual-root": {
|
|
132
|
+
support: "unsupported",
|
|
133
|
+
code: "UF4001",
|
|
134
|
+
severity: "error",
|
|
135
|
+
reason: "A root element HTML or ARIA ties to its parent (`li`, `tr`, `option`, …) renders inside Angular's host element, which breaks the parent's content model: axe reports `list` and `listitem`, and the HTML parser moves a host out of a table (ADR-0056)."
|
|
136
|
+
},
|
|
137
|
+
expose: { support: "native" },
|
|
138
|
+
context: { support: "native" },
|
|
139
|
+
"reactive-context": {
|
|
140
|
+
support: "emulated",
|
|
141
|
+
helper: "refObject",
|
|
142
|
+
note: "A provided ref is passed as an object with a `value` getter, `refObject`, which descendants read as it changes (ADR-0054)."
|
|
143
|
+
},
|
|
144
|
+
"dynamic-component": {
|
|
145
|
+
support: "emulated",
|
|
146
|
+
helper: "@switch",
|
|
147
|
+
note: "Angular has no `<component is>` over a known set in a strict template: an `@switch` renders each candidate (ADR-0054)."
|
|
148
|
+
},
|
|
149
|
+
"scoped-styles": { support: "native" },
|
|
150
|
+
"child-root-scope": {
|
|
151
|
+
support: "unsupported",
|
|
152
|
+
code: "UF4003",
|
|
153
|
+
severity: "info",
|
|
154
|
+
reason: "Each root node of a child binds an inline attribute directive, `[ufScope]`, that moves the scope attributes its styled parents put on its host onto the node, so their rules reach it. But the root renders inside its host element, which selectors see: a parent's rule that reaches the root through a combinator (`>`, `+`, `~`) or a structural pseudo-class (`:first-child`, `:nth-child()`, …) matches the host's place, not the root's, and may match differently. A root Angular binds nothing on (`{{` in an attribute, ADR-0037) takes no scope attribute."
|
|
155
|
+
},
|
|
156
|
+
"slotted-styles": { support: "native" },
|
|
157
|
+
"sheet-order": {
|
|
158
|
+
support: "unsupported",
|
|
159
|
+
code: "UF4002",
|
|
160
|
+
severity: "info",
|
|
161
|
+
reason: "Angular adds a component's sheet to the page when the component first renders (`styleUrl`, through `SharedStylesHost`): a parent's before its children's, and siblings in the order they render, where Vue loads them in import order, a child's first. Where two sheets' rules of equal specificity meet on one element (a child's root, content filled into a slot, `:deep()`, `:global()`), the winner can differ."
|
|
162
|
+
},
|
|
163
|
+
"generic-components": { support: "native" },
|
|
164
|
+
"generic-slot-props": {
|
|
165
|
+
support: "unsupported",
|
|
166
|
+
code: "UF4004",
|
|
167
|
+
severity: "info",
|
|
168
|
+
reason: "Angular infers a component's type parameter from its inputs alone, and a slot's directive cannot infer it, so a scoped slot's context takes the parameter's constraint, where the other targets type the consumer's fill by the type argument (ADR-0068)."
|
|
169
|
+
},
|
|
170
|
+
composables: { support: "native" },
|
|
171
|
+
"composable-effect-order": { support: "native" }
|
|
172
|
+
},
|
|
173
|
+
emit(component, context) {
|
|
174
|
+
return [emitComponent(component, context)];
|
|
175
|
+
},
|
|
176
|
+
emitComposable
|
|
177
|
+
});
|
|
178
|
+
/** A component's output file. */
|
|
179
|
+
function emitComponent(component, context) {
|
|
180
|
+
const { module } = context;
|
|
181
|
+
const plan = planComponent(component, module);
|
|
182
|
+
const { imports } = plan;
|
|
183
|
+
const composition = planComposition(plan);
|
|
184
|
+
for (const [local, selector] of composition.clashes) context.report({
|
|
185
|
+
code: "UF1104",
|
|
186
|
+
severity: "error",
|
|
187
|
+
message: `\`${local}\` is written \`<${selector}>\` on Angular, as another component of its name is: Angular would match both. Rename one of them.`,
|
|
188
|
+
span: firstUse(component, local)
|
|
189
|
+
});
|
|
190
|
+
for (const { fill, child, attribute, other } of composition.slotClashes) context.report({
|
|
191
|
+
code: "UF1104",
|
|
192
|
+
severity: "error",
|
|
193
|
+
message: `${child} is filled through \`${attribute}\` on Angular, as ${other} is: Angular would match both. Rename one of the slots.`,
|
|
194
|
+
span: fill.span
|
|
195
|
+
});
|
|
196
|
+
const models = planModels(plan);
|
|
197
|
+
const decoratorName = imports.add("@angular/core", "Component");
|
|
198
|
+
const className = imports.claim(component.name);
|
|
199
|
+
const placeholders = new Placeholders();
|
|
200
|
+
const nextTick = codeOf(component).some(({ code }) => code.refs.some((reference) => reference.kind === "Api")) ? plan.members.claim("nextTick") : void 0;
|
|
201
|
+
const rules = classRules(plan, nextTick);
|
|
202
|
+
const closure = componentTypes(component, module);
|
|
203
|
+
const listeners = planListeners(plan, nextTick, models.writes);
|
|
204
|
+
const reads = templateReads(plan, listeners);
|
|
205
|
+
const templateBindings = [
|
|
206
|
+
...reads.read,
|
|
207
|
+
...reads.keyed,
|
|
208
|
+
...listeners.members
|
|
209
|
+
].map((id) => bindingById(plan, id));
|
|
210
|
+
const templateMembers = new Set(templateBindings.filter((binding) => formOf(plan, binding) !== "module").map((binding) => memberOf(plan, binding).split(".")[0]));
|
|
211
|
+
const moduleMembers = new Map(templateBindings.filter((binding) => formOf(plan, binding) === "module").map((binding) => [binding.name, moduleMember(plan, binding)]));
|
|
212
|
+
for (const method of listeners.methods) templateMembers.add(method.name);
|
|
213
|
+
for (const guard of listeners.guards) templateMembers.add(guard);
|
|
214
|
+
for (const slot of reads.slots) templateMembers.add(slotQuery(slot));
|
|
215
|
+
const root = fallthroughRoot(component.render);
|
|
216
|
+
const fallthrough = component.inheritAttrs !== false && root !== void 0 && (root.kind === "Component" || angularDialect.literalRegion?.(root) === void 0);
|
|
217
|
+
const clearsHost = fallthrough || component.inheritAttrs === false;
|
|
218
|
+
reportApiClashes(component, composition, fallthrough, context);
|
|
219
|
+
const refObject = component.setup.some((item) => (item.kind === "Provide" || item.kind === "Inject" && item.fallback) && keyOf(plan, item.key).ref) ? imports.claim("refObject") : void 0;
|
|
220
|
+
const provided = providers(plan, rules, composition, className, refObject);
|
|
221
|
+
const nextId = component.setup.some((item) => item.kind === "Id") ? imports.claim("nextId") : void 0;
|
|
222
|
+
const directives = listeners.directives.map((spec) => directiveCode(spec, imports, placeholders));
|
|
223
|
+
const roots = scopedRoots(component);
|
|
224
|
+
const scope = roots.size ? {
|
|
225
|
+
...scopeNames(component, module, imports),
|
|
226
|
+
host: plan.members.claim("host")
|
|
227
|
+
} : void 0;
|
|
228
|
+
const { members, implements: interfaces } = classMembers({
|
|
229
|
+
plan,
|
|
230
|
+
placeholders,
|
|
231
|
+
declarations: closure,
|
|
232
|
+
rules,
|
|
233
|
+
templateMembers,
|
|
234
|
+
globals: reads.globals,
|
|
235
|
+
moduleMembers,
|
|
236
|
+
handlers: listeners.methods,
|
|
237
|
+
guards: listeners.guards,
|
|
238
|
+
...listeners.once === void 0 ? {} : { once: listeners.once },
|
|
239
|
+
...nextTick === void 0 ? {} : { nextTick },
|
|
240
|
+
...nextId === void 0 ? {} : { nextId },
|
|
241
|
+
...refObject === void 0 ? {} : { refObject },
|
|
242
|
+
slots: composition.slots,
|
|
243
|
+
fallthrough,
|
|
244
|
+
keyNames: composition.keyNames,
|
|
245
|
+
external: provided.external,
|
|
246
|
+
helpers: models.helpers,
|
|
247
|
+
...models.server === void 0 ? {} : { server: models.server }
|
|
248
|
+
});
|
|
249
|
+
const metadata = js.objectExpression([
|
|
250
|
+
["selector", js.stringLiteral(`uf-${kebabCase(component.name)}`)],
|
|
251
|
+
...composition.imports.length || directives.length || scope ? [["imports", js.arrayExpression([
|
|
252
|
+
...composition.imports,
|
|
253
|
+
...listeners.directives.map(({ className: name }) => name),
|
|
254
|
+
...scope ? [scope.className] : []
|
|
255
|
+
].map((name) => js.identifier(name)))]] : [],
|
|
256
|
+
...provided.code.length ? [["providers", js.arrayExpression(provided.code.map((code) => placeholders.expression(code)))]] : [],
|
|
257
|
+
["host", clearsHost ? js.objectExpression([["[attr.class]", js.stringLiteral("null")], ["[attr.style]", js.stringLiteral("'display: contents;'")]]) : js.objectExpression([["style", js.stringLiteral("display: contents")]])],
|
|
258
|
+
["preserveWhitespaces", js.booleanLiteral(false)],
|
|
259
|
+
["template", js.templateLiteral(`\n${template(plan, listeners, reads, composition, models, fallthrough, scope && {
|
|
260
|
+
roots,
|
|
261
|
+
host: scope.host
|
|
262
|
+
})}\n `)],
|
|
263
|
+
...component.styles ? [["styleUrl", js.stringLiteral(`./${module.file.slice(module.file.lastIndexOf("/") + 1)}.css`)], ["encapsulation", js.memberExpression(js.identifier(imports.add("@angular/core", "ViewEncapsulation")), "None")]] : []
|
|
264
|
+
]);
|
|
265
|
+
const decorator = () => `@${printExpression(js.callExpression(js.identifier(decoratorName), [metadata]))}`;
|
|
266
|
+
const hostMember = scope && [js.propertyDefinition(scope.host, placeholders.expression(`${imports.add("@angular/core", "inject")}<${imports.add("@angular/core", "ElementRef")}<HTMLElement>>(${imports.add("@angular/core", "ElementRef")}).nativeElement`), {
|
|
267
|
+
accessibility: "protected",
|
|
268
|
+
readonly: true
|
|
269
|
+
})];
|
|
270
|
+
const declaration = js.classDeclaration(className, [], [...members, ...hostMember ?? []], { implements: interfaces });
|
|
271
|
+
declaration.typeParameters = typeParameters(component, placeholders);
|
|
272
|
+
const statements = exportDeclaration(className, declaration, exportsOf(component.name, module.exports));
|
|
273
|
+
const types = declaredTypes(closure, typeRoots(component, module));
|
|
274
|
+
const exported = new Map([
|
|
275
|
+
...types.filter((type) => type.exported).map(({ name }) => [name, `the type`]),
|
|
276
|
+
...composition.keys.map(({ name }) => [name, `the injection key`]),
|
|
277
|
+
...exportsOf(component.name, module.exports).map(({ name }) => [name, `the component`])
|
|
278
|
+
]);
|
|
279
|
+
for (const { slot, exported: name } of composition.slots) {
|
|
280
|
+
const other = exported.get(name);
|
|
281
|
+
if (other === void 0) continue;
|
|
282
|
+
context.report({
|
|
283
|
+
code: "UF2003",
|
|
284
|
+
severity: "error",
|
|
285
|
+
message: `\`${name}\` names the directive of \`${component.name}\`'s \`${slot.name}\` slot on Angular, and ${other} \`${name}\` too. Rename one of them.`,
|
|
286
|
+
span: slot.span
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
const declarations = [
|
|
290
|
+
...composition.keys.map((key) => `export const ${key.name} = new ${imports.add("@angular/core", "InjectionToken")}<${tokenType(key)}>(${JSON.stringify(key.description)});\n`),
|
|
291
|
+
...refObject === void 0 ? [] : [refObjectCode(refObject)],
|
|
292
|
+
...[...models.helpers, ...models.support].map(({ code }) => `${code}\n`),
|
|
293
|
+
...composition.slots.map((slot) => slotDirectiveCode(component.name, slot, imports.add("@angular/core", "Directive"), component.typeParameters)),
|
|
294
|
+
...scope ? [scopeDirectiveCode(scope, imports)] : []
|
|
295
|
+
];
|
|
296
|
+
if (models.helpers.some(({ code }) => code.includes("WritableSignal"))) imports.add("@angular/core", "WritableSignal", { type: true });
|
|
297
|
+
addTypeImports(component, module, imports, kebabCase);
|
|
298
|
+
const own = new Set(types.map(({ name }) => name));
|
|
299
|
+
const constants = moduleReads(component.bindings, module, imports, kebabCase);
|
|
300
|
+
const typeCode = [...[...types, ...constants.types.filter(({ name }) => !own.has(name))].map((type) => ({
|
|
301
|
+
code: typeDeclarationCode(type),
|
|
302
|
+
span: type.span
|
|
303
|
+
})), ...typeExportsCode(component, module, kebabCase)].toSorted((a, b) => a.span.start - b.span.start);
|
|
304
|
+
const contents = placeholders.print(() => [
|
|
305
|
+
printProgram(js.program(imports.toDeclarations())),
|
|
306
|
+
...typeCode.map(({ code }) => `${code}\n`),
|
|
307
|
+
...constants.constants.length ? [`${constants.constants.join("\n")}\n`] : [],
|
|
308
|
+
...nextId === void 0 ? [] : [`let ${nextId} = 0;\n`],
|
|
309
|
+
...declarations,
|
|
310
|
+
...directives.map(({ decorator: written, statements: declared }) => `${written}\n${spaced(printProgram(js.program(declared)))}`),
|
|
311
|
+
decorator(),
|
|
312
|
+
spaced(printProgram(js.program(statements)))
|
|
313
|
+
].join("\n"));
|
|
314
|
+
return {
|
|
315
|
+
path: `${kebabCase(component.name)}.ts`,
|
|
316
|
+
contents
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
/** A span's key in a set of root nodes. */
|
|
320
|
+
function spanKey({ start, end }) {
|
|
321
|
+
return `${start}:${end}`;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* The component's root nodes that take its styled parents' scope attributes (`scopeRoots`, the
|
|
325
|
+
* child-root rule, ADR-0063), by {@link spanKey}: all but an element Angular binds nothing on
|
|
326
|
+
* (`{{` in an attribute, ADR-0037), which takes no fallthrough either.
|
|
327
|
+
*/
|
|
328
|
+
function scopedRoots(component) {
|
|
329
|
+
const roots = new Set(scopeRoots(component).map(spanKey));
|
|
330
|
+
walk(component.render, { enter(node) {
|
|
331
|
+
if (node.kind === "Element" && angularDialect.literalRegion?.(node) !== void 0) roots.delete(spanKey(node.span));
|
|
332
|
+
} });
|
|
333
|
+
return roots;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* The names of the scope directive's code. A source's components are joined into one module
|
|
337
|
+
* where it is loaded (ADR-0053): each claims them from bases of its own (`ScopeAttributes_1`),
|
|
338
|
+
* past the names any other component declares.
|
|
339
|
+
*/
|
|
340
|
+
function scopeNames(component, module, imports) {
|
|
341
|
+
const index = module.components.findIndex((each) => each.name === component.name);
|
|
342
|
+
const reserved = module.components.map((each) => sourceNames(each, module));
|
|
343
|
+
const claim = (helper) => {
|
|
344
|
+
const { base, others } = moduleHelperName(helper, reserved, index);
|
|
345
|
+
imports.reserve(...others);
|
|
346
|
+
imports.scope.rebase(helper, base);
|
|
347
|
+
return imports.claim(helper);
|
|
348
|
+
};
|
|
349
|
+
return {
|
|
350
|
+
className: claim("ScopeAttributes"),
|
|
351
|
+
scopes: claim("scopes")
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Reports each name of the component's API that another name of it takes on Angular (UF2003).
|
|
356
|
+
* Angular declares a class member for each prop, output, exposed function, slot query and
|
|
357
|
+
* lifecycle hook, and names each input and output in a consumer's template: a model's own name
|
|
358
|
+
* and its `Change` output (`value`, `valueChange`), and an event's output name (`ufChange` for
|
|
359
|
+
* `change`). The source's names are the component's API, which the target keeps as they are,
|
|
360
|
+
* so a clash is reported at the later declaration rather than renamed. A binding is no API:
|
|
361
|
+
* ./plan.ts renames it.
|
|
362
|
+
*/
|
|
363
|
+
function reportApiClashes(component, composition, fallthrough, context) {
|
|
364
|
+
const taken = /* @__PURE__ */ new Map();
|
|
365
|
+
const key = ({ space, name }) => `${space} ${name}`;
|
|
366
|
+
const declare = (names) => {
|
|
367
|
+
for (const name of names) if (!taken.has(key(name))) taken.set(key(name), name.what);
|
|
368
|
+
};
|
|
369
|
+
for (const name of ANGULAR_MEMBERS) declare([{
|
|
370
|
+
space: "member",
|
|
371
|
+
name,
|
|
372
|
+
what: name === "constructor" ? "the class's constructor" : `the lifecycle hook \`${name}\``
|
|
373
|
+
}]);
|
|
374
|
+
for (const { slot } of composition.slots) {
|
|
375
|
+
const what = `the query of the \`${slot.name}\` slot`;
|
|
376
|
+
declare([{
|
|
377
|
+
space: "member",
|
|
378
|
+
name: slotQuery(slot.name),
|
|
379
|
+
what
|
|
380
|
+
}]);
|
|
381
|
+
}
|
|
382
|
+
if (fallthrough) for (const name of ["class", "style"]) {
|
|
383
|
+
const what = `the input of the consumer's \`${name}\``;
|
|
384
|
+
declare([{
|
|
385
|
+
space: "member",
|
|
386
|
+
name,
|
|
387
|
+
what
|
|
388
|
+
}, {
|
|
389
|
+
space: "input",
|
|
390
|
+
name,
|
|
391
|
+
what
|
|
392
|
+
}]);
|
|
393
|
+
}
|
|
394
|
+
const source = [
|
|
395
|
+
...component.props.map(({ name, span }) => {
|
|
396
|
+
const what = `the prop \`${name}\``;
|
|
397
|
+
return {
|
|
398
|
+
kind: "prop",
|
|
399
|
+
label: name,
|
|
400
|
+
span,
|
|
401
|
+
names: [{
|
|
402
|
+
space: "member",
|
|
403
|
+
name,
|
|
404
|
+
what
|
|
405
|
+
}, {
|
|
406
|
+
space: "input",
|
|
407
|
+
name,
|
|
408
|
+
what
|
|
409
|
+
}]
|
|
410
|
+
};
|
|
411
|
+
}),
|
|
412
|
+
...component.setup.flatMap((item) => item.kind === "Model" ? [{
|
|
413
|
+
kind: "model",
|
|
414
|
+
label: item.name,
|
|
415
|
+
span: item.span,
|
|
416
|
+
names: [{
|
|
417
|
+
space: "input",
|
|
418
|
+
name: item.name,
|
|
419
|
+
what: `the model \`${item.name}\``
|
|
420
|
+
}, {
|
|
421
|
+
space: "output",
|
|
422
|
+
name: `${item.name}Change`,
|
|
423
|
+
what: `the output of the model \`${item.name}\``
|
|
424
|
+
}]
|
|
425
|
+
}] : []),
|
|
426
|
+
...(component.emits?.events ?? []).map(({ name: event, span }) => {
|
|
427
|
+
const name = outputName(event);
|
|
428
|
+
const what = name === event ? `the output of the event \`${event}\`` : `the output \`${name}\` of the event \`${event}\``;
|
|
429
|
+
return {
|
|
430
|
+
kind: "event",
|
|
431
|
+
label: event,
|
|
432
|
+
span,
|
|
433
|
+
names: [{
|
|
434
|
+
space: "member",
|
|
435
|
+
name,
|
|
436
|
+
what
|
|
437
|
+
}, {
|
|
438
|
+
space: "output",
|
|
439
|
+
name,
|
|
440
|
+
what
|
|
441
|
+
}]
|
|
442
|
+
};
|
|
443
|
+
}),
|
|
444
|
+
...(component.exposes?.functions ?? []).map((id) => {
|
|
445
|
+
const { name, span } = component.bindings.find((binding) => binding.id === id);
|
|
446
|
+
return {
|
|
447
|
+
kind: "function",
|
|
448
|
+
label: name,
|
|
449
|
+
span,
|
|
450
|
+
names: [{
|
|
451
|
+
space: "member",
|
|
452
|
+
name,
|
|
453
|
+
what: `the exposed function \`${name}\``
|
|
454
|
+
}]
|
|
455
|
+
};
|
|
456
|
+
})
|
|
457
|
+
];
|
|
458
|
+
for (const { kind, label, span, names } of source) {
|
|
459
|
+
const clash = names.find((name) => taken.has(key(name)));
|
|
460
|
+
if (clash === void 0) {
|
|
461
|
+
declare(names);
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
const named = clash.name === label ? `\`${label}\`` : `\`${clash.name}\`, ${clash.what},`;
|
|
465
|
+
context.report({
|
|
466
|
+
code: "UF2003",
|
|
467
|
+
severity: "error",
|
|
468
|
+
message: `${named} names ${taken.get(key(clash))} on Angular. Rename the ${kind}.`,
|
|
469
|
+
span
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* A provider per key the setup provides (ADR-0054): the value itself where it reads nothing of
|
|
475
|
+
* the component (`useValue`), or a factory that reads it from the component's instance, which a
|
|
476
|
+
* descendant's injector creates once the inputs are set; a key of a ref gets `refObject` over the
|
|
477
|
+
* signal. The members the factories read are public: they are read from outside the class.
|
|
478
|
+
*/
|
|
479
|
+
function providers(plan, rules, composition, className, refObject) {
|
|
480
|
+
const external = /* @__PURE__ */ new Set();
|
|
481
|
+
const code = [];
|
|
482
|
+
const instance = plan.locals.claim(`${className.charAt(0).toLowerCase()}${className.slice(1)}`);
|
|
483
|
+
for (const item of plan.component.setup) {
|
|
484
|
+
if (item.kind !== "Provide") continue;
|
|
485
|
+
for (const reference of item.value.refs) if (reference.kind === "Binding") external.add(bindingById(plan, reference.binding).name);
|
|
486
|
+
const written = rewriteCode(item.value, plan.component, rules, "pure");
|
|
487
|
+
const value = keyOf(plan, item.key).ref ? `${refObject}(${written})` : written;
|
|
488
|
+
const key = composition.keyNames.get(item.key) ?? item.key;
|
|
489
|
+
const reads = value.split("this.").length - 1;
|
|
490
|
+
const inject = () => `${plan.imports.add("@angular/core", "inject")}(${className})`;
|
|
491
|
+
code.push(reads === 0 ? `{ provide: ${key}, useValue: ${value} }` : reads === 1 ? `{ provide: ${key}, useFactory: () => ${value.replace("this.", `${inject()}.`)} }` : `{ provide: ${key}, useFactory: () => { const ${instance} = ${inject()}; return ${value.replaceAll("this.", `${instance}.`)}; } }`);
|
|
492
|
+
}
|
|
493
|
+
return {
|
|
494
|
+
code,
|
|
495
|
+
external
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* The names of the types the output reads: its inputs' and outputs' types, its type parameters'
|
|
500
|
+
* constraints and defaults, and every name the setup's code, annotations and functions read
|
|
501
|
+
* (`ref<Item[]>`, `as Item`, `(item: Item)`). An input is typed by its member, so the props type
|
|
502
|
+
* itself is printed only where another declaration declares a member, which an input reads from
|
|
503
|
+
* it by indexed access (`propType`).
|
|
504
|
+
*/
|
|
505
|
+
function typeRoots(component, module) {
|
|
506
|
+
const names = /* @__PURE__ */ new Set();
|
|
507
|
+
const add = (found) => {
|
|
508
|
+
for (const name of found) names.add(name);
|
|
509
|
+
};
|
|
510
|
+
const declared = new Set(module.types.map(({ name }) => name));
|
|
511
|
+
for (const prop of component.props) add(typeNames(prop.declaredBy && component.propsParameter ? component.propsParameter.type : prop.type));
|
|
512
|
+
for (const parameter of component.typeParameters ?? []) {
|
|
513
|
+
if (parameter.constraint) add(typeNames(parameter.constraint));
|
|
514
|
+
if (parameter.default) add(typeNames(parameter.default));
|
|
515
|
+
}
|
|
516
|
+
for (const event of component.emits?.events ?? []) for (const parameter of event.parameters) add(typeNames(parameter.type));
|
|
517
|
+
for (const item of component.setup) if ("type" in item && item.type) add(typeNames(item.type));
|
|
518
|
+
for (const { code } of codeOf(component)) add(codeNames(code.code, codeKind(code, component)));
|
|
519
|
+
for (const { function: fn } of functionsOf(component)) {
|
|
520
|
+
for (const parameter of fn.parameters) if (parameter.type) add(typeNames(parameter.type));
|
|
521
|
+
if (fn.returnType) add(typeNames(fn.returnType));
|
|
522
|
+
}
|
|
523
|
+
return new Set([...names].filter((name) => declared.has(name)));
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* A generic component's type parameters, `class List<T extends Item = Item>` (ADR-0068): a
|
|
527
|
+
* generic class, whose parameters ngtsc infers from a consumer's inputs, its constraints and
|
|
528
|
+
* defaults copied as written. `null` for a component without them.
|
|
529
|
+
*/
|
|
530
|
+
function typeParameters(component, placeholders) {
|
|
531
|
+
if (!component.typeParameters?.length) return null;
|
|
532
|
+
const type = (text) => text ? placeholders.type(text.code) : null;
|
|
533
|
+
return {
|
|
534
|
+
type: "TSTypeParameterDeclaration",
|
|
535
|
+
params: component.typeParameters.map((parameter) => ({
|
|
536
|
+
type: "TSTypeParameter",
|
|
537
|
+
name: js.bindingIdentifier(parameter.name),
|
|
538
|
+
constraint: type(parameter.constraint),
|
|
539
|
+
default: type(parameter.default),
|
|
540
|
+
in: false,
|
|
541
|
+
out: false,
|
|
542
|
+
const: false,
|
|
543
|
+
start: 0,
|
|
544
|
+
end: 0
|
|
545
|
+
})),
|
|
546
|
+
start: 0,
|
|
547
|
+
end: 0
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* A printed class with a blank line before each of its methods, as Angular code sets them apart
|
|
552
|
+
* from the fields and from each other (oxfmt keeps a blank line, and adds none). Read before the
|
|
553
|
+
* placeholders are spliced: a member starts at the class's indentation, and a method's line ends
|
|
554
|
+
* with its body's `{` where a field's holds ` = `.
|
|
555
|
+
*/
|
|
556
|
+
function spaced(printed) {
|
|
557
|
+
return printed.replace(/\n(?= {2}[^\s}][^\n]*\{\n)(?! {2}[^\n]* = )/g, "\n\n");
|
|
558
|
+
}
|
|
559
|
+
export { angular, angular as default };
|