@salesforce-ux/eslint-plugin-slds 1.0.2-internal → 1.0.3-internal

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/build/index.js CHANGED
@@ -1119,6 +1119,32 @@ function getRecommendation(lwcToken) {
1119
1119
  const hasRecommendation = oldValue in lwcToSlds && replacementCategory !== "empty" /* EMPTY */;
1120
1120
  return { hasRecommendation, recommendation, replacementCategory };
1121
1121
  }
1122
+ function extractLwcVariableWithFallback(node, sourceCode) {
1123
+ if (!node?.children || node.type !== "Function" || node.name !== "var") {
1124
+ return null;
1125
+ }
1126
+ const children = Array.from(node.children);
1127
+ const firstChild = children[0];
1128
+ if (!firstChild?.name?.startsWith("--lwc-") || firstChild.type !== "Identifier") {
1129
+ return null;
1130
+ }
1131
+ const commaIndex = children.findIndex(
1132
+ (child) => child.type === "Operator" && child.value === ","
1133
+ );
1134
+ let fallbackValue = null;
1135
+ if (commaIndex !== -1 && commaIndex + 1 < children.length) {
1136
+ const fallbackStart = children[commaIndex + 1];
1137
+ const fallbackEnd = children[children.length - 1];
1138
+ if (fallbackStart?.loc && fallbackEnd?.loc) {
1139
+ const fullText = sourceCode.getText();
1140
+ fallbackValue = fullText.substring(fallbackStart.loc.start.offset, fallbackEnd.loc.end.offset).trim();
1141
+ }
1142
+ }
1143
+ return {
1144
+ lwcToken: firstChild.name,
1145
+ fallbackValue
1146
+ };
1147
+ }
1122
1148
  function getReportMessage(cssVar, replacementCategory, recommendation) {
1123
1149
  if (!recommendation) {
1124
1150
  return {
@@ -1168,6 +1194,12 @@ var lwc_token_to_slds_hook_default = {
1168
1194
  if (textAtPosition === oldValue) {
1169
1195
  return fixer.replaceTextRange([propertyStart, propertyEnd], suggestedMatch);
1170
1196
  }
1197
+ } else if (node.type === "Function" && node.name === "var") {
1198
+ const sourceCode = context.sourceCode;
1199
+ const fullText = sourceCode.getText();
1200
+ const nodeOffset = node.loc.start.offset;
1201
+ const nodeEnd = node.loc.end.offset;
1202
+ return fixer.replaceTextRange([nodeOffset, nodeEnd], suggestedMatch);
1171
1203
  } else {
1172
1204
  const sourceCode = context.sourceCode;
1173
1205
  const fullText = sourceCode.getText();
@@ -1206,23 +1238,27 @@ var lwc_token_to_slds_hook_default = {
1206
1238
  reportAndFix(node, property, suggestedMatch, messageId, data);
1207
1239
  },
1208
1240
  // LWC tokens inside var() functions: var(--lwc-*)
1209
- "Function[name='var'] Identifier[name=/^--lwc-/]"(node) {
1210
- const tokenName = node.name;
1211
- if (shouldIgnoreDetection(tokenName)) {
1241
+ "Function[name='var']"(node) {
1242
+ const lwcVarInfo = extractLwcVariableWithFallback(node, context.sourceCode);
1243
+ if (!lwcVarInfo) {
1244
+ return;
1245
+ }
1246
+ const { lwcToken, fallbackValue } = lwcVarInfo;
1247
+ if (shouldIgnoreDetection(lwcToken)) {
1212
1248
  return;
1213
1249
  }
1214
- const { hasRecommendation, recommendation, replacementCategory } = getRecommendation(tokenName);
1215
- const { messageId, data } = getReportMessage(tokenName, replacementCategory, recommendation);
1250
+ const { hasRecommendation, recommendation, replacementCategory } = getRecommendation(lwcToken);
1251
+ const { messageId, data } = getReportMessage(lwcToken, replacementCategory, recommendation);
1216
1252
  let suggestedMatch = null;
1217
1253
  if (hasRecommendation) {
1218
1254
  if (replacementCategory === "slds_token" /* SLDS_TOKEN */) {
1219
- const originalVarCall = `var(${tokenName})`;
1255
+ const originalVarCall = fallbackValue ? `var(${lwcToken}, ${fallbackValue})` : `var(${lwcToken})`;
1220
1256
  suggestedMatch = `var(${recommendation}, ${originalVarCall})`;
1221
1257
  } else if (replacementCategory === "raw_value" /* RAW_VALUE */) {
1222
1258
  suggestedMatch = recommendation;
1223
1259
  }
1224
1260
  }
1225
- reportAndFix(node, tokenName, suggestedMatch, messageId, data);
1261
+ reportAndFix(node, lwcToken, suggestedMatch, messageId, data);
1226
1262
  }
1227
1263
  };
1228
1264
  }
@@ -2316,7 +2352,7 @@ var rules = {
2316
2352
  var plugin = {
2317
2353
  meta: {
2318
2354
  name: "@salesforce-ux/eslint-plugin-slds",
2319
- version: "1.0.2-internal"
2355
+ version: "1.0.3-internal"
2320
2356
  },
2321
2357
  rules,
2322
2358
  configs: {}