@trackunit/eslint-plugin-trackunit 0.6.150 → 0.6.152

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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## 0.6.152 (2026-09-25)
2
+
3
+ ### 🧱 Updated Dependencies
4
+
5
+ - Updated css-classname-utils to 0.0.53
6
+ - Updated shared-utils to 1.16.55
7
+
8
+ ## 0.6.151 (2026-09-24)
9
+
10
+ ### 🧱 Updated Dependencies
11
+
12
+ - Updated css-classname-utils to 0.0.52
13
+ - Updated shared-utils to 1.16.54
14
+
1
15
  ## 0.6.150 (2026-09-24)
2
16
 
3
17
  ### 🧱 Updated Dependencies
package/README.md CHANGED
@@ -57,7 +57,7 @@ All custom rules are enabled automatically through the presets under the `@track
57
57
  | `@trackunit/no-template-strings-in-classname-prop` | — | — | Disallows template literals in `className`. Use `cx()` or `cva()` instead. |
58
58
  | `@trackunit/require-classname-alternatives` | — | yes | Replaces banned class names with approved alternatives. |
59
59
  | `@trackunit/cva-merge-base-classes-as-array` | — | yes | Enforces array syntax for `cva` base classes when more than one class is used. |
60
- | `@trackunit/design-guideline-button-icon-size-match` | — | yes | Ensures `Icon` inside `Button` uses the correct `size` prop. |
60
+ | `@trackunit/design-guideline-icon-size-match` | — | yes | Ensures `Icon` in `Button`, `Tag`, and `getOptionPrefix` uses the correct `size` prop. |
61
61
  | `@trackunit/prefer-field-components` | — | yes | Prefers `SelectField`/`TextField` over manually combining `Label` + base input components. |
62
62
  | `@trackunit/prefer-mouse-event-handler-in-react-props` | — | — | Enforces `MouseEventHandler<T>` typing for `onClick*` props. |
63
63
  | `@trackunit/prefer-event-specific-callback-naming` | — | — | Enforces event-specific naming (e.g. `onClickClose` over `onClose`) for click handlers. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/eslint-plugin-trackunit",
3
- "version": "0.6.150",
3
+ "version": "0.6.152",
4
4
  "license": "SEE LICENSE IN LICENSE.txt",
5
5
  "repository": "https://github.com/Trackunit/manager",
6
6
  "engines": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@nx/eslint-plugin": "23.1.0",
11
- "@trackunit/css-classname-utils": "0.0.51",
11
+ "@trackunit/css-classname-utils": "0.0.53",
12
12
  "@typescript-eslint/eslint-plugin": "8.58.1",
13
13
  "@typescript-eslint/utils": "8.58.1",
14
14
  "eslint-config-prettier": "^10.1.8",
@@ -95,7 +95,7 @@ exports.reactCustomRules = {
95
95
  ],
96
96
  "@trackunit/require-list-item-virtualization-props": "error",
97
97
  "@trackunit/cva-merge-base-classes-as-array": "error",
98
- "@trackunit/design-guideline-button-icon-size-match": "error",
98
+ "@trackunit/design-guideline-icon-size-match": "error",
99
99
  "@trackunit/prefer-destructured-imports": [
100
100
  "error",
101
101
  {
@@ -35,6 +35,9 @@ export declare const configs: {
35
35
  "no-internal-graphql-when-tagged-with-gql-public": import("@typescript-eslint/utils/ts-eslint").RuleModule<"internalGraphqlFileFound", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
36
36
  name: string;
37
37
  };
38
+ "design-guideline-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
39
+ name: string;
40
+ };
38
41
  "design-guideline-button-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
39
42
  name: string;
40
43
  };
@@ -254,6 +257,9 @@ export declare const configs: {
254
257
  "no-internal-graphql-when-tagged-with-gql-public": import("@typescript-eslint/utils/ts-eslint").RuleModule<"internalGraphqlFileFound", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
255
258
  name: string;
256
259
  };
260
+ "design-guideline-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
261
+ name: string;
262
+ };
257
263
  "design-guideline-button-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
258
264
  name: string;
259
265
  };
@@ -375,6 +381,9 @@ export declare const configs: {
375
381
  "no-internal-graphql-when-tagged-with-gql-public": import("@typescript-eslint/utils/ts-eslint").RuleModule<"internalGraphqlFileFound", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
376
382
  name: string;
377
383
  };
384
+ "design-guideline-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
385
+ name: string;
386
+ };
378
387
  "design-guideline-button-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
379
388
  name: string;
380
389
  };
@@ -526,6 +535,9 @@ export declare const configs: {
526
535
  "no-internal-graphql-when-tagged-with-gql-public": import("@typescript-eslint/utils/ts-eslint").RuleModule<"internalGraphqlFileFound", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
527
536
  name: string;
528
537
  };
538
+ "design-guideline-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
539
+ name: string;
540
+ };
529
541
  "design-guideline-button-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
530
542
  name: string;
531
543
  };
@@ -53,6 +53,9 @@ export declare const localRulesPlugin: {
53
53
  "no-internal-graphql-when-tagged-with-gql-public": import("@typescript-eslint/utils/ts-eslint").RuleModule<"internalGraphqlFileFound", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
54
54
  name: string;
55
55
  };
56
+ "design-guideline-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
57
+ name: string;
58
+ };
56
59
  "design-guideline-button-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
57
60
  name: string;
58
61
  };
@@ -36,6 +36,9 @@ export declare const base: (import("eslint").Linter.FlatConfig<import("eslint").
36
36
  "no-internal-graphql-when-tagged-with-gql-public": import("@typescript-eslint/utils/ts-eslint").RuleModule<"internalGraphqlFileFound", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
37
37
  name: string;
38
38
  };
39
+ "design-guideline-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
40
+ name: string;
41
+ };
39
42
  "design-guideline-button-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
40
43
  name: string;
41
44
  };
@@ -255,6 +258,9 @@ export declare const base: (import("eslint").Linter.FlatConfig<import("eslint").
255
258
  "no-internal-graphql-when-tagged-with-gql-public": import("@typescript-eslint/utils/ts-eslint").RuleModule<"internalGraphqlFileFound", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
256
259
  name: string;
257
260
  };
261
+ "design-guideline-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
262
+ name: string;
263
+ };
258
264
  "design-guideline-button-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
259
265
  name: string;
260
266
  };
@@ -376,6 +382,9 @@ export declare const base: (import("eslint").Linter.FlatConfig<import("eslint").
376
382
  "no-internal-graphql-when-tagged-with-gql-public": import("@typescript-eslint/utils/ts-eslint").RuleModule<"internalGraphqlFileFound", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
377
383
  name: string;
378
384
  };
385
+ "design-guideline-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
386
+ name: string;
387
+ };
379
388
  "design-guideline-button-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
380
389
  name: string;
381
390
  };
@@ -34,6 +34,9 @@ export declare const reactPreset: (import("eslint").Linter.Config<import("eslint
34
34
  "no-internal-graphql-when-tagged-with-gql-public": import("@typescript-eslint/utils/ts-eslint").RuleModule<"internalGraphqlFileFound", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
35
35
  name: string;
36
36
  };
37
+ "design-guideline-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
38
+ name: string;
39
+ };
37
40
  "design-guideline-button-icon-size-match": import("@typescript-eslint/utils/ts-eslint").RuleModule<"incorrectIconSize", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
38
41
  name: string;
39
42
  };
@@ -0,0 +1,4 @@
1
+ import { ESLintUtils } from "@typescript-eslint/utils";
2
+ export declare const designGuidelineIconSizeMatch: ESLintUtils.RuleModule<"incorrectIconSize", [], unknown, ESLintUtils.RuleListener> & {
3
+ name: string;
4
+ };
@@ -0,0 +1,458 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.designGuidelineIconSizeMatch = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const utils_1 = require("@typescript-eslint/utils");
6
+ const ts = tslib_1.__importStar(require("typescript"));
7
+ const createRule = utils_1.ESLintUtils.RuleCreator(name => `https://github.com/trackunit/manager/blob/main/libs/eslint/plugin-trackunit/docs/rules/${name}.md`);
8
+ const getSizeInfo = (sizeAttr) => {
9
+ if (!sizeAttr?.value) {
10
+ return { type: "implicit", value: null };
11
+ }
12
+ if (sizeAttr.value.type === "Literal" && typeof sizeAttr.value.value === "string") {
13
+ return { type: "literal", value: sizeAttr.value.value };
14
+ }
15
+ if (sizeAttr.value.type === "JSXExpressionContainer") {
16
+ const expr = sizeAttr.value.expression;
17
+ if (expr.type === "Identifier") {
18
+ return { type: "variable", value: expr.name, expression: expr };
19
+ }
20
+ if (expr.type !== "JSXEmptyExpression") {
21
+ return { type: "expression", value: null, expression: expr };
22
+ }
23
+ }
24
+ return { type: "implicit", value: null };
25
+ };
26
+ const getJsxAttribute = (node, name) => node.attributes.find((attr) => attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name === name);
27
+ // Helper to get possible string literal values from a variable's type
28
+ const getPossibleStringLiterals = (context, identifier) => {
29
+ try {
30
+ const services = utils_1.ESLintUtils.getParserServices(context);
31
+ const checker = services.program.getTypeChecker();
32
+ const tsNode = services.esTreeNodeToTSNodeMap.get(identifier);
33
+ const inspectedType = checker.getTypeAtLocation(tsNode);
34
+ const literalValues = [];
35
+ // Type guard for string literal types
36
+ const isStringLiteralType = (type) => (type.flags & ts.TypeFlags.StringLiteral) !== 0;
37
+ if (inspectedType.isUnion()) {
38
+ for (const unionType of inspectedType.types) {
39
+ if (isStringLiteralType(unionType)) {
40
+ literalValues.push(unionType.value);
41
+ }
42
+ }
43
+ }
44
+ else if (isStringLiteralType(inspectedType)) {
45
+ literalValues.push(inspectedType.value);
46
+ }
47
+ return literalValues.length > 0 ? literalValues : null;
48
+ }
49
+ catch {
50
+ return null;
51
+ }
52
+ };
53
+ // Helper to check if a conditional expression matches the expected button-icon size pattern
54
+ const isCorrectConditionalExpression = (expression, buttonVariableName) => {
55
+ if (expression.type !== "ConditionalExpression") {
56
+ return false;
57
+ }
58
+ const { test, consequent, alternate } = expression;
59
+ // Check if test is: variable === "large"
60
+ if (test.type === "BinaryExpression" &&
61
+ test.operator === "===" &&
62
+ test.left.type === "Identifier" &&
63
+ test.left.name === buttonVariableName &&
64
+ test.right.type === "Literal" &&
65
+ test.right.value === "large") {
66
+ // Check if consequent is "medium" and alternate is "small"
67
+ return (consequent.type === "Literal" &&
68
+ consequent.value === "medium" &&
69
+ alternate.type === "Literal" &&
70
+ alternate.value === "small");
71
+ }
72
+ return false;
73
+ };
74
+ const isIconOpeningElement = (element) => element.name.type === "JSXIdentifier" && element.name.name === "Icon";
75
+ const isMemoizedCallbackCall = (expression) => {
76
+ if (expression.callee.type === "Identifier") {
77
+ return expression.callee.name === "useCallback" || expression.callee.name === "useMemo";
78
+ }
79
+ return (expression.callee.type === "MemberExpression" &&
80
+ !expression.callee.computed &&
81
+ expression.callee.property.type === "Identifier" &&
82
+ (expression.callee.property.name === "useCallback" || expression.callee.property.name === "useMemo"));
83
+ };
84
+ const collectReturnStatements = (statement, returns) => {
85
+ switch (statement.type) {
86
+ case "ReturnStatement": {
87
+ returns.push(statement);
88
+ return;
89
+ }
90
+ case "BlockStatement": {
91
+ for (const nested of statement.body) {
92
+ collectReturnStatements(nested, returns);
93
+ }
94
+ return;
95
+ }
96
+ case "IfStatement": {
97
+ collectReturnStatements(statement.consequent, returns);
98
+ if (statement.alternate) {
99
+ collectReturnStatements(statement.alternate, returns);
100
+ }
101
+ return;
102
+ }
103
+ case "SwitchStatement": {
104
+ for (const switchCase of statement.cases) {
105
+ for (const nested of switchCase.consequent) {
106
+ collectReturnStatements(nested, returns);
107
+ }
108
+ }
109
+ return;
110
+ }
111
+ default: {
112
+ return;
113
+ }
114
+ }
115
+ };
116
+ // Find Icon elements - could be direct, nested in JSX, or inside conditionals and callbacks
117
+ const findIconElements = (context, expression, seen = new Set()) => {
118
+ if (!expression || expression.type === "JSXEmptyExpression" || seen.has(expression)) {
119
+ return [];
120
+ }
121
+ seen.add(expression);
122
+ switch (expression.type) {
123
+ case "JSXElement": {
124
+ if (isIconOpeningElement(expression.openingElement)) {
125
+ return [expression.openingElement];
126
+ }
127
+ return expression.children.flatMap(child => {
128
+ if (child.type === "JSXElement" || child.type === "JSXFragment") {
129
+ return findIconElements(context, child, seen);
130
+ }
131
+ if (child.type === "JSXExpressionContainer") {
132
+ return findIconElements(context, child.expression, seen);
133
+ }
134
+ return [];
135
+ });
136
+ }
137
+ case "JSXFragment": {
138
+ return expression.children.flatMap(child => {
139
+ if (child.type === "JSXElement" || child.type === "JSXFragment") {
140
+ return findIconElements(context, child, seen);
141
+ }
142
+ if (child.type === "JSXExpressionContainer") {
143
+ return findIconElements(context, child.expression, seen);
144
+ }
145
+ return [];
146
+ });
147
+ }
148
+ case "ConditionalExpression": {
149
+ // Check both consequent and alternate
150
+ return [
151
+ ...findIconElements(context, expression.consequent, seen),
152
+ ...findIconElements(context, expression.alternate, seen),
153
+ ];
154
+ }
155
+ case "LogicalExpression": {
156
+ return [
157
+ ...findIconElements(context, expression.left, seen),
158
+ ...findIconElements(context, expression.right, seen),
159
+ ];
160
+ }
161
+ case "ArrowFunctionExpression":
162
+ case "FunctionExpression": {
163
+ if (expression.body.type !== "BlockStatement") {
164
+ return findIconElements(context, expression.body, seen);
165
+ }
166
+ const returns = [];
167
+ for (const statement of expression.body.body) {
168
+ collectReturnStatements(statement, returns);
169
+ }
170
+ return returns.flatMap(statement => findIconElements(context, statement.argument ?? undefined, seen));
171
+ }
172
+ case "CallExpression": {
173
+ if (!isMemoizedCallbackCall(expression) ||
174
+ !expression.arguments[0] ||
175
+ expression.arguments[0].type === "SpreadElement") {
176
+ return [];
177
+ }
178
+ return findIconElements(context, expression.arguments[0], seen);
179
+ }
180
+ case "Identifier": {
181
+ const variable = context.sourceCode
182
+ .getScope(expression)
183
+ .references.find(ref => ref.identifier === expression)?.resolved;
184
+ if (!variable) {
185
+ return [];
186
+ }
187
+ return variable.defs.flatMap(definition => {
188
+ if (definition.type === "Variable" && definition.node.init) {
189
+ return findIconElements(context, definition.node.init, seen);
190
+ }
191
+ if (definition.type === "FunctionName" && definition.node.type === "FunctionDeclaration") {
192
+ const returns = [];
193
+ collectReturnStatements(definition.node.body, returns);
194
+ return returns.flatMap(statement => findIconElements(context, statement.argument ?? undefined, seen));
195
+ }
196
+ return [];
197
+ });
198
+ }
199
+ default: {
200
+ return [];
201
+ }
202
+ }
203
+ };
204
+ const reportWrongIconSize = ({ context, iconElement, expectedSize, hostDescription, iconSizeInfo, autofixTarget, }) => {
205
+ const iconSizeAttr = getJsxAttribute(iconElement, "size");
206
+ context.report({
207
+ node: iconElement,
208
+ messageId: "incorrectIconSize",
209
+ data: {
210
+ expectedSize,
211
+ hostDescription,
212
+ iconSize: iconSizeInfo.type === "implicit"
213
+ ? "medium (default)"
214
+ : iconSizeInfo.type === "literal"
215
+ ? iconSizeInfo.value
216
+ : iconSizeInfo.type === "variable"
217
+ ? `{${iconSizeInfo.value}}`
218
+ : "expression",
219
+ },
220
+ fix(fixer) {
221
+ if (autofixTarget === null) {
222
+ return null;
223
+ }
224
+ if (iconSizeAttr) {
225
+ // Replace existing size attribute
226
+ if (!iconSizeAttr.value) {
227
+ return null;
228
+ }
229
+ return fixer.replaceText(iconSizeAttr.value, autofixTarget);
230
+ }
231
+ // Add size attribute
232
+ const lastAttr = iconElement.attributes[iconElement.attributes.length - 1];
233
+ if (lastAttr) {
234
+ return fixer.insertTextAfter(lastAttr, ` size=${autofixTarget}`);
235
+ }
236
+ return fixer.insertTextAfter(iconElement.name, ` size=${autofixTarget}`);
237
+ },
238
+ });
239
+ };
240
+ // Rule: large buttons → medium icons, small/medium buttons → small icons
241
+ const getButtonExpectedIconSize = (buttonSize) => (buttonSize === "large" ? "medium" : "small"); // both "small" and "medium" buttons get "small" icons
242
+ const checkButtonIconSize = ({ context, iconElement, buttonSizeInfo, }) => {
243
+ const iconSizeAttr = getJsxAttribute(iconElement, "size");
244
+ const iconSizeInfo = getSizeInfo(iconSizeAttr);
245
+ const shouldReport = (() => {
246
+ // For complex expressions, always warn but don't autofix
247
+ if (buttonSizeInfo.type === "expression") {
248
+ return true;
249
+ }
250
+ // For variables, check if icon already has the correct size
251
+ if (buttonSizeInfo.type === "variable") {
252
+ const possibleSizes = getPossibleStringLiterals(context, buttonSizeInfo.expression);
253
+ if (possibleSizes) {
254
+ // Check if all possible button sizes map to the same icon size
255
+ const iconSizes = possibleSizes.map(getButtonExpectedIconSize);
256
+ const uniqueIconSizes = [...new Set(iconSizes)];
257
+ if (uniqueIconSizes.length === 1) {
258
+ // All map to same icon size - check if icon already has correct literal
259
+ const [variableExpectedIconSize] = uniqueIconSizes;
260
+ const variableIconEffectiveSize = iconSizeInfo.value || "medium";
261
+ if (variableExpectedIconSize && variableIconEffectiveSize === variableExpectedIconSize) {
262
+ return false; // Icon already has correct literal size
263
+ }
264
+ }
265
+ }
266
+ // Mixed mapping or can't determine type - check for the canonical conditional pattern
267
+ if (iconSizeInfo.type === "expression" &&
268
+ isCorrectConditionalExpression(iconSizeInfo.expression, buttonSizeInfo.value)) {
269
+ return false; // Icon already has correct conditional
270
+ }
271
+ // Mixed mapping or can't determine - always warn
272
+ return true;
273
+ }
274
+ // For literals and implicit, check the mapping
275
+ const buttonEffectiveSize = buttonSizeInfo.value || "medium";
276
+ const iconEffectiveSize = iconSizeInfo.value || "medium";
277
+ return iconEffectiveSize !== getButtonExpectedIconSize(buttonEffectiveSize);
278
+ })();
279
+ if (!shouldReport) {
280
+ return;
281
+ }
282
+ const expectedSize = (() => {
283
+ if (buttonSizeInfo.type === "variable") {
284
+ const possibleSizes = getPossibleStringLiterals(context, buttonSizeInfo.expression);
285
+ if (possibleSizes) {
286
+ const iconSizes = possibleSizes.map(getButtonExpectedIconSize);
287
+ const uniqueIconSizes = [...new Set(iconSizes)];
288
+ if (uniqueIconSizes.length === 1) {
289
+ const [mappedSize] = uniqueIconSizes;
290
+ if (mappedSize) {
291
+ return mappedSize;
292
+ }
293
+ }
294
+ }
295
+ return `{${buttonSizeInfo.value} === "large" ? "medium" : "small"}`;
296
+ }
297
+ if (buttonSizeInfo.type === "expression") {
298
+ return "small (for small/medium buttons) or medium (for large buttons)";
299
+ }
300
+ return getButtonExpectedIconSize(buttonSizeInfo.value || "medium");
301
+ })();
302
+ const hostDescription = (() => {
303
+ if (buttonSizeInfo.type === "implicit") {
304
+ return "Button size 'medium (default)'";
305
+ }
306
+ if (buttonSizeInfo.type === "literal") {
307
+ return `Button size '${buttonSizeInfo.value}'`;
308
+ }
309
+ if (buttonSizeInfo.type === "variable") {
310
+ const possibleSizes = getPossibleStringLiterals(context, buttonSizeInfo.expression);
311
+ if (possibleSizes && possibleSizes.length > 0) {
312
+ const sizeList = possibleSizes.map(size => `"${size}"`).join(" | ");
313
+ return `Button size '{${buttonSizeInfo.value}} (of type ${sizeList})'`;
314
+ }
315
+ return `Button size '{${buttonSizeInfo.value}}'`;
316
+ }
317
+ return "Button size 'expression'";
318
+ })();
319
+ const autofixTarget = (() => {
320
+ // Don't autofix complex expressions, only variables and literals
321
+ if (buttonSizeInfo.type === "expression") {
322
+ return null;
323
+ }
324
+ if (buttonSizeInfo.type === "variable") {
325
+ // Analyze the variable's type to determine optimal autofix
326
+ const possibleSizes = getPossibleStringLiterals(context, buttonSizeInfo.expression);
327
+ if (possibleSizes) {
328
+ // Check if all possible button sizes map to the same icon size
329
+ const iconSizes = possibleSizes.map(getButtonExpectedIconSize);
330
+ const uniqueIconSizes = [...new Set(iconSizes)];
331
+ if (uniqueIconSizes.length === 1) {
332
+ // All possible button sizes map to the same icon size
333
+ const [mappedSize] = uniqueIconSizes;
334
+ if (mappedSize) {
335
+ return `"${mappedSize}"`;
336
+ }
337
+ }
338
+ // Mixed mapping, use conditional
339
+ return `{${buttonSizeInfo.value} === "large" ? "medium" : "small"}`;
340
+ }
341
+ // Can't determine type, use conditional
342
+ return `{${buttonSizeInfo.value} === "large" ? "medium" : "small"}`;
343
+ }
344
+ // Calculate target icon size based on button size (literal or implicit)
345
+ return `"${getButtonExpectedIconSize(buttonSizeInfo.value || "medium")}"`;
346
+ })();
347
+ reportWrongIconSize({
348
+ context,
349
+ iconElement,
350
+ expectedSize,
351
+ hostDescription,
352
+ iconSizeInfo,
353
+ autofixTarget,
354
+ });
355
+ };
356
+ // Tag icon and getOptionPrefix always require size="small"
357
+ const checkFixedSlotIconSize = ({ context, slotValue, expectedSize, hostDescription, }) => {
358
+ if (!slotValue || slotValue.type !== "JSXExpressionContainer" || slotValue.expression.type === "JSXEmptyExpression") {
359
+ return;
360
+ }
361
+ for (const iconElement of findIconElements(context, slotValue.expression)) {
362
+ const iconSizeAttr = getJsxAttribute(iconElement, "size");
363
+ const iconSizeInfo = getSizeInfo(iconSizeAttr);
364
+ const iconEffectiveSize = iconSizeInfo.type === "literal" ? iconSizeInfo.value : iconSizeInfo.type === "implicit" ? "medium" : null;
365
+ if (iconEffectiveSize === expectedSize) {
366
+ continue;
367
+ }
368
+ reportWrongIconSize({
369
+ context,
370
+ iconElement,
371
+ expectedSize,
372
+ hostDescription,
373
+ iconSizeInfo,
374
+ autofixTarget: iconSizeInfo.type === "expression" || iconSizeInfo.type === "variable" ? null : `"${expectedSize}"`,
375
+ });
376
+ }
377
+ };
378
+ exports.designGuidelineIconSizeMatch = createRule({
379
+ name: "design-guideline-icon-size-match",
380
+ meta: {
381
+ type: "suggestion",
382
+ docs: {
383
+ description: "Ensure Icon components in Button (prefix, suffix, or children), Tag, and select option-prefix slots use the size required by the design system.",
384
+ },
385
+ fixable: "code",
386
+ schema: [],
387
+ messages: {
388
+ incorrectIconSize: "Expected Icon size to be '{{expectedSize}}' for {{hostDescription}}, found '{{iconSize}}'.",
389
+ },
390
+ },
391
+ defaultOptions: [],
392
+ create(context) {
393
+ return {
394
+ JSXOpeningElement(node) {
395
+ if (node.name.type !== "JSXIdentifier") {
396
+ return;
397
+ }
398
+ const hostName = node.name.name;
399
+ if (hostName === "Tag") {
400
+ // Tag icon slot must always be small
401
+ checkFixedSlotIconSize({
402
+ context,
403
+ slotValue: getJsxAttribute(node, "icon")?.value,
404
+ expectedSize: "small",
405
+ hostDescription: "Tag icon",
406
+ });
407
+ return;
408
+ }
409
+ const optionPrefixAttr = getJsxAttribute(node, "getOptionPrefix");
410
+ if (optionPrefixAttr) {
411
+ // getOptionPrefix authors icons for both tags and menu items
412
+ checkFixedSlotIconSize({
413
+ context,
414
+ slotValue: optionPrefixAttr.value,
415
+ expectedSize: "small",
416
+ hostDescription: "getOptionPrefix",
417
+ });
418
+ }
419
+ if (hostName !== "Button") {
420
+ return;
421
+ }
422
+ const sizeAttr = getJsxAttribute(node, "size");
423
+ const buttonSizeInfo = getSizeInfo(sizeAttr);
424
+ const checkButtonIconsInExpression = (expression) => {
425
+ // Skip empty JSX expressions like {}
426
+ if (expression.type === "JSXEmptyExpression") {
427
+ return;
428
+ }
429
+ for (const iconElement of findIconElements(context, expression)) {
430
+ checkButtonIconSize({ context, iconElement, buttonSizeInfo });
431
+ }
432
+ };
433
+ // Check prefix and suffix slots
434
+ for (const attr of node.attributes) {
435
+ if (attr.type !== "JSXAttribute" ||
436
+ attr.name.type !== "JSXIdentifier" ||
437
+ (attr.name.name !== "prefix" && attr.name.name !== "suffix") ||
438
+ attr.value?.type !== "JSXExpressionContainer") {
439
+ continue;
440
+ }
441
+ checkButtonIconsInExpression(attr.value.expression);
442
+ }
443
+ // Check Icon children rendered inside the button body
444
+ for (const child of node.parent.children) {
445
+ if (child.type === "JSXElement" || child.type === "JSXFragment") {
446
+ for (const iconElement of findIconElements(context, child)) {
447
+ checkButtonIconSize({ context, iconElement, buttonSizeInfo });
448
+ }
449
+ }
450
+ else if (child.type === "JSXExpressionContainer") {
451
+ checkButtonIconsInExpression(child.expression);
452
+ }
453
+ }
454
+ },
455
+ };
456
+ },
457
+ });
458
+ //# sourceMappingURL=design-guideline-icon-size-match.js.map
@@ -31,6 +31,9 @@ export declare const rulesMap: {
31
31
  "no-internal-graphql-when-tagged-with-gql-public": ESLintUtils.RuleModule<"internalGraphqlFileFound", [], unknown, ESLintUtils.RuleListener> & {
32
32
  name: string;
33
33
  };
34
+ "design-guideline-icon-size-match": ESLintUtils.RuleModule<"incorrectIconSize", [], unknown, ESLintUtils.RuleListener> & {
35
+ name: string;
36
+ };
34
37
  "design-guideline-button-icon-size-match": ESLintUtils.RuleModule<"incorrectIconSize", [], unknown, ESLintUtils.RuleListener> & {
35
38
  name: string;
36
39
  };
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.rulesMap = void 0;
4
4
  const component_name_matches_filename_1 = require("./rules/component-name-matches-filename/component-name-matches-filename");
5
5
  const cva_merge_base_classes_as_array_1 = require("./rules/cva-merge-base-classes-as-array/cva-merge-base-classes-as-array");
6
- const design_guideline_button_icon_size_match_1 = require("./rules/design-guideline-button-icon-size-match/design-guideline-button-icon-size-match");
6
+ const design_guideline_icon_size_match_1 = require("./rules/design-guideline-icon-size-match/design-guideline-icon-size-match");
7
7
  const no_deprecated_graphql_fields_1 = require("./rules/no-deprecated-graphql-fields/no-deprecated-graphql-fields");
8
8
  const no_dynamic_translation_key_1 = require("./rules/no-dynamic-translation-key/no-dynamic-translation-key");
9
9
  const no_internal_barrel_files_1 = require("./rules/no-internal-barrel-files/no-internal-barrel-files");
@@ -31,7 +31,8 @@ exports.rulesMap = {
31
31
  "no-deprecated-graphql-fields": no_deprecated_graphql_fields_1.noDeprecatedGraphqlFields,
32
32
  "no-dynamic-translation-key": no_dynamic_translation_key_1.noDynamicTranslationKey,
33
33
  "no-internal-graphql-when-tagged-with-gql-public": no_internal_graphql_when_tagged_with_gql_public_1.noInternalGraphqlWhenTaggedWithGqlPublic,
34
- "design-guideline-button-icon-size-match": design_guideline_button_icon_size_match_1.designGuidelineButtonIconSizeMatch,
34
+ "design-guideline-icon-size-match": design_guideline_icon_size_match_1.designGuidelineIconSizeMatch,
35
+ "design-guideline-button-icon-size-match": design_guideline_icon_size_match_1.designGuidelineIconSizeMatch,
35
36
  "no-template-strings-in-classname-prop": no_template_strings_in_classname_prop_1.noTemplateStringsInClassName,
36
37
  "no-unused-translation-key": no_unused_translation_key_1.noUnusedTranslationKey,
37
38
  "require-classname-alternatives": require_classname_alternatives_1.requireClassnameAlternatives,
@@ -1,4 +0,0 @@
1
- import { ESLintUtils } from "@typescript-eslint/utils";
2
- export declare const designGuidelineButtonIconSizeMatch: ESLintUtils.RuleModule<"incorrectIconSize", [], unknown, ESLintUtils.RuleListener> & {
3
- name: string;
4
- };
@@ -1,297 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.designGuidelineButtonIconSizeMatch = void 0;
4
- const tslib_1 = require("tslib");
5
- const utils_1 = require("@typescript-eslint/utils");
6
- const ts = tslib_1.__importStar(require("typescript"));
7
- const createRule = utils_1.ESLintUtils.RuleCreator(name => `https://github.com/trackunit/manager/blob/main/libs/eslint/plugin-trackunit/docs/rules/${name}.md`);
8
- // Helper to get possible string literal values from a variable's type
9
- function getPossibleStringLiterals(context, identifier) {
10
- try {
11
- const services = utils_1.ESLintUtils.getParserServices(context);
12
- const checker = services.program.getTypeChecker();
13
- const tsNode = services.esTreeNodeToTSNodeMap.get(identifier);
14
- const type = checker.getTypeAtLocation(tsNode);
15
- const literalValues = [];
16
- // Type guard for string literal types
17
- const isStringLiteralType = (t) => {
18
- if ((t.flags & ts.TypeFlags.StringLiteral) === 0) {
19
- return false;
20
- }
21
- // Check if the type has a value property and it's a string
22
- // eslint-disable-next-line @trackunit/no-typescript-assertion
23
- const typeWithValue = t;
24
- return "value" in typeWithValue && typeof typeWithValue.value === "string";
25
- };
26
- if (type.isUnion()) {
27
- for (const unionType of type.types) {
28
- if (isStringLiteralType(unionType)) {
29
- literalValues.push(unionType.value);
30
- }
31
- }
32
- }
33
- else if (isStringLiteralType(type)) {
34
- literalValues.push(type.value);
35
- }
36
- return literalValues.length > 0 ? literalValues : null;
37
- }
38
- catch {
39
- return null;
40
- }
41
- }
42
- // Helper to check if a conditional expression matches the expected button-icon size pattern
43
- function isCorrectConditionalExpression(expression, buttonVariableName) {
44
- if (expression.type !== "ConditionalExpression") {
45
- return false;
46
- }
47
- const { test, consequent, alternate } = expression;
48
- // Check if test is: variable === "large"
49
- if (test.type === "BinaryExpression" &&
50
- test.operator === "===" &&
51
- test.left.type === "Identifier" &&
52
- test.left.name === buttonVariableName &&
53
- test.right.type === "Literal" &&
54
- test.right.value === "large") {
55
- // Check if consequent is "medium" and alternate is "small"
56
- return (consequent.type === "Literal" &&
57
- consequent.value === "medium" &&
58
- alternate.type === "Literal" &&
59
- alternate.value === "small");
60
- }
61
- return false;
62
- }
63
- exports.designGuidelineButtonIconSizeMatch = createRule({
64
- name: "design-guideline-button-icon-size-match",
65
- meta: {
66
- type: "suggestion",
67
- docs: {
68
- description: "Ensure Icon components within Button have the correct size prop based on the Button's size.",
69
- },
70
- fixable: "code",
71
- schema: [],
72
- messages: {
73
- incorrectIconSize: "Expected Icon size to be '{{expectedSize}}' for Button size '{{buttonSize}}', found '{{iconSize}}'.",
74
- },
75
- },
76
- defaultOptions: [],
77
- create(context) {
78
- return {
79
- JSXOpeningElement(node) {
80
- if (node.name.type === "JSXIdentifier" && node.name.name === "Button") {
81
- const sizeAttr = node.attributes.find((attr) => attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name === "size");
82
- const buttonSizeInfo = (() => {
83
- if (!sizeAttr?.value)
84
- return { type: "implicit", value: null };
85
- if (sizeAttr.value.type === "Literal" && typeof sizeAttr.value.value === "string") {
86
- return { type: "literal", value: sizeAttr.value.value };
87
- }
88
- if (sizeAttr.value.type === "JSXExpressionContainer") {
89
- const expr = sizeAttr.value.expression;
90
- if (expr.type === "Identifier") {
91
- return { type: "variable", value: expr.name, expression: expr };
92
- }
93
- return { type: "expression", value: null, expression: expr };
94
- }
95
- return { type: "implicit", value: null };
96
- })();
97
- node.attributes.forEach(attr => {
98
- if (attr.type === "JSXAttribute" &&
99
- attr.name.type === "JSXIdentifier" &&
100
- (attr.name.name === "prefix" || attr.name.name === "suffix") &&
101
- attr.value?.type === "JSXExpressionContainer") {
102
- // Find Icon element - could be direct or inside conditional
103
- const findIconElement = (expression) => {
104
- if (expression.type === "JSXElement" &&
105
- expression.openingElement.name.type === "JSXIdentifier" &&
106
- expression.openingElement.name.name === "Icon") {
107
- return expression.openingElement;
108
- }
109
- if (expression.type === "ConditionalExpression") {
110
- // Check both consequent and alternate
111
- const consequent = findIconElement(expression.consequent);
112
- if (consequent)
113
- return consequent;
114
- const alternate = findIconElement(expression.alternate);
115
- if (alternate)
116
- return alternate;
117
- }
118
- return null;
119
- };
120
- // Skip empty JSX expressions like {}
121
- if (attr.value.expression.type === "JSXEmptyExpression")
122
- return;
123
- const iconElement = findIconElement(attr.value.expression);
124
- if (!iconElement)
125
- return;
126
- const iconSizeAttr = iconElement.attributes.find((iconAttr) => iconAttr.type === "JSXAttribute" &&
127
- iconAttr.name.type === "JSXIdentifier" &&
128
- iconAttr.name.name === "size");
129
- const iconSizeInfo = (() => {
130
- if (!iconSizeAttr?.value)
131
- return { type: "implicit", value: null };
132
- if (iconSizeAttr.value.type === "Literal" && typeof iconSizeAttr.value.value === "string") {
133
- return { type: "literal", value: iconSizeAttr.value.value };
134
- }
135
- if (iconSizeAttr.value.type === "JSXExpressionContainer") {
136
- const expr = iconSizeAttr.value.expression;
137
- if (expr.type === "Identifier") {
138
- return { type: "variable", value: expr.name, expression: expr };
139
- }
140
- return { type: "expression", value: null, expression: expr };
141
- }
142
- return { type: "implicit", value: null };
143
- })();
144
- // Rule: large buttons → medium icons, small/medium buttons → small icons
145
- const getExpectedIconSize = (buttonSize) => {
146
- if (buttonSize === "large")
147
- return "medium";
148
- return "small"; // both "small" and "medium" buttons get "small" icons
149
- };
150
- const shouldReport = (() => {
151
- // For complex expressions, always warn but don't autofix
152
- if (buttonSizeInfo.type === "expression") {
153
- return true;
154
- }
155
- // For variables, check if icon already has the correct size
156
- if (buttonSizeInfo.type === "variable" && buttonSizeInfo.expression?.type === "Identifier") {
157
- const possibleSizes = getPossibleStringLiterals(context, buttonSizeInfo.expression);
158
- if (possibleSizes) {
159
- // Check if all possible button sizes map to the same icon size
160
- const iconSizes = possibleSizes.map(getExpectedIconSize);
161
- const uniqueIconSizes = [...new Set(iconSizes)];
162
- if (uniqueIconSizes.length === 1) {
163
- // All map to same icon size - check if icon already has correct literal
164
- const variableExpectedIconSize = uniqueIconSizes[0];
165
- const variableIconEffectiveSize = iconSizeInfo.value || "medium";
166
- if (variableIconEffectiveSize === variableExpectedIconSize) {
167
- return false; // Icon already has correct literal size
168
- }
169
- }
170
- else {
171
- // Mixed mapping - check if icon has correct conditional expression
172
- if (iconSizeInfo.type === "expression" &&
173
- iconSizeInfo.expression &&
174
- iconSizeInfo.expression.type !== "JSXEmptyExpression" &&
175
- buttonSizeInfo.value &&
176
- isCorrectConditionalExpression(iconSizeInfo.expression, buttonSizeInfo.value)) {
177
- return false; // Icon already has correct conditional
178
- }
179
- }
180
- }
181
- // Mixed mapping or can't determine - always warn
182
- return true;
183
- }
184
- // For literals and implicit, check the mapping
185
- const buttonEffectiveSize = buttonSizeInfo.value || "medium";
186
- const iconEffectiveSize = iconSizeInfo.value || "medium";
187
- const expectedIconSize = getExpectedIconSize(buttonEffectiveSize);
188
- return iconEffectiveSize !== expectedIconSize;
189
- })();
190
- if (shouldReport) {
191
- context.report({
192
- node: iconElement,
193
- messageId: "incorrectIconSize",
194
- data: {
195
- expectedSize: (() => {
196
- if (buttonSizeInfo.type === "variable" && buttonSizeInfo.expression?.type === "Identifier") {
197
- // Analyze the variable's type to determine expected message
198
- const possibleSizes = getPossibleStringLiterals(context, buttonSizeInfo.expression);
199
- if (possibleSizes) {
200
- const iconSizes = possibleSizes.map(getExpectedIconSize);
201
- const uniqueIconSizes = [...new Set(iconSizes)];
202
- if (uniqueIconSizes.length === 1) {
203
- return uniqueIconSizes[0];
204
- }
205
- }
206
- return `{${buttonSizeInfo.value} === "large" ? "medium" : "small"}`;
207
- }
208
- if (buttonSizeInfo.type === "expression") {
209
- return "small (for small/medium buttons) or medium (for large buttons)";
210
- }
211
- const buttonEffectiveSize = buttonSizeInfo.value || "medium";
212
- return getExpectedIconSize(buttonEffectiveSize);
213
- })(),
214
- buttonSize: buttonSizeInfo.type === "implicit"
215
- ? "medium (default)"
216
- : buttonSizeInfo.type === "literal"
217
- ? buttonSizeInfo.value
218
- : buttonSizeInfo.type === "variable"
219
- ? (() => {
220
- if (buttonSizeInfo.expression?.type === "Identifier") {
221
- const possibleSizes = getPossibleStringLiterals(context, buttonSizeInfo.expression);
222
- if (possibleSizes && possibleSizes.length > 0) {
223
- const sizeList = possibleSizes.map(s => `"${s}"`).join(" | ");
224
- return `{${buttonSizeInfo.value}} (of type ${sizeList})`;
225
- }
226
- }
227
- return `{${buttonSizeInfo.value}}`;
228
- })()
229
- : "expression",
230
- iconSize: iconSizeInfo.type === "implicit"
231
- ? "medium (default)"
232
- : iconSizeInfo.type === "literal"
233
- ? iconSizeInfo.value
234
- : iconSizeInfo.type === "variable"
235
- ? `{${iconSizeInfo.value}}`
236
- : "expression",
237
- },
238
- fix(fixer) {
239
- // Don't autofix complex expressions, only variables and literals
240
- if (buttonSizeInfo.type === "expression") {
241
- return null;
242
- }
243
- let targetSizeValue;
244
- if (buttonSizeInfo.type === "variable" && buttonSizeInfo.expression?.type === "Identifier") {
245
- // Analyze the variable's type to determine optimal autofix
246
- const possibleSizes = getPossibleStringLiterals(context, buttonSizeInfo.expression);
247
- if (possibleSizes) {
248
- // Check if all possible button sizes map to the same icon size
249
- const iconSizes = possibleSizes.map(getExpectedIconSize);
250
- const uniqueIconSizes = [...new Set(iconSizes)];
251
- if (uniqueIconSizes.length === 1) {
252
- // All possible button sizes map to the same icon size
253
- targetSizeValue = `"${uniqueIconSizes[0]}"`;
254
- }
255
- else {
256
- // Mixed mapping, use conditional
257
- targetSizeValue = `{${buttonSizeInfo.value} === "large" ? "medium" : "small"}`;
258
- }
259
- }
260
- else {
261
- // Can't determine type, use conditional
262
- targetSizeValue = `{${buttonSizeInfo.value} === "large" ? "medium" : "small"}`;
263
- }
264
- }
265
- else {
266
- // Calculate target icon size based on button size (literal or implicit)
267
- const buttonEffectiveSize = buttonSizeInfo.value || "medium";
268
- const targetIconSize = getExpectedIconSize(buttonEffectiveSize);
269
- targetSizeValue = `"${targetIconSize}"`;
270
- }
271
- if (iconSizeAttr) {
272
- // Replace existing size attribute
273
- if (!iconSizeAttr.value)
274
- return null;
275
- return fixer.replaceText(iconSizeAttr.value, targetSizeValue);
276
- }
277
- else {
278
- // Add size attribute
279
- const lastAttr = iconElement.attributes[iconElement.attributes.length - 1];
280
- if (lastAttr) {
281
- return fixer.insertTextAfter(lastAttr, ` size=${targetSizeValue}`);
282
- }
283
- else {
284
- return fixer.insertTextAfter(iconElement.name, ` size=${targetSizeValue}`);
285
- }
286
- }
287
- },
288
- });
289
- }
290
- }
291
- });
292
- }
293
- },
294
- };
295
- },
296
- });
297
- //# sourceMappingURL=design-guideline-button-icon-size-match.js.map