@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
|
@@ -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
|
+
});
|
|
@@ -1,7 +1,13 @@
|
|
|
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';
|
|
5
|
+
import requireTrackIndex from './require-track-index.mjs';
|
|
3
6
|
|
|
4
7
|
export default {
|
|
5
8
|
'no-direct-signal-mutation': noDirectSignalMutation,
|
|
9
|
+
'no-item-track-in-form-list': noItemTrackInFormList,
|
|
6
10
|
'no-uninvoked-signal-in-template': noUninvokedSignalInTemplate,
|
|
11
|
+
'require-model-change': requireModelChange,
|
|
12
|
+
'require-track-index': requireTrackIndex,
|
|
7
13
|
};
|
|
@@ -8,8 +8,12 @@ import {
|
|
|
8
8
|
VIEW_QUERY,
|
|
9
9
|
WRITABLE,
|
|
10
10
|
} from "../signal-kinds.mjs";
|
|
11
|
-
import { collectSignalKinds } from "../signal-names.mjs";
|
|
12
|
-
import {
|
|
11
|
+
import { collectSignalAliases, collectSignalKinds } from "../signal-names.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
|
|
@@ -25,6 +30,10 @@ import { handlerExpressionOf, isComponentReceiver } from "../template-ast.mjs";
|
|
|
25
30
|
*
|
|
26
31
|
* Only the signals the class really declares are inspected, resolved through the whole
|
|
27
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.
|
|
28
37
|
*/
|
|
29
38
|
|
|
30
39
|
// Kinds a mutation is not reported on: a view query hands out a DOM element or a component
|
|
@@ -106,6 +115,9 @@ const TEMPLATE_MEMBER_READS = new Set([
|
|
|
106
115
|
"SafeKeyedRead",
|
|
107
116
|
]);
|
|
108
117
|
|
|
118
|
+
// Template wrappers that leave the value as is: `form()!`, `(form())`.
|
|
119
|
+
const TEMPLATE_WRAPPERS = new Set(["NonNullAssert", "ParenthesizedExpression"]);
|
|
120
|
+
|
|
109
121
|
// ── TypeScript ───────────────────────────────────────────────────────────────
|
|
110
122
|
|
|
111
123
|
function getCalledName(callee) {
|
|
@@ -140,9 +152,18 @@ function isObjectAssign(callee) {
|
|
|
140
152
|
);
|
|
141
153
|
}
|
|
142
154
|
|
|
155
|
+
// TypeScript wrappers that leave the value as is: `form()!`, `form() as Form`...
|
|
156
|
+
const TYPE_WRAPPERS = new Set([
|
|
157
|
+
"TSNonNullExpression",
|
|
158
|
+
"TSAsExpression",
|
|
159
|
+
"TSSatisfiesExpression",
|
|
160
|
+
"TSTypeAssertion",
|
|
161
|
+
]);
|
|
162
|
+
|
|
143
163
|
/**
|
|
144
164
|
* What a member chain is rooted at, the property and index reads stripped away:
|
|
145
165
|
* - `model().deep.property` => the `model()` call
|
|
166
|
+
* - `model()!.deep.property` => the `model()` call (a type wrapper keeps the value)
|
|
146
167
|
* - `users[0].name` => the `users` name
|
|
147
168
|
*
|
|
148
169
|
* Only reads are walked through. A chain rooted at anything else — `items().filter(f)`,
|
|
@@ -151,8 +172,12 @@ function isObjectAssign(callee) {
|
|
|
151
172
|
*/
|
|
152
173
|
function baseOf(node) {
|
|
153
174
|
let current = node;
|
|
154
|
-
while (
|
|
155
|
-
current
|
|
175
|
+
while (
|
|
176
|
+
current?.type === "MemberExpression" ||
|
|
177
|
+
TYPE_WRAPPERS.has(current?.type)
|
|
178
|
+
) {
|
|
179
|
+
current =
|
|
180
|
+
current.type === "MemberExpression" ? current.object : current.expression;
|
|
156
181
|
}
|
|
157
182
|
return current;
|
|
158
183
|
}
|
|
@@ -180,6 +205,33 @@ function signalFactoryKind(node) {
|
|
|
180
205
|
return callee?.type === "Identifier" ? factoryKind(callee.name) : undefined;
|
|
181
206
|
}
|
|
182
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
|
+
|
|
183
235
|
function signalRootCallKind(node, scopeContext) {
|
|
184
236
|
if (node.arguments.length !== 0) {
|
|
185
237
|
return undefined;
|
|
@@ -212,14 +264,18 @@ function signalRootCallKind(node, scopeContext) {
|
|
|
212
264
|
}
|
|
213
265
|
|
|
214
266
|
/**
|
|
215
|
-
*
|
|
216
|
-
* The expression may be the signal read itself (`this.model()`), a member of
|
|
217
|
-
* (`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.
|
|
218
271
|
*/
|
|
219
272
|
function signalValueKind(node, scopeContext, seen = new Set()) {
|
|
220
273
|
const base = baseOf(node);
|
|
221
274
|
if (base?.type === "CallExpression") {
|
|
222
|
-
return signalRootCallKind(base, scopeContext);
|
|
275
|
+
return valueOf(signalRootCallKind(base, scopeContext));
|
|
276
|
+
}
|
|
277
|
+
if (base?.type === "ThisExpression") {
|
|
278
|
+
return aliasReadOf(node, scopeContext);
|
|
223
279
|
}
|
|
224
280
|
return base?.type === "Identifier"
|
|
225
281
|
? boundNameSignalKind(base, scopeContext, seen)
|
|
@@ -241,7 +297,7 @@ function boundNameSignalKind(identifier, scopeContext, seen) {
|
|
|
241
297
|
scopeContext.sourceCode.getScope(identifier),
|
|
242
298
|
identifier.name,
|
|
243
299
|
);
|
|
244
|
-
if (
|
|
300
|
+
if (variable?.defs.length !== 1 || seen.has(variable)) {
|
|
245
301
|
return undefined;
|
|
246
302
|
}
|
|
247
303
|
seen.add(variable);
|
|
@@ -293,22 +349,36 @@ function signalRootTemplateCallKind(node, templateContext) {
|
|
|
293
349
|
return undefined;
|
|
294
350
|
}
|
|
295
351
|
return isComponentReceiver(callee.receiver)
|
|
296
|
-
? templateContext.memberKind(callee.name ?? "")
|
|
352
|
+
? valueOf(templateContext.memberKind(callee.name ?? ""))
|
|
297
353
|
: undefined;
|
|
298
354
|
}
|
|
299
355
|
|
|
356
|
+
// The expression itself, a type wrapper stripped away: `form()!` is the `form()` call.
|
|
357
|
+
function unwrapTemplate(node) {
|
|
358
|
+
let current = node;
|
|
359
|
+
while (TEMPLATE_WRAPPERS.has(current?.type)) {
|
|
360
|
+
current = current.expression;
|
|
361
|
+
}
|
|
362
|
+
return current;
|
|
363
|
+
}
|
|
364
|
+
|
|
300
365
|
/**
|
|
301
366
|
* Expression a template name is bound to, `undefined` when the name is a component
|
|
302
367
|
* 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
|
|
368
|
+
* `@for (user of model().users)` hands out the elements of the signal's array, and
|
|
369
|
+
* `@if (model(); as value)` the value of the signal itself.
|
|
304
370
|
*
|
|
305
|
-
* The item of a `@for`
|
|
306
|
-
*
|
|
307
|
-
* uses.
|
|
371
|
+
* The item of a `@for` and the alias of an `@if` are only bound inside their own block,
|
|
372
|
+
* which the ancestor walk enforces exactly, the nearest one shadowing the others; a `@let`
|
|
373
|
+
* is looked up by name, its declaration always preceding its uses.
|
|
308
374
|
*/
|
|
309
375
|
function boundTemplateExpression(read, templateContext) {
|
|
310
376
|
for (let current = read; current; current = current.parent) {
|
|
311
|
-
if (
|
|
377
|
+
if (
|
|
378
|
+
(current.type === "ForLoopBlock" && current.item?.name === read.name) ||
|
|
379
|
+
(current.type === "IfBlockBranch" &&
|
|
380
|
+
current.expressionAlias?.name === read.name)
|
|
381
|
+
) {
|
|
312
382
|
return current.expression?.ast ?? current.expression;
|
|
313
383
|
}
|
|
314
384
|
}
|
|
@@ -317,23 +387,34 @@ function boundTemplateExpression(read, templateContext) {
|
|
|
317
387
|
}
|
|
318
388
|
|
|
319
389
|
/**
|
|
320
|
-
* Template counterpart of `signalValueKind`: the value a signal holds, a member of it,
|
|
321
|
-
* 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.
|
|
322
396
|
*/
|
|
323
|
-
function signalTemplateValueKind(
|
|
324
|
-
|
|
325
|
-
|
|
397
|
+
function signalTemplateValueKind(
|
|
398
|
+
node,
|
|
399
|
+
templateContext,
|
|
400
|
+
seen = new Set(),
|
|
401
|
+
assigned = false,
|
|
402
|
+
) {
|
|
403
|
+
const value = unwrapTemplate(node);
|
|
404
|
+
if (value?.type === "Call" || value?.type === "SafeCall") {
|
|
405
|
+
return signalRootTemplateCallKind(value, templateContext);
|
|
326
406
|
}
|
|
327
|
-
if (!TEMPLATE_MEMBER_READS.has(
|
|
407
|
+
if (!TEMPLATE_MEMBER_READS.has(value?.type)) {
|
|
328
408
|
return undefined;
|
|
329
409
|
}
|
|
330
410
|
|
|
331
|
-
// Property and index reads are walked through
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
411
|
+
// Property and index reads are walked through, as are the type wrappers; anything else
|
|
412
|
+
// roots a value of its own.
|
|
413
|
+
let base = value;
|
|
414
|
+
while (TEMPLATE_MEMBER_READS.has(unwrapTemplate(base.receiver)?.type)) {
|
|
415
|
+
base = unwrapTemplate(base.receiver);
|
|
335
416
|
}
|
|
336
|
-
const receiver = base.receiver;
|
|
417
|
+
const receiver = unwrapTemplate(base.receiver);
|
|
337
418
|
|
|
338
419
|
if (receiver?.type === "Call" || receiver?.type === "SafeCall") {
|
|
339
420
|
return signalRootTemplateCallKind(receiver, templateContext);
|
|
@@ -342,11 +423,15 @@ function signalTemplateValueKind(node, templateContext, seen = new Set()) {
|
|
|
342
423
|
return undefined;
|
|
343
424
|
}
|
|
344
425
|
seen.add(base.name);
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
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);
|
|
350
435
|
}
|
|
351
436
|
|
|
352
437
|
// Method a template call invokes, `undefined` when the name is not statically known.
|
|
@@ -376,6 +461,8 @@ export default {
|
|
|
376
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.",
|
|
377
462
|
readOnlyMutation:
|
|
378
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}}.",
|
|
379
466
|
},
|
|
380
467
|
},
|
|
381
468
|
|
|
@@ -385,18 +472,41 @@ export default {
|
|
|
385
472
|
const declaringPath =
|
|
386
473
|
resolveComponentPath(context.filename) ?? context.filename;
|
|
387
474
|
const memberKinds = collectSignalKinds(declaringPath);
|
|
475
|
+
const aliases = collectSignalAliases(declaringPath);
|
|
388
476
|
|
|
389
477
|
// `memberKinds` is undefined when the declaring file could not be read: its signals
|
|
390
478
|
// are unknown, so fall back to treating every argument-less accessor as a writable
|
|
391
|
-
// 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.
|
|
392
481
|
const memberKind = (name) =>
|
|
393
482
|
memberKinds ? memberKinds.get(name) : WRITABLE;
|
|
483
|
+
const alias = (name) => aliases?.get(name);
|
|
394
484
|
|
|
395
|
-
const reportMutation = (node,
|
|
485
|
+
const reportMutation = (node, value, method) => {
|
|
486
|
+
const kind = value?.kind;
|
|
396
487
|
if (!kind || TOLERATED_KINDS.has(kind)) {
|
|
397
488
|
return;
|
|
398
489
|
}
|
|
399
|
-
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) {
|
|
400
510
|
const [name, advice] = READ_ONLY_ADVICE[kind];
|
|
401
511
|
context.report({
|
|
402
512
|
node,
|
|
@@ -410,9 +520,9 @@ export default {
|
|
|
410
520
|
}
|
|
411
521
|
};
|
|
412
522
|
|
|
413
|
-
const scopeContext = { memberKind, sourceCode: context.sourceCode };
|
|
523
|
+
const scopeContext = { memberKind, alias, sourceCode: context.sourceCode };
|
|
414
524
|
// `@let` bindings, always declared before they are used.
|
|
415
|
-
const templateContext = { memberKind, letBindings: new Map() };
|
|
525
|
+
const templateContext = { memberKind, alias, letBindings: new Map() };
|
|
416
526
|
|
|
417
527
|
const checkTemplateInPlaceCall = (node) => {
|
|
418
528
|
const method = templateMethodName(node.receiver);
|
|
@@ -434,6 +544,16 @@ export default {
|
|
|
434
544
|
}
|
|
435
545
|
},
|
|
436
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
|
+
|
|
437
557
|
UnaryExpression(node) {
|
|
438
558
|
// `delete model().users[0]` drops an entry of the value, notifying nothing.
|
|
439
559
|
if (
|
|
@@ -482,7 +602,7 @@ export default {
|
|
|
482
602
|
if (TEMPLATE_ASSIGNMENTS.has(node.operation)) {
|
|
483
603
|
reportMutation(
|
|
484
604
|
node,
|
|
485
|
-
signalTemplateValueKind(node.left, templateContext),
|
|
605
|
+
signalTemplateValueKind(node.left, templateContext, new Set(), true),
|
|
486
606
|
);
|
|
487
607
|
}
|
|
488
608
|
},
|
|
@@ -492,14 +612,15 @@ export default {
|
|
|
492
612
|
|
|
493
613
|
BoundEvent(node) {
|
|
494
614
|
// `[(x)]="model().property"` makes Angular assign into the value of the signal.
|
|
495
|
-
// Reporting on the BoundEvent handler reports the binding exactly once.
|
|
496
|
-
|
|
615
|
+
// Reporting on the BoundEvent handler reports the binding exactly once. An
|
|
616
|
+
// explicit `(xChange)="…"` handler is a statement, checked as any other.
|
|
617
|
+
if (!isTwoWayBinding(node)) {
|
|
497
618
|
return;
|
|
498
619
|
}
|
|
499
620
|
const handler = handlerExpressionOf(node);
|
|
500
621
|
reportMutation(
|
|
501
622
|
handler,
|
|
502
|
-
signalTemplateValueKind(handler, templateContext),
|
|
623
|
+
signalTemplateValueKind(handler, templateContext, new Set(), true),
|
|
503
624
|
);
|
|
504
625
|
},
|
|
505
626
|
};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { isComponentReceiver } from "../template-ast.mjs";
|
|
2
|
+
import { astOf, resolveRoot } from "../form-reads.mjs";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* ESLint rule: a `@for` over a list of the form of the page, or over the list held by the
|
|
6
|
+
* `model` of the component, whose rows edit their item must not track the rows by the item
|
|
7
|
+
* itself (`track child`).
|
|
8
|
+
*
|
|
9
|
+
* These lists are never modified in place: writing a field of an item of the form
|
|
10
|
+
* (`patchForm()`, `patchItem()`) replaces the item with a copy, and a component extending
|
|
11
|
+
* `FoehnInputComponent` writes a new list in its model, its items replaced (`updateNgModel()`).
|
|
12
|
+
* A row tracked by its item is then destroyed and created again at every input, and the field
|
|
13
|
+
* being edited loses the focus; two items equal by value (e.g. two empty strings) even collide.
|
|
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).
|
|
17
|
+
*
|
|
18
|
+
* A row edits its item when one of its elements has a `model` read from it
|
|
19
|
+
* (`[model]="child.firstName"`, `[(model)]="child"`), or read from the list at the index of
|
|
20
|
+
* the row (`[model]="model()[i]"`, `let i = $index`). A list only displayed is left alone.
|
|
21
|
+
* `$any(child)` counts as `child`.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
// The members holding a list whose items are replaced by a write: the form of the page, and
|
|
25
|
+
// the model of the component
|
|
26
|
+
const REPLACED_LISTS = new Set(["form", "model"]);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Whether the track expression of a `@for` is its item itself.
|
|
30
|
+
*/
|
|
31
|
+
function tracksItemItself(forBlock) {
|
|
32
|
+
let track = astOf(forBlock.trackBy);
|
|
33
|
+
if (
|
|
34
|
+
track?.type === "Call" &&
|
|
35
|
+
track.receiver?.name === "$any" &&
|
|
36
|
+
track.args?.length === 1
|
|
37
|
+
) {
|
|
38
|
+
track = track.args[0];
|
|
39
|
+
}
|
|
40
|
+
return (
|
|
41
|
+
track?.type === "PropertyRead" &&
|
|
42
|
+
isComponentReceiver(track.receiver) &&
|
|
43
|
+
track.name === forBlock.item?.name
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Whether an expression reads a list whose items are replaced by a write: the form, or the
|
|
49
|
+
* model, directly or through a template name.
|
|
50
|
+
*/
|
|
51
|
+
function readsReplacedList(expression, node) {
|
|
52
|
+
const root = resolveRoot(expression, node);
|
|
53
|
+
if (root?.forBlock) {
|
|
54
|
+
return readsReplacedList(
|
|
55
|
+
astOf(root.forBlock.expression),
|
|
56
|
+
root.forBlock.parent,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return REPLACED_LISTS.has(root?.member);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The `@for` whose list an expression reads at the index of the row (`model()[i]`,
|
|
64
|
+
* `form.children[$index].firstName`): an enclosing `@for` declaring the key as its `$index`
|
|
65
|
+
* (or an alias of it), over the same list, compared by its source.
|
|
66
|
+
*/
|
|
67
|
+
function forBlockReadAtIndex(expression, node, textOf) {
|
|
68
|
+
for (let read = expression; read; read = read.receiver) {
|
|
69
|
+
if (read.type !== "KeyedRead" && read.type !== "SafeKeyedRead") {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const key = read.key;
|
|
73
|
+
if (key?.type !== "PropertyRead" || !isComponentReceiver(key.receiver)) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
for (let current = node; current; current = current.parent) {
|
|
77
|
+
if (current.type !== "ForLoopBlock") {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const variable = current.contextVariables?.find(
|
|
81
|
+
(contextVariable) => contextVariable.name === key.name,
|
|
82
|
+
);
|
|
83
|
+
if (!variable) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
// The innermost @for declaring the name binds it
|
|
87
|
+
if (
|
|
88
|
+
variable.value === "$index" &&
|
|
89
|
+
textOf(read.receiver) === textOf(astOf(current.expression))
|
|
90
|
+
) {
|
|
91
|
+
return current;
|
|
92
|
+
}
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export default {
|
|
100
|
+
meta: {
|
|
101
|
+
type: "problem",
|
|
102
|
+
docs: {
|
|
103
|
+
description:
|
|
104
|
+
"Disallow tracking by the item itself the rows of a @for editing a list of the form, or of the model of the component",
|
|
105
|
+
category: "Possible Errors",
|
|
106
|
+
recommended: true,
|
|
107
|
+
},
|
|
108
|
+
hasSuggestions: true,
|
|
109
|
+
schema: [],
|
|
110
|
+
messages: {
|
|
111
|
+
itemTrack:
|
|
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).",
|
|
113
|
+
trackByIndex: "Track the rows by $index",
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
create(context) {
|
|
118
|
+
const candidates = [];
|
|
119
|
+
const editingBlocks = new Set();
|
|
120
|
+
const textOf = (expression) =>
|
|
121
|
+
context.sourceCode.text
|
|
122
|
+
.slice(expression.sourceSpan.start, expression.sourceSpan.end)
|
|
123
|
+
.replace(/\s+/g, "");
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
ForLoopBlock(node) {
|
|
127
|
+
if (
|
|
128
|
+
tracksItemItself(node) &&
|
|
129
|
+
readsReplacedList(astOf(node.expression), node.parent)
|
|
130
|
+
) {
|
|
131
|
+
candidates.push(node);
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
Element(node) {
|
|
135
|
+
const model = node.inputs?.find((input) => input.name === "model");
|
|
136
|
+
if (!model) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const expression = astOf(model.value);
|
|
140
|
+
const root = resolveRoot(expression, node);
|
|
141
|
+
if (root?.forBlock) {
|
|
142
|
+
editingBlocks.add(root.forBlock);
|
|
143
|
+
}
|
|
144
|
+
const readAtIndex = forBlockReadAtIndex(expression, node, textOf);
|
|
145
|
+
if (readAtIndex) {
|
|
146
|
+
editingBlocks.add(readAtIndex);
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
"Program:exit"() {
|
|
150
|
+
for (const forBlock of candidates) {
|
|
151
|
+
if (!editingBlocks.has(forBlock)) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const { start, end } = astOf(forBlock.trackBy).sourceSpan;
|
|
155
|
+
context.report({
|
|
156
|
+
messageId: "itemTrack",
|
|
157
|
+
data: { item: forBlock.item.name },
|
|
158
|
+
loc: {
|
|
159
|
+
start: context.sourceCode.getLocFromIndex(start),
|
|
160
|
+
end: context.sourceCode.getLocFromIndex(end),
|
|
161
|
+
},
|
|
162
|
+
suggest: [
|
|
163
|
+
{
|
|
164
|
+
messageId: "trackByIndex",
|
|
165
|
+
fix: (fixer) => fixer.replaceTextRange([start, end], "$index"),
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
},
|
|
173
|
+
};
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { resolveComponentPath } from "../component-path.mjs";
|
|
2
2
|
import { collectSignalNames } from "../signal-names.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
handlerExpressionOf,
|
|
5
|
+
isComponentReceiver,
|
|
6
|
+
isTwoWayBinding,
|
|
7
|
+
} from "../template-ast.mjs";
|
|
4
8
|
|
|
5
9
|
/**
|
|
6
10
|
* ESLint rule to detect signals referenced without being called in a template.
|
|
@@ -98,8 +102,9 @@ export default {
|
|
|
98
102
|
BoundEvent(node) {
|
|
99
103
|
// `[(x)]="expr"` generates a BoundEvent `xChange` whose handler IS the written
|
|
100
104
|
// target. Its span is the one of the twin BoundAttribute expression: marking
|
|
101
|
-
// the span neutralizes both copies at once.
|
|
102
|
-
|
|
105
|
+
// the span neutralizes both copies at once. An explicit `(xChange)="…"` handler
|
|
106
|
+
// is a statement, checked as any other.
|
|
107
|
+
if (isTwoWayBinding(node)) {
|
|
103
108
|
markHandled(handlerExpressionOf(node));
|
|
104
109
|
}
|
|
105
110
|
},
|