@dsivd/prestations-ng 19.2.0-beta.2 → 19.2.0-beta.21

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.
Files changed (55) hide show
  1. package/ANGULAR_SIGNALS.md +4 -0
  2. package/CHANGELOG.md +396 -0
  3. package/CONTRIBUTING.md +74 -4
  4. package/ESLINT_PLUGIN.md +103 -8
  5. package/GOOD_PRACTICES.md +731 -380
  6. package/INTRODUCTION_ANGULAR_SIGNALS.md +199 -154
  7. package/UPGRADING_V18.md +5 -5
  8. package/UPGRADING_V19.md +439 -18
  9. package/eslint/configs/spec-ts-recommended.mjs +34 -0
  10. package/eslint/configs/template-recommended.mjs +2 -0
  11. package/eslint/configs/template-rules.mjs +11 -2
  12. package/eslint/configs/ts-recommended.mjs +6 -0
  13. package/eslint/configs/ts-rules.mjs +6 -2
  14. package/eslint/form-reads.mjs +63 -0
  15. package/eslint/index.mjs +17 -0
  16. package/eslint/rules/index.mjs +4 -0
  17. package/eslint/rules/no-direct-signal-mutation.mjs +58 -20
  18. package/eslint/rules/no-item-track-in-form-list.mjs +99 -0
  19. package/eslint/rules/no-uninvoked-signal-in-template.mjs +8 -3
  20. package/eslint/rules/require-model-change.mjs +89 -0
  21. package/eslint/template-ast.mjs +12 -2
  22. package/fesm2022/dsivd-prestations-ng.mjs +2766 -2073
  23. package/fesm2022/dsivd-prestations-ng.mjs.map +1 -1
  24. package/package.json +1 -2
  25. package/schematics/migration-collection.json +5 -0
  26. package/schematics/ng-add/index.js +2 -4
  27. package/schematics/ng-update/migrations/update-13_2_0/index.js +7 -2
  28. package/schematics/ng-update/migrations/update-19_2_0/index.js +39 -0
  29. package/schematics/utility/add-allowed-common-js-dependencies.js +39 -29
  30. package/schematics/utility/form-signal/class-migration.js +396 -0
  31. package/schematics/utility/form-signal/edits.js +34 -0
  32. package/schematics/utility/form-signal/form-signal-migration.js +184 -0
  33. package/schematics/utility/form-signal/page-classes.js +129 -0
  34. package/schematics/utility/form-signal/template-migration.js +666 -0
  35. package/schematics/utility/remove-emit-decorator-metadata.js +8 -9
  36. package/src/eslint/configs/__tests__/configs.test.mjs +46 -2
  37. package/src/eslint/configs/spec-ts-recommended.mjs +34 -0
  38. package/src/eslint/configs/template-recommended.mjs +2 -0
  39. package/src/eslint/configs/template-rules.mjs +11 -2
  40. package/src/eslint/configs/ts-recommended.mjs +6 -0
  41. package/src/eslint/configs/ts-rules.mjs +6 -2
  42. package/src/eslint/form-reads.mjs +63 -0
  43. package/src/eslint/index.mjs +17 -0
  44. package/src/eslint/rules/__tests__/no-direct-signal-mutation.test.mjs +58 -0
  45. package/src/eslint/rules/__tests__/no-item-track-in-form-list.test.mjs +127 -0
  46. package/src/eslint/rules/__tests__/no-uninvoked-signal-in-template.test.mjs +6 -0
  47. package/src/eslint/rules/__tests__/require-model-change.test.mjs +93 -0
  48. package/src/eslint/rules/index.mjs +4 -0
  49. package/src/eslint/rules/no-direct-signal-mutation.mjs +58 -20
  50. package/src/eslint/rules/no-item-track-in-form-list.mjs +99 -0
  51. package/src/eslint/rules/no-uninvoked-signal-in-template.mjs +8 -3
  52. package/src/eslint/rules/require-model-change.mjs +89 -0
  53. package/src/eslint/template-ast.mjs +12 -2
  54. package/types/dsivd-prestations-ng.d.ts +652 -240
  55. package/dsivd-prestations-ng-19.2.0-beta.2.tgz +0 -0
