@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/README.md
CHANGED
|
@@ -1,3 +1,427 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @unframework.dev/target-angular
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Angular 22 target of the Unframework compiler: standalone, zoneless components with inline
|
|
4
|
+
templates, whose host element has `display: contents` (decision D6, ADR-0010).
|
|
5
|
+
|
|
6
|
+
## Output
|
|
7
|
+
|
|
8
|
+
One `<kebab-name>.ts` file per component (plan §6), in this order: the imports from
|
|
9
|
+
`@angular/core` and `@angular/common`, of the children's output files, of the composables'
|
|
10
|
+
(`./use-toggle`, ADR-0069) and of the values the source imports from `.ts` modules, each under a
|
|
11
|
+
name no source name takes; the type declarations; the module's constants the component reads, and
|
|
12
|
+
those they read, as written (`const GAP = sizes.sm * 2;`, ADR-0070); `let nextId = 0;` when the
|
|
13
|
+
component makes ids; the
|
|
14
|
+
injection tokens of the module's keys; the inline helpers (`refObject`, the native controls'
|
|
15
|
+
`modelText`, `toggle`, `selectedValues`, `optionText`); the slot directives; the directives its listeners'
|
|
16
|
+
options need; the component.
|
|
17
|
+
|
|
18
|
+
- **Types.** The source's type declarations the output reads (its inputs' and outputs' types, the
|
|
19
|
+
setup's annotations and code) and every exported one, copied as written and exported as the
|
|
20
|
+
source exports them. An input is typed by its member's type, so a props type the source does
|
|
21
|
+
not export is left out (it would be declared and never used), unless an input reads a member
|
|
22
|
+
from it (below).
|
|
23
|
+
- **Imported types** (ADR-0067). The types the source imports, by name, where the output reads
|
|
24
|
+
them: from a `.ts` module or a package as written, `import type { BadgeProps } from "./types"`,
|
|
25
|
+
and from a child's output, `import Field, { type FieldProps } from "./field"`. The output of the
|
|
26
|
+
module's main component also writes the module's type exports, in source order among its
|
|
27
|
+
types (ADR-0068): `export type { Tone } from "./types"`, and an exported type no component
|
|
28
|
+
reaches.
|
|
29
|
+
- **Inputs.** Every prop is a public signal input, whether the template reads it or not (a
|
|
30
|
+
prop is the component's API, and Angular reports a value for an undeclared input):
|
|
31
|
+
`input.required<T>()` when required, `input<T>()` when optional, and
|
|
32
|
+
`input<T, T | undefined>(default, { transform })` with a default, whose transform is
|
|
33
|
+
`(value) => (value === undefined ? default : value)`: an explicit `undefined` takes the
|
|
34
|
+
default, as JavaScript's destructuring does, and `null` stays a value. A member another
|
|
35
|
+
declaration declares (inherited, intersected, picked or imported) is written in that file's
|
|
36
|
+
names, so its input reads it from the props type by indexed access (ADR-0067):
|
|
37
|
+
`input.required<BadgeProps["label"]>()`, and with a default
|
|
38
|
+
`input<Exclude<BadgeProps["tone"], undefined>, BadgeProps["tone"]>(…)`.
|
|
39
|
+
- **Generic components** (ADR-0068). A generic component is a generic class,
|
|
40
|
+
`export default class List<T extends Item = Item>`, its constraints and defaults as written,
|
|
41
|
+
whose type parameters ngtsc infers from a consumer's inputs. A select's `v-model` of a state of
|
|
42
|
+
such a type takes the chosen value through the template's own cast,
|
|
43
|
+
`this.value.set($any(valueField.value))`, since a template writes no `as`.
|
|
44
|
+
- **Outputs.** Every event `defineEmits` declares is a public `output()` of its name (ADR-0012),
|
|
45
|
+
or, for a name angular-eslint's `no-output-native` knows as a native DOM event, `uf` and the name
|
|
46
|
+
in PascalCase (`change` → `ufChange`, ADR-0056), which the native event bubbling out of the
|
|
47
|
+
component through its host cannot reach (`src/outputs.ts`):
|
|
48
|
+
an `output()` emits one value, so the payload is nothing for an event without members
|
|
49
|
+
(`output<void>()`, `emit()`), the value for one required member (`output<number>()`), and the
|
|
50
|
+
named tuple of exactly the arguments given otherwise (`output<[path: string, note?: string]>()`,
|
|
51
|
+
`emit([path])`), which the mount adapter spreads back (ADR-0047). The output keeps the event's
|
|
52
|
+
name, which is the component's API, also where a setup binding has it (`function save() {
|
|
53
|
+
emit("save") }`): the class's own member takes another name instead (`src/plan.ts`,
|
|
54
|
+
`withOutputNames`), `onSave` for a local function, `fieldElement` for a template ref and
|
|
55
|
+
`currentPage` for any other value, since angular-eslint's `no-output-rename` rejects an alias.
|
|
56
|
+
- **Template.** One `@let name = this.name();` per signal the template reads (an input, a
|
|
57
|
+
state, a derived value, a setup-once constant: props first, then the setup's in source order),
|
|
58
|
+
then the markup of the Angular dialect (`@unframework.dev/codegen`'s `angularDialect`): `{{ }}`,
|
|
59
|
+
`@if`/`@for`, `[attr.x]` bindings, a static `class` beside one `[class]`, a static `style`
|
|
60
|
+
beside `[style.x]`, `#name` for a template ref. The template reads signals through those
|
|
61
|
+
variables because Angular's type checker narrows a template variable as TypeScript narrows a
|
|
62
|
+
local, but never a signal call, so every expression type-checks as it does in the source. A
|
|
63
|
+
`track` expression may read only its item, `$index` and the component's members, so a signal
|
|
64
|
+
a key reads is read there as `this.label()`. Constants, ids and methods are members, read by
|
|
65
|
+
name.
|
|
66
|
+
- **Members.** A template sees only its component's members. So each allowed global the template
|
|
67
|
+
reads (`Math`, `String`) is a `protected readonly` member of the same name. So is each module
|
|
68
|
+
value the template reads, a constant or an import: `protected readonly SIZES = SIZES;`. Class
|
|
69
|
+
code reads a module value by its module name (ADR-0070). Where an output, the `class` and
|
|
70
|
+
`style` inputs or a slot's query takes that name, the template's member takes another
|
|
71
|
+
(`currentSave`). A template statement that calls a module's function is `void`
|
|
72
|
+
(`(click)="void track('z')"`), since nothing here says what it returns. A member the template
|
|
73
|
+
uses is `protected`; one only the class uses is `private`.
|
|
74
|
+
- **Names.** Angular's compiler accepts a signal input only in a class whose decorator is
|
|
75
|
+
imported as `Component`, so a component named `Component` takes the class name `Component_1`
|
|
76
|
+
and is exported under its own; an import a source name takes is imported under another name
|
|
77
|
+
(`input as input_1`).
|
|
78
|
+
|
|
79
|
+
### The setup (ADR-0045 to ADR-0049)
|
|
80
|
+
|
|
81
|
+
The class declares the setup's bindings as members, in source order (`src/members.ts`); class
|
|
82
|
+
code reads each through `this.` (`src/rules.ts`): a signal by calling it, a template ref's
|
|
83
|
+
element as `this.field()?.nativeElement`. The source's template ref value is `T | null`, so a read
|
|
84
|
+
that keeps, passes, returns or compares the element is `this.field()?.nativeElement ?? null`; one
|
|
85
|
+
that only tests it, asserts it or reads through it (`.focus()`) needs no `?? null`.
|
|
86
|
+
|
|
87
|
+
TypeScript narrows a ref's value and a prop where a condition shows it present (`if
|
|
88
|
+
(selected.value) emit("select", selected.value)`), but never a call, which is how the class reads
|
|
89
|
+
both. So each read the analyser marks narrowed (`BindingReference.narrowed`, ADR-0046) asserts its
|
|
90
|
+
path present (`src/narrowing.ts`): `this.selected()!`, `this.draft().email!` for a member path,
|
|
91
|
+
and `this.field()!.nativeElement` for a template ref (inside `?.nativeElement` the assertion would
|
|
92
|
+
leave the chain optional). A template statement asserts a state's read so too (`this.selected()!`);
|
|
93
|
+
the template itself, which reads through `@let`, and a template statement's input need none.
|
|
94
|
+
|
|
95
|
+
| Source | Angular output |
|
|
96
|
+
| ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
|
97
|
+
| `const count = ref(0)` | `signal(0)`; `ref<T>()` is `signal<T \| undefined>(undefined)` |
|
|
98
|
+
| `ref(initial)` reading an input | `linkedSignal(() => untracked(() => this.initial()))`, read once in `ngOnInit` |
|
|
99
|
+
| `count.value = x`, `count.value++` | `this.count.set(x)`, `this.count.update((count) => count + 1)`; `+= value` reading a local or a field: `set(this.count() + …)` |
|
|
100
|
+
| `const total = computed(() => …)` | `computed(() => …)` |
|
|
101
|
+
| `const label = …` reading an input | `computed(() => untracked(() => …))`, read once in `ngOnInit` (setup-once) |
|
|
102
|
+
| `const rates = …` | a `readonly` field |
|
|
103
|
+
| `let timer: T` | a field: `private timer: T \| undefined;`, `private refreshes = 0;` |
|
|
104
|
+
| `let remaining = limit` (an input) | `private remaining!: number;`, assigned in `ngOnInit` (below) |
|
|
105
|
+
| `const input = useTemplateRef<T>()` | `viewChild<ElementRef<T>>("input")`, with `#input` on the element |
|
|
106
|
+
| `const id = useId()` | `` `uf-id-<component>-${nextId++}` ``: a module counter (emulated, `use-id`) |
|
|
107
|
+
| `function save()`, arrow `const` | a method; one passed as a value (`setTimeout(tick, 100)`) an arrow field, which keeps `this`, declared before the other fields |
|
|
108
|
+
| `const { on, toggle } = useToggle(false)` | `protected readonly toggle_1 = useToggle(false)`, read as `this.toggle_1.on()` and `@let on = this.toggle_1.on();` (below) |
|
|
109
|
+
| `emit("change", value)` | `this.change.emit(value)`, by the payload rule |
|
|
110
|
+
| `nextTick()` | `this.nextTick()`, a private method that renders in a microtask with `ApplicationRef.tick()` (emulated, `next-tick`) |
|
|
111
|
+
| `watch(source, callback, options)` | an `effect()` created in `ngOnInit`; `afterRenderEffect()` with `flush: "post"` (below) |
|
|
112
|
+
| `watchEffect(effect)` | `afterRenderEffect(effect)` in the constructor: after the render, in the browser only |
|
|
113
|
+
| `onMounted(callback)` | `afterNextRender(callback)` in the constructor: in the browser, once the view is in the document |
|
|
114
|
+
| `onUnmounted(callback)` | in `ngOnDestroy`, in the browser only |
|
|
115
|
+
|
|
116
|
+
A watcher is created in `ngOnInit`, once the inputs are set, so the source's value at creation is
|
|
117
|
+
known and typed: `let lastCount = currentCount();`. Its source goes through a `computed`
|
|
118
|
+
(`const currentCount = computed(() => this.count())`), whose value changes only when it differs
|
|
119
|
+
(`Object.is`; an array of sources compares each element), so the effect runs again only when the
|
|
120
|
+
source changed, and Angular's `onCleanup` runs only before the next callback and on destruction,
|
|
121
|
+
as Vue's does. An input or a `computed` is watched as it is. The effect's first run only records
|
|
122
|
+
the value (`if (Object.is(value, lastCount)) return;`), or, with `immediate`, runs the callback
|
|
123
|
+
in the browser (`if (!isPlatformBrowser(this.platformId)) return;`). The callback runs
|
|
124
|
+
`untracked`; an async callback is `untracked(async () => …)`, whose promise nothing awaits. An
|
|
125
|
+
array of sources is `computed(() => [a, b] satisfies [unknown, unknown], …)`, one new array only
|
|
126
|
+
when a source changed, compared by identity: `satisfies` types the literal as a mutable tuple
|
|
127
|
+
(`[number, string]`), so the callback reads, destructures and passes on its values as Vue types
|
|
128
|
+
them, and gets the last array again as its previous values, as Vue hands it. An immediate
|
|
129
|
+
watcher's first previous values are `[]`, typed as Vue types them, each value or `undefined`
|
|
130
|
+
(`as [(typeof lastValues)[0] | undefined, …]`). An async `watchEffect`
|
|
131
|
+
runs as a floating async function inside the effect (`void (async () => { … })();`), so its reads
|
|
132
|
+
before its first `await` are tracked. Effects run during change detection, which zoneless Angular
|
|
133
|
+
schedules as a macrotask: the writes of one handler run a watcher once, and writes on both sides of
|
|
134
|
+
an `await` may run it once or twice (the semantics contract allows both).
|
|
135
|
+
|
|
136
|
+
A field initialiser runs while the class constructs, before Angular sets any input. So a setup
|
|
137
|
+
`let` whose initial value reads an input, a seeded state or constant, a derived value or another
|
|
138
|
+
such `let` is declared without one and assigned in `ngOnInit`, in source order with the seeded
|
|
139
|
+
values: `private remaining!: number;` and `this.remaining = this.limit();`, definitely assigned
|
|
140
|
+
(`!`) whatever its type, since `ngOnInit` assigns it before any code reads it. Its type is its
|
|
141
|
+
annotation, or the type TypeScript gives the `let` where the value's text makes it certain
|
|
142
|
+
(`src/seeded.ts`: literals widened, operators by their operands as in `step * 2`,
|
|
143
|
+
`note ?? "none"` and `a ? "x" : "y"`, `.length`, the string, number and array methods whose
|
|
144
|
+
result the receiver fixes, the pure globals' results, and reads of a prop or of a member whose
|
|
145
|
+
type is known so); otherwise, as a last resort, a private method computes the value, and the field
|
|
146
|
+
is typed by it (`ReturnType<typeof this.initialFound>`), since a target reads no type oracle.
|
|
147
|
+
|
|
148
|
+
A local function code passes as a value is an arrow field, declared before every other field of
|
|
149
|
+
the setup: a field initialiser runs while the class constructs, in field order, and an initial
|
|
150
|
+
value that calls a function which passes it on (`names.toSorted(compare)`) finds it there, as the
|
|
151
|
+
source finds its hoisted function.
|
|
152
|
+
|
|
153
|
+
Angular destroys a component's outputs before its view effects and its `DestroyRef` callbacks
|
|
154
|
+
(an emit then is NG0953 and lost), while Vue stops a watcher and runs `onUnmounted` while the
|
|
155
|
+
component still emits. So a watcher or a `watchEffect` with an `onCleanup` keeps its
|
|
156
|
+
`EffectRef`/`AfterRenderRef` in a member, and `ngOnDestroy`, which runs first, destroys it
|
|
157
|
+
(running the cleanup), then runs the `onUnmounted` callbacks, each in turn: one whose body
|
|
158
|
+
returns runs in a function of its own (`(() => { … })();`), so its `return` ends it and not
|
|
159
|
+
`ngOnDestroy`. A callback the class runs (a hook, an effect, a watcher's) returns no value, as
|
|
160
|
+
none is read: its `return x;` keeps only what `x` changes.
|
|
161
|
+
|
|
162
|
+
### Composables (ADR-0069)
|
|
163
|
+
|
|
164
|
+
A `.uf.ts` module's composable is its own module, `use-toggle.ts` (`src/composable.ts`). It is an
|
|
165
|
+
exported function that needs an injection context, as Angular's own `inject` functions do. Its
|
|
166
|
+
parameters and type parameters are as written.
|
|
167
|
+
|
|
168
|
+
Its setup is the function's body, in source order, and each binding is a local:
|
|
169
|
+
|
|
170
|
+
- A state is `signal(initial)`. It is read as `on()` and written through `set`. It is returned as
|
|
171
|
+
`on.asReadonly()`, since a caller never writes it (UF2036).
|
|
172
|
+
- A derived value is `computed`.
|
|
173
|
+
- A constant, a `let` and a function are as written. A getter argument is called, and a callback
|
|
174
|
+
argument is called as the source calls it.
|
|
175
|
+
- `watch` is an `effect()` over a `computed` of its source, as a component's is. Its first run
|
|
176
|
+
records the value: the composable may run before the caller's inputs are set, and a getter
|
|
177
|
+
argument may read one.
|
|
178
|
+
- `watchEffect` is an `afterRenderEffect`, and `onMounted` is `afterNextRender`.
|
|
179
|
+
- `onUnmounted` is `inject(DestroyRef).onDestroy(…)`, registered in the browser only.
|
|
180
|
+
|
|
181
|
+
The module imports what it uses from Angular, the types its parameters and setup name, and the
|
|
182
|
+
values and constants it reads of its own module, as a component does.
|
|
183
|
+
|
|
184
|
+
A caller holds each call's result in a member named for the composable: `useMediaQuery` gives
|
|
185
|
+
`mediaQuery`. The name skips every name the class declares, the `class` and `style` inputs
|
|
186
|
+
included. So `useToggle` gives `toggle_1` where the call destructures a `toggle`. Class code reads
|
|
187
|
+
a returned ref as `this.mediaQuery.matches()`, and the template reads it through a `@let`. A
|
|
188
|
+
returned function or constant is read as `mediaQuery.update()`. A template statement that calls a
|
|
189
|
+
returned function is `void` (`(click)="void toggle_1.toggle()"`). The API says only whether the
|
|
190
|
+
function is async, and Angular prevents the default on `false`.
|
|
191
|
+
|
|
192
|
+
Where the call runs keeps its order among the caller's hooks and effects, in source order
|
|
193
|
+
(`src/plan.ts`, `CallPlacement`):
|
|
194
|
+
|
|
195
|
+
- By default, a field initialiser.
|
|
196
|
+
- The constructor, for a composable with effects that comes after a hook or an effect the
|
|
197
|
+
constructor registers (`afterNextRender`, `afterRenderEffect`).
|
|
198
|
+
- `ngOnInit`, where a value or a getter argument reads an input, or reads what reads one. The
|
|
199
|
+
composable reads a value, and may call a getter, while it sets up, and an input is set only
|
|
200
|
+
then. A callback runs only from client code (UF2035), so it doesn't count.
|
|
201
|
+
- `ngOnInit` too, for a composable with effects that comes after a watcher, which `ngOnInit`
|
|
202
|
+
creates, or after another such call.
|
|
203
|
+
|
|
204
|
+
A call in `ngOnInit` runs through `runInInjectionContext`, in the composable's own injector where it
|
|
205
|
+
has effects (below), or the component's. Once a call with effects runs in `ngOnInit`, the caller's
|
|
206
|
+
later `onMounted` hooks and `watchEffect`s are registered there after it, with
|
|
207
|
+
`{ injector: this.injector }`, rather than in the constructor before it
|
|
208
|
+
(`semantics/composable-effect-order`).
|
|
209
|
+
|
|
210
|
+
A call in the constructor or in `ngOnInit` is a private method's, `createTracked()`. The holder is
|
|
211
|
+
typed `ReturnType<typeof this.createTracked>`, so a generic composable keeps the type the call
|
|
212
|
+
infers.
|
|
213
|
+
|
|
214
|
+
A composable with effects runs in an injector of its own,
|
|
215
|
+
`Injector.create({ providers: [], parent: this.injector })`. `ngOnDestroy` destroys it where the
|
|
216
|
+
call sits among the component's `onUnmounted` hooks. Its `DestroyRef` is that injector, so the
|
|
217
|
+
composable's teardown runs then.
|
|
218
|
+
|
|
219
|
+
The component's own `DestroyRef` would run too late. It runs only once Angular has removed the
|
|
220
|
+
parent's listeners of the component's outputs, so a callback the teardown calls could no longer
|
|
221
|
+
emit to the parent (`semantics/composable-unmount-order`). The injector reads its view through its
|
|
222
|
+
parent, so the composable's effects stay the component's view effects.
|
|
223
|
+
|
|
224
|
+
### Composition (ADR-0053 to ADR-0056)
|
|
225
|
+
|
|
226
|
+
| Source | Angular output |
|
|
227
|
+
| ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
228
|
+
| `import Field from "./Field.uf.tsx"`, `<Field>` | `import Field from "./field"`, `imports: [Field]`, `<uf-field>`; a component that renders itself uses its own selector; two children of one name, or two children's slots of one directive attribute, are UF1104; a prop, model, event or exposed function whose Angular name another member, input or output takes is UF2003 |
|
|
229
|
+
| `label="Name"`, `tone={tone.value}` | `[label]="'Name'"`, `[tone]="tone"`: a static prop is bound too, so the host keeps no attribute |
|
|
230
|
+
| `onClear={clear}` | `(clear)="clear($event)"`, the payload spread by the tuple rule; any other handler a method (`onPickedURL($event)`) |
|
|
231
|
+
| `{slots.default?.()}`, with fallback | `<ng-content />`, `<ng-content>fallback</ng-content>` |
|
|
232
|
+
| `{slots.title?.()}`, `{slots.item?.({ item })}` | `@if (this.itemTemplate(); as template) { <ng-container [ngTemplateOutlet]="template" [ngTemplateOutletContext]="…" /> }`, the alias a name the context reads none of |
|
|
233
|
+
| `defineSlots<…>()` | a directive per named or scoped slot, `ng-template[ufListItem]`, with `ngTemplateContextGuard`, read by `contentChild`, and exported as `ListItem` even where an import takes that name |
|
|
234
|
+
| a fill, `{{ item: ({ item }) => … }}` | `<ng-template ufListItem let-item="item">`, its directive in `imports` |
|
|
235
|
+
| `slots.title` as a condition | `this.titleTemplate()`, read from the class past any template variable |
|
|
236
|
+
| `defineModel("value", { default: 0 })` | `readonly value = model<number>(0)`, read and written as a signal; `model<number>(0, { alias: "value" })` where its binding has another name |
|
|
237
|
+
| `v-model:value={cups.value}` | `[(value)]="this.cups"`, on the signal |
|
|
238
|
+
| `v-model` on a control | `#nameField [value]="this.name()" (input)="this.name.set(nameField.value)"`, `[checked]` and `(change)` for a box, `[selected]` on a select's options |
|
|
239
|
+
| `class` and `style` on a component | `[class]`, `[style]`: the child's `class` and `style` inputs, merged into its root; the host clears both |
|
|
240
|
+
| `defineExpose({ focus })`, `ref={search}` | `focus` public; `viewChild<{ focus(): void }>("search")`, read as `this.search()?.focus()` |
|
|
241
|
+
| `export const LocaleKey: InjectionKey<T>` | `export const LocaleKey = new InjectionToken<T>("…")` in the module's main component's file |
|
|
242
|
+
| `provide(LocaleKey, locale)` | `providers: [{ provide: LocaleKey, useFactory: () => inject(LocaleProvider).locale() }]`; `useValue` for a constant |
|
|
243
|
+
| `inject(LocaleKey, "en")` | `computed(() => untracked(() => this.injector.get(LocaleKey, null, { optional: true, skipSelf: true }) ?? "en"))` |
|
|
244
|
+
| `<component is={nested.value ? "h3" : "h2"}>` | `@switch (nested ? 'h3' : 'h2') { @case ('h2') { <h2>…</h2> } … }`; a component candidate is matched by its name |
|
|
245
|
+
|
|
246
|
+
- **Slots.** The default slot without props is `<ng-content>`; every other slot is a directive the
|
|
247
|
+
file exports, `<Component><Slot>` with the selector `ng-template[uf<Component><Slot>]`, whose
|
|
248
|
+
context guard types the consumer's `let-` variables (its props by name, and whole as
|
|
249
|
+
`$implicit`; a prop no template variable can read, `"data-id"`, also as `dataId`). A slot without
|
|
250
|
+
props has an empty context, so a `let-` variable on its template is an error. The template
|
|
251
|
+
ends with `<ng-content select="[ufListItem]" />` per directive, so a slot's template never counts
|
|
252
|
+
as the default slot's content. A forwarded named slot re-declares the child's template under
|
|
253
|
+
`@if (this.titleTemplate(); as template)`.
|
|
254
|
+
- **Models.** A text field, a text area and a select write the state through a template variable
|
|
255
|
+
of the control, which types its value. Vue's `vModelText` (trim, lazy, a number's cast) goes
|
|
256
|
+
through `modelText`, which keeps the field's own text where it already means the value
|
|
257
|
+
(`12.5kg` for `12.5`), and `writeModelText`; a checkbox group through `toggle`; a multiple select
|
|
258
|
+
through `selectedValues`. Each option of a select binds `[selected]` from its own value (bound,
|
|
259
|
+
static, or its text stripped and collapsed as the browser does, through `optionText` when it
|
|
260
|
+
holds an interpolation), wherever it sits: in a list, a conditional or an `<optgroup>`. On the
|
|
261
|
+
server, whose DOM has no selectedness, it binds `[defaultSelected]` too (`onServer`), so the
|
|
262
|
+
server HTML marks the option. Vue's `vModel*` directives listen before the element's own
|
|
263
|
+
listeners, so the model's write runs first in the element's one template listener of its event,
|
|
264
|
+
ahead of an author's listener there, a `once` or `passive` one included (a capture listener runs
|
|
265
|
+
before the bubble phase at its target, on Vue too, and keeps its directive).
|
|
266
|
+
- **Fallthrough.** A component whose root is an element or a component, and that does not set
|
|
267
|
+
`inheritAttrs: false`, declares `class` and `style` inputs and merges them into its root: the
|
|
268
|
+
class among its parts, the style in one map after the root's own, so a property the consumer
|
|
269
|
+
leaves `undefined` removes the root's, as Vue's merge does. The host clears both with
|
|
270
|
+
`[attr.class]` and `[attr.style]` bindings, keeping only `display: contents`.
|
|
271
|
+
- **Context.** An injected value is read once the inputs are set, from the injector the class
|
|
272
|
+
holds: a consumer projected into its provider is created before the provider's inputs are set,
|
|
273
|
+
which a provided value may read (NG0950). A key of a ref (`InjectionKey<Ref<number>>`) holds
|
|
274
|
+
`{ readonly value: number }`, which `refObject` makes from the provided signal. Members a
|
|
275
|
+
provider's factory reads are public.
|
|
276
|
+
|
|
277
|
+
### Listeners (ADR-0047)
|
|
278
|
+
|
|
279
|
+
Angular's template statements are not JavaScript functions (no block bodies, no `++`, a signal
|
|
280
|
+
is written through `set`), so a listener is (`src/listeners.ts`):
|
|
281
|
+
|
|
282
|
+
- `(click)="save()"`, `(keydown)="recordKey($event)"`: a setup function by name, with `$event`
|
|
283
|
+
when its first parameter is the event.
|
|
284
|
+
- `(click)="select(task.id)"`, `(click)="share.emit([path])"`: an inline handler that is one call
|
|
285
|
+
of a setup function or of `emit`, with arguments the template reads as the source does
|
|
286
|
+
(literals, inputs, constants, a list's item and index, the event as `$event`). A handler that
|
|
287
|
+
reads a list's item is always one (UF3029): the `@for` declares and types the item. So is one
|
|
288
|
+
that reads a prop a condition around it tests, when Angular's expressions can hold its
|
|
289
|
+
arguments (`record('thank ' + organiser.name)`, `save({ ...owner, name: owner.name })`, as
|
|
290
|
+
Angular 22 reads spreads): the template keeps the prop narrowed. A state
|
|
291
|
+
an argument reads is read from the class (`this.selected()`), and declares no `@let`.
|
|
292
|
+
- In both, a call of a function that may return a value drops it (`void toggle()`,
|
|
293
|
+
`void moveFrom(row, $event)`): Angular calls `preventDefault()` when a listener's value is
|
|
294
|
+
`false`, and every other target drops a handler's value.
|
|
295
|
+
- `(click)="onAddOne()"`: any other inline handler moves to a `protected` method named `on` and
|
|
296
|
+
what its element says it is (its static `aria-label` or its text, for a click; a form
|
|
297
|
+
control's `name` and the event; or the event alone), its event parameter annotated with its
|
|
298
|
+
event's DOM interface when the source leaves it to the JSX types. The method returns nothing:
|
|
299
|
+
an expression body becomes a statement (`a && b()` is `if (a) b();`, `a || b()` is
|
|
300
|
+
`if (!a) b();`, `c ? x() : y()` an `if`/`else`), and a block body's `return false;` is
|
|
301
|
+
`return;`, `return save();` is `this.save(); return;` (a value would prevent the event, and a
|
|
302
|
+
value on some paths only fails `noImplicitReturns`). Only what changes something stays
|
|
303
|
+
(`src/statements.ts`): a read of a signal (`this.count()`) is no change, so `return
|
|
304
|
+
count.value > 0;` is `return;`, and a branch or a side that changes nothing goes (`c ? save() :
|
|
305
|
+
false` is `if (c) this.save();`).
|
|
306
|
+
- `(ufClickCapture)="record('capture')"`: a listener with an option. Templates have no syntax for
|
|
307
|
+
one, so the file declares an exported attribute directive per event and option
|
|
308
|
+
(`[ufClickCapture]`, `[ufClickOnce]`, `[ufWheelPassive]`, emulated), which listens to its element
|
|
309
|
+
through `Renderer2.listen(element, "click", …, { capture: true })`, re-emits the event through an
|
|
310
|
+
output of its selector's name, and stops listening when its element goes. Being a directive, it
|
|
311
|
+
works in `@if` and `@for` blocks; Angular's template type checker imports it (NG3004 for one
|
|
312
|
+
that is not exported).
|
|
313
|
+
- `(click)="bump(); void (once(clickOnce, $event) && started.emit(count))"`: the listeners of one
|
|
314
|
+
element and event in the bubble phase, when there are two or three (a plain one beside a
|
|
315
|
+
`once` or a `passive` one). A directive listens when its element is created, before the
|
|
316
|
+
template's own listeners, so they would not run in their attributes' order: one template
|
|
317
|
+
listener chains their statements in that order, each written as it would be alone, so the
|
|
318
|
+
template keeps typing a list's item and narrowing what a condition narrows. A `once` listener
|
|
319
|
+
runs under a guard, `once(clickOnce, $event) && …`: a `protected` method that marks the element
|
|
320
|
+
in a `WeakSet<EventTarget>` per event and tells whether it ran there before, as `{ once: true }`
|
|
321
|
+
removes a listener from its own element only (each row of a list runs its own once). A
|
|
322
|
+
`passive` listener joins them: the element has a listener that is not passive, so scrolling
|
|
323
|
+
waits either way (the analyser rejects `preventDefault()` in a passive listener). The chain's
|
|
324
|
+
last statement is `void` when it may have a value. Within one listener, an async listener's
|
|
325
|
+
continuation runs after the later listeners' synchronous code, where the browser may run it
|
|
326
|
+
between two listeners of a person's input; an error thrown by one stops the later ones.
|
|
327
|
+
- An event named after a JavaScript reserved word (`delete`) is emitted from a template statement
|
|
328
|
+
through `this.` (`this.delete.emit(id)`), which reads it as a member.
|
|
329
|
+
|
|
330
|
+
Handlers run synchronously during dispatch, so `preventDefault()` and `stopPropagation()` work as
|
|
331
|
+
written; listeners keep DOM semantics (`change` on commit, `focus` and `blur` without bubbling).
|
|
332
|
+
|
|
333
|
+
### Scoped styles (M4)
|
|
334
|
+
|
|
335
|
+
A component that imports a stylesheet names the sheet `compile()` scoped for it in its metadata,
|
|
336
|
+
`styleUrl: "./Badge.uf.tsx.css"`, with `encapsulation: ViewEncapsulation.None`, since the sheet is
|
|
337
|
+
scoped already (ADR-0063). The shared markup printer writes `data-uf-<id>=""` on each element, the
|
|
338
|
+
parent's attribute on a child's tag, and the child's `-s` on the content filled into a child whose
|
|
339
|
+
sheet uses `:slotted()`, through `<ng-content>` and `ng-template` slots alike. The parent's
|
|
340
|
+
attribute lands on the child's host, which must keep only `display: contents`, and a template
|
|
341
|
+
cannot write an attribute whose name it does not know. So each root node of a component binds an
|
|
342
|
+
inline attribute directive to its host, `[ufScope]="this.host"` (`ScopeAttributes`, with the host
|
|
343
|
+
in a protected `host` member): in `ngOnInit` it takes the host's `data-uf-*` attributes off it,
|
|
344
|
+
keeps their names in a module `WeakMap` for a root that renders later, and sets them on its node.
|
|
345
|
+
Its root nodes are those Vue's fallthrough reaches (`scopeRoots`): the root element, component or
|
|
346
|
+
each candidate of a root `<component is>`, or each branch of a root conditional that renders one
|
|
347
|
+
alone. A root component's own root directive runs after this one has set them on its host, so they
|
|
348
|
+
pass through it. A root Angular binds nothing on (`{{` in an attribute, ADR-0037) takes none. The
|
|
349
|
+
root still renders inside its host, which selectors see: a parent's rule that reaches it through
|
|
350
|
+
a combinator or a structural pseudo-class (`>`, `+`, `:first-child`, …) matches the host's place.
|
|
351
|
+
So `child-root-scope` is unsupported (UF4003, info), reported at the first child a styled
|
|
352
|
+
component renders.
|
|
353
|
+
|
|
354
|
+
Angular adds a sheet to the page as its component first renders, a parent's before its children's,
|
|
355
|
+
where the other targets load sheets in import order: `sheet-order` is unsupported (UF4002, info),
|
|
356
|
+
reported at a component's first stylesheet import.
|
|
357
|
+
|
|
358
|
+
### Capabilities
|
|
359
|
+
|
|
360
|
+
Native: everything but `event-capture`, `event-once` and `event-passive` (emulated, the directives
|
|
361
|
+
above, helper `uf<Event><Option>`), `use-id` (emulated, `nextId`), `next-tick` (emulated,
|
|
362
|
+
`nextTick`), `class-binding` (emulated, `classNames`, for a class part that may be an array or an
|
|
363
|
+
object), `style-binding` (emulated, `styleObject`, for a style bound to a value that a merge
|
|
364
|
+
spreads; ADR-0065) and composition's cells that ADR-0056 decides: `named-slot`, `scoped-slot` and
|
|
365
|
+
`slot-forwarding` (emulated, the slot directives `uf<Component><Slot>`), `model-array`
|
|
366
|
+
(`toggle`), `model-modifiers` (`modelText`), `fallthrough` (`fallthrough`), `reactive-context`
|
|
367
|
+
(`refObject`) and `dynamic-component` (`@switch`), all emulated, `default-slot-presence` and
|
|
368
|
+
`contextual-root`, unsupported (UF4001, errors), `child-root-scope` and `sheet-order`,
|
|
369
|
+
unsupported (UF4003 and UF4002, info), and `generic-slot-props`, unsupported (UF4004, info): slot
|
|
370
|
+
directives cannot infer a component's type parameter, so a scoped slot's context takes its
|
|
371
|
+
constraint (ADR-0068), or `any` for a parameter without one, as ngtsc types a generic context it
|
|
372
|
+
cannot infer. Where an unsupported cell is reported, the
|
|
373
|
+
output still compiles: default-slot presence reads `true`, and the forwarded default slot
|
|
374
|
+
re-projects `<ng-content />`.
|
|
375
|
+
|
|
376
|
+
Inline content on either side of a child component's host reads with a space in the accessible
|
|
377
|
+
name: Chrome adds one at every `display: contents` element, so Angular reads `Build passing` where
|
|
378
|
+
the other targets read `Buildpassing`. `harness/quarantine/angular.ts` lists the cells until M3's
|
|
379
|
+
follow-up on Angular's host settles the difference.
|
|
380
|
+
|
|
381
|
+
## Toolchain
|
|
382
|
+
|
|
383
|
+
Tests and tooling build, check and run the Angular output through three subpaths:
|
|
384
|
+
|
|
385
|
+
- `@unframework.dev/target-angular/toolchain` (Node): the `toolchain` the harness drives.
|
|
386
|
+
- `vite("browser" | "ssr", context)`: Angular's AOT compiler (ngtsc, strict templates) for
|
|
387
|
+
the unframework plugin's virtual `X.uf.tsx.ts` modules and its modules of composables,
|
|
388
|
+
`X.uf.ts.ts`, which a component's program reads so its template is checked against the
|
|
389
|
+
composable's types (ADR-0069), Analog with `jit: false`, and the
|
|
390
|
+
Angular linker, over the pre-bundled framework in the browser and over the packages the SSR
|
|
391
|
+
module graph inlines on the server, so no JIT compiler runs.
|
|
392
|
+
- `frameworkCompile(files, context)` (L3) and `typecheck(files, context)` (L4): one ngtsc
|
|
393
|
+
program over every file, reported per file. ngtsc type-checks as it compiles, so L3, L4 and
|
|
394
|
+
the Vite path add `context.declarations` to their programs: the declarations of the modules
|
|
395
|
+
no package types, such as a styling engine's virtual modules (ADR-0064).
|
|
396
|
+
- `frameworkEmit(files, context)` (L15, ADR-0078): each file's JavaScript from one ngtsc
|
|
397
|
+
program, AOT only, for the harness to bundle and measure.
|
|
398
|
+
- `lint(files, context)` (L5, ADR-0042): oxlint's shared baseline over the TypeScript, and
|
|
399
|
+
ESLint with angular-eslint over the file and its inline template.
|
|
400
|
+
`@angular-eslint/no-output-native` is on: an output named like a native event takes the `uf`
|
|
401
|
+
prefix (ADR-0056).
|
|
402
|
+
- `@unframework.dev/target-angular/toolchain/client`: the browser mount adapter (`createApplication`
|
|
403
|
+
and `createComponent`, zoneless, settled with `whenStable`). `rerender(props)` sets each new
|
|
404
|
+
prop with `setInput`, and `setInput(name, undefined)` for a prop the new props leave out,
|
|
405
|
+
since Angular cannot unset an input (ADR-0043). The test's listeners are bound to the outputs
|
|
406
|
+
of their events (`outputBinding`, `ufChange` for `change`), and each payload is given back by
|
|
407
|
+
its event's shape.
|
|
408
|
+
- `@unframework.dev/target-angular/toolchain/server`: the SSR renderer (`renderApplication`), which
|
|
409
|
+
returns the component's HTML only. It runs on the `ssr` Vite configuration, which links
|
|
410
|
+
Angular's packages as they load.
|
|
411
|
+
|
|
412
|
+
Angular's compiler stack runs on TypeScript 6, while this repository runs TypeScript 7, so it is
|
|
413
|
+
never a dependency of this package. The toolchain loads `@angular/compiler-cli`, `@angular/build`,
|
|
414
|
+
`@analogjs/vite-plugin-angular` and TypeScript 6 from `context.toolchainDir`, which must have
|
|
415
|
+
them in its `node_modules` (in this repository, `tests/toolchains/angular`), together with a
|
|
416
|
+
`tsconfig.json` for the type check.
|
|
417
|
+
|
|
418
|
+
## Tests
|
|
419
|
+
|
|
420
|
+
`pnpm test` runs three projects: `unit` (Node: the emitter, L3 to L5 over the corpus and
|
|
421
|
+
`test/lint-probes.ts`'s shapes, the adapter on domino), `ssr` (the server renderer on the SSR
|
|
422
|
+
configuration, effects kept off the server) and `browser` (Chromium: the corpus's components as
|
|
423
|
+
this target emits them now, served by `test/corpus-plugin.ts` and mounted by the adapter, judged
|
|
424
|
+
on the semantics contract: reads after writes, watch timing, previous values and cleanups,
|
|
425
|
+
lifecycle, listener options, emits). The `composable` test compiles the composable and shared-module
|
|
426
|
+
cases through `compile()`, from `tests/integration/cases/` (`../codegen/test/composable-cases.ts`),
|
|
427
|
+
and runs L3 to L5 on their outputs.
|