@dsivd/prestations-ng 19.2.0-beta.20 → 19.2.0-beta.21

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.
Files changed (39) hide show
  1. package/ANGULAR_SIGNALS.md +4 -0
  2. package/CHANGELOG.md +98 -3
  3. package/CONTRIBUTING.md +2 -2
  4. package/ESLINT_PLUGIN.md +103 -8
  5. package/GOOD_PRACTICES.md +79 -29
  6. package/UPGRADING_V19.md +187 -31
  7. package/eslint/configs/template-rules.mjs +5 -0
  8. package/eslint/configs/ts-recommended.mjs +4 -0
  9. package/eslint/form-reads.mjs +63 -0
  10. package/eslint/rules/index.mjs +4 -0
  11. package/eslint/rules/no-direct-signal-mutation.mjs +57 -19
  12. package/eslint/rules/no-item-track-in-form-list.mjs +99 -0
  13. package/eslint/rules/no-uninvoked-signal-in-template.mjs +8 -3
  14. package/eslint/rules/require-model-change.mjs +89 -0
  15. package/eslint/template-ast.mjs +12 -2
  16. package/fesm2022/dsivd-prestations-ng.mjs +907 -692
  17. package/fesm2022/dsivd-prestations-ng.mjs.map +1 -1
  18. package/package.json +1 -1
  19. package/schematics/migration-collection.json +5 -0
  20. package/schematics/ng-update/migrations/update-19_2_0/index.js +39 -0
  21. package/schematics/utility/form-signal/class-migration.js +396 -0
  22. package/schematics/utility/form-signal/edits.js +34 -0
  23. package/schematics/utility/form-signal/form-signal-migration.js +184 -0
  24. package/schematics/utility/form-signal/page-classes.js +129 -0
  25. package/schematics/utility/form-signal/template-migration.js +666 -0
  26. package/src/eslint/configs/template-rules.mjs +5 -0
  27. package/src/eslint/configs/ts-recommended.mjs +4 -0
  28. package/src/eslint/form-reads.mjs +63 -0
  29. package/src/eslint/rules/__tests__/no-direct-signal-mutation.test.mjs +58 -0
  30. package/src/eslint/rules/__tests__/no-item-track-in-form-list.test.mjs +127 -0
  31. package/src/eslint/rules/__tests__/no-uninvoked-signal-in-template.test.mjs +6 -0
  32. package/src/eslint/rules/__tests__/require-model-change.test.mjs +93 -0
  33. package/src/eslint/rules/index.mjs +4 -0
  34. package/src/eslint/rules/no-direct-signal-mutation.mjs +57 -19
  35. package/src/eslint/rules/no-item-track-in-form-list.mjs +99 -0
  36. package/src/eslint/rules/no-uninvoked-signal-in-template.mjs +8 -3
  37. package/src/eslint/rules/require-model-change.mjs +89 -0
  38. package/src/eslint/template-ast.mjs +12 -2
  39. package/types/dsivd-prestations-ng.d.ts +234 -35
@@ -15,6 +15,11 @@ const templateRules = (plugin) => [
15
15
  // template visitor, and each fires on its own kind of file.
16
16
  '@dsivd/prestations-ng/no-direct-signal-mutation': 'error',
17
17
  '@dsivd/prestations-ng/no-uninvoked-signal-in-template': 'error',
18
+ // The form of the pages is a signal: a one-way [model] without
19
+ // (modelChange) loses the input, and a row tracked by its item
20
+ // loses the focus at every input
21
+ '@dsivd/prestations-ng/require-model-change': 'error',
22
+ '@dsivd/prestations-ng/no-item-track-in-form-list': 'error',
18
23
  },
19
24
  },
20
25
  ];
@@ -73,6 +73,10 @@ const tsRecommended = (plugin) => [
73
73
  ],
74
74
  '@typescript-eslint/member-ordering': 'error',
75
75
  '@typescript-eslint/no-base-to-string': 'warn',
76
+ // a template literal only takes strings, numbers, booleans, null,
77
+ // undefined, any and RegExp: also reports a signal not called
78
+ // (`${this.reference}`), which no-uncalled-signals skips there
79
+ '@typescript-eslint/restrict-template-expressions': 'error',
76
80
  // stylistic enables no-inferrable-types with default options (ignoreParameters: false);
77
81
  // override here to allow typed parameters explicitly