@@ -1,18 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.removeEmitDecoratorMetadata = void 0;
4
+ const json_file_1 = require("@schematics/angular/utility/json-file");
4
5
  const removeEmitDecoratorMetadata = (tree) => {
5
6
  const tsConfig = 'tsconfig.json';
6
- const file = tree.read(tsConfig);
7
- if (!file) {
7
+ if (!tree.exists(tsConfig)) {
8
8
  return;
9
9
  }
10
- const json = JSON.parse(file.toString());
11
- const compilerOptions = json.compilerOptions;
12
- if (Object.hasOwn(compilerOptions, 'emitDecoratorMetadata')) {
13
- delete json.compilerOptions.emitDecoratorMetadata;
14
- tree.overwrite(tsConfig, JSON.stringify(json, null, 2));
15
- }
10
+ // JSONFile keeps the formatting and the comments of the file
11
+ new json_file_1.JSONFile(tree, tsConfig).remove([
12
+ 'compilerOptions',
13
+ 'emitDecoratorMetadata',
14
+ ]);
16
15
  };
17
16
  exports.removeEmitDecoratorMetadata = removeEmitDecoratorMetadata;
18
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVtb3ZlLWVtaXQtZGVjb3JhdG9yLW1ldGFkYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vcHJvamVjdHMvcHJlc3RhdGlvbnMtbmcvc2NoZW1hdGljcy91dGlsaXR5L3JlbW92ZS1lbWl0LWRlY29yYXRvci1tZXRhZGF0YS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFFTyxNQUFNLDJCQUEyQixHQUFHLENBQUMsSUFBVSxFQUFRLEVBQUU7SUFDNUQsTUFBTSxRQUFRLEdBQUcsZUFBZSxDQUFDO0lBQ2pDLE1BQU0sSUFBSSxHQUFHLElBQUksQ0FBQyxJQUFJLENBQUMsUUFBUSxDQUFDLENBQUM7SUFDakMsSUFBSSxDQUFDLElBQUksRUFBRSxDQUFDO1FBQ1IsT0FBTztJQUNYLENBQUM7SUFFRCxNQUFNLElBQUksR0FBRyxJQUFJLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxRQUFRLEVBQUUsQ0FBQyxDQUFDO0lBQ3pDLE1BQU0sZUFBZSxHQUFHLElBQUksQ0FBQyxlQUFlLENBQUM7SUFDN0MsSUFBSSxNQUFNLENBQUMsTUFBTSxDQUFDLGVBQWUsRUFBRSx1QkFBdUIsQ0FBQyxFQUFFLENBQUM7UUFDMUQsT0FBTyxJQUFJLENBQUMsZUFBZSxDQUFDLHFCQUFxQixDQUFDO1FBRWxELElBQUksQ0FBQyxTQUFTLENBQUMsUUFBUSxFQUFFLElBQUksQ0FBQyxTQUFTLENBQUMsSUFBSSxFQUFFLElBQUksRUFBRSxDQUFDLENBQUMsQ0FBQyxDQUFDO0lBQzVELENBQUM7QUFDTCxDQUFDLENBQUM7QUFkVyxRQUFBLDJCQUEyQiwrQkFjdEMifQ==
17
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVtb3ZlLWVtaXQtZGVjb3JhdG9yLW1ldGFkYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vcHJvamVjdHMvcHJlc3RhdGlvbnMtbmcvc2NoZW1hdGljcy91dGlsaXR5L3JlbW92ZS1lbWl0LWRlY29yYXRvci1tZXRhZGF0YS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFDQSxxRUFBaUU7QUFFMUQsTUFBTSwyQkFBMkIsR0FBRyxDQUFDLElBQVUsRUFBUSxFQUFFO0lBQzVELE1BQU0sUUFBUSxHQUFHLGVBQWUsQ0FBQztJQUNqQyxJQUFJLENBQUMsSUFBSSxDQUFDLE1BQU0sQ0FBQyxRQUFRLENBQUMsRUFBRSxDQUFDO1FBQ3pCLE9BQU87SUFDWCxDQUFDO0lBRUQsNkRBQTZEO0lBQzdELElBQUksb0JBQVEsQ0FBQyxJQUFJLEVBQUUsUUFBUSxDQUFDLENBQUMsTUFBTSxDQUFDO1FBQ2hDLGlCQUFpQjtRQUNqQix1QkFBdUI7S0FDMUIsQ0FBQyxDQUFDO0FBQ1AsQ0FBQyxDQUFDO0FBWFcsUUFBQSwyQkFBMkIsK0JBV3RDIn0=
@@ -1,3 +1,4 @@
1
+ import vitest from '@vitest/eslint-plugin';
1
2
  import { builtinRules } from 'eslint/use-at-your-own-risk';
2
3
  import { describe, expect, it } from 'vitest';
3
4
 
@@ -6,6 +7,10 @@ import prestationsNg, { configs, plugin, rules } from '../../index.mjs';
6
7
  const PLUGIN_KEY = '@dsivd/prestations-ng';
7
8
 
8
9
  const configNames = Object.keys(configs);
10
+ // The configs of this library's plugin (the preset of the Vitest specs only configures @vitest/eslint-plugin)
11
+ const pluginConfigNames = configNames.filter(
12
+ (name) => name !== 'specTsRecommended',
13
+ );
9
14
 
10
15
  // Configs that only register this library's rules, and the presets built on top of them.
11
16
  const RULE_LAYERS = ['tsRules', 'templateRules'];
@@ -52,6 +57,7 @@ describe('eslint configs', () => {
52
57
  'templateRecommended',
53
58
  'tsRules',
54
59
  'templateRules',
60
+ 'specTsRecommended',
55
61
  ]);
56
62
  });
