@salesforce-ux/eslint-plugin-slds 0.5.2 → 1.0.0-internal-alpha.0

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 (114) hide show
  1. package/build/index.js +2309 -50
  2. package/build/index.js.map +4 -4
  3. package/build/rules/enforce-bem-usage.js +325 -85
  4. package/build/rules/enforce-bem-usage.js.map +4 -4
  5. package/build/rules/modal-close-button-issue.js +324 -179
  6. package/build/rules/modal-close-button-issue.js.map +4 -4
  7. package/build/rules/no-deprecated-classes-slds2.js +247 -53
  8. package/build/rules/no-deprecated-classes-slds2.js.map +4 -4
  9. package/build/rules/v9/enforce-bem-usage.js +236 -0
  10. package/build/rules/v9/enforce-bem-usage.js.map +7 -0
  11. package/build/rules/v9/enforce-component-hook-naming-convention.js +262 -0
  12. package/build/rules/v9/enforce-component-hook-naming-convention.js.map +7 -0
  13. package/build/rules/v9/enforce-sds-to-slds-hooks.js +260 -0
  14. package/build/rules/v9/enforce-sds-to-slds-hooks.js.map +7 -0
  15. package/build/rules/v9/lwc-token-to-slds-hook.js +330 -0
  16. package/build/rules/v9/lwc-token-to-slds-hook.js.map +7 -0
  17. package/build/rules/v9/no-deprecated-slds-classes.js +227 -0
  18. package/build/rules/v9/no-deprecated-slds-classes.js.map +7 -0
  19. package/build/rules/v9/no-deprecated-tokens-slds1.js +270 -0
  20. package/build/rules/v9/no-deprecated-tokens-slds1.js.map +7 -0
  21. package/build/rules/v9/no-hardcoded-values/handlers/boxShadowHandler.js +339 -0
  22. package/build/rules/v9/no-hardcoded-values/handlers/boxShadowHandler.js.map +7 -0
  23. package/build/rules/v9/no-hardcoded-values/handlers/colorHandler.js +348 -0
  24. package/build/rules/v9/no-hardcoded-values/handlers/colorHandler.js.map +7 -0
  25. package/build/rules/v9/no-hardcoded-values/handlers/densityHandler.js +344 -0
  26. package/build/rules/v9/no-hardcoded-values/handlers/densityHandler.js.map +7 -0
  27. package/build/rules/v9/no-hardcoded-values/handlers/fontHandler.js +388 -0
  28. package/build/rules/v9/no-hardcoded-values/handlers/fontHandler.js.map +7 -0
  29. package/build/rules/v9/no-hardcoded-values/handlers/index.js +834 -0
  30. package/build/rules/v9/no-hardcoded-values/handlers/index.js.map +7 -0
  31. package/build/rules/v9/no-hardcoded-values/no-hardcoded-values-slds1.js +1112 -0
  32. package/build/rules/v9/no-hardcoded-values/no-hardcoded-values-slds1.js.map +7 -0
  33. package/build/rules/v9/no-hardcoded-values/no-hardcoded-values-slds2.js +1112 -0
  34. package/build/rules/v9/no-hardcoded-values/no-hardcoded-values-slds2.js.map +7 -0
  35. package/build/rules/v9/no-hardcoded-values/noHardcodedValueRule.js +945 -0
  36. package/build/rules/v9/no-hardcoded-values/noHardcodedValueRule.js.map +7 -0
  37. package/build/rules/v9/no-slds-class-overrides.js +233 -0
  38. package/build/rules/v9/no-slds-class-overrides.js.map +7 -0
  39. package/build/rules/v9/no-slds-namespace-for-custom-hooks.js +250 -0
  40. package/build/rules/v9/no-slds-namespace-for-custom-hooks.js.map +7 -0
  41. package/build/rules/v9/no-slds-private-var.js +230 -0
  42. package/build/rules/v9/no-slds-private-var.js.map +7 -0
  43. package/build/rules/v9/no-slds-var-without-fallback.js +253 -0
  44. package/build/rules/v9/no-slds-var-without-fallback.js.map +7 -0
  45. package/build/rules/v9/no-sldshook-fallback-for-lwctoken.js +253 -0
  46. package/build/rules/v9/no-sldshook-fallback-for-lwctoken.js.map +7 -0
  47. package/build/rules/v9/no-unsupported-hooks-slds2.js +243 -0
  48. package/build/rules/v9/no-unsupported-hooks-slds2.js.map +7 -0
  49. package/build/rules/v9/reduce-annotations.js +234 -0
  50. package/build/rules/v9/reduce-annotations.js.map +7 -0
  51. package/build/src/rules/enforce-bem-usage.d.ts +17 -0
  52. package/build/{rules → src/rules}/modal-close-button-issue.d.ts +2 -1
  53. package/build/{rules → src/rules}/no-deprecated-classes-slds2.d.ts +2 -1
  54. package/build/src/rules/v9/enforce-bem-usage.d.ts +3 -0
  55. package/build/src/rules/v9/enforce-component-hook-naming-convention.d.ts +3 -0
  56. package/build/src/rules/v9/enforce-sds-to-slds-hooks.d.ts +3 -0
  57. package/build/src/rules/v9/lwc-token-to-slds-hook.d.ts +3 -0
  58. package/build/src/rules/v9/no-deprecated-slds-classes.d.ts +3 -0
  59. package/build/src/rules/v9/no-deprecated-tokens-slds1.d.ts +3 -0
  60. package/build/src/rules/v9/no-hardcoded-values/handlers/boxShadowHandler.d.ts +5 -0
  61. package/build/src/rules/v9/no-hardcoded-values/handlers/colorHandler.d.ts +7 -0
  62. package/build/src/rules/v9/no-hardcoded-values/handlers/densityHandler.d.ts +7 -0
  63. package/build/src/rules/v9/no-hardcoded-values/handlers/fontHandler.d.ts +6 -0
  64. package/build/src/rules/v9/no-hardcoded-values/handlers/index.d.ts +8 -0
  65. package/build/src/rules/v9/no-hardcoded-values/no-hardcoded-values-slds1.d.ts +3 -0
  66. package/build/src/rules/v9/no-hardcoded-values/no-hardcoded-values-slds2.d.ts +3 -0
  67. package/build/src/rules/v9/no-hardcoded-values/noHardcodedValueRule.d.ts +9 -0
  68. package/build/src/rules/v9/no-slds-class-overrides.d.ts +8 -0
  69. package/build/src/rules/v9/no-slds-namespace-for-custom-hooks.d.ts +3 -0
  70. package/build/src/rules/v9/no-slds-private-var.d.ts +3 -0
  71. package/build/src/rules/v9/no-slds-var-without-fallback.d.ts +3 -0
  72. package/build/src/rules/v9/no-sldshook-fallback-for-lwctoken.d.ts +3 -0
  73. package/build/src/rules/v9/no-unsupported-hooks-slds2.d.ts +3 -0
  74. package/build/src/rules/v9/reduce-annotations.d.ts +3 -0
  75. package/build/src/types/index.d.ts +26 -0
  76. package/build/src/utils/boxShadowValueParser.d.ts +17 -0
  77. package/build/src/utils/color-lib-utils.d.ts +18 -0
  78. package/build/src/utils/css-functions.d.ts +7 -0
  79. package/build/src/utils/css-utils.d.ts +7 -0
  80. package/build/src/utils/hardcoded-shared-utils.d.ts +60 -0
  81. package/build/src/utils/property-matcher.d.ts +23 -0
  82. package/build/src/utils/styling-hook-utils.d.ts +7 -0
  83. package/build/src/utils/value-utils.d.ts +40 -0
  84. package/build/types/index.js +18 -0
  85. package/build/types/index.js.map +7 -0
  86. package/build/types/yaml.d.js +1 -0
  87. package/build/types/yaml.d.js.map +7 -0
  88. package/build/utils/boxShadowValueParser.js +197 -0
  89. package/build/utils/boxShadowValueParser.js.map +7 -0
  90. package/build/utils/color-lib-utils.js +160 -0
  91. package/build/utils/color-lib-utils.js.map +7 -0
  92. package/build/utils/css-functions.js +68 -0
  93. package/build/utils/css-functions.js.map +7 -0
  94. package/build/utils/css-utils.js +33 -0
  95. package/build/utils/css-utils.js.map +7 -0
  96. package/build/utils/hardcoded-shared-utils.js +300 -0
  97. package/build/utils/hardcoded-shared-utils.js.map +7 -0
  98. package/build/utils/node.js +158 -0
  99. package/build/{rules/utils → utils}/node.js.map +2 -2
  100. package/build/utils/property-matcher.js +159 -0
  101. package/build/utils/property-matcher.js.map +7 -0
  102. package/build/utils/styling-hook-utils.js +80 -0
  103. package/build/utils/styling-hook-utils.js.map +7 -0
  104. package/build/utils/value-utils.js +65 -0
  105. package/build/utils/value-utils.js.map +7 -0
  106. package/package.json +9 -4
  107. package/src/config/rule-messages.yml +143 -0
  108. package/build/rules/enforce-bem-usage.d.ts +0 -28
  109. package/build/rules/utils/node.js +0 -197
  110. package/build/rules/utils/rule.d.ts +0 -7
  111. package/build/rules/utils/rule.js +0 -10
  112. package/build/rules/utils/rule.js.map +0 -7
  113. /package/build/{index.d.ts → src/index.d.ts} +0 -0
  114. /package/build/{rules → src}/utils/node.d.ts +0 -0