78
82
  '@typescript-eslint/no-inferrable-types': [
@@ -0,0 +1,63 @@
1
+ import { isComponentReceiver } from "./template-ast.mjs";
2
+
3
+ /**
4
+ * Where a template expression reads its value from: the form of the page (a signal since
5
+ * 19.2.0), through the names bound by `@if (form(); as form)`, `@let form = this.form()` and
6
+ * `@for (child of form.children)`.
7
+ */
8
+
9
+ const MEMBER_READS = new Set([
10
+ "PropertyRead",
11
+ "SafePropertyRead",
12
+ "KeyedRead",
13
+ "SafeKeyedRead",
14
+ ]);
15
+
16
+ export const astOf = (expression) =>
17
+ expression?.type === "ASTWithSource" ? expression.ast : expression;
18
+
19
+ /**
20
+ * The root of an expression: a member of the component (`{ member }`), or the item of an
21
+ * enclosing `@for` (`{ forBlock }`). A name bound by `@if … as` is followed to its expression,
22
+ * evaluated outside its block (in `@if (form(); as form)`, `form()` is the member).
23
+ * `undefined` for anything else (a literal, a call with arguments...).
24
+ */
25
+ export function resolveRoot(expression, node) {
26
+ let base = expression;
27
+ while (MEMBER_READS.has(base?.type) && MEMBER_READS.has(base.receiver?.type)) {
28
+ base = base.receiver;
29
+ }
30
+ if (base?.type === "Call" || base?.type === "SafeCall") {
31
+ base = base.receiver;
32
+ }
33
+ const receiver = base?.receiver;
34
+ if (receiver?.type === "Call" || receiver?.type === "SafeCall") {
35
+ return resolveRoot(receiver, node);
36
+ }
37
+ if (!MEMBER_READS.has(base?.type) || !isComponentReceiver(receiver)) {
38
+ return undefined;
39
+ }
40
+ for (let current = node; current; current = current.parent) {
41
+ if (current.type === "ForLoopBlock" && current.item?.name === base.name) {
42
+ return { forBlock: current };
43
+ }
44
+ if (
45
+ current.type === "IfBlockBranch" &&
46
+ current.expressionAlias?.name === base.name
47
+ ) {
48
+ return resolveRoot(astOf(current.expression), current.parent);
49
+ }
50
+ }
51
+ return { member: base.name };
52
+ }
53
+
54
+ /**
55
+ * Whether an expression reads the form of the page, directly or through a template name.
56
+ */
57
+ export function readsForm(expression, node) {
58
+ const root = resolveRoot(expression, node);
59
+ if (root?.forBlock) {
60
+ return readsForm(astOf(root.forBlock.expression), root.forBlock.parent);
61
+ }
62
+ return root?.member === "form";
63
+ }
@@ -2,6 +2,7 @@ import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';
2
2
  import { tmpdir } from 'node:os';
3
3
  import { join } from 'node:path';
4
4
  import * as templateParser from '@angular-eslint/template-parser';
5
+ import typeScriptParser from '@typescript-eslint/parser';
5
6
  import { RuleTester } from 'eslint';
6
7
  import { beforeAll, describe, it } from 'vitest';
7
8
  import rule from '../no-direct-signal-mutation.mjs';
@@ -266,6 +267,23 @@ describe('no-direct-signal-mutation', () => {
266
267
  errors: [{ messageId }],
267
268
  });
268
269
 
270
+ it('should see through the TypeScript wrappers of the value', () => {
271
+ const typeScriptRuleTester = new RuleTester({
272
+ languageOptions: { parser: typeScriptParser },
273
+ });
274
+
275
+ typeScriptRuleTester.run('no-direct-signal-mutation', rule, {
276
+ valid: [ts('const name = this.inherited()!.name;')],
277
+ invalid: [
278
+ // The form of a page read with a non-null assertion (the ng update migration writes so)
279
+ invalidTs('this.inherited()!.name = value', 'directMutation'),
280
+ invalidTs('this.inherited()!.users.push(user)', 'inPlaceMutation'),
281
+ invalidTs('(this.partenaire() as Partenaire).name = value', 'directMutation'),
282
+ invalidTs('const form = this.inherited()!; form.name = value', 'directMutation'),
283
+ ],
284
+ });
285
+ });
286
+
269
287
  it('should validate signal mutation rule in templates', () => {
270
288
  templateRuleTester.run('no-direct-signal-mutation', rule, {
271
289
  valid: [
@@ -305,6 +323,10 @@ describe('no-direct-signal-mutation', () => {
305
323
 
306
324
  // An output named `…Change` that carries no assignment
307
325
  template('<app-child (valueChange)="comment.set($event)"></app-child>'),
326
+ // An explicit handler is a statement, not the target of a two-way binding: a call
327
+ // assigns nothing
328
+ template('<app-child (valueChange)="partenaire()"></app-child>'),
329
+ template('<app-child [value]="partenaire().name" (valueChange)="partenaire()"></app-child>'),
308
330
 
309
331
  // A `@for` item is only a signal's value when the loop iterates a signal
310
332
  template('@for (u of getConfig().users; track u) { <button (click)="u.done = 1"></button> }'),
@@ -314,6 +336,17 @@ describe('no-direct-signal-mutation', () => {
314
336
  template('@for (u of items(); track u) { {{ u.name }} }'),
315
337
  template('@let copy = getConfig().users; <button (click)="copy.push(item)"></button>'),
316
338
 
339
+ // An `@if` alias is only a signal's value when the condition is one
340
+ template('@if (getConfig(); as config) { <input [(ngModel)]="config.name" /> }'),
341
+ // Reading it mutates nothing
342
+ template('@if (partenaire(); as p) { {{ p.name }} }'),
343
+ // It is bound only inside its own block
344
+ template('@if (partenaire(); as p) { } <button (click)="p.name = value"></button>'),
345
+ // The nearest name shadows the others
346
+ template(`@if (partenaire(); as u) {
347
+ @for (u of getConfig().users; track u) { <button (click)="u.done = 1"></button> }
348
+ }`),
349
+
317
350
  // A computed member whose method does not mutate
318
351
  template(`<button (click)="items()['toSorted']()"></button>`),
319
352
  template('<button (click)="items().filter(active).sort()"></button>'),
@@ -355,6 +388,25 @@ describe('no-direct-signal-mutation', () => {
355
388
  invalidTemplate('@let users = partenaire().users; <button (click)="users.push(user)"></button>', 'inPlaceMutation'),
356
389
  invalidTemplate('@let value = partenaire(); <button (click)="value.name = other"></button>', 'directMutation'),
357
390
 
391
+ // Same through the alias of an `@if`, bound only inside its own branch
392
+ invalidTemplate('@if (partenaire(); as p) { <input [(ngModel)]="p.name" /> }', 'directMutation'),
393
+ invalidTemplate('@if (partenaire(); as p) { <button (click)="p.name = value"></button> }', 'directMutation'),
394
+ invalidTemplate('@if (partenaire(); as p) { <button (click)="p.users.push(user)"></button> }', 'inPlaceMutation'),
395
+ invalidTemplate('@if (ready) { } @else if (partenaire(); as p) { <input [(ngModel)]="p.name" /> }', 'directMutation'),
396
+ invalidTemplate(`@if (partenaire(); as p) {
397
+ @if (p.users; as users) { <button (click)="users.push(user)"></button> }
398
+ }`, 'inPlaceMutation'),
399
+ // The alias named as the signal, as in `@if (form(); as form)`
400
+ invalidTemplate('@if (partenaire(); as partenaire) { <input [(ngModel)]="partenaire.name" /> }', 'directMutation'),
401
+ invalidTemplate('@if (double(); as d) { <button (click)="d.users.push(user)"></button> }', 'readOnlyMutation'),
402
+
403
+ // A non-null assertion or parentheses keep the value as is
404
+ invalidTemplate('<input [(ngModel)]="partenaire()!.name" />', 'directMutation'),
405
+ invalidTemplate('<button (click)="(partenaire()).name = value"></button>', 'directMutation'),
406
+ invalidTemplate('<button (click)="items()!.push(item)"></button>', 'inPlaceMutation'),
407
+ invalidTemplate('@let value = partenaire()!; <button (click)="value.name = other"></button>', 'directMutation'),
408
+ invalidTemplate('@let value = partenaire(); <button (click)="value!.name = other"></button>', 'directMutation'),
409
+
358
410
  // Every other kind of signal, each with the advice that applies to it
359
411
  invalidTemplate('<button (click)="double().users.push(user)"></button>', 'readOnlyMutation'),
360
412
  invalidTemplate('<button (click)="formArray().push(control)"></button>', 'readOnlyMutation'),
@@ -399,6 +451,12 @@ describe('no-direct-signal-mutation', () => {
399
451
  code: '<button (click)="map.get(key).name = value"></button>',
400
452
  filename: unknownTemplatePath,
401
453
  },
454
+ // A template shared by several components: a `…Change` handler calling a method
455
+ // assigns nothing
456
+ {
457
+ code: '<app-child [(value)]="item.weight" (valueChange)="updateModel()"></app-child>',
458
+ filename: unknownTemplatePath,
459
+ },
402
460
  ],
403
461
  invalid: [
404
462
  {
@@ -0,0 +1,127 @@
1
+ import * as templateParser from '@angular-eslint/template-parser';
2
+ import { RuleTester } from 'eslint';
3
+ import { describe, it } from 'vitest';
4
+ import rule from '../no-item-track-in-form-list.mjs';
5
+
6
+ describe('no-item-track-in-form-list', () => {
7
+ const ruleTester = new RuleTester({
8
+ languageOptions: { parser: templateParser },
9
+ });
10
+
11
+ const editedRows = (forHeader) => `@if (form(); as form) {
12
+ @for (${forHeader}) {
13
+ <foehn-input-text
14
+ [model]="child.firstName"
15
+ (modelChange)="patchForm(['children', $index, 'firstName'], $event)"
16
+ />
17
+ }
18
+ }`;
19
+
20
+ it('should report the rows of a list of the form tracked by their item', () => {
21
+ ruleTester.run('no-item-track-in-form-list', rule, {
22
+ valid: [
23
+ editedRows('child of form.children; track $index'),
24
+ editedRows('child of form.children; track child.id'),
25
+ editedRows('child of form.children; track child.trackingIndex'),
26
+ // Not the form: the items keep their identity
27
+ `@for (child of children(); track child) {
28
+ <foehn-input-text [(model)]="child.firstName" />
29
+ }`,
30
+ // Only displayed: a row created again loses no focus
31
+ `@if (form(); as form) {
32
+ @for (child of form.children; track child) {
33
+ <p>{{ child.firstName }}</p>
34
+ }
35
+ }`,
36
+ // The rows edit another list
37
+ `@if (form(); as form) {
38
+ @for (child of form.children; track child) {
39
+ <foehn-input-text [model]="form.firstName" (modelChange)="patchForm(['firstName'], $event)" />
40
+ }
41
+ }`,
42
+ ],
43
+ invalid: [
44
+ {
45
+ code: editedRows('child of form.children; track child'),
46
+ errors: [
47
+ {
48
+ messageId: 'itemTrack',
49
+ data: { item: 'child' },
50
+ suggestions: [
51
+ {
52
+ messageId: 'trackByIndex',
53
+ output: editedRows('child of form.children; track $index'),
54
+ },
55
+ ],
56
+ },
57
+ ],
58
+ },
59
+ {
60
+ code: editedRows('child of form.children; track $any(child)'),
61
+ errors: [
62
+ {
63
+ messageId: 'itemTrack',
64
+ suggestions: [
65
+ {
66
+ messageId: 'trackByIndex',
67
+ output: editedRows('child of form.children; track $index'),
68
+ },
69
+ ],
70
+ },
71
+ ],
72
+ },
73
+ {
74
+ // A list of an item of the form, an item bound as a whole
75
+ code: `@for (child of form().children; track $index) {
76
+ @for (diploma of child.diplomas; track diploma) {
77
+ <app-diploma [(model)]="diploma" />
78
+ }
79
+ }`,
80
+ errors: [
81
+ {
82
+ messageId: 'itemTrack',
83
+ data: { item: 'diploma' },
84
+ suggestions: [
85
+ {
86
+ messageId: 'trackByIndex',
87
+ output: `@for (child of form().children; track $index) {
88
+ @for (diploma of child.diplomas; track $index) {
89
+ <app-diploma [(model)]="diploma" />
90
+ }
91
+ }`,
92
+ },
93
+ ],
94
+ },
95
+ ],
96
+ },
97
+ {
98
+ // A field of an object of the item, read through an alias
99
+ code: `@if (form(); as form) {
100
+ @for (child of form.children; track child) {
101
+ @if (child.address; as address) {
102
+ <foehn-input-text [model]="address.street" (modelChange)="patchForm(['children', $index, 'address', 'street'], $event)" />
103
+ }
104
+ }
105
+ }`,
106
+ errors: [
107
+ {
108
+ messageId: 'itemTrack',
109
+ suggestions: [
110
+ {
111
+ messageId: 'trackByIndex',
112
+ output: `@if (form(); as form) {
113
+ @for (child of form.children; track $index) {
114
+ @if (child.address; as address) {
115
+ <foehn-input-text [model]="address.street" (modelChange)="patchForm(['children', $index, 'address', 'street'], $event)" />
116
+ }
117
+ }
118
+ }`,
119
+ },
120
+ ],
121
+ },
122
+ ],
123
+ },
124
+ ],
125
+ });
126
+ });
127
+ });
@@ -219,6 +219,12 @@ describe('no-uninvoked-signal-in-template', () => {
219
219
  '<app-child [value]="comment()"></app-child>',
220
220
  'comment',
221
221
  ),
222
+ // Nor is an explicit `(valueChange)`: its handler is a statement, not a target
223
+ invalidTemplate(
224
+ '<app-child (valueChange)="comment"></app-child>',
225
+ '<app-child (valueChange)="comment()"></app-child>',
226
+ 'comment',
227
+ ),
222
228
  ],
223
229
  });
224
230
  });
@@ -0,0 +1,93 @@
1
+ import * as templateParser from '@angular-eslint/template-parser';
2
+ import { RuleTester } from 'eslint';
3
+ import { describe, it } from 'vitest';
4
+ import rule from '../require-model-change.mjs';
5
+
6
+ describe('require-model-change', () => {
7
+ const ruleTester = new RuleTester({
8
+ languageOptions: { parser: templateParser },
9
+ });
10
+
11
+ it('should require (modelChange) next to a one-way [model]', () => {
12
+ ruleTester.run('require-model-change', rule, {
13
+ valid: [
14
+ '<foehn-input-text name="a" [(model)]="form.a" />',
15
+ `<foehn-input-text
16
+ name="a"
17
+ [model]="form.a"
18
+ (modelChange)="patchForm(['a'], $event)"
19
+ />`,
20
+ '<foehn-input-text name="a" model="static" />',
21
+ '<my-input [model]="form.a" />',
22
+ // Not the form: a one-way [model] is legitimate
23
+ '<foehn-table [model]="orders()" />',
24
+ '<foehn-dropdown-menu [model]="menuItems" />',
25
+ `@for (order of orders(); track order.id) {
26
+ <foehn-input-text [model]="order.label" />
27
+ }`,
28
+ `@if (form(); as form) {
29
+ @for (child of form.children; track $index) {
30
+ <foehn-input-text
31
+ [model]="child.firstName"
32
+ (modelChange)="patchForm(['children', $index, 'firstName'], $event)"
33
+ />
34
+ }
35
+ }`,
36
+ // The input of the user can't be lost: always disabled, never written, or written by (userInput)
37
+ '<foehn-input-text name="a" [model]="form.a" [disabled]="true" />',
38
+ '<foehn-input-number name="a" [model]="form.a" disabled="true" />',
39
+ '<foehn-input-hidden name="a" [model]="form.a" />',
40
+ '<foehn-table [model]="form.items" />',
41
+ `<foehn-boolean-radio
42
+ name="a"
43
+ [model]="form.a"
44
+ (userInput)="onAChange($event)"
45
+ />`,
46
+ ],
47
+ invalid: [
48
+ {
49
+ code: '<foehn-input-text name="a" [model]="form.a" />',
50
+ errors: [{ messageId: 'missing', data: { element: 'foehn-input-text' } }],
51
+ },
52
+ {
53
+ code: `@if (form(); as form) {
54
+ <foehn-list-summary name="items" [model]="form.items" />
55
+ }`,
56
+ errors: [{ messageId: 'missing', data: { element: 'foehn-list-summary' } }],
57
+ },
58
+ {
59
+ code: '<foehn-input-text name="a" [model]="form()?.a" />',
60
+ errors: [{ messageId: 'missing', data: { element: 'foehn-input-text' } }],
61
+ },
62
+ {
63
+ code: `@if (form(); as currentForm) {
64
+ @for (child of currentForm.children; track $index) {
65
+ <foehn-input-text [model]="child.firstName" />
66
+ }
67
+ }`,
68
+ errors: [{ messageId: 'missing', data: { element: 'foehn-input-text' } }],
69
+ },
70
+ // Enabled once the condition is false
71
+ {
72
+ code: '<foehn-input-text name="a" [model]="form.a" [disabled]="readOnly()" />',
73
+ errors: [{ messageId: 'missing', data: { element: 'foehn-input-text' } }],
74
+ },
75
+ // A bare disabled gives '', which doesn't disable the component
76
+ {
77
+ code: '<foehn-input-text name="a" [model]="form.a" disabled />',
78
+ errors: [{ messageId: 'missing', data: { element: 'foehn-input-text' } }],
79
+ },
80
+ // Disabled, but still writing its model
81
+ {
82
+ code: `<foehn-input-address
83
+ name="a"
84
+ [model]="form.address"
85
+ [disabled]="true"
86
+ [updateModelWhenDisabled]="true"
87
+ />`,
88
+ errors: [{ messageId: 'missing', data: { element: 'foehn-input-address' } }],
89
+ },
90
+ ],
91
+ });
92
+ });
93
+ });
@@ -1,7 +1,11 @@
1
1
  import noDirectSignalMutation from './no-direct-signal-mutation.mjs';
2
+ import noItemTrackInFormList from './no-item-track-in-form-list.mjs';
2
3
  import noUninvokedSignalInTemplate from './no-uninvoked-signal-in-template.mjs';
4
+ import requireModelChange from './require-model-change.mjs';
3
5
 
4
6
  export default {
5
7
  'no-direct-signal-mutation': noDirectSignalMutation,
8
+ 'no-item-track-in-form-list': noItemTrackInFormList,
6
9
  'no-uninvoked-signal-in-template': noUninvokedSignalInTemplate,
10
+ 'require-model-change': requireModelChange,
7
11
  };
@@ -9,7 +9,11 @@ import {
9
9
  WRITABLE,
10
10
  } from "../signal-kinds.mjs";
11
11
  import { collectSignalKinds } from "../signal-names.mjs";
12
- import { handlerExpressionOf, isComponentReceiver } from "../template-ast.mjs";
12
+ import {
13
+ handlerExpressionOf,
14
+ isComponentReceiver,
15
+ isTwoWayBinding,
16
+ } from "../template-ast.mjs";
13
17
 
14
18
  /**
15
19
  * ESLint rule to detect mutations of the value held by a signal.
@@ -17,7 +21,8 @@ import { handlerExpressionOf, isComponentReceiver } from "../template-ast.mjs";
17
21
  * Suggests using .update() or .set() instead.
18
22
  *
19
23
  * Applies to TypeScript files and to Angular templates, where the mutation is also
20
- * hidden in every two-way binding on a call result: [(ngModel)]="model().property".
24
+ * hidden in every two-way binding on a call result: [(ngModel)]="model().property", or on
25
+ * a name bound to it: [(model)]="form.property" inside @if (form(); as form).
21
26
  *
22
27
  * No signal's value is to be mutated, whatever its kind — a mutation notifies none of its
23
28
  * dependents. A view query is the single exception: it hands out a DOM element or a
@@ -106,6 +111,9 @@ const TEMPLATE_MEMBER_READS = new Set([
106
111
  "SafeKeyedRead",
107
112
  ]);
108
113
 
114
+ // Template wrappers that leave the value as is: `form()!`, `(form())`.
115
+ const TEMPLATE_WRAPPERS = new Set(["NonNullAssert", "ParenthesizedExpression"]);
116
+
109
117
  // ── TypeScript ───────────────────────────────────────────────────────────────
110
118
 
111
119
  function getCalledName(callee) {
@@ -140,9 +148,18 @@ function isObjectAssign(callee) {
140
148
  );
141
149
  }
142
150
 
151
+ // TypeScript wrappers that leave the value as is: `form()!`, `form() as Form`...
152
+ const TYPE_WRAPPERS = new Set([
153
+ "TSNonNullExpression",
154
+ "TSAsExpression",
155
+ "TSSatisfiesExpression",
156
+ "TSTypeAssertion",
157
+ ]);
158
+
143
159
  /**
144
160
  * What a member chain is rooted at, the property and index reads stripped away:
145
161
  * - `model().deep.property` => the `model()` call
162
+ * - `model()!.deep.property` => the `model()` call (a type wrapper keeps the value)
146
163
  * - `users[0].name` => the `users` name
147
164
  *
148
165
  * Only reads are walked through. A chain rooted at anything else — `items().filter(f)`,
@@ -151,8 +168,12 @@ function isObjectAssign(callee) {
151
168
  */
152
169
  function baseOf(node) {
153
170
  let current = node;
154
- while (current?.type === "MemberExpression") {
155
- current = current.object;
171
+ while (
172
+ current?.type === "MemberExpression" ||
173
+ TYPE_WRAPPERS.has(current?.type)
174
+ ) {
175
+ current =
176
+ current.type === "MemberExpression" ? current.object : current.expression;
156
177
  }
157
178
  return current;
158
179
  }
@@ -297,18 +318,32 @@ function signalRootTemplateCallKind(node, templateContext) {
297
318
  : undefined;
298
319
  }
299
320
 
321
+ // The expression itself, a type wrapper stripped away: `form()!` is the `form()` call.
322
+ function unwrapTemplate(node) {
323
+ let current = node;
324
+ while (TEMPLATE_WRAPPERS.has(current?.type)) {
325
+ current = current.expression;
326
+ }
327
+ return current;
328
+ }
329
+
300
330
  /**
301
331
  * Expression a template name is bound to, `undefined` when the name is a component
302
332
  * member. Mutating through such a name mutates whatever the expression yields:
303
- * `@for (user of model().users)` hands out the elements of the signal's array.
333
+ * `@for (user of model().users)` hands out the elements of the signal's array, and
334
+ * `@if (model(); as value)` the value of the signal itself.
304
335
  *
305
- * The item of a `@for` is only bound inside its own block, which the ancestor walk
306
- * enforces exactly; a `@let` is looked up by name, its declaration always preceding its
307
- * uses.
336
+ * The item of a `@for` and the alias of an `@if` are only bound inside their own block,
337
+ * which the ancestor walk enforces exactly, the nearest one shadowing the others; a `@let`
338
+ * is looked up by name, its declaration always preceding its uses.
308
339
  */
309
340
  function boundTemplateExpression(read, templateContext) {
310
341
  for (let current = read; current; current = current.parent) {
311
- if (current.type === "ForLoopBlock" && current.item?.name === read.name) {
342
+ if (
343
+ (current.type === "ForLoopBlock" && current.item?.name === read.name) ||
344
+ (current.type === "IfBlockBranch" &&
345
+ current.expressionAlias?.name === read.name)
346
+ ) {
312
347
  return current.expression?.ast ?? current.expression;
313
348
  }
314
349
  }
@@ -321,19 +356,21 @@ function boundTemplateExpression(read, templateContext) {
321
356
  * a template name bound to either.
322
357
  */
323
358
  function signalTemplateValueKind(node, templateContext, seen = new Set()) {
324
- if (node?.type === "Call" || node?.type === "SafeCall") {
325
- return signalRootTemplateCallKind(node, templateContext);
359
+ const value = unwrapTemplate(node);
360
+ if (value?.type === "Call" || value?.type === "SafeCall") {
361
+ return signalRootTemplateCallKind(value, templateContext);
326
362
  }
327
- if (!TEMPLATE_MEMBER_READS.has(node?.type)) {
363
+ if (!TEMPLATE_MEMBER_READS.has(value?.type)) {
328
364
  return undefined;
329
365
  }
330
366
 
331
- // Property and index reads are walked through; anything else roots a value of its own.
332
- let base = node;
333
- while (TEMPLATE_MEMBER_READS.has(base.receiver?.type)) {
334
- base = base.receiver;
367
+ // Property and index reads are walked through, as are the type wrappers; anything else
368
+ // roots a value of its own.
369
+ let base = value;
370
+ while (TEMPLATE_MEMBER_READS.has(unwrapTemplate(base.receiver)?.type)) {
371
+ base = unwrapTemplate(base.receiver);
335
372
  }
336
- const receiver = base.receiver;
373
+ const receiver = unwrapTemplate(base.receiver);
337
374
 
338
375
  if (receiver?.type === "Call" || receiver?.type === "SafeCall") {
339
376
  return signalRootTemplateCallKind(receiver, templateContext);
@@ -492,8 +529,9 @@ export default {
492
529
 
493
530
  BoundEvent(node) {
494
531
  // `[(x)]="model().property"` makes Angular assign into the value of the signal.
495
- // Reporting on the BoundEvent handler reports the binding exactly once.
496
- if (!node.name?.endsWith("Change")) {
532
+ // Reporting on the BoundEvent handler reports the binding exactly once. An
533
+ // explicit `(xChange)="…"` handler is a statement, checked as any other.
534
+ if (!isTwoWayBinding(node)) {
497
535
  return;
498
536
  }
499
537
  const handler = handlerExpressionOf(node);