@dsivd/prestations-ng 19.2.0-beta.3 → 19.2.0-beta.30
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/AI_GUIDELINES.md +152 -0
- package/ANGULAR_SIGNALS.md +6 -2
- package/CHANGELOG.md +476 -0
- package/CONTRIBUTING.md +128 -8
- package/ESLINT_PLUGIN.md +262 -30
- package/GOOD_PRACTICES.md +983 -388
- package/INTRODUCTION_ANGULAR_SIGNALS.md +227 -155
- package/README.md +22 -12
- package/UPGRADING_V18.md +5 -5
- package/UPGRADING_V19.md +636 -19
- package/eslint/configs/spec-ts-recommended.mjs +34 -0
- package/eslint/configs/template-recommended.mjs +2 -0
- package/eslint/configs/template-rules.mjs +14 -2
- package/eslint/configs/ts-recommended.mjs +11 -0
- package/eslint/configs/ts-rules.mjs +6 -2
- package/eslint/form-reads.mjs +63 -0
- package/eslint/index.mjs +17 -0
- package/eslint/rules/index.mjs +6 -0
- package/eslint/rules/no-direct-signal-mutation.mjs +162 -41
- package/eslint/rules/no-item-track-in-form-list.mjs +173 -0
- package/eslint/rules/no-uninvoked-signal-in-template.mjs +8 -3
- package/eslint/rules/require-model-change.mjs +89 -0
- package/eslint/rules/require-track-index.mjs +162 -0
- package/eslint/signal-names.mjs +127 -21
- package/eslint/template-ast.mjs +12 -2
- package/fesm2022/dsivd-prestations-ng.mjs +2889 -2061
- package/fesm2022/dsivd-prestations-ng.mjs.map +1 -1
- package/package.json +1 -2
- package/schematics/migration-collection.json +5 -0
- package/schematics/ng-add/index.js +2 -4
- package/schematics/ng-update/migrations/update-13_2_0/index.js +7 -2
- package/schematics/ng-update/migrations/update-19_2_0/index.js +52 -0
- package/schematics/utility/add-allowed-common-js-dependencies.js +39 -29
- package/schematics/utility/form-signal/class-migration.js +795 -0
- package/schematics/utility/form-signal/edits.js +34 -0
- package/schematics/utility/form-signal/form-members.js +318 -0
- package/schematics/utility/form-signal/form-signal-migration.js +134 -0
- package/schematics/utility/form-signal/page-classes.js +139 -0
- package/schematics/utility/form-signal/template-migration.js +775 -0
- package/schematics/utility/never-connected-migration.js +87 -0
- package/schematics/utility/remove-emit-decorator-metadata.js +8 -9
- package/schematics/utility/typescript-files.js +95 -0
- package/src/eslint/configs/__tests__/configs.test.mjs +61 -2
- package/src/eslint/configs/spec-ts-recommended.mjs +34 -0
- package/src/eslint/configs/template-recommended.mjs +2 -0
- package/src/eslint/configs/template-rules.mjs +14 -2
- package/src/eslint/configs/ts-recommended.mjs +11 -0
- package/src/eslint/configs/ts-rules.mjs +6 -2
- package/src/eslint/form-reads.mjs +63 -0
- package/src/eslint/index.mjs +17 -0
- package/src/eslint/rules/__tests__/no-direct-signal-mutation.test.mjs +200 -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/__tests__/require-track-index.test.mjs +106 -0
- package/src/eslint/rules/index.mjs +6 -0
- package/src/eslint/rules/no-direct-signal-mutation.mjs +162 -41
- package/src/eslint/rules/no-item-track-in-form-list.mjs +173 -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/rules/require-track-index.mjs +162 -0
- package/src/eslint/signal-names.mjs +127 -21
- package/src/eslint/template-ast.mjs +12 -2
- package/types/dsivd-prestations-ng.d.ts +719 -243
- package/dsivd-prestations-ng-19.2.0-beta.3.tgz +0 -0
|
@@ -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';
|
|
@@ -15,6 +16,8 @@ describe('no-direct-signal-mutation', () => {
|
|
|
15
16
|
let templatePath;
|
|
16
17
|
let inlineTemplatePath;
|
|
17
18
|
let unknownTemplatePath;
|
|
19
|
+
let pagePath;
|
|
20
|
+
let pageTemplatePath;
|
|
18
21
|
|
|
19
22
|
const write = (path, content) => {
|
|
20
23
|
mkdirSync(join(path, '..'), { recursive: true });
|
|
@@ -65,9 +68,47 @@ describe('no-direct-signal-mutation', () => {
|
|
|
65
68
|
readonly fromStream = toSignal(this.source$);
|
|
66
69
|
|
|
67
70
|
getConfig() { return { users: [] }; }
|
|
71
|
+
|
|
72
|
+
// Getters handing out the value of a signal, or a part of it
|
|
73
|
+
get firstUser() { return this.partenaire().users[0]; }
|
|
74
|
+
get partenaireUsers() { return this.partenaire()!.users; }
|
|
75
|
+
get users() { return this.partenaireUsers; }
|
|
76
|
+
get doubleUsers() { return this.double().users; }
|
|
77
|
+
get rowsElement() { return this.rows(); }
|
|
78
|
+
// Getters handing out a value of their own
|
|
79
|
+
get copy() { return { ...this.partenaire() }; }
|
|
80
|
+
get activeItems() { return this.items().filter((item) => item.active); }
|
|
81
|
+
get config() { return this.getConfig().users; }
|
|
82
|
+
get computedOnce() { const users = this.partenaire().users; return users; }
|
|
68
83
|
}`,
|
|
69
84
|
);
|
|
70
85
|
|
|
86
|
+
// A page whose getter is declared by its abstract parent, in the project
|
|
87
|
+
write(
|
|
88
|
+
join(root, 'src', 'abstract-page.ts'),
|
|
89
|
+
`import { signal } from '@angular/core';
|
|
90
|
+
|
|
91
|
+
export abstract class AbstractPage {
|
|
92
|
+
readonly form = signal<{ step: { motif: string; items: string[] } } | null>(null);
|
|
93
|
+
|
|
94
|
+
get step() { return this.form()!.step; }
|
|
95
|
+
get overridden() { return this.form()!.step; }
|
|
96
|
+
}`,
|
|
97
|
+
);
|
|
98
|
+
pagePath = join(root, 'src', 'page.component.ts');
|
|
99
|
+
write(
|
|
100
|
+
pagePath,
|
|
101
|
+
`import { Component } from '@angular/core';
|
|
102
|
+
import { AbstractPage } from './abstract-page';
|
|
103
|
+
|
|
104
|
+
@Component({ selector: 'app-page', templateUrl: './page.component.html' })
|
|
105
|
+
export class PageComponent extends AbstractPage {
|
|
106
|
+
get motif() { return this.step.motif; }
|
|
107
|
+
get overridden() { return structuredClone(this.form()!.step); }
|
|
108
|
+
}`,
|
|
109
|
+
);
|
|
110
|
+
pageTemplatePath = join(root, 'src', 'page.component.html');
|
|
111
|
+
|
|
71
112
|
templatePath = join(root, 'src', 'sample.component.html');
|
|
72
113
|
// Virtual block name produced by `angular.processInlineTemplates`.
|
|
73
114
|
inlineTemplatePath = join(
|
|
@@ -266,6 +307,125 @@ describe('no-direct-signal-mutation', () => {
|
|
|
266
307
|
errors: [{ messageId }],
|
|
267
308
|
});
|
|
268
309
|
|
|
310
|
+
it('should see through the TypeScript wrappers of the value', () => {
|
|
311
|
+
const typeScriptRuleTester = new RuleTester({
|
|
312
|
+
languageOptions: { parser: typeScriptParser },
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
typeScriptRuleTester.run('no-direct-signal-mutation', rule, {
|
|
316
|
+
valid: [ts('const name = this.inherited()!.name;')],
|
|
317
|
+
invalid: [
|
|
318
|
+
// The form of a page read with a non-null assertion (the ng update migration writes so)
|
|
319
|
+
invalidTs('this.inherited()!.name = value', 'directMutation'),
|
|
320
|
+
invalidTs('this.inherited()!.users.push(user)', 'inPlaceMutation'),
|
|
321
|
+
invalidTs('(this.partenaire() as Partenaire).name = value', 'directMutation'),
|
|
322
|
+
invalidTs('const form = this.inherited()!; form.name = value', 'directMutation'),
|
|
323
|
+
],
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it('should follow the getters handing out the value of a signal', () => {
|
|
328
|
+
const page = (code) => ({ code, filename: pagePath });
|
|
329
|
+
const viaGetter = (code, filename = componentPath) => ({
|
|
330
|
+
code,
|
|
331
|
+
filename,
|
|
332
|
+
errors: [{ messageId: 'getterMutation' }],
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
tsRuleTester.run('no-direct-signal-mutation', rule, {
|
|
336
|
+
valid: [
|
|
337
|
+
// Reads, and the setter of the getter itself
|
|
338
|
+
ts('const name = this.firstUser.name'),
|
|
339
|
+
ts('this.partenaireUsers = users'),
|
|
340
|
+
ts('this.users.filter((user) => user.active)'),
|
|
341
|
+
// A getter computing or copying its value
|
|
342
|
+
ts('this.copy.name = value'),
|
|
343
|
+
ts('this.activeItems.push(item)'),
|
|
344
|
+
ts('this.config.push(user)'),
|
|
345
|
+
// Only a getter made of a single return is followed
|
|
346
|
+
ts('this.computedOnce.push(user)'),
|
|
347
|
+
// A view query is there to be mutated, whatever reads it
|
|
348
|
+
ts('this.rowsElement.nativeElement.hidden = true'),
|
|
349
|
+
// Not a getter of the class
|
|
350
|
+
ts('this.other.motif = value'),
|
|
351
|
+
// A subclass overriding the getter with a copy
|
|
352
|
+
page('this.overridden.motif = value'),
|
|
353
|
+
],
|
|
354
|
+
invalid: [
|
|
355
|
+
viaGetter('this.firstUser.name = value'),
|
|
356
|
+
viaGetter('this.firstUser.name ??= value'),
|
|
357
|
+
viaGetter('this.firstUser.count++'),
|
|
358
|
+
viaGetter('this.partenaireUsers.push(user)'),
|
|
359
|
+
viaGetter('this.partenaireUsers[0] = user'),
|
|
360
|
+
viaGetter('delete this.partenaireUsers[0]'),
|
|
361
|
+
viaGetter('Object.assign(this.firstUser, changes)'),
|
|
362
|
+
viaGetter('this.partenaireUsers.forEach((user) => { user.active = true; })'),
|
|
363
|
+
viaGetter('const users = this.partenaireUsers; users.splice(0, 1)'),
|
|
364
|
+
// A getter reading another one
|
|
365
|
+
viaGetter('this.users.sort()'),
|
|
366
|
+
// A computed(): the advice of its kind
|
|
367
|
+
{
|
|
368
|
+
...ts('this.doubleUsers.push(user)'),
|
|
369
|
+
errors: [
|
|
370
|
+
{
|
|
371
|
+
message:
|
|
372
|
+
'Mutation of the value of a computed() `double` through the getter `doubleUsers` detected, ' +
|
|
373
|
+
'which notifies none of its dependents. It is rebuilt from its sources, so the change is dropped ' +
|
|
374
|
+
'on the next recomputation — mutate the signal it is computed from.',
|
|
375
|
+
},
|
|
376
|
+
],
|
|
377
|
+
},
|
|
378
|
+
// The getter of the abstract parent page, and a getter of the page reading it
|
|
379
|
+
{
|
|
380
|
+
...page('this.step.motif = value'),
|
|
381
|
+
errors: [
|
|
382
|
+
{
|
|
383
|
+
message:
|
|
384
|
+
'Mutation of the value of the signal `form` through the getter `step` detected, which notifies ' +
|
|
385
|
+
'none of its dependents. Write a new value with .set() or .update() instead (patchForm() for the form ' +
|
|
386
|
+
'of a page).',
|
|
387
|
+
},
|
|
388
|
+
],
|
|
389
|
+
},
|
|
390
|
+
viaGetter('this.step.items.push(item)', pagePath),
|
|
391
|
+
viaGetter('this.motif.length = 0', pagePath),
|
|
392
|
+
],
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
templateRuleTester.run('no-direct-signal-mutation', rule, {
|
|
396
|
+
valid: [
|
|
397
|
+
template('{{ firstUser.name }}'),
|
|
398
|
+
template('<input [value]="partenaireUsers[0]" />'),
|
|
399
|
+
// The setter of the getter itself
|
|
400
|
+
template('<button (click)="partenaireUsers = users"></button>'),
|
|
401
|
+
template('<app-users [(users)]="partenaireUsers" />'),
|
|
402
|
+
template('<button (click)="copy.name = value"></button>'),
|
|
403
|
+
template('<button (click)="activeItems.push(item)"></button>'),
|
|
404
|
+
// A name of the template shadows the getter
|
|
405
|
+
template('@for (firstUser of list; track $index) { <button (click)="firstUser.name = value"></button> }'),
|
|
406
|
+
{ code: '<button (click)="overridden.motif = value"></button>', filename: pageTemplatePath },
|
|
407
|
+
],
|
|
408
|
+
invalid: [
|
|
409
|
+
viaGetter('<button (click)="firstUser.name = value"></button>', templatePath),
|
|
410
|
+
viaGetter('<input [(ngModel)]="firstUser.name" />', templatePath),
|
|
411
|
+
viaGetter('<button (click)="partenaireUsers.push(user)"></button>', templatePath),
|
|
412
|
+
viaGetter('@for (user of partenaireUsers; track $index) { <button (click)="user.active = true"></button> }', templatePath),
|
|
413
|
+
viaGetter('<foehn-input-text name="motif" [(model)]="step.motif" />', pageTemplatePath),
|
|
414
|
+
viaGetter('<button (click)="step.items.splice(0, 1)"></button>', pageTemplatePath),
|
|
415
|
+
],
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it('should report an increment of a member of the value', () => {
|
|
420
|
+
tsRuleTester.run('no-direct-signal-mutation', rule, {
|
|
421
|
+
valid: [ts('this.count++'), ts('let count = 0; count++')],
|
|
422
|
+
invalid: [
|
|
423
|
+
invalidTs('this.partenaire().count++', 'directMutation'),
|
|
424
|
+
invalidTs('--this.partenaire().users.length', 'directMutation'),
|
|
425
|
+
],
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
|
|
269
429
|
it('should validate signal mutation rule in templates', () => {
|
|
270
430
|
templateRuleTester.run('no-direct-signal-mutation', rule, {
|
|
271
431
|
valid: [
|
|
@@ -305,6 +465,10 @@ describe('no-direct-signal-mutation', () => {
|
|
|
305
465
|
|
|
306
466
|
// An output named `…Change` that carries no assignment
|
|
307
467
|
template('<app-child (valueChange)="comment.set($event)"></app-child>'),
|
|
468
|
+
// An explicit handler is a statement, not the target of a two-way binding: a call
|
|
469
|
+
// assigns nothing
|
|
470
|
+
template('<app-child (valueChange)="partenaire()"></app-child>'),
|
|
471
|
+
template('<app-child [value]="partenaire().name" (valueChange)="partenaire()"></app-child>'),
|
|
308
472
|
|
|
309
473
|
// A `@for` item is only a signal's value when the loop iterates a signal
|
|
310
474
|
template('@for (u of getConfig().users; track u) { <button (click)="u.done = 1"></button> }'),
|
|
@@ -314,6 +478,17 @@ describe('no-direct-signal-mutation', () => {
|
|
|
314
478
|
template('@for (u of items(); track u) { {{ u.name }} }'),
|
|
315
479
|
template('@let copy = getConfig().users; <button (click)="copy.push(item)"></button>'),
|
|
316
480
|
|
|
481
|
+
// An `@if` alias is only a signal's value when the condition is one
|
|
482
|
+
template('@if (getConfig(); as config) { <input [(ngModel)]="config.name" /> }'),
|
|
483
|
+
// Reading it mutates nothing
|
|
484
|
+
template('@if (partenaire(); as p) { {{ p.name }} }'),
|
|
485
|
+
// It is bound only inside its own block
|
|
486
|
+
template('@if (partenaire(); as p) { } <button (click)="p.name = value"></button>'),
|
|
487
|
+
// The nearest name shadows the others
|
|
488
|
+
template(`@if (partenaire(); as u) {
|
|
489
|
+
@for (u of getConfig().users; track u) { <button (click)="u.done = 1"></button> }
|
|
490
|
+
}`),
|
|
491
|
+
|
|
317
492
|
// A computed member whose method does not mutate
|
|
318
493
|
template(`<button (click)="items()['toSorted']()"></button>`),
|
|
319
494
|
template('<button (click)="items().filter(active).sort()"></button>'),
|
|
@@ -355,6 +530,25 @@ describe('no-direct-signal-mutation', () => {
|
|
|
355
530
|
invalidTemplate('@let users = partenaire().users; <button (click)="users.push(user)"></button>', 'inPlaceMutation'),
|
|
356
531
|
invalidTemplate('@let value = partenaire(); <button (click)="value.name = other"></button>', 'directMutation'),
|
|
357
532
|
|
|
533
|
+
// Same through the alias of an `@if`, bound only inside its own branch
|
|
534
|
+
invalidTemplate('@if (partenaire(); as p) { <input [(ngModel)]="p.name" /> }', 'directMutation'),
|
|
535
|
+
invalidTemplate('@if (partenaire(); as p) { <button (click)="p.name = value"></button> }', 'directMutation'),
|
|
536
|
+
invalidTemplate('@if (partenaire(); as p) { <button (click)="p.users.push(user)"></button> }', 'inPlaceMutation'),
|
|
537
|
+
invalidTemplate('@if (ready) { } @else if (partenaire(); as p) { <input [(ngModel)]="p.name" /> }', 'directMutation'),
|
|
538
|
+
invalidTemplate(`@if (partenaire(); as p) {
|
|
539
|
+
@if (p.users; as users) { <button (click)="users.push(user)"></button> }
|
|
540
|
+
}`, 'inPlaceMutation'),
|
|
541
|
+
// The alias named as the signal, as in `@if (form(); as form)`
|
|
542
|
+
invalidTemplate('@if (partenaire(); as partenaire) { <input [(ngModel)]="partenaire.name" /> }', 'directMutation'),
|
|
543
|
+
invalidTemplate('@if (double(); as d) { <button (click)="d.users.push(user)"></button> }', 'readOnlyMutation'),
|
|
544
|
+
|
|
545
|
+
// A non-null assertion or parentheses keep the value as is
|
|
546
|
+
invalidTemplate('<input [(ngModel)]="partenaire()!.name" />', 'directMutation'),
|
|
547
|
+
invalidTemplate('<button (click)="(partenaire()).name = value"></button>', 'directMutation'),
|
|
548
|
+
invalidTemplate('<button (click)="items()!.push(item)"></button>', 'inPlaceMutation'),
|
|
549
|
+
invalidTemplate('@let value = partenaire()!; <button (click)="value.name = other"></button>', 'directMutation'),
|
|
550
|
+
invalidTemplate('@let value = partenaire(); <button (click)="value!.name = other"></button>', 'directMutation'),
|
|
551
|
+
|
|
358
552
|
// Every other kind of signal, each with the advice that applies to it
|
|
359
553
|
invalidTemplate('<button (click)="double().users.push(user)"></button>', 'readOnlyMutation'),
|
|
360
554
|
invalidTemplate('<button (click)="formArray().push(control)"></button>', 'readOnlyMutation'),
|
|
@@ -399,6 +593,12 @@ describe('no-direct-signal-mutation', () => {
|
|
|
399
593
|
code: '<button (click)="map.get(key).name = value"></button>',
|
|
400
594
|
filename: unknownTemplatePath,
|
|
401
595
|
},
|
|
596
|
+
// A template shared by several components: a `…Change` handler calling a method
|
|
597
|
+
// assigns nothing
|
|
598
|
+
{
|
|
599
|
+
code: '<app-child [(value)]="item.weight" (valueChange)="updateModel()"></app-child>',
|
|
600
|
+
filename: unknownTemplatePath,
|
|
601
|
+
},
|
|
402
602
|
],
|
|
403
603
|
invalid: [
|
|
404
604
|
{
|
|
@@ -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
|
+
});
|