@dsivd/prestations-ng 19.2.0-beta.20 → 19.2.0-beta.22
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/ANGULAR_SIGNALS.md +4 -0
- package/CHANGELOG.md +113 -3
- package/CONTRIBUTING.md +2 -2
- package/ESLINT_PLUGIN.md +146 -28
- package/GOOD_PRACTICES.md +258 -58
- package/INTRODUCTION_ANGULAR_SIGNALS.md +29 -2
- package/UPGRADING_V19.md +233 -31
- package/eslint/configs/template-rules.mjs +5 -0
- package/eslint/configs/ts-recommended.mjs +4 -0
- package/eslint/form-reads.mjs +63 -0
- package/eslint/rules/index.mjs +4 -0
- package/eslint/rules/no-direct-signal-mutation.mjs +57 -19
- package/eslint/rules/no-item-track-in-form-list.mjs +172 -0
- package/eslint/rules/no-uninvoked-signal-in-template.mjs +8 -3
- package/eslint/rules/require-model-change.mjs +89 -0
- package/eslint/template-ast.mjs +12 -2
- package/fesm2022/dsivd-prestations-ng.mjs +948 -692
- package/fesm2022/dsivd-prestations-ng.mjs.map +1 -1
- package/package.json +1 -1
- package/schematics/migration-collection.json +5 -0
- package/schematics/ng-update/migrations/update-19_2_0/index.js +39 -0
- package/schematics/utility/form-signal/class-migration.js +671 -0
- package/schematics/utility/form-signal/edits.js +34 -0
- package/schematics/utility/form-signal/form-signal-migration.js +184 -0
- package/schematics/utility/form-signal/page-classes.js +129 -0
- package/schematics/utility/form-signal/template-migration.js +666 -0
- package/src/eslint/configs/template-rules.mjs +5 -0
- package/src/eslint/configs/ts-recommended.mjs +4 -0
- package/src/eslint/form-reads.mjs +63 -0
- package/src/eslint/rules/__tests__/no-direct-signal-mutation.test.mjs +58 -0
- package/src/eslint/rules/__tests__/no-item-track-in-form-list.test.mjs +234 -0
- package/src/eslint/rules/__tests__/no-uninvoked-signal-in-template.test.mjs +6 -0
- package/src/eslint/rules/__tests__/require-model-change.test.mjs +93 -0
- package/src/eslint/rules/index.mjs +4 -0
- package/src/eslint/rules/no-direct-signal-mutation.mjs +57 -19
- package/src/eslint/rules/no-item-track-in-form-list.mjs +172 -0
- package/src/eslint/rules/no-uninvoked-signal-in-template.mjs +8 -3
- package/src/eslint/rules/require-model-change.mjs +89 -0
- package/src/eslint/template-ast.mjs +12 -2
- package/types/dsivd-prestations-ng.d.ts +266 -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,234 @@
|
|
|
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
|
+
// A field of the item read at the index of the row
|
|
126
|
+
code: `@if (form(); as form) {
|
|
127
|
+
@for (child of form.children; track child) {
|
|
128
|
+
<foehn-input-text [model]="form.children[$index].firstName" (modelChange)="patchForm(['children', $index, 'firstName'], $event)" />
|
|
129
|
+
}
|
|
130
|
+
}`,
|
|
131
|
+
errors: [
|
|
132
|
+
{
|
|
133
|
+
messageId: 'itemTrack',
|
|
134
|
+
suggestions: [
|
|
135
|
+
{
|
|
136
|
+
messageId: 'trackByIndex',
|
|
137
|
+
output: `@if (form(); as form) {
|
|
138
|
+
@for (child of form.children; track $index) {
|
|
139
|
+
<foehn-input-text [model]="form.children[$index].firstName" (modelChange)="patchForm(['children', $index, 'firstName'], $event)" />
|
|
140
|
+
}
|
|
141
|
+
}`,
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// A component extending FoehnInputComponent writes a new list in its model, its items replaced
|
|
152
|
+
const modelRows = (forHeader, model) => `@for (${forHeader}) {
|
|
153
|
+
<foehn-input-textarea [model]="${model}" (modelChange)="updateItem(i, $event)" />
|
|
154
|
+
}`;
|
|
155
|
+
|
|
156
|
+
it('should report the rows of the list of the model tracked by their item', () => {
|
|
157
|
+
ruleTester.run('no-item-track-in-form-list', rule, {
|
|
158
|
+
valid: [
|
|
159
|
+
modelRows('text of model(); track $index; let i = $index', 'model()[i]'),
|
|
160
|
+
modelRows('text of model(); track $index; let i = $index', 'text'),
|
|
161
|
+
// Another list than the model: its items keep their identity
|
|
162
|
+
modelRows('text of texts(); track text; let i = $index', 'texts()[i]'),
|
|
163
|
+
// Another list read at the index
|
|
164
|
+
modelRows('text of model(); track text; let i = $index', 'others()[i]'),
|
|
165
|
+
// Only displayed
|
|
166
|
+
`@for (text of model(); track text) {
|
|
167
|
+
<p>{{ text }}</p>
|
|
168
|
+
}`,
|
|
169
|
+
// The $index of the inner @for, over another list
|
|
170
|
+
`@for (row of model(); track row) {
|
|
171
|
+
@for (cell of row.cells; track $index) {
|
|
172
|
+
<foehn-input-text [model]="model()[$index]" (modelChange)="updateCell($index, $event)" />
|
|
173
|
+
}
|
|
174
|
+
}`,
|
|
175
|
+
],
|
|
176
|
+
invalid: [
|
|
177
|
+
{
|
|
178
|
+
// The list of the model read at the index of the row, through an alias of $index
|
|
179
|
+
code: modelRows('text of model(); track text; let i = $index', 'model()[i]'),
|
|
180
|
+
errors: [
|
|
181
|
+
{
|
|
182
|
+
messageId: 'itemTrack',
|
|
183
|
+
data: { item: 'text' },
|
|
184
|
+
suggestions: [
|
|
185
|
+
{
|
|
186
|
+
messageId: 'trackByIndex',
|
|
187
|
+
output: modelRows(
|
|
188
|
+
'text of model(); track $index; let i = $index',
|
|
189
|
+
'model()[i]',
|
|
190
|
+
),
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
// The list of the model read at $index
|
|
198
|
+
code: modelRows('text of model(); track text', 'model()[$index]'),
|
|
199
|
+
errors: [
|
|
200
|
+
{
|
|
201
|
+
messageId: 'itemTrack',
|
|
202
|
+
suggestions: [
|
|
203
|
+
{
|
|
204
|
+
messageId: 'trackByIndex',
|
|
205
|
+
output: modelRows('text of model(); track $index', 'model()[$index]'),
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
// An item of the list of the model, bound as a whole
|
|
213
|
+
code: `@for (vehicule of model(); track vehicule) {
|
|
214
|
+
<app-vehicule-item [model]="vehicule" (modelChange)="updateVehiculesList($event, $index)" />
|
|
215
|
+
}`,
|
|
216
|
+
errors: [
|
|
217
|
+
{
|
|
218
|
+
messageId: 'itemTrack',
|
|
219
|
+
data: { item: 'vehicule' },
|
|
220
|
+
suggestions: [
|
|
221
|
+
{
|
|
222
|
+
messageId: 'trackByIndex',
|
|
223
|
+
output: `@for (vehicule of model(); track $index) {
|
|
224
|
+
<app-vehicule-item [model]="vehicule" (modelChange)="updateVehiculesList($event, $index)" />
|
|
225
|
+
}`,
|
|
226
|
+
},
|
|
227
|
+
],
|
|
228
|
+
},
|
|
229
|
+
],
|
|
230
|
+
},
|
|
231
|
+
],
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
});
|
|
@@ -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
|
};
|