@dsivd/prestations-ng 19.2.0-beta.27 → 19.2.0-beta.29
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 +30 -9
- package/ANGULAR_SIGNALS.md +2 -2
- package/CHANGELOG.md +51 -3
- package/CONTRIBUTING.md +54 -12
- package/ESLINT_PLUGIN.md +110 -13
- package/GOOD_PRACTICES.md +34 -10
- package/README.md +18 -12
- package/UPGRADING_V19.md +154 -7
- package/eslint/configs/template-rules.mjs +3 -0
- package/eslint/rules/index.mjs +2 -0
- package/eslint/rules/no-direct-signal-mutation.mjs +104 -21
- package/eslint/rules/no-item-track-in-form-list.mjs +4 -3
- package/eslint/rules/require-track-index.mjs +162 -0
- package/eslint/signal-names.mjs +127 -21
- package/fesm2022/dsivd-prestations-ng.mjs +68 -22
- package/fesm2022/dsivd-prestations-ng.mjs.map +1 -1
- package/package.json +1 -1
- package/schematics/migration-collection.json +1 -1
- package/schematics/ng-update/migrations/update-19_2_0/index.js +15 -3
- package/schematics/utility/form-signal/class-migration.js +130 -6
- package/schematics/utility/form-signal/form-members.js +318 -0
- package/schematics/utility/form-signal/form-signal-migration.js +13 -75
- package/schematics/utility/form-signal/page-classes.js +11 -1
- package/schematics/utility/form-signal/template-migration.js +119 -10
- package/schematics/utility/never-connected-migration.js +87 -0
- package/schematics/utility/typescript-files.js +95 -0
- package/src/eslint/configs/template-rules.mjs +3 -0
- package/src/eslint/rules/__tests__/no-direct-signal-mutation.test.mjs +142 -0
- package/src/eslint/rules/__tests__/require-track-index.test.mjs +106 -0
- package/src/eslint/rules/index.mjs +2 -0
- package/src/eslint/rules/no-direct-signal-mutation.mjs +104 -21
- package/src/eslint/rules/no-item-track-in-form-list.mjs +4 -3
- package/src/eslint/rules/require-track-index.mjs +162 -0
- package/src/eslint/signal-names.mjs +127 -21
- package/types/dsivd-prestations-ng.d.ts +19 -3
|
@@ -16,6 +16,8 @@ describe('no-direct-signal-mutation', () => {
|
|
|
16
16
|
let templatePath;
|
|
17
17
|
let inlineTemplatePath;
|
|
18
18
|
let unknownTemplatePath;
|
|
19
|
+
let pagePath;
|
|
20
|
+
let pageTemplatePath;
|
|
19
21
|
|
|
20
22
|
const write = (path, content) => {
|
|
21
23
|
mkdirSync(join(path, '..'), { recursive: true });
|
|
@@ -66,8 +68,46 @@ describe('no-direct-signal-mutation', () => {
|
|
|
66
68
|
readonly fromStream = toSignal(this.source$);
|
|
67
69
|
|
|
68
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; }
|
|
83
|
+
}`,
|
|
84
|
+
);
|
|
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); }
|
|
69
108
|
}`,
|
|
70
109
|
);
|
|
110
|
+
pageTemplatePath = join(root, 'src', 'page.component.html');
|
|
71
111
|
|
|
72
112
|
templatePath = join(root, 'src', 'sample.component.html');
|
|
73
113
|
// Virtual block name produced by `angular.processInlineTemplates`.
|
|
@@ -284,6 +324,108 @@ describe('no-direct-signal-mutation', () => {
|
|
|
284
324
|
});
|
|
285
325
|
});
|
|
286
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
|
+
|
|
287
429
|
it('should validate signal mutation rule in templates', () => {
|
|
288
430
|
templateRuleTester.run('no-direct-signal-mutation', rule, {
|
|
289
431
|
valid: [
|
|
@@ -0,0 +1,106 @@
|
|
|
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-track-index.mjs';
|
|
5
|
+
|
|
6
|
+
describe('require-track-index', () => {
|
|
7
|
+
const ruleTester = new RuleTester({
|
|
8
|
+
languageOptions: { parser: templateParser },
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const namedRows = (forHeader, name = `[name]="'children[' + $index + '].firstName'"`) =>
|
|
12
|
+
`@for (${forHeader}) {
|
|
13
|
+
<foehn-input-text
|
|
14
|
+
${name}
|
|
15
|
+
[model]="child.firstName"
|
|
16
|
+
(modelChange)="patchForm(['children', $index, 'firstName'], $event)"
|
|
17
|
+
/>
|
|
18
|
+
}`;
|
|
19
|
+
|
|
20
|
+
const reported = (code, track, binding) => ({
|
|
21
|
+
code,
|
|
22
|
+
errors: [
|
|
23
|
+
{
|
|
24
|
+
messageId: 'indexTrack',
|
|
25
|
+
data: { track, binding },
|
|
26
|
+
suggestions: [
|
|
27
|
+
{
|
|
28
|
+
messageId: 'trackByIndex',
|
|
29
|
+
output: code.replace(`track ${track}`, 'track $index'),
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should report the rows naming their fields with their index, not tracked by it', () => {
|
|
37
|
+
ruleTester.run('require-track-index', rule, {
|
|
38
|
+
valid: [
|
|
39
|
+
namedRows('child of children(); track $index'),
|
|
40
|
+
namedRows(
|
|
41
|
+
'child of children(); track i; let i = $index',
|
|
42
|
+
`[name]="'children[' + i + '].firstName'"`,
|
|
43
|
+
),
|
|
44
|
+
namedRows('child of children(); track $index', 'name="child{{ $index }}"'),
|
|
45
|
+
// The names don't depend on the position of the row
|
|
46
|
+
namedRows('child of children(); track child.id', `[name]="'child-' + child.id"`),
|
|
47
|
+
namedRows('child of children(); track child.id', 'name="firstName"'),
|
|
48
|
+
// Another variable of the @for
|
|
49
|
+
namedRows(
|
|
50
|
+
'child of children(); track child.id; let first = $first',
|
|
51
|
+
`[name]="'children' + first"`,
|
|
52
|
+
),
|
|
53
|
+
// The names use the index of the inner @for only, tracked by it
|
|
54
|
+
`@for (group of groups(); track group.id) {
|
|
55
|
+
@for (child of group.children; track $index) {
|
|
56
|
+
<input [name]="'children[' + $index + ']'" />
|
|
57
|
+
}
|
|
58
|
+
}`,
|
|
59
|
+
// A name read on another object
|
|
60
|
+
namedRows('child of children(); track child.id', `[name]="child.$index"`),
|
|
61
|
+
// An input that isn't a name
|
|
62
|
+
`@for (child of children(); track child.id) {
|
|
63
|
+
<foehn-input-text [label]="'Child ' + $index" name="firstName" />
|
|
64
|
+
}`,
|
|
65
|
+
],
|
|
66
|
+
invalid: [
|
|
67
|
+
reported(
|
|
68
|
+
namedRows('child of children(); track child.id'),
|
|
69
|
+
'child.id',
|
|
70
|
+
`name="'children[' + $index + '].firstName'"`,
|
|
71
|
+
),
|
|
72
|
+
reported(
|
|
73
|
+
namedRows('child of form.children; track child'),
|
|
74
|
+
'child',
|
|
75
|
+
`name="'children[' + $index + '].firstName'"`,
|
|
76
|
+
),
|
|
77
|
+
reported(
|
|
78
|
+
namedRows(
|
|
79
|
+
'child of children(); track child.trackingIndex; let i = $index',
|
|
80
|
+
'name="child{{ i }}"',
|
|
81
|
+
),
|
|
82
|
+
'child.trackingIndex',
|
|
83
|
+
'name="child{{ i }}"',
|
|
84
|
+
),
|
|
85
|
+
// The errors of the row (foehn-list-summary)
|
|
86
|
+
reported(
|
|
87
|
+
`@for (item of model(); track trackFoehnListItem(itemIndex, item); let itemIndex = $index) {
|
|
88
|
+
<foehn-error-pill [errorPrefix]="name() + '[' + itemIndex + ']'" />
|
|
89
|
+
}`,
|
|
90
|
+
'trackFoehnListItem(itemIndex, item)',
|
|
91
|
+
`errorPrefix="name() + '[' + itemIndex + ']'"`,
|
|
92
|
+
),
|
|
93
|
+
// The names use the index of the outer @for: the outer one is reported
|
|
94
|
+
reported(
|
|
95
|
+
`@for (group of groups(); track group.id; let g = $index) {
|
|
96
|
+
@for (child of group.children; track $index) {
|
|
97
|
+
<input [name]="'groups[' + g + '].children[' + $index + ']'" />
|
|
98
|
+
}
|
|
99
|
+
}`,
|
|
100
|
+
'group.id',
|
|
101
|
+
`name="'groups[' + g + '].children[' + $index + ']'"`,
|
|
102
|
+
),
|
|
103
|
+
],
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
});
|
|
@@ -2,10 +2,12 @@ import noDirectSignalMutation from './no-direct-signal-mutation.mjs';
|
|
|
2
2
|
import noItemTrackInFormList from './no-item-track-in-form-list.mjs';
|
|
3
3
|
import noUninvokedSignalInTemplate from './no-uninvoked-signal-in-template.mjs';
|
|
4
4
|
import requireModelChange from './require-model-change.mjs';
|
|
5
|
+
import requireTrackIndex from './require-track-index.mjs';
|
|
5
6
|
|
|
6
7
|
export default {
|
|
7
8
|
'no-direct-signal-mutation': noDirectSignalMutation,
|
|
8
9
|
'no-item-track-in-form-list': noItemTrackInFormList,
|
|
9
10
|
'no-uninvoked-signal-in-template': noUninvokedSignalInTemplate,
|
|
10
11
|
'require-model-change': requireModelChange,
|
|
12
|
+
'require-track-index': requireTrackIndex,
|
|
11
13
|
};
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
VIEW_QUERY,
|
|
9
9
|
WRITABLE,
|
|
10
10
|
} from "../signal-kinds.mjs";
|
|
11
|
-
import { collectSignalKinds } from "../signal-names.mjs";
|
|
11
|
+
import { collectSignalAliases, collectSignalKinds } from "../signal-names.mjs";
|
|
12
12
|
import {
|
|
13
13
|
handlerExpressionOf,
|
|
14
14
|
isComponentReceiver,
|
|
@@ -30,6 +30,10 @@ import {
|
|
|
30
30
|
*
|
|
31
31
|
* Only the signals the class really declares are inspected, resolved through the whole
|
|
32
32
|
* inheritance chain, so a plain method (`this.getConfig().items.push(x)`) is left alone.
|
|
33
|
+
*
|
|
34
|
+
* A getter of the class returning the value of a signal, or a part of it
|
|
35
|
+
* (`get step() { return this.form()!.step; }`), hands out that value too: a write through
|
|
36
|
+
* it (`this.step.motif = x`, `[(model)]="step.motif"`) is reported as well.
|
|
33
37
|
*/
|
|
34
38
|
|
|
35
39
|
// Kinds a mutation is not reported on: a view query hands out a DOM element or a component
|
|
@@ -201,6 +205,33 @@ function signalFactoryKind(node) {
|
|
|
201
205
|
return callee?.type === "Identifier" ? factoryKind(callee.name) : undefined;
|
|
202
206
|
}
|
|
203
207
|
|
|
208
|
+
// The value of a signal of the class: its kind, and the getter it was read through, if any.
|
|
209
|
+
const valueOf = (kind) => (kind ? { kind } : undefined);
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The getter of the class a member chain reads first, `this.step` in `this.step.motif`,
|
|
213
|
+
* when it aliases the value of a signal. `undefined` for anything else.
|
|
214
|
+
*/
|
|
215
|
+
function aliasReadOf(node, scopeContext) {
|
|
216
|
+
let current = node;
|
|
217
|
+
while (
|
|
218
|
+
current?.type === "MemberExpression" ||
|
|
219
|
+
TYPE_WRAPPERS.has(current?.type)
|
|
220
|
+
) {
|
|
221
|
+
if (
|
|
222
|
+
current.type === "MemberExpression" &&
|
|
223
|
+
current.object.type === "ThisExpression"
|
|
224
|
+
) {
|
|
225
|
+
const name = current.computed ? undefined : getCalledName(current);
|
|
226
|
+
const alias = name ? scopeContext.alias(name) : undefined;
|
|
227
|
+
return alias ? { ...alias, getter: name } : undefined;
|
|
228
|
+
}
|
|
229
|
+
current =
|
|
230
|
+
current.type === "MemberExpression" ? current.object : current.expression;
|
|
231
|
+
}
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
|
|
204
235
|
function signalRootCallKind(node, scopeContext) {
|
|
205
236
|
if (node.arguments.length !== 0) {
|
|
206
237
|
return undefined;
|
|
@@ -233,14 +264,18 @@ function signalRootCallKind(node, scopeContext) {
|
|
|
233
264
|
}
|
|
234
265
|
|
|
235
266
|
/**
|
|
236
|
-
*
|
|
237
|
-
* The expression may be the signal read itself (`this.model()`), a member of
|
|
238
|
-
* (`this.model().items`),
|
|
267
|
+
* The value of a signal an expression evaluates to (`{ kind, getter? }`), `undefined` when
|
|
268
|
+
* it is not one. The expression may be the signal read itself (`this.model()`), a member of
|
|
269
|
+
* its value (`this.model().items`), a getter aliasing either (`this.step.items`), or a name
|
|
270
|
+
* bound to any of them.
|
|
239
271
|
*/
|
|
240
272
|
function signalValueKind(node, scopeContext, seen = new Set()) {
|
|
241
273
|
const base = baseOf(node);
|
|
242
274
|
if (base?.type === "CallExpression") {
|
|
243
|
-
return signalRootCallKind(base, scopeContext);
|
|
275
|
+
return valueOf(signalRootCallKind(base, scopeContext));
|
|
276
|
+
}
|
|
277
|
+
if (base?.type === "ThisExpression") {
|
|
278
|
+
return aliasReadOf(node, scopeContext);
|
|
244
279
|
}
|
|
245
280
|
return base?.type === "Identifier"
|
|
246
281
|
? boundNameSignalKind(base, scopeContext, seen)
|
|
@@ -314,7 +349,7 @@ function signalRootTemplateCallKind(node, templateContext) {
|
|
|
314
349
|
return undefined;
|
|
315
350
|
}
|
|
316
351
|
return isComponentReceiver(callee.receiver)
|
|
317
|
-
? templateContext.memberKind(callee.name ?? "")
|
|
352
|
+
? valueOf(templateContext.memberKind(callee.name ?? ""))
|
|
318
353
|
: undefined;
|
|
319
354
|
}
|
|
320
355
|
|
|
@@ -352,10 +387,19 @@ function boundTemplateExpression(read, templateContext) {
|
|
|
352
387
|
}
|
|
353
388
|
|
|
354
389
|
/**
|
|
355
|
-
* Template counterpart of `signalValueKind`: the value a signal holds, a member of it,
|
|
356
|
-
* a template name bound to
|
|
390
|
+
* Template counterpart of `signalValueKind`: the value a signal holds, a member of it, a
|
|
391
|
+
* getter of the component aliasing either, or a template name bound to any of them.
|
|
392
|
+
*
|
|
393
|
+
* `assigned`: the expression is the target of an assignment (`step.motif = x`, or the
|
|
394
|
+
* expression of a two-way binding), so the getter itself as the whole target
|
|
395
|
+
* (`step = x`) calls its setter and mutates nothing.
|
|
357
396
|
*/
|
|
358
|
-
function signalTemplateValueKind(
|
|
397
|
+
function signalTemplateValueKind(
|
|
398
|
+
node,
|
|
399
|
+
templateContext,
|
|
400
|
+
seen = new Set(),
|
|
401
|
+
assigned = false,
|
|
402
|
+
) {
|
|
359
403
|
const value = unwrapTemplate(node);
|
|
360
404
|
if (value?.type === "Call" || value?.type === "SafeCall") {
|
|
361
405
|
return signalRootTemplateCallKind(value, templateContext);
|
|
@@ -379,11 +423,15 @@ function signalTemplateValueKind(node, templateContext, seen = new Set()) {
|
|
|
379
423
|
return undefined;
|
|
380
424
|
}
|
|
381
425
|
seen.add(base.name);
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
426
|
+
const bound = boundTemplateExpression(base, templateContext);
|
|
427
|
+
if (bound === undefined) {
|
|
428
|
+
// A member of the component, not shadowed by a name of the template.
|
|
429
|
+
const alias = templateContext.alias(base.name);
|
|
430
|
+
return alias && (value !== base || !assigned)
|
|
431
|
+
? { ...alias, getter: base.name }
|
|
432
|
+
: undefined;
|
|
433
|
+
}
|
|
434
|
+
return signalTemplateValueKind(bound, templateContext, seen);
|
|
387
435
|
}
|
|
388
436
|
|
|
389
437
|
// Method a template call invokes, `undefined` when the name is not statically known.
|
|
@@ -413,6 +461,8 @@ export default {
|
|
|
413
461
|
"In-place mutation of a signal's value detected. `{{method}}` changes the value without notifying the signal: use .update() with a new value instead.",
|
|
414
462
|
readOnlyMutation:
|
|
415
463
|
"Mutation of the value of {{kind}} detected, which notifies none of its dependents. {{advice}}.",
|
|
464
|
+
getterMutation:
|
|
465
|
+
"Mutation of the value of {{signal}} through the getter `{{getter}}` detected, which notifies none of its dependents. {{advice}}.",
|
|
416
466
|
},
|
|
417
467
|
},
|
|
418
468
|
|
|
@@ -422,18 +472,41 @@ export default {
|
|
|
422
472
|
const declaringPath =
|
|
423
473
|
resolveComponentPath(context.filename) ?? context.filename;
|
|
424
474
|
const memberKinds = collectSignalKinds(declaringPath);
|
|
475
|
+
const aliases = collectSignalAliases(declaringPath);
|
|
425
476
|
|
|
426
477
|
// `memberKinds` is undefined when the declaring file could not be read: its signals
|
|
427
478
|
// are unknown, so fall back to treating every argument-less accessor as a writable
|
|
428
|
-
// one. A false positive costs a disable comment; a missed mutation is silent.
|
|
479
|
+
// one. A false positive costs a disable comment; a missed mutation is silent. Its
|
|
480
|
+
// getters are unknown too: none is taken for an alias.
|
|
429
481
|
const memberKind = (name) =>
|
|
430
482
|
memberKinds ? memberKinds.get(name) : WRITABLE;
|
|
483
|
+
const alias = (name) => aliases?.get(name);
|
|
431
484
|
|
|
432
|
-
const reportMutation = (node,
|
|
485
|
+
const reportMutation = (node, value, method) => {
|
|
486
|
+
const kind = value?.kind;
|
|
433
487
|
if (!kind || TOLERATED_KINDS.has(kind)) {
|
|
434
488
|
return;
|
|
435
489
|
}
|
|
436
|
-
if (
|
|
490
|
+
if (value.getter) {
|
|
491
|
+
const [name, advice] =
|
|
492
|
+
kind === WRITABLE
|
|
493
|
+
? [
|
|
494
|
+
"the signal",
|
|
495
|
+
method
|
|
496
|
+
? `\`${method}\` changes it in place: write a new value with .set() or .update() instead (patchForm() for the form of a page)`
|
|
497
|
+
: "Write a new value with .set() or .update() instead (patchForm() for the form of a page)",
|
|
498
|
+
]
|
|
499
|
+
: READ_ONLY_ADVICE[kind];
|
|
500
|
+
context.report({
|
|
501
|
+
node,
|
|
502
|
+
messageId: "getterMutation",
|
|
503
|
+
data: {
|
|
504
|
+
signal: `${name} \`${value.signal}\``,
|
|
505
|
+
getter: value.getter,
|
|
506
|
+
advice,
|
|
507
|
+
},
|
|
508
|
+
});
|
|
509
|
+
} else if (kind !== WRITABLE) {
|
|
437
510
|
const [name, advice] = READ_ONLY_ADVICE[kind];
|
|
438
511
|
context.report({
|
|
439
512
|
node,
|
|
@@ -447,9 +520,9 @@ export default {
|
|
|
447
520
|
}
|
|
448
521
|
};
|
|
449
522
|
|
|
450
|
-
const scopeContext = { memberKind, sourceCode: context.sourceCode };
|
|
523
|
+
const scopeContext = { memberKind, alias, sourceCode: context.sourceCode };
|
|
451
524
|
// `@let` bindings, always declared before they are used.
|
|
452
|
-
const templateContext = { memberKind, letBindings: new Map() };
|
|
525
|
+
const templateContext = { memberKind, alias, letBindings: new Map() };
|
|
453
526
|
|
|
454
527
|
const checkTemplateInPlaceCall = (node) => {
|
|
455
528
|
const method = templateMethodName(node.receiver);
|
|
@@ -471,6 +544,16 @@ export default {
|
|
|
471
544
|
}
|
|
472
545
|
},
|
|
473
546
|
|
|
547
|
+
// `this.model().count++` writes into the value, like `+= 1`.
|
|
548
|
+
UpdateExpression(node) {
|
|
549
|
+
if (node.argument.type === "MemberExpression") {
|
|
550
|
+
reportMutation(
|
|
551
|
+
node,
|
|
552
|
+
signalValueKind(node.argument.object, scopeContext),
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
},
|
|
556
|
+
|
|
474
557
|
UnaryExpression(node) {
|
|
475
558
|
// `delete model().users[0]` drops an entry of the value, notifying nothing.
|
|
476
559
|
if (
|
|
@@ -519,7 +602,7 @@ export default {
|
|
|
519
602
|
if (TEMPLATE_ASSIGNMENTS.has(node.operation)) {
|
|
520
603
|
reportMutation(
|
|
521
604
|
node,
|
|
522
|
-
signalTemplateValueKind(node.left, templateContext),
|
|
605
|
+
signalTemplateValueKind(node.left, templateContext, new Set(), true),
|
|
523
606
|
);
|
|
524
607
|
}
|
|
525
608
|
},
|
|
@@ -537,7 +620,7 @@ export default {
|
|
|
537
620
|
const handler = handlerExpressionOf(node);
|
|
538
621
|
reportMutation(
|
|
539
622
|
handler,
|
|
540
|
-
signalTemplateValueKind(handler, templateContext),
|
|
623
|
+
signalTemplateValueKind(handler, templateContext, new Set(), true),
|
|
541
624
|
);
|
|
542
625
|
},
|
|
543
626
|
};
|
|
@@ -11,8 +11,9 @@ import { astOf, resolveRoot } from "../form-reads.mjs";
|
|
|
11
11
|
* `FoehnInputComponent` writes a new list in its model, its items replaced (`updateNgModel()`).
|
|
12
12
|
* A row tracked by its item is then destroyed and created again at every input, and the field
|
|
13
13
|
* being edited loses the focus; two items equal by value (e.g. two empty strings) even collide.
|
|
14
|
-
* Track the rows by `$index
|
|
15
|
-
*
|
|
14
|
+
* Track the rows by `$index`: an identifier of the item (`track child.id`), which the copy keeps,
|
|
15
|
+
* only when the rows name nothing with their index (see `require-track-index`, which reports
|
|
16
|
+
* these lists too when they do).
|
|
16
17
|
*
|
|
17
18
|
* A row edits its item when one of its elements has a `model` read from it
|
|
18
19
|
* (`[model]="child.firstName"`, `[(model)]="child"`), or read from the list at the index of
|
|
@@ -108,7 +109,7 @@ export default {
|
|
|
108
109
|
schema: [],
|
|
109
110
|
messages: {
|
|
110
111
|
itemTrack:
|
|
111
|
-
"The rows of this list are tracked by their item ('track {{item}}'): writing an item replaces it, so its row is created again and the field being edited loses the focus. Track the rows by $index
|
|
112
|
+
"The rows of this list are tracked by their item ('track {{item}}'): writing an item replaces it, so its row is created again and the field being edited loses the focus. Track the rows by $index (by an identifier of the item only when the rows name nothing with their index).",
|
|
112
113
|
trackByIndex: "Track the rows by $index",
|
|
113
114
|
},
|
|
114
115
|
},
|