@angular-eslint/eslint-plugin-template 17.4.2-alpha.3 → 17.4.2-alpha.5

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/dist/index.d.ts CHANGED
@@ -117,6 +117,7 @@ declare const _default: {
117
117
  readonly inputs?: readonly string[] | undefined;
118
118
  readonly selector: string;
119
119
  }[] | undefined;
120
+ readonly checkIds?: boolean | undefined;
120
121
  }], import("@typescript-eslint/utils/ts-eslint").RuleListener>;
121
122
  "mouse-events-have-key-events": import("@typescript-eslint/utils/ts-eslint").RuleModule<"mouseEventsHaveKeyEvents", [], import("@typescript-eslint/utils/ts-eslint").RuleListener>;
122
123
  "no-any": import("@typescript-eslint/utils/ts-eslint").RuleModule<import("./rules/no-any").MessageIds, [], import("@typescript-eslint/utils/ts-eslint").RuleListener>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmFA,kBAuCE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmFA,kBAuCE"}
@@ -6,6 +6,7 @@ type Options = [
6
6
  {
7
7
  readonly controlComponents?: readonly string[];
8
8
  readonly labelComponents?: readonly LabelComponent[];
9
+ readonly checkIds?: boolean;
9
10
  }
10
11
  ];
11
12
  export type MessageIds = 'labelHasAssociatedControl';
