@salesforce-ux/eslint-plugin-slds 1.0.3 → 1.0.4

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 (32) hide show
  1. package/build/index.js +70 -99
  2. package/build/index.js.map +4 -4
  3. package/build/rules/enforce-bem-usage.js.map +1 -1
  4. package/build/rules/modal-close-button-issue.js.map +1 -1
  5. package/build/rules/no-deprecated-classes-slds2.js.map +1 -1
  6. package/build/rules/v9/enforce-bem-usage.js.map +1 -1
  7. package/build/rules/v9/enforce-component-hook-naming-convention.js.map +1 -1
  8. package/build/rules/v9/enforce-sds-to-slds-hooks.js.map +1 -1
  9. package/build/rules/v9/lwc-token-to-slds-hook.js +117 -95
  10. package/build/rules/v9/lwc-token-to-slds-hook.js.map +3 -3
  11. package/build/rules/v9/no-deprecated-slds-classes.js.map +1 -1
  12. package/build/rules/v9/no-deprecated-tokens-slds1.js.map +1 -1
  13. package/build/rules/v9/no-hardcoded-values/handlers/boxShadowHandler.js.map +2 -2
  14. package/build/rules/v9/no-hardcoded-values/handlers/colorHandler.js.map +2 -2
  15. package/build/rules/v9/no-hardcoded-values/handlers/densityHandler.js.map +2 -2
  16. package/build/rules/v9/no-hardcoded-values/handlers/fontHandler.js.map +2 -2
  17. package/build/rules/v9/no-hardcoded-values/handlers/index.js.map +2 -2
  18. package/build/rules/v9/no-hardcoded-values/no-hardcoded-values-slds1.js.map +3 -3
  19. package/build/rules/v9/no-hardcoded-values/no-hardcoded-values-slds2.js.map +3 -3
  20. package/build/rules/v9/no-hardcoded-values/noHardcodedValueRule.js.map +2 -2
  21. package/build/rules/v9/no-slds-class-overrides.js.map +1 -1
  22. package/build/rules/v9/no-slds-namespace-for-custom-hooks.js.map +2 -2
  23. package/build/rules/v9/no-slds-private-var.js.map +1 -1
  24. package/build/rules/v9/no-slds-var-without-fallback.js.map +2 -2
  25. package/build/rules/v9/no-sldshook-fallback-for-lwctoken.js.map +1 -1
  26. package/build/rules/v9/no-unsupported-hooks-slds2.js.map +1 -1
  27. package/build/rules/v9/reduce-annotations.js.map +1 -1
  28. package/build/src/utils/css-utils.d.ts +5 -0
  29. package/build/utils/css-utils.js +14 -0
  30. package/build/utils/css-utils.js.map +2 -2
  31. package/eslint.config.mjs +7 -1
  32. package/package.json +1 -1
package/build/index.js CHANGED
@@ -1098,6 +1098,18 @@ function forEachNamespacedVariable(valueText, callback) {
1098
1098
  });
1099
1099
  forEachValue(valueText, extractor, () => false, callback);
1100
1100
  }
