@dsivd/prestations-ng 19.2.0-beta.26 → 19.2.0-beta.28

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.
@@ -0,0 +1,162 @@
1
+ import { isComponentReceiver } from "../template-ast.mjs";
2
+ import { astOf } from "../form-reads.mjs";
3
+
4
+ /**
5
+ * ESLint rule: a `@for` whose rows name their fields with the index of the row
6
+ * (`[name]="'children[' + $index + '].firstName'"`, `name="child{{ i }}"`), or the errors of the
7
+ * row (`[errorPrefix]` of `foehn-error-pill`), must track its rows by `$index`.
8
+ *
9
+ * The names of the fields follow the paths of the errors of GESDEM (`children[1].firstName`).
10
+ * Tracked by anything else (an identifier, the item), a row whose item moves (an earlier item
11
+ * removed or inserted, the list sorted) is kept with its fields renamed: `foehn-form` keeps them
12
+ * registered under their former names, so the row taking one of these names shares their
13
+ * `FormControl`, and the errors, read by name when they are emitted, stay those of the former
14
+ * name. Tracked by `$index`, a row never changes its names: only its item changes.
15
+ *
16
+ * The index is `$index` or an alias of it (`let i = $index`), of the innermost `@for` declaring
17
+ * the name: a name built with the index of an enclosing `@for` reports that one.
18
+ */
19
+
20
+ // The inputs naming a field, or the errors of a row
21
+ const NAME_INPUTS = new Set(["name", "errorPrefix"]);
22
+
23
+ // The members of an expression node that aren't expressions
24
+ const NOT_EXPRESSIONS = new Set([
25
+ "span",
26
+ "sourceSpan",
27
+ "nameSpan",
28
+ "keySpan",
29
+ "loc",
30
+ "parent",
31
+ ]);
32
+
33
+ /**
34
+ * The names read on the component by an expression (`i`, `$index`), nested reads included.
35
+ */
36
+ function namesReadBy(expression, names = []) {
37
+ if (Array.isArray(expression)) {
38
+ expression.forEach((item) => namesReadBy(item, names));
39
+ return names;
40
+ }
41
+ if (!expression || typeof expression !== "object") {
42
+ return names;
43
+ }
44
+ if (
45
+ expression.type === "PropertyRead" &&
46
+ isComponentReceiver(expression.receiver)
47
+ ) {
48
+ names.push(expression.name);
49
+ }
50
+ for (const [key, value] of Object.entries(expression)) {
51
+ if (!NOT_EXPRESSIONS.has(key) && value && typeof value === "object") {
52
+ namesReadBy(value, names);
53
+ }
54
+ }
55
+ return names;
56
+ }
57
+
58
+ /**
59
+ * The `@for` whose `$index` a name is: the innermost enclosing `@for` declaring the name, when
60
+ * it declares it as its `$index` (itself or an alias).
61
+ */
62
+ function forBlockIndexed(name, node) {
63
+ for (let current = node; current; current = current.parent) {
64
+ if (current.type !== "ForLoopBlock") {
65
+ continue;
66
+ }
67
+ const variable = current.contextVariables?.find(
68
+ (contextVariable) => contextVariable.name === name,
69
+ );
70
+ if (variable) {
71
+ return variable.value === "$index" ? current : undefined;
72
+ }
73
+ }
74
+ return undefined;
75
+ }
76
+
77
+ /**
78
+ * Whether a `@for` tracks its rows by its `$index`, or an alias of it.
79
+ */
80
+ function tracksIndex(forBlock) {
81
+ const track = astOf(forBlock.trackBy);
82
+ return (
83
+ track?.type === "PropertyRead" &&
84
+ isComponentReceiver(track.receiver) &&
85
+ !!forBlock.contextVariables?.some(
86
+ (contextVariable) =>
87
+ contextVariable.name === track.name &&
88
+ contextVariable.value === "$index",
89
+ )
90
+ );
91
+ }
92
+
93
+ export default {
94
+ meta: {
95
+ type: "problem",
96
+ docs: {
97
+ description:
98
+ "Require tracking by $index the rows of a @for that name their fields, or their errors, with the index of the row",
99
+ category: "Possible Errors",
100
+ recommended: true,
101
+ },
102
+ hasSuggestions: true,
103
+ schema: [],
104
+ messages: {
105
+ indexTrack:
106
+ "The rows of this list name their fields with the index ({{binding}}) but are tracked by '{{track}}': a row whose item moves keeps its fields registered under their former names, and mixes up their values and errors with the row taking these names. Track the rows by $index.",
107
+ trackByIndex: "Track the rows by $index",
108
+ },
109
+ },
110
+
111
+ create(context) {
112
+ // The @for naming the fields of their rows with their index, and the first such binding
113
+ const indexedBlocks = new Map();
114
+ const textOf = (expression) =>
115
+ context.sourceCode.text
116
+ .slice(expression.sourceSpan.start, expression.sourceSpan.end)
117
+ .replace(/\s+/g, " ")
118
+ .trim();
119
+
120
+ return {
121
+ Element(node) {
122
+ for (const input of node.inputs ?? []) {
123
+ if (!NAME_INPUTS.has(input.name)) {
124
+ continue;
125
+ }
126
+ for (const name of namesReadBy(astOf(input.value))) {
127
+ const forBlock = forBlockIndexed(name, node);
128
+ if (forBlock && !indexedBlocks.has(forBlock)) {
129
+ indexedBlocks.set(
130
+ forBlock,
131
+ `${input.name}="${input.value.source ?? textOf(astOf(input.value))}"`,
132
+ );
133
+ }
134
+ }
135
+ }
136
+ },
137
+ "Program:exit"() {
138
+ for (const [forBlock, binding] of indexedBlocks) {
139
+ if (tracksIndex(forBlock)) {
140
+ continue;
141
+ }
142
+ const track = astOf(forBlock.trackBy);
143
+ const { start, end } = track.sourceSpan;
144
+ context.report({
145
+ messageId: "indexTrack",
146
+ data: { binding, track: textOf(track) },
147
+ loc: {
148
+ start: context.sourceCode.getLocFromIndex(start),
149
+ end: context.sourceCode.getLocFromIndex(end),
150
+ },
151
+ suggest: [
152
+ {
153
+ messageId: "trackByIndex",
154
+ fix: (fixer) => fixer.replaceTextRange([start, end], "$index"),
155
+ },
156
+ ],
157
+ });
158
+ }
159
+ },
160
+ };
161
+ },
162
+ };
@@ -3524,7 +3524,6 @@ declare abstract class AbstractPageFromMenuComponent<T extends I18nForm> extends
3524
3524
  }
3525
3525
 
3526
3526
  declare class FoehnListItem {
3527
- trackingIndex: number | undefined;
3528
3527
  disableEdition?: boolean;
3529
3528
  disableDeletion?: boolean;
3530
3529
  }
@@ -3575,7 +3574,6 @@ declare abstract class AbstractListDetailPageComponent<FORM_TYPE extends I18nFor
3575
3574
  setItem(item: ITEM_TYPE): void;
3576
3575
  ngOnInit(): void;
3577
3576
  ngOnDestroy(): void;
3578
- protected getNextTrackingIndex(): number;
3579
3577
  protected returnToParent(): void;
3580
3578
  protected resetBreadCrumb(): void;
3581
3579
  protected appendPageToBreadCrumb(): void;