@@ -1 +1 @@
1
- {"version":3,"file":"label-has-associated-control.d.ts","sourceRoot":"","sources":["../../src/rules/label-has-associated-control.ts"],"names":[],"mappings":"AAKA,KAAK,cAAc,GAAG;IACpB,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B,CAAC;AACF,KAAK,OAAO,GAAG;IACb;QACE,QAAQ,CAAC,iBAAiB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;QAC/C,QAAQ,CAAC,eAAe,CAAC,EAAE,SAAS,cAAc,EAAE,CAAC;KACtD;CACF,CAAC;AACF,MAAM,MAAM,UAAU,GAAG,2BAA2B,CAAC;AACrD,eAAO,MAAM,SAAS,iCAAiC,CAAC;;AAiBxD,wBAqFG"}
1
+ {"version":3,"file":"label-has-associated-control.d.ts","sourceRoot":"","sources":["../../src/rules/label-has-associated-control.ts"],"names":[],"mappings":"AAQA,KAAK,cAAc,GAAG;IACpB,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B,CAAC;AACF,KAAK,OAAO,GAAG;IACb;QACE,QAAQ,CAAC,iBAAiB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;QAC/C,QAAQ,CAAC,eAAe,CAAC,EAAE,SAAS,cAAc,EAAE,CAAC;QACrD,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;KAC7B;CACF,CAAC;AACF,MAAM,MAAM,UAAU,GAAG,2BAA2B,CAAC;AACrD,eAAO,MAAM,SAAS,iCAAiC,CAAC;;AAkBxD,wBA8GG"}
@@ -19,6 +19,7 @@ const DEFAULT_LABEL_COMPONENTS = [
19
19
  const DEFAULT_OPTIONS = {
20
20
  controlComponents: DEFAULT_CONTROL_COMPONENTS,
21
21
  labelComponents: DEFAULT_LABEL_COMPONENTS,
22
+ checkIds: false,
22
23
  };
23
24
  exports.default = (0, create_eslint_rule_1.createESLintRule)({
24
25
  name: exports.RULE_NAME,
@@ -31,6 +32,7 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
31
32
  {
32
33
  additionalProperties: false,
33
34
  properties: {
35
+ checkIds: { type: 'boolean' },
34
36
  controlComponents: {
35
37
  items: { type: 'string' },
36
38
  type: 'array',
@@ -62,7 +64,7 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
62
64
  },
63
65
  },
64
66
  defaultOptions: [DEFAULT_OPTIONS],
65
- create(context, [{ controlComponents, labelComponents }]) {
67
+ create(context, [{ checkIds, controlComponents, labelComponents }]) {
66
68
  const parserServices = (0, utils_1.getTemplateParserServices)(context);
67
69
  const allControlComponents = new Set([
68
70
  ...DEFAULT_CONTROL_COMPONENTS,
@@ -72,23 +74,44 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
72
74
  ...DEFAULT_LABEL_COMPONENTS,
73
75
  ...(labelComponents ?? []),
74
76
  ];
75
- const labelSelectors = allLabelComponents.map(({ selector }) => selector);
76
- const labelComponentsPattern = toPattern(labelSelectors);
77
+ let inputItems = [];
78
+ let labelItems = [];
77
79
  return {
78
- [`Element$1[name=${labelComponentsPattern}]`](node) {
80
+ [`Element$1`](node) {
81
+ if (allControlComponents.has(node.name)) {
82
+ inputItems.push(node);
83
+ }
79
84
  const element = allLabelComponents.find(({ selector }) => selector === node.name);
80
- if (!element)
81
- return;
82
- const attributesInputs = new Set([...node.attributes, ...node.inputs].map(({ name }) => name));
83
- const hasInput = element.inputs?.some((input) => attributesInputs.has(input));
84
- if (hasInput || hasControlComponentIn(allControlComponents, node)) {
85
- return;
85
+ if (element) {
86
+ labelItems.push(node);
87
+ }
88
+ },
89
+ onCodePathEnd() {
90
+ for (const node of labelItems) {
91
+ const element = allLabelComponents.find(({ selector }) => selector === node.name);
92
+ if (!element)
93
+ continue;
94
+ const attributesInputs = new Map([...node.attributes, ...node.inputs].map(({ name, value }) => [
95
+ name,
96
+ value,
97
+ ]));
98
+ const inputValues = (element.inputs?.map((input) => attributesInputs.get(input)) ?? []).filter(filterUndefined);
99
+ let hasFor = inputValues.length > 0;
100
+ if (hasFor && checkIds) {
101
+ const value = inputValues[0];
102
+ hasFor = hasControlComponentWithId(inputItems, value);
103
+ }
104
+ if (hasFor || hasControlComponentIn(allControlComponents, node)) {
105
+ continue;
106
+ }
107
+ const loc = parserServices.convertNodeSourceSpanToLoc(node.sourceSpan);
108
+ context.report({
109
+ loc,
110
+ messageId: 'labelHasAssociatedControl',
111
+ });
86
112
  }
87
- const loc = parserServices.convertNodeSourceSpanToLoc(node.sourceSpan);
88
- context.report({
89
- loc,
90
- messageId: 'labelHasAssociatedControl',
91
- });
113
+ inputItems = [];
114
+ labelItems = [];
92
115
  },
93
116
  };
94
117
  },
@@ -96,6 +119,11 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
96
119
  function hasControlComponentIn(controlComponents, element) {
97
120
  return Boolean([...controlComponents].some((controlComponent) => (0, is_child_node_of_1.isChildNodeOf)(element, controlComponent)));
98
121
  }
99
- function toPattern(value) {
100
- return RegExp(`^(${value.join('|')})$`);
122
+ function hasControlComponentWithId(controlComponents, id) {
123
+ return Boolean([...controlComponents].some((node) => {
124
+ return !![...node.attributes, ...node.inputs].find((input) => input.name === 'id' && input.value === id);
125
+ }));
126
+ }
127
+ function filterUndefined(value) {
128
+ return value !== undefined && value !== null;
101
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@angular-eslint/eslint-plugin-template",
3
- "version": "17.4.2-alpha.3",
3
+ "version": "17.4.2-alpha.5",
4
4
  "description": "ESLint plugin for Angular Templates",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -18,8 +18,8 @@
18
18
  "LICENSE"
19
19
  ],
20
20
  "dependencies": {
21
- "@angular-eslint/bundled-angular-compiler": "17.4.2-alpha.3",
22
- "@angular-eslint/utils": "17.4.2-alpha.3",
21
+ "@angular-eslint/bundled-angular-compiler": "17.4.2-alpha.5",
22
+ "@angular-eslint/utils": "17.4.2-alpha.5",
23
23
  "@typescript-eslint/type-utils": "7.10.0",
24
24
  "@typescript-eslint/utils": "7.10.0",
25
25
  "aria-query": "5.3.0",