@@ -0,0 +1,834 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/rules/v9/no-hardcoded-values/handlers/index.ts
30
+ var handlers_exports = {};
31
+ __export(handlers_exports, {
32
+ handleBoxShadowDeclaration: () => handleBoxShadowDeclaration,
33
+ handleColorDeclaration: () => handleColorDeclaration,
34
+ handleDensityDeclaration: () => handleDensityDeclaration,
35
+ handleFontDeclaration: () => handleFontDeclaration
36
+ });
37
+ module.exports = __toCommonJS(handlers_exports);
38
+
39
+ // src/utils/color-lib-utils.ts
40
+ var import_chroma_js = __toESM(require("chroma-js"));
41
+ var import_css_tree = require("@eslint/css-tree");
42
+
43
+ // src/utils/css-functions.ts
44
+ var CSS_FUNCTIONS = [
45
+ "attr",
46
+ "calc",
47
+ "color-mix",
48
+ "conic-gradient",
49
+ "counter",
50
+ "cubic-bezier",
51
+ "linear-gradient",
52
+ "max",
53
+ "min",
54
+ "radial-gradient",
55
+ "repeating-conic-gradient",
56
+ "repeating-linear-gradient",
57
+ "repeating-radial-gradient",
58
+ "var"
59
+ ];
60
+ var CSS_MATH_FUNCTIONS = ["calc", "min", "max"];
61
+ var RGB_COLOR_FUNCTIONS = ["rgb", "rgba", "hsl", "hsla"];
62
+ var cssFunctionsRegex = new RegExp(`(?:${CSS_FUNCTIONS.join("|")})`);
63
+ var cssFunctionsExactRegex = new RegExp(`^(?:${CSS_FUNCTIONS.join("|")})$`);
64
+ var cssMathFunctionsRegex = new RegExp(`^(?:${CSS_MATH_FUNCTIONS.join("|")})$`);
65
+ function isCssFunction(value) {
66
+ return cssFunctionsExactRegex.test(value);
67
+ }
68
+ function isCssColorFunction(value) {
69
+ return RGB_COLOR_FUNCTIONS.includes(value);
70
+ }
71
+
72
+ // src/utils/color-lib-utils.ts
73
+ var LAB_THRESHOLD = 25;
74
+ var isHexCode = (color) => {
75
+ const hexPattern = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
76
+ return hexPattern.test(color);
77
+ };
78
+ var convertToHex = (color) => {
79
+ try {
80
+ return (0, import_chroma_js.default)(color).hex();
81
+ } catch (e) {
82
+ return null;
83
+ }
84
+ };
85
+ var findClosestColorHook = (color, supportedColors, cssProperty) => {
86
+ const returnStylingHooks = [];
87
+ const closestHooksWithSameProperty = [];
88
+ const closestHooksWithoutSameProperty = [];
89
+ const closestHooksWithAllProperty = [];
90
+ const labColor = (0, import_chroma_js.default)(color).lab();
91
+ Object.entries(supportedColors).forEach(([sldsValue, data]) => {
92
+ if (sldsValue && isHexCode(sldsValue)) {
93
+ const hooks = data;
94
+ hooks.forEach((hook) => {
95
+ const labSupportedColor = (0, import_chroma_js.default)(sldsValue).lab();
96
+ const distance = JSON.stringify(labColor) === JSON.stringify(labSupportedColor) ? 0 : import_chroma_js.default.distance(import_chroma_js.default.lab(...labColor), import_chroma_js.default.lab(...labSupportedColor), "lab");
97
+ if (hook.properties.includes(cssProperty)) {
98
+ if (distance <= LAB_THRESHOLD) {
99
+ closestHooksWithSameProperty.push({ name: hook.name, distance });
100
+ }
101
+ } else if (hook.properties.includes("*")) {
102
+ if (distance <= LAB_THRESHOLD) {
103
+ closestHooksWithAllProperty.push({ name: hook.name, distance });
104
+ }
105
+ } else {
106
+ if (distance <= LAB_THRESHOLD) {
107
+ closestHooksWithoutSameProperty.push({ name: hook.name, distance });
108
+ }
109
+ }
110
+ });
111
+ }
112
+ });
113
+ const closesthookGroups = [
114
+ { hooks: closestHooksWithSameProperty, distance: 0 },
115
+ { hooks: closestHooksWithAllProperty, distance: 0 },
116
+ { hooks: closestHooksWithSameProperty, distance: Infinity },
117
+ // For hooks with distance > 0
118
+ { hooks: closestHooksWithAllProperty, distance: Infinity },
119
+ { hooks: closestHooksWithoutSameProperty, distance: Infinity }
120
+ ];
121
+ for (const group of closesthookGroups) {
122
+ const filteredHooks = group.hooks.filter(
123
+ (h) => group.distance === 0 ? h.distance === 0 : h.distance > 0
124
+ );
125
+ if (returnStylingHooks.length < 1 && filteredHooks.length > 0) {
126
+ filteredHooks.sort((a, b) => a.distance - b.distance);
127
+ returnStylingHooks.push(...filteredHooks.slice(0, 5).map((h) => h.name));
128
+ }
129
+ }
130
+ return Array.from(new Set(returnStylingHooks));
131
+ };
132
+ var isValidColor = (val) => import_chroma_js.default.valid(val);
133
+ var extractColorValue = (node) => {
134
+ let colorValue = null;
135
+ switch (node.type) {
136
+ case "Hash":
137
+ colorValue = `#${node.value}`;
138
+ break;
139
+ case "Identifier":
140
+ colorValue = node.name;
141
+ break;
142
+ case "Function":
143
+ if (isCssColorFunction(node.name)) {
144
+ colorValue = (0, import_css_tree.generate)(node);
145
+ }
146
+ break;
147
+ }
148
+ return colorValue && isValidColor(colorValue) ? colorValue : null;
149
+ };
150
+
151
+ // src/utils/property-matcher.ts
152
+ var DIRECTION_VALUES = "(?:top|right|bottom|left|inline|block|inline-start|inline-end|start|end|block-start|block-end)";
153
+ var CORNER_VALUES = "(?:top-left|top-right|bottom-right|bottom-left|start-start|start-end|end-start|end-end)";
154
+ var INSET_VALUES = "(?:inline|block|inline-start|inline-end|block-start|block-end)";
155
+ var BORDER_COLOR_REGEX = new RegExp(`^border(?:-${DIRECTION_VALUES})?-color$`);
156
+ var BORDER_WIDTH_REGEX = new RegExp(`^border(?:-${DIRECTION_VALUES})?-width$`);
157
+ var MARGIN_REGEX = new RegExp(`^margin(?:-${DIRECTION_VALUES})?$`);
158
+ var PADDING_REGEX = new RegExp(`^padding(?:-${DIRECTION_VALUES})?$`);
159
+ var BORDER_RADIUS_REGEX = new RegExp(`^border(?:-${CORNER_VALUES})?-radius$`);
160
+ var INSET_REGEX = new RegExp(`^inset(?:-${INSET_VALUES})?$`);
161
+ function isBorderColorProperty(cssProperty) {
162
+ return BORDER_COLOR_REGEX.test(cssProperty);
163
+ }
164
+ function isBorderWidthProperty(cssProperty) {
165
+ return BORDER_WIDTH_REGEX.test(cssProperty);
166
+ }
167
+ function isMarginProperty(cssProperty) {
168
+ return MARGIN_REGEX.test(cssProperty);
169
+ }
170
+ function isPaddingProperty(cssProperty) {
171
+ return PADDING_REGEX.test(cssProperty);
172
+ }
173
+ function isBorderRadius(cssProperty) {
174
+ return BORDER_RADIUS_REGEX.test(cssProperty);
175
+ }
176
+ function isDimensionProperty(cssProperty) {
177
+ return ["width", "height", "min-width", "max-width", "min-height", "max-height"].includes(cssProperty);
178
+ }
179
+ function isInsetProperty(cssProperty) {
180
+ return INSET_REGEX.test(cssProperty);
181
+ }
182
+ function resolvePropertyToMatch(cssProperty) {
183
+ const propertyToMatch = cssProperty.toLowerCase();
184
+ if (propertyToMatch === "outline" || propertyToMatch === "outline-width" || isBorderWidthProperty(propertyToMatch)) {
185
+ return "border-width";
186
+ } else if (isMarginProperty(propertyToMatch)) {
187
+ return "margin";
188
+ } else if (isPaddingProperty(propertyToMatch)) {
189
+ return "padding";
190
+ } else if (isBorderRadius(propertyToMatch)) {
191
+ return "border-radius";
192
+ } else if (isDimensionProperty(propertyToMatch)) {
193
+ return "width";
194
+ } else if (isInsetProperty(propertyToMatch)) {
195
+ return "top";
196
+ } else if (cssProperty === "background" || cssProperty === "background-color") {
197
+ return "background-color";
198
+ } else if (cssProperty === "outline" || cssProperty === "outline-color" || isBorderColorProperty(cssProperty)) {
199
+ return "border-color";
200
+ }
201
+ return propertyToMatch;
202
+ }
203
+
204
+ // src/utils/hardcoded-shared-utils.ts
205
+ var import_css_tree2 = require("@eslint/css-tree");
206
+ var FONT_WEIGHTS = [
207
+ "normal",
208
+ "bold",
209
+ "bolder",
210
+ "lighter",
211
+ "100",
212
+ "200",
213
+ "300",
214
+ "400",
215
+ "500",
216
+ "600",
217
+ "700",
218
+ "800",
219
+ "900"
220
+ ];
221
+ function isKnownFontWeight(value) {
222
+ const stringValue = value.toString();
223
+ return FONT_WEIGHTS.includes(stringValue.toLowerCase());
224
+ }
225
+ function handleShorthandAutoFix(declarationNode, context, valueText, replacements) {
226
+ const sortedReplacements = replacements.sort((a, b) => a.start - b.start);
227
+ const hasAnyHooks = sortedReplacements.some((r) => r.hasHook);
228
+ const canAutoFix = hasAnyHooks;
229
+ sortedReplacements.forEach(({ start, end, replacement, displayValue, hasHook }) => {
230
+ const originalValue = valueText.substring(start, end);
231
+ const valueStartColumn = declarationNode.value.loc.start.column;
232
+ const valueColumn = valueStartColumn + start;
233
+ const { loc: { start: locStart, end: locEnd } } = declarationNode.value;
234
+ const reportNode = {
235
+ ...declarationNode.value,
236
+ loc: {
237
+ ...declarationNode.value.loc,
238
+ start: {
239
+ ...locStart,
240
+ column: valueColumn
241
+ },
242
+ end: {
243
+ ...locEnd,
244
+ column: valueColumn + originalValue.length
245
+ }
246
+ }
247
+ };
248
+ if (hasHook) {
249
+ const fix = canAutoFix ? (fixer) => {
250
+ let newValue = valueText;
251
+ for (let i = sortedReplacements.length - 1; i >= 0; i--) {
252
+ const { start: rStart, end: rEnd, replacement: rReplacement } = sortedReplacements[i];
253
+ newValue = newValue.substring(0, rStart) + rReplacement + newValue.substring(rEnd);
254
+ }
255
+ return fixer.replaceText(declarationNode.value, newValue);
256
+ } : void 0;
257
+ context.context.report({
258
+ node: reportNode,
259
+ messageId: "hardcodedValue",
260
+ data: {
261
+ oldValue: originalValue,
262
+ newValue: displayValue
263
+ },
264
+ fix
265
+ });
266
+ } else {
267
+ context.context.report({
268
+ node: reportNode,
269
+ messageId: "noReplacement",
270
+ data: {
271
+ oldValue: originalValue
272
+ }
273
+ });
274
+ }
275
+ });
276
+ }
277
+ function forEachValue(valueText, extractValue, shouldSkipNode, callback) {
278
+ if (!valueText || typeof valueText !== "string") {
279
+ return;
280
+ }
281
+ try {
282
+ const ast = (0, import_css_tree2.parse)(valueText, { context: "value", positions: true });
283
+ (0, import_css_tree2.walk)(ast, {
284
+ enter(node) {
285
+ if (shouldSkipNode(node)) {
286
+ return this.skip;
287
+ }
288
+ const value = extractValue(node);
289
+ if (value !== null) {
290
+ const positionInfo = {
291
+ start: node.loc?.start,
292
+ end: node.loc?.end
293
+ };
294
+ callback(value, positionInfo);
295
+ }
296
+ }
297
+ });
298
+ } catch (error) {
299
+ return;
300
+ }
301
+ }
302
+ function shouldSkipColorNode(node) {
303
+ return node.type === "Function" && isCssFunction(node.name);
304
+ }
305
+ function shouldSkipDimensionNode(node) {
306
+ return node.type === "Function";
307
+ }
308
+ function extractDimensionValue(valueNode, cssProperty) {
309
+ if (!valueNode) return null;
310
+ switch (valueNode.type) {
311
+ case "Dimension":
312
+ const numValue = Number(valueNode.value);
313
+ if (numValue === 0) return null;
314
+ const unit = valueNode.unit.toLowerCase();
315
+ if (unit !== "px" && unit !== "rem" && unit !== "%") return null;
316
+ return {
317
+ number: numValue,
318
+ unit
319
+ };
320
+ case "Number":
321
+ const numberValue = Number(valueNode.value);
322
+ if (numberValue === 0) return null;
323
+ return {
324
+ number: numberValue,
325
+ unit: null
326
+ };
327
+ case "Percentage":
328
+ const percentValue = Number(valueNode.value);
329
+ if (percentValue === 0) return null;
330
+ return {
331
+ number: percentValue,
332
+ unit: "%"
333
+ };
334
+ case "Value":
335
+ return valueNode.children?.[0] ? extractDimensionValue(valueNode.children[0], cssProperty) : null;
336
+ }
337
+ return null;
338
+ }
339
+ function forEachColorValue(valueText, callback) {
340
+ forEachValue(valueText, extractColorValue, shouldSkipColorNode, callback);
341
+ }
342
+ function forEachDensityValue(valueText, cssProperty, callback) {
343
+ forEachValue(
344
+ valueText,
345
+ (node) => extractDimensionValue(node, cssProperty),
346
+ shouldSkipDimensionNode,
347
+ callback
348
+ );
349
+ }
350
+ function extractFontValue(node) {
351
+ if (!node) return null;
352
+ switch (node.type) {
353
+ case "Dimension":
354
+ const numValue = Number(node.value);
355
+ if (numValue <= 0) return null;
356
+ const unit = node.unit.toLowerCase();
357
+ if (unit !== "px" && unit !== "rem" && unit !== "%") return null;
358
+ return {
359
+ number: numValue,
360
+ unit
361
+ };
362
+ case "Number":
363
+ const numberValue = Number(node.value);
364
+ if (numberValue <= 0) {
365
+ return null;
366
+ }
367
+ if (!isKnownFontWeight(numberValue)) {
368
+ return null;
369
+ }
370
+ return {
371
+ number: numberValue,
372
+ unit: null
373
+ };
374
+ case "Identifier":
375
+ const namedValue = node.name.toLowerCase();
376
+ if (!isKnownFontWeight(namedValue)) {
377
+ return null;
378
+ }
379
+ if (namedValue === "normal") {
380
+ return { number: 400, unit: null };
381
+ }
382
+ return { number: namedValue, unit: null };
383
+ case "Percentage":
384
+ const percentValue = Number(node.value);
385
+ if (percentValue === 0) return null;
386
+ return {
387
+ number: percentValue,
388
+ unit: "%"
389
+ };
390
+ case "Value":
391
+ return node.children?.[0] ? extractFontValue(node.children[0]) : null;
392
+ }
393
+ return null;
394
+ }
395
+ function shouldSkipFontNode(node) {
396
+ return node.type === "Function";
397
+ }
398
+ function forEachFontValue(valueText, callback) {
399
+ forEachValue(valueText, extractFontValue, shouldSkipFontNode, callback);
400
+ }
401
+
402
+ // src/rules/v9/no-hardcoded-values/handlers/colorHandler.ts
403
+ var handleColorDeclaration = (node, context) => {
404
+ const cssProperty = node.property.toLowerCase();
405
+ const valueText = context.sourceCode.getText(node.value);
406
+ const replacements = [];
407
+ forEachColorValue(valueText, (colorValue, positionInfo) => {
408
+ if (colorValue !== "transparent" && isValidColor(colorValue)) {
409
+ const replacement = createColorReplacement(colorValue, cssProperty, context, positionInfo, valueText);
410
+ if (replacement) {
411
+ replacements.push(replacement);
412
+ }
413
+ }
414
+ });
415
+ handleShorthandAutoFix(node, context, valueText, replacements);
416
+ };
417
+ function createColorReplacement(colorValue, cssProperty, context, positionInfo, originalValueText) {
418
+ if (!positionInfo?.start) {
419
+ return null;
420
+ }
421
+ const hexValue = convertToHex(colorValue);
422
+ if (!hexValue) {
423
+ return null;
424
+ }
425
+ const propToMatch = resolvePropertyToMatch(cssProperty);
426
+ const closestHooks = findClosestColorHook(hexValue, context.valueToStylinghook, propToMatch);
427
+ const start = positionInfo.start.offset;
428
+ const end = positionInfo.end.offset;
429
+ const originalValue = originalValueText ? originalValueText.substring(start, end) : colorValue;
430
+ if (closestHooks.length === 1) {
431
+ return {
432
+ start,
433
+ end,
434
+ replacement: `var(${closestHooks[0]}, ${colorValue})`,
435
+ displayValue: closestHooks[0],
436
+ hasHook: true
437
+ };
438
+ } else if (closestHooks.length > 1) {
439
+ return {
440
+ start,
441
+ end,
442
+ replacement: originalValue,
443
+ // Use original value to preserve spacing
444
+ displayValue: closestHooks.join(", "),
445
+ hasHook: true
446
+ // ← THE FIX: Multiple hooks still means "has hooks"
447
+ };
448
+ } else {
449
+ return {
450
+ start,
451
+ end,
452
+ replacement: originalValue,
453
+ // Use original value to preserve spacing
454
+ displayValue: originalValue,
455
+ hasHook: false
456
+ };
457
+ }
458
+ }
459
+
460
+ // src/utils/value-utils.ts
461
+ function parseUnitValue(value) {
462
+ if (!value) return null;
463
+ const match = value.match(/^(-?\d*\.?\d+)(px|rem|%)?$/);
464
+ if (!match) return null;
465
+ const number = parseFloat(match[1]);
466
+ const unit = match[2] ? match[2] : null;
467
+ if (isNaN(number)) return null;
468
+ return { number, unit };
469
+ }
470
+ function toAlternateUnitValue(numberVal, unitType) {
471
+ if (unitType === "px") {
472
+ let floatValue = parseFloat(`${numberVal / 16}`);
473
+ if (!isNaN(floatValue)) {
474
+ return {
475
+ unit: "rem",
476
+ number: parseFloat(floatValue.toFixed(4))
477
+ };
478
+ }
479
+ } else if (unitType === "rem") {
480
+ const intValue = parseInt(`${numberVal * 16}`);
481
+ if (!isNaN(intValue)) {
482
+ return {
483
+ unit: "px",
484
+ number: intValue
485
+ };
486
+ }
487
+ }
488
+ return null;
489
+ }
490
+
491
+ // src/utils/styling-hook-utils.ts
492
+ function isValueMatch(valueToMatch, sldsValue) {
493
+ if (!valueToMatch || !sldsValue) {
494
+ return false;
495
+ }
496
+ return valueToMatch.unit == sldsValue.unit && valueToMatch.number === sldsValue.number;
497
+ }
498
+ function getStylingHooksForDensityValue(parsedValue, supportedStylinghooks, cssProperty) {
499
+ if (!parsedValue) return [];
500
+ const alternateValue = toAlternateUnitValue(parsedValue.number, parsedValue.unit);
501
+ const matchedHooks = [];
502
+ for (const [sldsValue, hooks] of Object.entries(supportedStylinghooks)) {
503
+ const parsedSldsValue = parseUnitValue(sldsValue);
504
+ if (isValueMatch(parsedValue, parsedSldsValue) || alternateValue && isValueMatch(alternateValue, parsedSldsValue)) {
505
+ hooks.filter((hook) => hook.properties.includes(cssProperty)).forEach((hook) => matchedHooks.push(hook.name));
506
+ }
507
+ }
508
+ return matchedHooks;
509
+ }
510
+
511
+ // src/rules/v9/no-hardcoded-values/handlers/densityHandler.ts
512
+ var handleDensityDeclaration = (node, context) => {
513
+ const cssProperty = node.property.toLowerCase();
514
+ const valueText = context.sourceCode.getText(node.value);
515
+ const replacements = [];
516
+ forEachDensityValue(valueText, cssProperty, (parsedDimension, positionInfo) => {
517
+ if (parsedDimension) {
518
+ const replacement = createDimensionReplacement(parsedDimension, cssProperty, context, positionInfo);
519
+ if (replacement) {
520
+ replacements.push(replacement);
521
+ }
522
+ }
523
+ });
524
+ handleShorthandAutoFix(node, context, valueText, replacements);
525
+ };
526
+ function createDimensionReplacement(parsedDimension, cssProperty, context, positionInfo) {
527
+ if (!parsedDimension || !positionInfo?.start) {
528
+ return null;
529
+ }
530
+ const rawValue = parsedDimension.unit ? `${parsedDimension.number}${parsedDimension.unit}` : parsedDimension.number.toString();
531
+ const propToMatch = resolvePropertyToMatch(cssProperty);
532
+ const closestHooks = getStylingHooksForDensityValue(parsedDimension, context.valueToStylinghook, propToMatch);
533
+ const start = positionInfo.start.offset;
534
+ const end = positionInfo.end.offset;
535
+ if (closestHooks.length === 1) {
536
+ return {
537
+ start,
538
+ end,
539
+ replacement: `var(${closestHooks[0]}, ${rawValue})`,
540
+ displayValue: closestHooks[0],
541
+ hasHook: true
542
+ };
543
+ } else if (closestHooks.length > 1) {
544
+ return {
545
+ start,
546
+ end,
547
+ replacement: rawValue,
548
+ displayValue: closestHooks.join(", "),
549
+ hasHook: true
550
+ };
551
+ } else {
552
+ return {
553
+ start,
554
+ end,
555
+ replacement: rawValue,
556
+ displayValue: rawValue,
557
+ hasHook: false
558
+ };
559
+ }
560
+ }
561
+
562
+ // src/rules/v9/no-hardcoded-values/handlers/fontHandler.ts
563
+ var handleFontDeclaration = (node, context) => {
564
+ const cssProperty = node.property.toLowerCase();
565
+ const valueText = context.sourceCode.getText(node.value);
566
+ const replacements = [];
567
+ forEachFontValue(valueText, (fontValue, positionInfo) => {
568
+ if (fontValue && isValidFontValue(fontValue, cssProperty)) {
569
+ const replacement = createFontReplacement(fontValue, cssProperty, context, positionInfo);
570
+ if (replacement) {
571
+ replacements.push(replacement);
572
+ }
573
+ }
574
+ });
575
+ handleShorthandAutoFix(node, context, valueText, replacements);
576
+ };
577
+ function isValidFontValue(fontValue, cssProperty) {
578
+ if (cssProperty === "font-size") {
579
+ return !!fontValue.unit;
580
+ } else if (cssProperty === "font-weight") {
581
+ return !fontValue.unit && isKnownFontWeight(fontValue.number);
582
+ } else if (cssProperty === "font") {
583
+ if (!fontValue.unit && isKnownFontWeight(fontValue.number)) {
584
+ return true;
585
+ } else if (fontValue.unit) {
586
+ return true;
587
+ } else {
588
+ return false;
589
+ }
590
+ }
591
+ return false;
592
+ }
593
+ function createFontReplacement(fontValue, cssProperty, context, positionInfo) {
594
+ if (!positionInfo?.start) {
595
+ return null;
596
+ }
597
+ const rawValue = fontValue.unit ? `${fontValue.number}${fontValue.unit}` : fontValue.number.toString();
598
+ const propToMatch = !fontValue.unit && isKnownFontWeight(fontValue.number) ? resolvePropertyToMatch("font-weight") : resolvePropertyToMatch("font-size");
599
+ const closestHooks = getStylingHooksForDensityValue(fontValue, context.valueToStylinghook, propToMatch);
600
+ const start = positionInfo.start.offset;
601
+ const end = positionInfo.end.offset;
602
+ if (closestHooks.length === 1) {
603
+ return {
604
+ start,
605
+ end,
606
+ replacement: `var(${closestHooks[0]}, ${rawValue})`,
607
+ displayValue: closestHooks[0],
608
+ hasHook: true
609
+ };
610
+ } else if (closestHooks.length > 1) {
611
+ return {
612
+ start,
613
+ end,
614
+ replacement: rawValue,
615
+ displayValue: closestHooks.join(", "),
616
+ hasHook: true
617
+ };
618
+ } else {
619
+ return {
620
+ start,
621
+ end,
622
+ replacement: rawValue,
623
+ displayValue: rawValue,
624
+ hasHook: false
625
+ };
626
+ }
627
+ }
628
+
629
+ // src/utils/boxShadowValueParser.ts
630
+ var import_css_tree3 = require("@eslint/css-tree");
631
+ function isColorValue(node) {
632
+ if (!node) return false;
633
+ switch (node.type) {
634
+ case "Hash":
635
+ return true;
636
+ // #hex colors
637
+ case "Identifier":
638
+ return isValidColor(node.name);
639
+ case "Function":
640
+ return isCssColorFunction(node.name.toLowerCase());
641
+ default:
642
+ return false;
643
+ }
644
+ }
645
+ function isLengthValue(node) {
646
+ if (!node) return false;
647
+ switch (node.type) {
648
+ case "Dimension":
649
+ const dimensionStr = `${node.value}${node.unit}`;
650
+ return parseUnitValue(dimensionStr) !== null;
651
+ case "Number":
652
+ return Number(node.value) === 0;
653
+ default:
654
+ return false;
655
+ }
656
+ }
657
+ function isInsetKeyword(node) {
658
+ return node?.type === "Identifier" && node.name.toLowerCase() === "inset";
659
+ }
660
+ function extractShadowParts(valueText) {
661
+ const shadows = [];
662
+ let currentShadow = {
663
+ lengthParts: [],
664
+ colorParts: [],
665
+ inset: false
666
+ };
667
+ try {
668
+ const ast = (0, import_css_tree3.parse)(valueText, { context: "value" });
669
+ (0, import_css_tree3.walk)(ast, {
670
+ enter(node) {
671
+ if (node.type === "Function") {
672
+ return this.skip;
673
+ }
674
+ if (isInsetKeyword(node)) {
675
+ currentShadow.inset = true;
676
+ } else if (isLengthValue(node)) {
677
+ currentShadow.lengthParts.push((0, import_css_tree3.generate)(node));
678
+ } else if (isColorValue(node)) {
679
+ currentShadow.colorParts.push((0, import_css_tree3.generate)(node));
680
+ }
681
+ }
682
+ });
683
+ if (currentShadow.lengthParts.length > 0 || currentShadow.colorParts.length > 0 || currentShadow.inset) {
684
+ shadows.push(currentShadow);
685
+ }
686
+ } catch (error) {
687
+ return [];
688
+ }
689
+ return shadows;
690
+ }
691
+ function parseBoxShadowValue(value) {
692
+ const shadowStrings = value.split(",").map((s) => s.trim());
693
+ const allShadows = [];
694
+ for (const shadowString of shadowStrings) {
695
+ const shadows = extractShadowParts(shadowString);
696
+ const parsedShadows = shadows.map((shadow) => {
697
+ const shadowValue = {};
698
+ const lengthProps = ["offsetX", "offsetY", "blurRadius", "spreadRadius"];
699
+ lengthProps.forEach((prop, index) => {
700
+ if (shadow.lengthParts.length > index) {
701
+ shadowValue[prop] = shadow.lengthParts[index];
702
+ }
703
+ });
704
+ if (shadow.colorParts.length > 0) {
705
+ shadowValue.color = shadow.colorParts[0];
706
+ }
707
+ if (shadow.inset) {
708
+ shadowValue.inset = true;
709
+ }
710
+ return shadowValue;
711
+ });
712
+ allShadows.push(...parsedShadows);
713
+ }
714
+ return allShadows;
715
+ }
716
+ function normalizeLengthValue(value) {
717
+ if (!value) return "0px";
718
+ if (value === "0") return "0px";
719
+ return value;
720
+ }
721
+ function isBoxShadowMatch(parsedCssValue, parsedValueHook) {
722
+ if (parsedCssValue.length !== parsedValueHook.length) {
723
+ return false;
724
+ }
725
+ for (let i = 0; i < parsedCssValue.length; i++) {
726
+ const cssShadow = parsedCssValue[i];
727
+ const hookShadow = parsedValueHook[i];
728
+ if (cssShadow.color !== hookShadow.color || cssShadow.inset !== hookShadow.inset) {
729
+ return false;
730
+ }
731
+ const lengthProps = ["offsetX", "offsetY", "blurRadius", "spreadRadius"];
732
+ for (const prop of lengthProps) {
733
+ if (normalizeLengthValue(cssShadow[prop]) !== normalizeLengthValue(hookShadow[prop])) {
734
+ return false;
735
+ }
736
+ }
737
+ }
738
+ return true;
739
+ }
740
+
741
+ // src/rules/v9/no-hardcoded-values/handlers/boxShadowHandler.ts
742
+ function containsCssVariable(valueText) {
743
+ return valueText.includes("var(");
744
+ }
745
+ var handleBoxShadowDeclaration = (node, context) => {
746
+ const cssProperty = node.property.toLowerCase();
747
+ const valueText = context.sourceCode.getText(node.value);
748
+ if (containsCssVariable(valueText)) {
749
+ return;
750
+ }
751
+ const replacements = [];
752
+ const parsedCssValue = parseAndValidateBoxShadow(valueText);
753
+ if (parsedCssValue) {
754
+ const shadowHooks = getBoxShadowHooks(context.valueToStylinghook);
755
+ const closestHooks = findMatchingBoxShadowHooks(parsedCssValue, shadowHooks);
756
+ const positionInfo = {
757
+ start: { offset: 0, line: 1, column: 1 },
758
+ end: { offset: valueText.length, line: 1, column: valueText.length + 1 }
759
+ };
760
+ const replacement = createBoxShadowReplacement(
761
+ valueText,
762
+ closestHooks,
763
+ context,
764
+ positionInfo
765
+ );
766
+ if (replacement) {
767
+ replacements.push(replacement);
768
+ }
769
+ }
770
+ handleShorthandAutoFix(node, context, valueText, replacements);
771
+ };
772
+ function getBoxShadowHooks(supportedStylinghooks) {
773
+ return Object.entries(supportedStylinghooks).filter(([key, value]) => {
774
+ return value.some((hook) => hook.properties.includes("box-shadow"));
775
+ }).map(([key, value]) => {
776
+ return [key, value.map((hook) => hook.name)];
777
+ });
778
+ }
779
+ function parseAndValidateBoxShadow(cssValue) {
780
+ const parsedCssValue = parseBoxShadowValue(cssValue).filter((shadow) => Object.keys(shadow).length > 0);
781
+ if (parsedCssValue.length === 0) {
782
+ return null;
783
+ }
784
+ return parsedCssValue;
785
+ }
786
+ function findMatchingBoxShadowHooks(parsedCssValue, shadowHooks) {
787
+ for (const [shadowHookValue, closestHooks] of shadowHooks) {
788
+ const parsedHookValue = parseAndValidateBoxShadow(shadowHookValue);
789
+ if (parsedHookValue && isBoxShadowMatch(parsedCssValue, parsedHookValue)) {
790
+ return closestHooks;
791
+ }
792
+ }
793
+ return [];
794
+ }
795
+ function createBoxShadowReplacement(originalValue, hooks, context, positionInfo) {
796
+ if (!positionInfo?.start) {
797
+ return null;
798
+ }
799
+ const start = positionInfo.start.offset;
800
+ const end = positionInfo.end.offset;
801
+ if (hooks.length === 1) {
802
+ return {
803
+ start,
804
+ end,
805
+ replacement: `var(${hooks[0]}, ${originalValue})`,
806
+ displayValue: hooks[0],
807
+ hasHook: true
808
+ };
809
+ } else if (hooks.length > 1) {
810
+ return {
811
+ start,
812
+ end,
813
+ replacement: originalValue,
814
+ displayValue: hooks.join(", "),
815
+ hasHook: true
816
+ };
817
+ } else {
818
+ return {
819
+ start,
820
+ end,
821
+ replacement: originalValue,
822
+ displayValue: originalValue,
823
+ hasHook: false
824
+ };
825
+ }
826
+ }
827
+ // Annotate the CommonJS export names for ESM import in node:
828
+ 0 && (module.exports = {
829
+ handleBoxShadowDeclaration,
830
+ handleColorDeclaration,
831
+ handleDensityDeclaration,
832
+ handleFontDeclaration
833
+ });
834
+ //# sourceMappingURL=index.js.map