57
63
 
@@ -88,7 +94,7 @@ describe('eslint configs', () => {
88
94
 
89
95
  // Two entries declaring the same plugin key from different copies is a hard ESLint
90
96
  // failure, so the preset must register ours exactly once.
91
- it.each(configNames)('%s should register the plugin once', (name) => {
97
+ it.each(pluginConfigNames)('%s should register the plugin once', (name) => {
92
98
  const declarations = configs[name]
93
99
  .map((entry) => entry.plugins?.[PLUGIN_KEY])
94
100
  .filter(Boolean);
@@ -101,7 +107,9 @@ describe('eslint configs', () => {
101
107
  });
102
108
 
103
109
  it('should enable every rule in at least one config', () => {
104
- const enabled = new Set(configNames.flatMap((n) => ownIdsOf(configs[n])));
110
+ const enabled = new Set(
111
+ pluginConfigNames.flatMap((n) => ownIdsOf(configs[n])),
112
+ );
105
113
 
106
114
  for (const ruleName of Object.keys(rules)) {
107
115
  expect(enabled).toContain(`${PLUGIN_KEY}/${ruleName}`);
@@ -118,6 +126,42 @@ describe('eslint configs', () => {
118
126
  );
119
127
  });
120
128
 
129
+ describe('specTsRecommended', () => {
130
+ const rulesOf = (config) =>
131
+ Object.assign({}, ...config.map((entry) => entry.rules ?? {}));
132
+
133
+ it('should build on the recommended config of @vitest/eslint-plugin', () => {
134
+ expect(configs.specTsRecommended[0]).toBe(vitest.configs.recommended);
135
+ expect(pluginsOf(configs.specTsRecommended)).toEqual({ vitest });
136
+ });
137
+
138
+ it('should count the assertions of the helpers and of the HttpTestingController', () => {
139
+ const [, options] = rulesOf(configs.specTsRecommended)[
140
+ 'vitest/expect-expect'
141
+ ];
142
+
143
+ expect(options.assertFunctionNames).toEqual(
144
+ expect.arrayContaining([
145
+ 'expect',
146
+ 'expect*',
147
+ '*.expectOne',
148
+ '*.verify',
149
+ ]),
150
+ );
151
+ });
152
+
153
+ // An expect which may not run lets a test pass without asserting anything
154
+ it('should keep vitest/no-conditional-expect enabled', () => {
155
+ expect(
156
+ isOff(
157
+ rulesOf(configs.specTsRecommended)[
158
+ 'vitest/no-conditional-expect'
159
+ ],
160
+ ),
161
+ ).toBe(false);
162
+ });
163
+ });
164
+
121
165
  describe('eslint plugin', () => {
122
166
  it('should be named after the npm package', () => {
123
167
  expect(plugin.meta.name).toBe(PLUGIN_KEY);
@@ -0,0 +1,34 @@
1
+ /**
2
+ * The house preset of the Vitest specs: the recommended rules of `@vitest/eslint-plugin`, with the
3
+ * assertions also made by the helpers of the specs and by the `HttpTestingController`.
4
+ *
5
+ * Meant for the `extends` of a `**\/*.spec.ts` block (the Cypress `*.spec.cy.ts` files are not
6
+ * Vitest specs).
7
+ *
8
+ * @param {import('@vitest/eslint-plugin').default} vitest the `@vitest/eslint-plugin` module
9
+ * @returns {import('typescript-eslint').ConfigArray}
10
+ */
11
+ const specTsRecommended = (vitest) => [
12
+ vitest.configs.recommended,
13
+ {
14
+ name: 'prestations-ng/spec-ts-recommended',
15
+ rules: {
16
+ 'vitest/expect-expect': [
17
+ 'error',
18
+ {
19
+ assertFunctionNames: [
20
+ 'expect',
21
+ 'expect*',
22
+ 'assert*',
23
+ 'test*',
24
+ '*.expectOne',
25
+ '*.expectNone',
26
+ '*.verify',
27
+ ],
28
+ },
29
+ ],
30
+ },
31
+ },
32
+ ];
33
+
34
+ export default specTsRecommended;
@@ -8,6 +8,8 @@ import templateRules from './template-rules.mjs';
8
8
  *
9
9
  * Meant for the `extends` of a `**\/*.html` block. It brings its own parser and plugins,
10
10
  * so do not spread `angular.configs.templateRecommended` alongside it.
11
+ *
12
+ * @returns {import('typescript-eslint').ConfigArray}
11
13
  */
12
14
  const templateRecommended = (plugin) => [
13
15
  ...angular.configs.templateRecommended,
@@ -1,7 +1,11 @@
1
1
  import templateBase from './template-base.mjs';
2
2
 
3
- // Only the rules written by this library. `templateRecommended` includes it, alongside
4
- // the house style; use this layer directly to opt out of the latter.
3
+ /**
4
+ * Only the rules written by this library. `templateRecommended` includes it, alongside
5
+ * the house style; use this layer directly to opt out of the latter.
6
+ *
7
+ * @returns {import('typescript-eslint').ConfigArray}
8
+ */
5
9
  const templateRules = (plugin) => [
6
10
  templateBase(plugin),
7
11
  {
@@ -11,6 +15,11 @@ const templateRules = (plugin) => [
11
15
  // template visitor, and each fires on its own kind of file.
12
16
  '@dsivd/prestations-ng/no-direct-signal-mutation': 'error',
13
17
  '@dsivd/prestations-ng/no-uninvoked-signal-in-template': 'error',
18
+ // The form of the pages is a signal: a one-way [model] without
19
+ // (modelChange) loses the input, and a row tracked by its item
20
+ // loses the focus at every input
21
+ '@dsivd/prestations-ng/require-model-change': 'error',
22
+ '@dsivd/prestations-ng/no-item-track-in-form-list': 'error',
14
23
  },
15
24
  },
16
25
  ];
@@ -14,6 +14,8 @@ import tsRules from './ts-rules.mjs';
14
14
  * Meant for the `extends` of a `**\/*.ts` block. It brings its own parser and plugins,
15
15
  * so do not spread `angular.configs.tsRecommended` alongside it — declaring the same
16
16
  * plugin twice from two different copies makes ESLint fail outright.
17
+ *
18
+ * @returns {import('typescript-eslint').ConfigArray}
17
19
  */
18
20
  const tsRecommended = (plugin) => [
19
21
  eslint.configs.recommended,
@@ -71,6 +73,10 @@ const tsRecommended = (plugin) => [
71
73
  ],
72
74
  '@typescript-eslint/member-ordering': 'error',
73
75
  '@typescript-eslint/no-base-to-string': 'warn',
76
+ // a template literal only takes strings, numbers, booleans, null,
77
+ // undefined, any and RegExp: also reports a signal not called
78
+ // (`${this.reference}`), which no-uncalled-signals skips there
79
+ '@typescript-eslint/restrict-template-expressions': 'error',
74
80
  // stylistic enables no-inferrable-types with default options (ignoreParameters: false);
75
81
  // override here to allow typed parameters explicitly
76
82
  '@typescript-eslint/no-inferrable-types': [
@@ -1,7 +1,11 @@
1
1
  import tsBase from './ts-base.mjs';
2
2
 
3
- // Only the rules written by this library. `tsRecommended` includes it, alongside the
4
- // house style; use this layer directly to opt out of the latter.
3
+ /**
4
+ * Only the rules written by this library. `tsRecommended` includes it, alongside the
5
+ * house style; use this layer directly to opt out of the latter.
6
+ *
7
+ * @returns {import('typescript-eslint').ConfigArray}
8
+ */
5
9
  const tsRules = (plugin) => [
6
10
  tsBase(plugin),
7
11
  {
@@ -0,0 +1,63 @@
1
+ import { isComponentReceiver } from "./template-ast.mjs";
2
+
3
+ /**
4
+ * Where a template expression reads its value from: the form of the page (a signal since
5
+ * 19.2.0), through the names bound by `@if (form(); as form)`, `@let form = this.form()` and
6
+ * `@for (child of form.children)`.
7
+ */
8
+
9
+ const MEMBER_READS = new Set([
10
+ "PropertyRead",
11
+ "SafePropertyRead",
12
+ "KeyedRead",
13
+ "SafeKeyedRead",
14
+ ]);
15
+
16
+ export const astOf = (expression) =>
17
+ expression?.type === "ASTWithSource" ? expression.ast : expression;
18
+
19
+ /**
20
+ * The root of an expression: a member of the component (`{ member }`), or the item of an
21
+ * enclosing `@for` (`{ forBlock }`). A name bound by `@if … as` is followed to its expression,
22
+ * evaluated outside its block (in `@if (form(); as form)`, `form()` is the member).
23
+ * `undefined` for anything else (a literal, a call with arguments...).
24
+ */
25
+ export function resolveRoot(expression, node) {
26
+ let base = expression;
27
+ while (MEMBER_READS.has(base?.type) && MEMBER_READS.has(base.receiver?.type)) {
28
+ base = base.receiver;
29
+ }
30
+ if (base?.type === "Call" || base?.type === "SafeCall") {
31
+ base = base.receiver;
32
+ }
33
+ const receiver = base?.receiver;
34
+ if (receiver?.type === "Call" || receiver?.type === "SafeCall") {
35
+ return resolveRoot(receiver, node);
36
+ }
37
+ if (!MEMBER_READS.has(base?.type) || !isComponentReceiver(receiver)) {
38
+ return undefined;
39
+ }
40
+ for (let current = node; current; current = current.parent) {
41
+ if (current.type === "ForLoopBlock" && current.item?.name === base.name) {
42
+ return { forBlock: current };
43
+ }
44
+ if (
45
+ current.type === "IfBlockBranch" &&
46
+ current.expressionAlias?.name === base.name
47
+ ) {
48
+ return resolveRoot(astOf(current.expression), current.parent);
49
+ }
50
+ }
51
+ return { member: base.name };
52
+ }
53
+
54
+ /**
55
+ * Whether an expression reads the form of the page, directly or through a template name.
56
+ */
57
+ export function readsForm(expression, node) {
58
+ const root = resolveRoot(expression, node);
59
+ if (root?.forBlock) {
60
+ return readsForm(astOf(root.forBlock.expression), root.forBlock.parent);
61
+ }
62
+ return root?.member === "form";
63
+ }
@@ -1,9 +1,17 @@
1
+ import specTsRecommended from './configs/spec-ts-recommended.mjs';
1
2
  import templateRecommended from './configs/template-recommended.mjs';
2
3
  import templateRules from './configs/template-rules.mjs';
3
4
  import tsRecommended from './configs/ts-recommended.mjs';
4
5
  import tsRules from './configs/ts-rules.mjs';
5
6
  import rules from './rules/index.mjs';
6
7
 
8
+ // Only needed by the projects linting Vitest specs: the other configs can be used without it.
9
+ const vitest = await import('@vitest/eslint-plugin').then(
10
+ (module) => module.default,
11
+ () => null,
12
+ );
13
+ const specTsRecommendedConfig = vitest ? specTsRecommended(vitest) : null;
14
+
7
15
  const plugin = {
8
16
  meta: { name: '@dsivd/prestations-ng' },
9
17
  rules,
@@ -16,6 +24,15 @@ const configs = {
16
24
  // Only this library's own rules, for a project that supplies its own style.
17
25
  tsRules: tsRules(plugin),
18
26
  templateRules: templateRules(plugin),
27
+ // The preset of the Vitest specs (needs @vitest/eslint-plugin)
28
+ get specTsRecommended() {
29
+ if (!specTsRecommendedConfig) {
30
+ throw new Error(
31
+ 'prestationsNg.configs.specTsRecommended needs @vitest/eslint-plugin: npm install --save-dev @vitest/eslint-plugin@1',
32
+ );
33
+ }
34
+ return specTsRecommendedConfig;
35
+ },
19
36
  };
20
37
 
21
38
  export { configs, plugin, rules };
@@ -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';
@@ -266,6 +267,23 @@ describe('no-direct-signal-mutation', () => {
266
267
  errors: [{ messageId }],
267
268
  });
268
269
 
270
+ it('should see through the TypeScript wrappers of the value', () => {
271
+ const typeScriptRuleTester = new RuleTester({
272
+ languageOptions: { parser: typeScriptParser },
273
+ });
274
+
275
+ typeScriptRuleTester.run('no-direct-signal-mutation', rule, {
276
+ valid: [ts('const name = this.inherited()!.name;')],
277
+ invalid: [
278
+ // The form of a page read with a non-null assertion (the ng update migration writes so)
279
+ invalidTs('this.inherited()!.name = value', 'directMutation'),
280
+ invalidTs('this.inherited()!.users.push(user)', 'inPlaceMutation'),
281
+ invalidTs('(this.partenaire() as Partenaire).name = value', 'directMutation'),
282
+ invalidTs('const form = this.inherited()!; form.name = value', 'directMutation'),
283
+ ],
284
+ });
285
+ });
286
+
269
287
  it('should validate signal mutation rule in templates', () => {
270
288
  templateRuleTester.run('no-direct-signal-mutation', rule, {
271
289
  valid: [
@@ -305,6 +323,10 @@ describe('no-direct-signal-mutation', () => {
305
323
 
306
324
  // An output named `…Change` that carries no assignment
307
325
  template('<app-child (valueChange)="comment.set($event)"></app-child>'),
326
+ // An explicit handler is a statement, not the target of a two-way binding: a call
327
+ // assigns nothing
328
+ template('<app-child (valueChange)="partenaire()"></app-child>'),
329
+ template('<app-child [value]="partenaire().name" (valueChange)="partenaire()"></app-child>'),
308
330
 
309
331
  // A `@for` item is only a signal's value when the loop iterates a signal
310
332
  template('@for (u of getConfig().users; track u) { <button (click)="u.done = 1"></button> }'),
@@ -314,6 +336,17 @@ describe('no-direct-signal-mutation', () => {
314
336
  template('@for (u of items(); track u) { {{ u.name }} }'),
315
337
  template('@let copy = getConfig().users; <button (click)="copy.push(item)"></button>'),
316
338
 
339
+ // An `@if` alias is only a signal's value when the condition is one
340
+ template('@if (getConfig(); as config) { <input [(ngModel)]="config.name" /> }'),
341
+ // Reading it mutates nothing
342
+ template('@if (partenaire(); as p) { {{ p.name }} }'),
343
+ // It is bound only inside its own block
344
+ template('@if (partenaire(); as p) { } <button (click)="p.name = value"></button>'),
345
+ // The nearest name shadows the others
346
+ template(`@if (partenaire(); as u) {
347
+ @for (u of getConfig().users; track u) { <button (click)="u.done = 1"></button> }
348
+ }`),
349
+
317
350
  // A computed member whose method does not mutate
318
351
  template(`<button (click)="items()['toSorted']()"></button>`),
319
352
  template('<button (click)="items().filter(active).sort()"></button>'),
@@ -355,6 +388,25 @@ describe('no-direct-signal-mutation', () => {
355
388
  invalidTemplate('@let users = partenaire().users; <button (click)="users.push(user)"></button>', 'inPlaceMutation'),
356
389
  invalidTemplate('@let value = partenaire(); <button (click)="value.name = other"></button>', 'directMutation'),
357
390
 
391
+ // Same through the alias of an `@if`, bound only inside its own branch
392
+ invalidTemplate('@if (partenaire(); as p) { <input [(ngModel)]="p.name" /> }', 'directMutation'),
393
+ invalidTemplate('@if (partenaire(); as p) { <button (click)="p.name = value"></button> }', 'directMutation'),
394
+ invalidTemplate('@if (partenaire(); as p) { <button (click)="p.users.push(user)"></button> }', 'inPlaceMutation'),
395
+ invalidTemplate('@if (ready) { } @else if (partenaire(); as p) { <input [(ngModel)]="p.name" /> }', 'directMutation'),
396
+ invalidTemplate(`@if (partenaire(); as p) {
397
+ @if (p.users; as users) { <button (click)="users.push(user)"></button> }
398
+ }`, 'inPlaceMutation'),
399
+ // The alias named as the signal, as in `@if (form(); as form)`
400
+ invalidTemplate('@if (partenaire(); as partenaire) { <input [(ngModel)]="partenaire.name" /> }', 'directMutation'),
401
+ invalidTemplate('@if (double(); as d) { <button (click)="d.users.push(user)"></button> }', 'readOnlyMutation'),
402
+
403
+ // A non-null assertion or parentheses keep the value as is
404
+ invalidTemplate('<input [(ngModel)]="partenaire()!.name" />', 'directMutation'),
405
+ invalidTemplate('<button (click)="(partenaire()).name = value"></button>', 'directMutation'),
406
+ invalidTemplate('<button (click)="items()!.push(item)"></button>', 'inPlaceMutation'),
407
+ invalidTemplate('@let value = partenaire()!; <button (click)="value.name = other"></button>', 'directMutation'),
408
+ invalidTemplate('@let value = partenaire(); <button (click)="value!.name = other"></button>', 'directMutation'),
409
+
358
410
  // Every other kind of signal, each with the advice that applies to it
359
411
  invalidTemplate('<button (click)="double().users.push(user)"></button>', 'readOnlyMutation'),
360
412
  invalidTemplate('<button (click)="formArray().push(control)"></button>', 'readOnlyMutation'),
@@ -399,6 +451,12 @@ describe('no-direct-signal-mutation', () => {
399
451
  code: '<button (click)="map.get(key).name = value"></button>',
400
452
  filename: unknownTemplatePath,
401
453
  },
454
+ // A template shared by several components: a `…Change` handler calling a method
455
+ // assigns nothing
456
+ {
457
+ code: '<app-child [(value)]="item.weight" (valueChange)="updateModel()"></app-child>',
458
+ filename: unknownTemplatePath,
459
+ },
402
460
  ],
403
461
  invalid: [
404
462
  {
@@ -0,0 +1,127 @@
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
+ });
126
+ });
127
+ });
@@ -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
+ });