1101
+ function forEachLwcVariable(valueText, callback) {
1102
+ const extractor = (node) => extractCssVariable(node, (variableName, childrenArray) => {
1103
+ if (!variableName.startsWith("--lwc-")) {
1104
+ return null;
1105
+ }
1106
+ const hasFallback = childrenArray.some(
1107
+ (child) => child.type === "Operator" && child.value === ","
1108
+ );
1109
+ return { name: variableName, hasFallback };
1110
+ });
1111
+ forEachValue(valueText, extractor, () => false, callback);
1112
+ }
1101
1113
  function formatSuggestionHooks(hooks) {
1102
1114
  if (hooks.length === 1) {
1103
1115
  return `${hooks[0]}`;
@@ -1131,32 +1143,6 @@ function getRecommendation(lwcToken) {
1131
1143
  const hasRecommendation = oldValue in lwcToSlds && replacementCategory !== "empty" /* EMPTY */;
1132
1144
  return { hasRecommendation, recommendation, replacementCategory };
1133
1145
  }
1134
- function extractLwcVariableWithFallback(node, sourceCode) {
1135
- if (!node?.children || node.type !== "Function" || node.name !== "var") {
1136
- return null;
1137
- }
1138
- const children = Array.from(node.children);
1139
- const firstChild = children[0];
1140
- if (!firstChild?.name?.startsWith("--lwc-") || firstChild.type !== "Identifier") {
1141
- return null;
1142
- }
1143
- const commaIndex = children.findIndex(
1144
- (child) => child.type === "Operator" && child.value === ","
1145
- );
1146
- let fallbackValue = null;
1147
- if (commaIndex !== -1 && commaIndex + 1 < children.length) {
1148
- const fallbackStart = children[commaIndex + 1];
1149
- const fallbackEnd = children[children.length - 1];
1150
- if (fallbackStart?.loc && fallbackEnd?.loc) {
1151
- const fullText = sourceCode.getText();
1152
- fallbackValue = fullText.substring(fallbackStart.loc.start.offset, fallbackEnd.loc.end.offset).trim();
1153
- }
1154
- }
1155
- return {
1156
- lwcToken: firstChild.name,
1157
- fallbackValue
1158
- };
1159
- }
1160
1146
  function getReportMessage(cssVar, replacementCategory, recommendation) {
1161
1147
  if (!recommendation) {
1162
1148
  return {
@@ -1192,85 +1178,73 @@ var lwc_token_to_slds_hook_default = {
1192
1178
  messages: messages4
1193
1179
  },
1194
1180
  create(context) {
1195
- function reportAndFix(node, oldValue, suggestedMatch, messageId, data) {
1196
- let fixFunction = null;
1197
- if (suggestedMatch) {
1198
- fixFunction = (fixer) => {
1199
- if (node.type === "Declaration") {
1200
- const sourceCode = context.sourceCode;
1201
- const fullText = sourceCode.getText();
1202
- const nodeOffset = node.loc.start.offset;
1203
- const propertyStart = nodeOffset;
1204
- const propertyEnd = propertyStart + oldValue.length;
1205
- const textAtPosition = fullText.substring(propertyStart, propertyEnd);
1206
- if (textAtPosition === oldValue) {
1207
- return fixer.replaceTextRange([propertyStart, propertyEnd], suggestedMatch);
1208
- }
1209
- } else if (node.type === "Function" && node.name === "var") {
1210
- const sourceCode = context.sourceCode;
1211
- const fullText = sourceCode.getText();
1212
- const nodeOffset = node.loc.start.offset;
1213
- const nodeEnd = node.loc.end.offset;
1214
- return fixer.replaceTextRange([nodeOffset, nodeEnd], suggestedMatch);
1215
- } else {
1216
- const sourceCode = context.sourceCode;
1217
- const fullText = sourceCode.getText();
1218
- const varFunctionCall = `var(${oldValue})`;
1219
- const nodeOffset = node.loc.start.offset;
1220
- const searchStart = Math.max(0, nodeOffset - 4);
1221
- const searchEnd = nodeOffset + oldValue.length + 1;
1222
- const searchArea = fullText.substring(searchStart, searchEnd);
1223
- const functionCallIndex = searchArea.indexOf(varFunctionCall);
1224
- if (functionCallIndex !== -1) {
1225
- const actualStart = searchStart + functionCallIndex;
1226
- const actualEnd = actualStart + varFunctionCall.length;
1227
- return fixer.replaceTextRange([actualStart, actualEnd], suggestedMatch);
1228
- }
1229
- }
1230
- return null;
1231
- };
1232
- }
1181
+ function reportAndFix(node, suggestedMatch, messageId, data, fixRange, loc) {
1233
1182
  context.report({
1234
1183
  node,
1184
+ loc: loc || node.loc,
1235
1185
  messageId,
1236
1186
  data,
1237
- fix: fixFunction
1187
+ fix: suggestedMatch && fixRange ? (fixer) => {
1188
+ return fixer.replaceTextRange(fixRange, suggestedMatch);
1189
+ } : void 0
1238
1190
  });
1239
1191
  }
1240
1192
  return {
1241
- // CSS custom property declarations: --lwc-* properties
1242
- "Declaration[property=/^--lwc-/]"(node) {
1193
+ // CSS custom property declarations: Check both property name and value
1194
+ "Declaration"(node) {
1243
1195
  const property = node.property;
1244
- if (shouldIgnoreDetection(property)) {
1245
- return;
1246
- }
1247
- const { hasRecommendation, recommendation, replacementCategory } = getRecommendation(property);
1248
- const { messageId, data } = getReportMessage(property, replacementCategory, recommendation);
1249
- const suggestedMatch = hasRecommendation && replacementCategory === "slds_token" /* SLDS_TOKEN */ ? recommendation : null;
1250
- reportAndFix(node, property, suggestedMatch, messageId, data);
1251
- },
1252
- // LWC tokens inside var() functions: var(--lwc-*)
1253
- "Function[name='var']"(node) {
1254
- const lwcVarInfo = extractLwcVariableWithFallback(node, context.sourceCode);
1255
- if (!lwcVarInfo) {
1256
- return;
1257
- }
1258
- const { lwcToken, fallbackValue } = lwcVarInfo;
1259
- if (shouldIgnoreDetection(lwcToken)) {
1260
- return;
1261
- }
1262
- const { hasRecommendation, recommendation, replacementCategory } = getRecommendation(lwcToken);
1263
- const { messageId, data } = getReportMessage(lwcToken, replacementCategory, recommendation);
1264
- let suggestedMatch = null;
1265
- if (hasRecommendation) {
1266
- if (replacementCategory === "slds_token" /* SLDS_TOKEN */) {
1267
- const originalVarCall = fallbackValue ? `var(${lwcToken}, ${fallbackValue})` : `var(${lwcToken})`;
1268
- suggestedMatch = `var(${recommendation}, ${originalVarCall})`;
1269
- } else if (replacementCategory === "raw_value" /* RAW_VALUE */) {
1270
- suggestedMatch = recommendation;
1196
+ if (property && property.startsWith("--lwc-")) {
1197
+ if (!shouldIgnoreDetection(property)) {
1198
+ const { hasRecommendation, recommendation, replacementCategory } = getRecommendation(property);
1199
+ const { messageId, data } = getReportMessage(property, replacementCategory, recommendation);
1200
+ const suggestedMatch = hasRecommendation && replacementCategory === "slds_token" /* SLDS_TOKEN */ ? recommendation : null;
1201
+ const propertyStart = node.loc.start.offset;
1202
+ const propertyEnd = propertyStart + property.length;
1203
+ reportAndFix(node, suggestedMatch, messageId, data, [propertyStart, propertyEnd]);
1271
1204
  }
1272
1205
  }
1273
- reportAndFix(node, lwcToken, suggestedMatch, messageId, data);
1206
+ const valueText = context.sourceCode.getText(node.value);
1207
+ if (valueText) {
1208
+ forEachLwcVariable(valueText, (variableInfo, positionInfo) => {
1209
+ const { name: lwcToken, hasFallback } = variableInfo;
1210
+ if (shouldIgnoreDetection(lwcToken)) {
1211
+ return;
1212
+ }
1213
+ const { hasRecommendation, recommendation, replacementCategory } = getRecommendation(lwcToken);
1214
+ const { messageId, data } = getReportMessage(lwcToken, replacementCategory, recommendation);
1215
+ let suggestedMatch = null;
1216
+ if (hasRecommendation) {
1217
+ if (replacementCategory === "slds_token" /* SLDS_TOKEN */) {
1218
+ let fallbackValue = null;
1219
+ if (hasFallback && positionInfo.start && positionInfo.end && positionInfo.start.offset !== void 0 && positionInfo.end.offset !== void 0) {
1220
+ const varCallText = valueText.substring(positionInfo.start.offset, positionInfo.end.offset);
1221
+ const commaIndex = varCallText.indexOf(",");
1222
+ if (commaIndex !== -1) {
1223
+ fallbackValue = varCallText.substring(commaIndex + 1, varCallText.length - 1).trim();
1224
+ }
1225
+ }
1226
+ const originalVarCall = fallbackValue ? `var(${lwcToken}, ${fallbackValue})` : `var(${lwcToken})`;
1227
+ suggestedMatch = `var(${recommendation}, ${originalVarCall})`;
1228
+ } else if (replacementCategory === "raw_value" /* RAW_VALUE */) {
1229
+ suggestedMatch = recommendation;
1230
+ }
1231
+ }
1232
+ const valueStartOffset = node.value.loc.start.offset;
1233
+ const varStartOffset = valueStartOffset + (positionInfo.start?.offset || 0);
1234
+ const varEndOffset = valueStartOffset + (positionInfo.end?.offset || valueText.length);
1235
+ const preciseLoc = positionInfo.start && positionInfo.end && node.value.loc ? {
1236
+ start: {
1237
+ line: node.value.loc.start.line + positionInfo.start.line - 1,
1238
+ column: node.value.loc.start.column + positionInfo.start.column - 1
1239
+ },
1240
+ end: {
1241
+ line: node.value.loc.start.line + positionInfo.end.line - 1,
1242
+ column: node.value.loc.start.column + positionInfo.end.column - 1
1243
+ }
1244
+ } : node.value.loc;
1245
+ reportAndFix(node, suggestedMatch, messageId, data, [varStartOffset, varEndOffset], preciseLoc);
1246
+ });
1247
+ }
1274
1248
  }
1275
1249
  };
1276
1250
  }
@@ -2334,7 +2308,6 @@ var no_hardcoded_values_slds2_default = defineNoHardcodedValueRule({
2334
2308
 
2335
2309
  // src/index.ts
2336
2310
  var import_parser2 = __toESM(require("@html-eslint/parser"));
2337
- var import_css = __toESM(require("@eslint/css"));
2338
2311
 
2339
2312
  // eslint.rules.json
2340
2313
  var eslint_rules_default = {
@@ -2382,7 +2355,7 @@ var rules = {
2382
2355
  var plugin = {
2383
2356
  meta: {
2384
2357
  name: "@salesforce-ux/eslint-plugin-slds",
2385
- version: "1.0.3"
2358
+ version: "1.0.4"
2386
2359
  },
2387
2360
  rules,
2388
2361
  configs: {}
@@ -2392,13 +2365,11 @@ var cssConfigArray = [
2392
2365
  {
2393
2366
  files: ["**/*.{css,scss}"],
2394
2367
  language: "css/css",
2395
- ...import_css.default.configs.recommended,
2396
2368
  languageOptions: {
2397
2369
  tolerant: true
2398
2370
  // Allow recoverable parsing errors for SCSS syntax
2399
2371
  },
2400
2372
  plugins: {
2401
- css: import_css.default,
2402
2373
  "@salesforce-ux/slds": plugin
2403
2374
  },
2404
2375
  rules: eslint_rules_default.css,