@dsivd/prestations-ng 19.2.0-beta.20 → 19.2.0-beta.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ANGULAR_SIGNALS.md +4 -0
- package/CHANGELOG.md +113 -3
- package/CONTRIBUTING.md +2 -2
- package/ESLINT_PLUGIN.md +146 -28
- package/GOOD_PRACTICES.md +258 -58
- package/INTRODUCTION_ANGULAR_SIGNALS.md +29 -2
- package/UPGRADING_V19.md +233 -31
- package/eslint/configs/template-rules.mjs +5 -0
- package/eslint/configs/ts-recommended.mjs +4 -0
- package/eslint/form-reads.mjs +63 -0
- package/eslint/rules/index.mjs +4 -0
- package/eslint/rules/no-direct-signal-mutation.mjs +57 -19
- package/eslint/rules/no-item-track-in-form-list.mjs +172 -0
- package/eslint/rules/no-uninvoked-signal-in-template.mjs +8 -3
- package/eslint/rules/require-model-change.mjs +89 -0
- package/eslint/template-ast.mjs +12 -2
- package/fesm2022/dsivd-prestations-ng.mjs +948 -692
- package/fesm2022/dsivd-prestations-ng.mjs.map +1 -1
- package/package.json +1 -1
- package/schematics/migration-collection.json +5 -0
- package/schematics/ng-update/migrations/update-19_2_0/index.js +39 -0
- package/schematics/utility/form-signal/class-migration.js +671 -0
- package/schematics/utility/form-signal/edits.js +34 -0
- package/schematics/utility/form-signal/form-signal-migration.js +184 -0
- package/schematics/utility/form-signal/page-classes.js +129 -0
- package/schematics/utility/form-signal/template-migration.js +666 -0
- package/src/eslint/configs/template-rules.mjs +5 -0
- package/src/eslint/configs/ts-recommended.mjs +4 -0
- package/src/eslint/form-reads.mjs +63 -0
- package/src/eslint/rules/__tests__/no-direct-signal-mutation.test.mjs +58 -0
- package/src/eslint/rules/__tests__/no-item-track-in-form-list.test.mjs +234 -0
- package/src/eslint/rules/__tests__/no-uninvoked-signal-in-template.test.mjs +6 -0
- package/src/eslint/rules/__tests__/require-model-change.test.mjs +93 -0
- package/src/eslint/rules/index.mjs +4 -0
- package/src/eslint/rules/no-direct-signal-mutation.mjs +57 -19
- package/src/eslint/rules/no-item-track-in-form-list.mjs +172 -0
- package/src/eslint/rules/no-uninvoked-signal-in-template.mjs +8 -3
- package/src/eslint/rules/require-model-change.mjs +89 -0
- package/src/eslint/template-ast.mjs +12 -2
- package/types/dsivd-prestations-ng.d.ts +266 -35
|
@@ -0,0 +1,172 @@
|
|
|
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`, or by an identifier of the item (`track child.id`), which the copy
|
|
15
|
+
* keeps.
|
|
16
|
+
*
|
|
17
|
+
* A row edits its item when one of its elements has a `model` read from it
|
|
18
|
+
* (`[model]="child.firstName"`, `[(model)]="child"`), or read from the list at the index of
|
|
19
|
+
* the row (`[model]="model()[i]"`, `let i = $index`). A list only displayed is left alone.
|
|
20
|
+
* `$any(child)` counts as `child`.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
// The members holding a list whose items are replaced by a write: the form of the page, and
|
|
24
|
+
// the model of the component
|
|
25
|
+
const REPLACED_LISTS = new Set(["form", "model"]);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Whether the track expression of a `@for` is its item itself.
|
|
29
|
+
*/
|
|
30
|
+
function tracksItemItself(forBlock) {
|
|
31
|
+
let track = astOf(forBlock.trackBy);
|
|
32
|
+
if (
|
|
33
|
+
track?.type === "Call" &&
|
|
34
|
+
track.receiver?.name === "$any" &&
|
|
35
|
+
track.args?.length === 1
|
|
36
|
+
) {
|
|
37
|
+
track = track.args[0];
|
|
38
|
+
}
|
|
39
|
+
return (
|
|
40
|
+
track?.type === "PropertyRead" &&
|
|
41
|
+
isComponentReceiver(track.receiver) &&
|
|
42
|
+
track.name === forBlock.item?.name
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Whether an expression reads a list whose items are replaced by a write: the form, or the
|
|
48
|
+
* model, directly or through a template name.
|
|
49
|
+
*/
|
|
50
|
+
function readsReplacedList(expression, node) {
|
|
51
|
+
const root = resolveRoot(expression, node);
|
|
52
|
+
if (root?.forBlock) {
|
|
53
|
+
return readsReplacedList(
|
|
54
|
+
astOf(root.forBlock.expression),
|
|
55
|
+
root.forBlock.parent,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
return REPLACED_LISTS.has(root?.member);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The `@for` whose list an expression reads at the index of the row (`model()[i]`,
|
|
63
|
+
* `form.children[$index].firstName`): an enclosing `@for` declaring the key as its `$index`
|
|
64
|
+
* (or an alias of it), over the same list, compared by its source.
|
|
65
|
+
*/
|
|
66
|
+
function forBlockReadAtIndex(expression, node, textOf) {
|
|
67
|
+
for (let read = expression; read; read = read.receiver) {
|
|
68
|
+
if (read.type !== "KeyedRead" && read.type !== "SafeKeyedRead") {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const key = read.key;
|
|
72
|
+
if (key?.type !== "PropertyRead" || !isComponentReceiver(key.receiver)) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
for (let current = node; current; current = current.parent) {
|
|
76
|
+
if (current.type !== "ForLoopBlock") {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const variable = current.contextVariables?.find(
|
|
80
|
+
(contextVariable) => contextVariable.name === key.name,
|
|
81
|
+
);
|
|
82
|
+
if (!variable) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
// The innermost @for declaring the name binds it
|
|
86
|
+
if (
|
|
87
|
+
variable.value === "$index" &&
|
|
88
|
+
textOf(read.receiver) === textOf(astOf(current.expression))
|
|
89
|
+
) {
|
|
90
|
+
return current;
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export default {
|
|
99
|
+
meta: {
|
|
100
|
+
type: "problem",
|
|
101
|
+
docs: {
|
|
102
|
+
description:
|
|
103
|
+
"Disallow tracking by the item itself the rows of a @for editing a list of the form, or of the model of the component",
|
|
104
|
+
category: "Possible Errors",
|
|
105
|
+
recommended: true,
|
|
106
|
+
},
|
|
107
|
+
hasSuggestions: true,
|
|
108
|
+
schema: [],
|
|
109
|
+
messages: {
|
|
110
|
+
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 or by an identifier of the item.",
|
|
112
|
+
trackByIndex: "Track the rows by $index",
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
create(context) {
|
|
117
|
+
const candidates = [];
|
|
118
|
+
const editingBlocks = new Set();
|
|
119
|
+
const textOf = (expression) =>
|
|
120
|
+
context.sourceCode.text
|
|
121
|
+
.slice(expression.sourceSpan.start, expression.sourceSpan.end)
|
|
122
|
+
.replace(/\s+/g, "");
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
ForLoopBlock(node) {
|
|
126
|
+
if (
|
|
127
|
+
tracksItemItself(node) &&
|
|
128
|
+
readsReplacedList(astOf(node.expression), node.parent)
|
|
129
|
+
) {
|
|
130
|
+
candidates.push(node);
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
Element(node) {
|
|
134
|
+
const model = node.inputs?.find((input) => input.name === "model");
|
|
135
|
+
if (!model) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const expression = astOf(model.value);
|
|
139
|
+
const root = resolveRoot(expression, node);
|
|
140
|
+
if (root?.forBlock) {
|
|
141
|
+
editingBlocks.add(root.forBlock);
|
|
142
|
+
}
|
|
143
|
+
const readAtIndex = forBlockReadAtIndex(expression, node, textOf);
|
|
144
|
+
if (readAtIndex) {
|
|
145
|
+
editingBlocks.add(readAtIndex);
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
"Program:exit"() {
|
|
149
|
+
for (const forBlock of candidates) {
|
|
150
|
+
if (!editingBlocks.has(forBlock)) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const { start, end } = astOf(forBlock.trackBy).sourceSpan;
|
|
154
|
+
context.report({
|
|
155
|
+
messageId: "itemTrack",
|
|
156
|
+
data: { item: forBlock.item.name },
|
|
157
|
+
loc: {
|
|
158
|
+
start: context.sourceCode.getLocFromIndex(start),
|
|
159
|
+
end: context.sourceCode.getLocFromIndex(end),
|
|
160
|
+
},
|
|
161
|
+
suggest: [
|
|
162
|
+
{
|
|
163
|
+
messageId: "trackByIndex",
|
|
164
|
+
fix: (fixer) => fixer.replaceTextRange([start, end], "$index"),
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
|
+
};
|
|
@@ -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
|
},
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { astOf, readsForm } from "../form-reads.mjs";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ESLint rule: a `foehn-*` component given a field of the form of the page with a
|
|
5
|
+
* one-way `[model]` must also listen to `(modelChange)`. Without it, what the user enters never
|
|
6
|
+
* reaches the form: the component changes its own model only. With the form held by a signal,
|
|
7
|
+
* bind `[model]="form.firstName" (modelChange)="patchForm(['firstName'], $event)"`.
|
|
8
|
+
*
|
|
9
|
+
* Only the models read from the form are checked (`form()`, `form.x`, and the names bound to
|
|
10
|
+
* it by `@if (form(); as form)`, `@let form = this.form()` or `@for (child of form.children)`):
|
|
11
|
+
* a one-way `[model]` is legitimate elsewhere (a table displaying a list, a menu...).
|
|
12
|
+
* `[(model)]` declares both bindings and is never reported.
|
|
13
|
+
*
|
|
14
|
+
* Not reported either, as the input of the user can't be lost: a component always disabled
|
|
15
|
+
* (`disabled="true"`, `[disabled]="true"`), the components which never write their model, and a
|
|
16
|
+
* component listening to `(userInput)`, whose handler writes the form.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** Components which never write their model: they only display it. */
|
|
20
|
+
const READ_ONLY_COMPONENTS = new Set(["foehn-input-hidden", "foehn-table"]);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Whether the component is always disabled: a disabled component doesn't write its model, unless
|
|
24
|
+
* it is given `updateModelWhenDisabled`. A bare `disabled` gives `''`, which doesn't disable it.
|
|
25
|
+
*/
|
|
26
|
+
function isAlwaysDisabled(node) {
|
|
27
|
+
const named = (name) => (binding) => binding.name === name;
|
|
28
|
+
const bindings = [...(node.attributes ?? []), ...(node.inputs ?? [])];
|
|
29
|
+
if (bindings.some(named("updateModelWhenDisabled"))) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const disabled = astOf(node.inputs?.find(named("disabled"))?.value);
|
|
33
|
+
return (
|
|
34
|
+
node.attributes?.find(named("disabled"))?.value === "true" ||
|
|
35
|
+
(disabled?.type === "LiteralPrimitive" && disabled.value === true)
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default {
|
|
40
|
+
meta: {
|
|
41
|
+
type: "problem",
|
|
42
|
+
docs: {
|
|
43
|
+
description:
|
|
44
|
+
"Require (modelChange) next to a one-way [model] reading the form of the page",
|
|
45
|
+
category: "Possible Errors",
|
|
46
|
+
recommended: true,
|
|
47
|
+
},
|
|
48
|
+
schema: [],
|
|
49
|
+
messages: {
|
|
50
|
+
missing:
|
|
51
|
+
"'{{element}}' gets a field of the form with a one-way [model] without (modelChange): what the user enters never reaches the form. Add (modelChange), e.g. (modelChange)=\"patchForm([...], $event)\".",
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
create(context) {
|
|
56
|
+
const offset = (location) =>
|
|
57
|
+
typeof location === "number" ? location : location.offset;
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
Element(node) {
|
|
61
|
+
if (!node.name?.startsWith("foehn-")) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const model = node.inputs?.find((input) => input.name === "model");
|
|
65
|
+
if (
|
|
66
|
+
!model ||
|
|
67
|
+
READ_ONLY_COMPONENTS.has(node.name) ||
|
|
68
|
+
node.outputs?.some(
|
|
69
|
+
(output) =>
|
|
70
|
+
output.name === "modelChange" || output.name === "userInput",
|
|
71
|
+
) ||
|
|
72
|
+
isAlwaysDisabled(node) ||
|
|
73
|
+
!readsForm(astOf(model.value), node)
|
|
74
|
+
) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const span = model.sourceSpan;
|
|
78
|
+
context.report({
|
|
79
|
+
messageId: "missing",
|
|
80
|
+
data: { element: node.name },
|
|
81
|
+
loc: {
|
|
82
|
+
start: context.sourceCode.getLocFromIndex(offset(span.start)),
|
|
83
|
+
end: context.sourceCode.getLocFromIndex(offset(span.end)),
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
},
|
|
89
|
+
};
|
package/eslint/template-ast.mjs
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Three subtleties of the Angular template AST that every template rule runs into.
|
|
3
|
+
* All are easy to get wrong, hence the shared names.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Whether a `BoundEvent` is the one generated by a two-way binding `[(x)]="expr"`, whose
|
|
8
|
+
* handler is the target Angular writes into, not a statement.
|
|
9
|
+
*
|
|
10
|
+
* Its name alone can't tell: an explicit `(xChange)="refresh()"` has the same name. The
|
|
11
|
+
* key can: `x` for the two-way binding, `xChange` for the explicit handler.
|
|
12
|
+
*/
|
|
13
|
+
export const isTwoWayBinding = (boundEvent) =>
|
|
14
|
+
boundEvent.name === `${boundEvent.keySpan?.details}Change`;
|
|
15
|
+
|
|
6
16
|
/**
|
|
7
17
|
* The expression carried by a `BoundEvent` handler.
|
|
8
18
|
*
|