@salesforce-ux/eslint-plugin-slds
Version:
ESLint plugin provides custom linting rules specifically built for Salesforce Lightning Design System 2 (SLDS 2 beta)
1,133 lines (1,113 loc) • 43 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// yaml-file:rule-messages.yml
var require_rule_messages = __commonJS({
"yaml-file:rule-messages.yml"(exports2, module2) {
module2.exports = {
"no-slds-class-overrides": {
"description": "Create new custom CSS classes instead of overriding SLDS selectors",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#no-slds-class-overrides",
"type": "problem",
"messages": {
"sldsClassOverride": "Overriding .{{className}} isn't supported. To differentiate SLDS and custom classes, create a CSS class in your namespace. Examples: myapp-input, myapp-button."
}
},
"no-deprecated-slds-classes": {
"description": "Please replace the deprecated classes with a modern equivalent",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#no-deprecated-slds-classes",
"type": "problem",
"messages": {
"deprecatedClass": "The class {{className}} is deprecated and not available in SLDS2. Please update to a supported class."
}
},
"no-deprecated-tokens-slds1": {
"description": "Update outdated design tokens to SLDS 2 styling hooks with similar values. For more information, see Styling Hooks on lightningdesignsystem.com.",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#no-deprecated-tokens-slds1",
"type": "problem",
"messages": {
"deprecatedToken": "Consider removing {{oldValue}} or replacing it with {{newValue}}. Set the fallback to {{oldValue}}. For more info, see Styling Hooks on lightningdesignsystem.com.",
"noReplacement": "Update outdated design tokens to SLDS 2 styling hooks with similar values. For more information, see Styling Hooks on lightningdesignsystem.com."
}
},
"enforce-sds-to-slds-hooks": {
"description": "Convert your existing --sds styling hooks to --slds styling hooks. See lightningdesignsystem.com for more info.",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#enforce-sds-to-slds-hooks",
"type": "problem",
"messages": {
"replaceSdsWithSlds": "Replace {{oldValue}} with {{suggestedMatch}} styling hook."
}
},
"enforce-bem-usage": {
"description": "Replace BEM double-dash syntax in class names with single underscore syntax",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#enforce-bem-usage",
"type": "problem",
"messages": {
"bemDoubleDash": "{{actual}} has been retired. Update it to the new name {{newValue}}.",
"fixBemNaming": "Update to correct BEM naming convention"
}
},
"modal-close-button-issue": {
"description": "Update component attributes or CSS classes for the modal close button to comply with the modal component blueprint",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#modal-close-button-issue",
"type": "problem",
"messages": {
"modalCloseButtonIssue": "Update component attributes or CSS classes for the modal close button to comply with the modal component blueprint.",
"removeClass": "Remove the slds-button_icon-inverse class from the modal close button in components that use the SLDS modal blueprint.",
"changeVariant": "Change the variant attribute value from bare-inverse to bare in <lightning-button-icon> or <lightning-icon>.",
"removeVariant": "Remove the variant attribute from the <lightning-icon> component inside the <button> element.",
"ensureButtonClasses": "Add or move slds-button and slds-button_icon to the class attribute of the <button> element or <lightning-button-icon> component.",
"ensureSizeAttribute": "To size icons properly, set the size attribute \u200Cto large in the <lightning-icon> and <lightning-button-icon> components."
}
},
"no-deprecated-classes-slds2": {
"description": "Replace classes that aren't available with SLDS 2 classes",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#no-deprecated-classes-slds2",
"type": "problem",
"messages": {
"deprecatedClass": "The class {{className}} isn't available in SLDS 2. Update it to a class supported in SLDS 2. See lightningdesignsystem.com for more information.",
"updateToModernClass": "Replace deprecated class with modern equivalent",
"checkDocumentation": "See lightningdesignsystem.com for SLDS 2 class alternatives"
}
},
"lwc-token-to-slds-hook": {
"description": "Replace the deprecated --lwc tokens with the latest --slds tokens. See lightningdesignsystem.com for more info.",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#lwc-token-to-slds-hook",
"type": "problem",
"messages": {
"errorWithReplacement": "The '{{oldValue}}' design token is deprecated. Replace it with '{{newValue}}'. For more info, see Global Styling Hooks on lightningdesignsystem.com.",
"errorWithStyleHooks": "The '{{oldValue}}' design token is deprecated. Replace it with the SLDS 2 '{{newValue}}' styling hook and set the fallback to '{{oldValue}}'. For more info, see Global Styling Hooks on lightningdesignsystem.com.",
"errorWithNoRecommendation": "The '{{oldValue}}' design token is deprecated. For more info, see the New Global Styling Hook Guidance on lightningdesignsystem.com."
}
},
"no-sldshook-fallback-for-lwctoken": {
"description": "Avoid using --slds styling hooks as fallback values for --lwc tokens.",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#no-sldshook-fallback-for-lwctoken",
"type": "problem",
"messages": {
"unsupportedFallback": "Remove the {{sldsToken}} styling hook that is used as a fallback value for {{lwcToken}}."
}
},
"no-unsupported-hooks-slds2": {
"description": "Identifies styling hooks that aren't present in SLDS 2. They must be replaced with styling hooks that have a similar effect, or they must be removed.",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#no-unsupported-hooks-slds2",
"type": "problem",
"messages": {
"deprecated": "The {{token}} styling hook isn't present in SLDS 2 and there's no equivalent replacement. Remove it or replace it with a styling hook with a similar effect."
}
},
"no-slds-var-without-fallback": {
"description": "Add fallback values to SLDS styling hooks. The fallback values are used in Salesforce environments where styling hooks are unavailable.",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#no-slds-var-without-fallback",
"type": "problem",
"messages": {
"varWithoutFallback": "Your code uses the {{cssVar}} styling hook without a fallback value. Styling hooks are unavailable in some Salesforce environments. To render your component correctly in all environments, add this fallback value: var({{cssVar}}, {{recommendation}}). To make this fallback value brand-aware, use a branded design token instead of a static value. See Design Tokens on v1.lightningdesignsystem.com."
}
},
"no-slds-namespace-for-custom-hooks": {
"description": "To differentiate custom styling hooks from SLDS styling hooks, create custom styling hooks in your namespace.",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#no-slds-namespace-for-custom-hooks",
"type": "problem",
"messages": {
"customHookNamespace": "Using the --slds namespace for {{token}} isn't supported. Create the custom styling hook in your namespace. Example: --myapp-{{tokenWithoutNamespace}}"
}
},
"no-slds-private-var": {
"description": "Some SLDS styling hooks are private and reserved only for internal Salesforce use. Private SLDS styling hooks have prefixes --_slds- and --slds-s-. For more information, look up private CSS in lightningdesignsystem.com.",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#no-slds-private-var",
"type": "problem",
"messages": {
"privateVar": "This styling hook is reserved for internal Salesforce use. Remove the --_slds- or \u2013slds-s private variable within selector {{prop}}. For more information, look up private CSS in lightningdesignsystem.com."
}
},
"enforce-component-hook-naming-convention": {
"description": "Replace component styling hooks that use a deprecated naming convention.",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#enforce-component-hook-naming-convention",
"type": "problem",
"messages": {
"replace": "Replace the deprecated {{oldValue}} component styling hook with {{suggestedMatch}}."
}
},
"no-hardcoded-values-slds1": {
"description": "Replace static values with SLDS 1 design tokens. For more information, look up design tokens on lightningdesignsystem.com.",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#no-hardcoded-value",
"type": "suggestion",
"messages": {
"hardcodedValue": "Replace the {{oldValue}} static value with an SLDS 1 styling hook: {{newValue}}.",
"noReplacement": "There's no replacement styling hook for the {{oldValue}} static value. Remove the static value."
}
},
"no-hardcoded-values-slds2": {
"description": "Replace static values with SLDS 2 styling hooks. For more information, look up design tokens on lightningdesignsystem.com.",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#no-hardcoded-values-slds2",
"type": "suggestion",
"messages": {
"hardcodedValue": "Consider replacing the {{oldValue}} static value with an SLDS 2 styling hook that has a similar value: {{newValue}}.",
"noReplacement": "There's no replacement styling hook for the {{oldValue}} static value. Remove the static value."
}
},
"reduce-annotations": {
"description": "Remove your annotations and update your code.",
"url": "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#reduce-annotations",
"type": "problem",
"messages": {
"removeAnnotation": "Remove this annotation and update the code to SLDS best practices. For help, file an issue at https://github.com/salesforce-ux/slds-linter/"
}
}
};
}
});
// src/rules/v9/no-hardcoded-values/no-hardcoded-values-slds2.ts
var no_hardcoded_values_slds2_exports = {};
__export(no_hardcoded_values_slds2_exports, {
default: () => no_hardcoded_values_slds2_default
});
module.exports = __toCommonJS(no_hardcoded_values_slds2_exports);
var import_sds_metadata = __toESM(require("@salesforce-ux/sds-metadata"));
var import_rule_messages = __toESM(require_rule_messages());
// src/utils/color-lib-utils.ts
var import_chroma_js = __toESM(require("chroma-js"));
var import_css_tree = require("@eslint/css-tree");
// src/utils/css-functions.ts
var CSS_FUNCTIONS = [
"attr",
"calc",
"color-mix",
"conic-gradient",
"counter",
"cubic-bezier",
"linear-gradient",
"max",
"min",
"radial-gradient",
"repeating-conic-gradient",
"repeating-linear-gradient",
"repeating-radial-gradient",
"var"
];
var CSS_MATH_FUNCTIONS = ["calc", "min", "max"];
var RGB_COLOR_FUNCTIONS = ["rgb", "rgba", "hsl", "hsla"];
var cssFunctionsRegex = new RegExp(`(?:${CSS_FUNCTIONS.join("|")})`);
var cssFunctionsExactRegex = new RegExp(`^(?:${CSS_FUNCTIONS.join("|")})$`);
var cssMathFunctionsRegex = new RegExp(`^(?:${CSS_MATH_FUNCTIONS.join("|")})$`);
function isCssFunction(value) {
return cssFunctionsExactRegex.test(value);
}
function isCssColorFunction(value) {
return RGB_COLOR_FUNCTIONS.includes(value);
}
// src/utils/color-lib-utils.ts
var LAB_THRESHOLD = 25;
var isHexCode = (color) => {
const hexPattern = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
return hexPattern.test(color);
};
var convertToHex = (color) => {
try {
return (0, import_chroma_js.default)(color).hex();
} catch (e) {
return null;
}
};
var findClosestColorHook = (color, supportedColors, cssProperty) => {
const returnStylingHooks = [];
const closestHooksWithSameProperty = [];
const closestHooksWithoutSameProperty = [];
const closestHooksWithAllProperty = [];
const labColor = (0, import_chroma_js.default)(color).lab();
Object.entries(supportedColors).forEach(([sldsValue, data]) => {
if (sldsValue && isHexCode(sldsValue)) {
const hooks = data;
hooks.forEach((hook) => {
const labSupportedColor = (0, import_chroma_js.default)(sldsValue).lab();
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");
if (hook.properties.includes(cssProperty)) {
if (distance <= LAB_THRESHOLD) {
closestHooksWithSameProperty.push({ name: hook.name, distance });
}
} else if (hook.properties.includes("*")) {
if (distance <= LAB_THRESHOLD) {
closestHooksWithAllProperty.push({ name: hook.name, distance });
}
} else {
if (distance <= LAB_THRESHOLD) {
closestHooksWithoutSameProperty.push({ name: hook.name, distance });
}
}
});
}
});
const closesthookGroups = [
{ hooks: closestHooksWithSameProperty, distance: 0 },
{ hooks: closestHooksWithAllProperty, distance: 0 },
{ hooks: closestHooksWithSameProperty, distance: Infinity },
// For hooks with distance > 0
{ hooks: closestHooksWithAllProperty, distance: Infinity },
{ hooks: closestHooksWithoutSameProperty, distance: Infinity }
];
for (const group of closesthookGroups) {
const filteredHooks = group.hooks.filter(
(h) => group.distance === 0 ? h.distance === 0 : h.distance > 0
);
if (returnStylingHooks.length < 1 && filteredHooks.length > 0) {
filteredHooks.sort((a, b) => a.distance - b.distance);
returnStylingHooks.push(...filteredHooks.slice(0, 5).map((h) => h.name));
}
}
return Array.from(new Set(returnStylingHooks));
};
var isValidColor = (val) => import_chroma_js.default.valid(val);
var extractColorValue = (node) => {
let colorValue = null;
switch (node.type) {
case "Hash":
colorValue = `#${node.value}`;
break;
case "Identifier":
colorValue = node.name;
break;
case "Function":
if (isCssColorFunction(node.name)) {
colorValue = (0, import_css_tree.generate)(node);
}
break;
}
return colorValue && isValidColor(colorValue) ? colorValue : null;
};
// src/utils/property-matcher.ts
var DIRECTION_VALUES = "(?:top|right|bottom|left|inline|block|inline-start|inline-end|start|end|block-start|block-end)";
var CORNER_VALUES = "(?:top-left|top-right|bottom-right|bottom-left|start-start|start-end|end-start|end-end)";
var INSET_VALUES = "(?:inline|block|inline-start|inline-end|block-start|block-end)";
var BORDER_COLOR_REGEX = new RegExp(`^border(?:-${DIRECTION_VALUES})?-color$`);
var BORDER_WIDTH_REGEX = new RegExp(`^border(?:-${DIRECTION_VALUES})?-width$`);
var MARGIN_REGEX = new RegExp(`^margin(?:-${DIRECTION_VALUES})?$`);
var PADDING_REGEX = new RegExp(`^padding(?:-${DIRECTION_VALUES})?$`);
var BORDER_RADIUS_REGEX = new RegExp(`^border(?:-${CORNER_VALUES})?-radius$`);
var INSET_REGEX = new RegExp(`^inset(?:-${INSET_VALUES})?$`);
function isBorderColorProperty(cssProperty) {
return BORDER_COLOR_REGEX.test(cssProperty);
}
function isBorderWidthProperty(cssProperty) {
return BORDER_WIDTH_REGEX.test(cssProperty);
}
function isMarginProperty(cssProperty) {
return MARGIN_REGEX.test(cssProperty);
}
function isPaddingProperty(cssProperty) {
return PADDING_REGEX.test(cssProperty);
}
function isBorderRadius(cssProperty) {
return BORDER_RADIUS_REGEX.test(cssProperty);
}
function isDimensionProperty(cssProperty) {
return ["width", "height", "min-width", "max-width", "min-height", "max-height"].includes(cssProperty);
}
function isInsetProperty(cssProperty) {
return INSET_REGEX.test(cssProperty);
}
var fontProperties = [
"font",
"font-size",
"font-weight"
];
var colorProperties = [
"color",
"fill",
"background",
"background-color",
"stroke",
"border",
"border*",
"border*-color",
"outline",
"outline-color"
];
var densificationProperties = [
"border*",
"margin*",
"padding*",
"width",
"height",
"min-width",
"max-width",
"min-height",
"max-height",
"inset",
"top",
"right",
"left",
"bottom",
"outline",
"outline-width",
"line-height"
];
function toSelector(properties) {
const selectorParts = properties.map((prop) => {
if (prop.includes("*")) {
const regexPattern = prop.replace(/\*/g, ".*");
return `Declaration[property=/^${regexPattern}$/]`;
} else {
return `Declaration[property='${prop}']`;
}
});
return selectorParts.join(", ");
}
function resolvePropertyToMatch(cssProperty) {
const propertyToMatch = cssProperty.toLowerCase();
if (propertyToMatch === "outline" || propertyToMatch === "outline-width" || isBorderWidthProperty(propertyToMatch)) {
return "border-width";
} else if (isMarginProperty(propertyToMatch)) {
return "margin";
} else if (isPaddingProperty(propertyToMatch)) {
return "padding";
} else if (isBorderRadius(propertyToMatch)) {
return "border-radius";
} else if (isDimensionProperty(propertyToMatch)) {
return "width";
} else if (isInsetProperty(propertyToMatch)) {
return "top";
} else if (cssProperty === "background" || cssProperty === "background-color") {
return "background-color";
} else if (cssProperty === "outline" || cssProperty === "outline-color" || isBorderColorProperty(cssProperty)) {
return "border-color";
}
return propertyToMatch;
}
// src/utils/hardcoded-shared-utils.ts
var import_css_tree2 = require("@eslint/css-tree");
// src/utils/value-utils.ts
var ALLOWED_UNITS = ["px", "em", "rem", "%", "ch"];
function parseUnitValue(value) {
if (!value) return null;
const unitsPattern = ALLOWED_UNITS.join("|");
const regex = new RegExp(`^(-?\\d*\\.?\\d+)(${unitsPattern})?$`);
const match = value.match(regex);
if (!match) return null;
const number = parseFloat(match[1]);
const unit = match[2] ? match[2] : null;
if (isNaN(number)) return null;
return { number, unit };
}
function toAlternateUnitValue(numberVal, unitType) {
if (unitType === "px") {
let floatValue = parseFloat(`${numberVal / 16}`);
if (!isNaN(floatValue)) {
return {
unit: "rem",
number: parseFloat(floatValue.toFixed(4))
};
}
} else if (unitType === "rem") {
const intValue = parseInt(`${numberVal * 16}`);
if (!isNaN(intValue)) {
return {
unit: "px",
number: intValue
};
}
}
return null;
}
// src/utils/hardcoded-shared-utils.ts
var FONT_WEIGHTS = [
"normal",
"bold",
"bolder",
"lighter",
"100",
"200",
"300",
"400",
"500",
"600",
"700",
"800",
"900"
];
function isKnownFontWeight(value) {
const stringValue = value.toString();
return FONT_WEIGHTS.includes(stringValue.toLowerCase());
}
function handleShorthandAutoFix(declarationNode, context, valueText, replacements) {
const sortedReplacements = replacements.sort((a, b) => a.start - b.start);
const hasAnyHooks = sortedReplacements.some((r) => r.hasHook);
const canAutoFix = hasAnyHooks;
sortedReplacements.forEach(({ start, end, replacement, displayValue, hasHook }) => {
const originalValue = valueText.substring(start, end);
const valueStartColumn = declarationNode.value.loc.start.column;
const valueColumn = valueStartColumn + start;
const { loc: { start: locStart, end: locEnd } } = declarationNode.value;
const reportNode = {
...declarationNode.value,
loc: {
...declarationNode.value.loc,
start: {
...locStart,
column: valueColumn
},
end: {
...locEnd,
column: valueColumn + originalValue.length
}
}
};
if (hasHook) {
const fix = canAutoFix ? (fixer) => {
let newValue = valueText;
for (let i = sortedReplacements.length - 1; i >= 0; i--) {
const { start: rStart, end: rEnd, replacement: rReplacement } = sortedReplacements[i];
newValue = newValue.substring(0, rStart) + rReplacement + newValue.substring(rEnd);
}
return fixer.replaceText(declarationNode.value, newValue);
} : void 0;
context.context.report({
node: reportNode,
messageId: "hardcodedValue",
data: {
oldValue: originalValue,
newValue: displayValue
},
fix
});
} else {
context.context.report({
node: reportNode,
messageId: "noReplacement",
data: {
oldValue: originalValue
}
});
}
});
}
function forEachValue(valueText, extractValue, shouldSkipNode, callback) {
if (!valueText || typeof valueText !== "string") {
return;
}
try {
const ast = (0, import_css_tree2.parse)(valueText, { context: "value", positions: true });
(0, import_css_tree2.walk)(ast, {
enter(node) {
if (shouldSkipNode(node)) {
return this.skip;
}
const value = extractValue(node);
if (value !== null) {
const positionInfo = {
start: node.loc?.start,
end: node.loc?.end
};
callback(value, positionInfo);
}
}
});
} catch (error) {
return;
}
}
function shouldSkipColorNode(node) {
return node.type === "Function" && isCssFunction(node.name);
}
function shouldSkipDimensionNode(node) {
return node.type === "Function";
}
function extractDimensionValue(valueNode, cssProperty) {
if (!valueNode) return null;
switch (valueNode.type) {
case "Dimension":
const numValue = Number(valueNode.value);
if (numValue === 0) return null;
const unit = valueNode.unit.toLowerCase();
if (!ALLOWED_UNITS.includes(unit)) return null;
return {
number: numValue,
unit
};
case "Number":
const numberValue = Number(valueNode.value);
if (numberValue === 0) return null;
return {
number: numberValue,
unit: null
};
case "Percentage":
const percentValue = Number(valueNode.value);
if (percentValue === 0) return null;
return {
number: percentValue,
unit: "%"
};
case "Value":
return valueNode.children?.[0] ? extractDimensionValue(valueNode.children[0], cssProperty) : null;
}
return null;
}
function forEachColorValue(valueText, callback) {
forEachValue(valueText, extractColorValue, shouldSkipColorNode, callback);
}
function forEachDensityValue(valueText, cssProperty, callback) {
forEachValue(
valueText,
(node) => extractDimensionValue(node, cssProperty),
shouldSkipDimensionNode,
callback
);
}
function extractFontValue(node) {
if (!node) return null;
switch (node.type) {
case "Dimension":
const numValue = Number(node.value);
if (numValue <= 0) return null;
const unit = node.unit.toLowerCase();
if (!ALLOWED_UNITS.includes(unit)) return null;
return {
number: numValue,
unit
};
case "Number":
const numberValue = Number(node.value);
if (numberValue <= 0) {
return null;
}
if (!isKnownFontWeight(numberValue)) {
return null;
}
return {
number: numberValue,
unit: null
};
case "Identifier":
const namedValue = node.name.toLowerCase();
if (!isKnownFontWeight(namedValue)) {
return null;
}
if (namedValue === "normal") {
return { number: 400, unit: null };
}
return { number: namedValue, unit: null };
case "Percentage":
const percentValue = Number(node.value);
if (percentValue === 0) return null;
return {
number: percentValue,
unit: "%"
};
case "Value":
return node.children?.[0] ? extractFontValue(node.children[0]) : null;
}
return null;
}
function shouldSkipFontNode(node) {
return node.type === "Function";
}
function forEachFontValue(valueText, callback) {
forEachValue(valueText, extractFontValue, shouldSkipFontNode, callback);
}
// src/utils/css-utils.ts
function formatSuggestionHooks(hooks) {
if (hooks.length === 1) {
return `${hooks[0]}`;
}
return "\n" + hooks.map((hook, index) => `${index + 1}. ${hook}`).join("\n");
}
// src/rules/v9/no-hardcoded-values/handlers/colorHandler.ts
var handleColorDeclaration = (node, context) => {
const cssProperty = node.property.toLowerCase();
const valueText = context.sourceCode.getText(node.value);
const replacements = [];
forEachColorValue(valueText, (colorValue, positionInfo) => {
if (colorValue !== "transparent" && isValidColor(colorValue)) {
const replacement = createColorReplacement(colorValue, cssProperty, context, positionInfo, valueText);
if (replacement) {
replacements.push(replacement);
}
}
});
handleShorthandAutoFix(node, context, valueText, replacements);
};
function createColorReplacement(colorValue, cssProperty, context, positionInfo, originalValueText) {
if (!positionInfo?.start) {
return null;
}
const hexValue = convertToHex(colorValue);
if (!hexValue) {
return null;
}
const propToMatch = resolvePropertyToMatch(cssProperty);
const closestHooks = findClosestColorHook(hexValue, context.valueToStylinghook, propToMatch);
const start = positionInfo.start.offset;
const end = positionInfo.end.offset;
const originalValue = originalValueText ? originalValueText.substring(start, end) : colorValue;
if (closestHooks.length === 1) {
return {
start,
end,
replacement: `var(${closestHooks[0]}, ${colorValue})`,
displayValue: closestHooks[0],
hasHook: true
};
} else if (closestHooks.length > 1) {
return {
start,
end,
replacement: originalValue,
// Use original value to preserve spacing
displayValue: formatSuggestionHooks(closestHooks),
hasHook: true
};
} else {
return {
start,
end,
replacement: originalValue,
// Use original value to preserve spacing
displayValue: originalValue,
hasHook: false
};
}
}
// src/utils/styling-hook-utils.ts
function isValueMatch(valueToMatch, sldsValue) {
if (!valueToMatch || !sldsValue) {
return false;
}
return valueToMatch.unit == sldsValue.unit && valueToMatch.number === sldsValue.number;
}
function getStylingHooksForDensityValue(parsedValue, supportedStylinghooks, cssProperty) {
if (!parsedValue) return [];
const alternateValue = toAlternateUnitValue(parsedValue.number, parsedValue.unit);
const matchedHooks = [];
for (const [sldsValue, hooks] of Object.entries(supportedStylinghooks)) {
const parsedSldsValue = parseUnitValue(sldsValue);
if (isValueMatch(parsedValue, parsedSldsValue) || alternateValue && isValueMatch(alternateValue, parsedSldsValue)) {
hooks.filter((hook) => hook.properties.includes(cssProperty)).forEach((hook) => matchedHooks.push(hook.name));
}
}
return matchedHooks;
}
// src/rules/v9/no-hardcoded-values/handlers/densityHandler.ts
var handleDensityDeclaration = (node, context) => {
const cssProperty = node.property.toLowerCase();
const valueText = context.sourceCode.getText(node.value);
const replacements = [];
forEachDensityValue(valueText, cssProperty, (parsedDimension, positionInfo) => {
if (parsedDimension) {
const replacement = createDimensionReplacement(parsedDimension, cssProperty, context, positionInfo);
if (replacement) {
replacements.push(replacement);
}
}
});
handleShorthandAutoFix(node, context, valueText, replacements);
};
function createDimensionReplacement(parsedDimension, cssProperty, context, positionInfo) {
if (!parsedDimension || !positionInfo?.start) {
return null;
}
const rawValue = parsedDimension.unit ? `${parsedDimension.number}${parsedDimension.unit}` : parsedDimension.number.toString();
const propToMatch = resolvePropertyToMatch(cssProperty);
const closestHooks = getStylingHooksForDensityValue(parsedDimension, context.valueToStylinghook, propToMatch);
const start = positionInfo.start.offset;
const end = positionInfo.end.offset;
if (closestHooks.length === 1) {
return {
start,
end,
replacement: `var(${closestHooks[0]}, ${rawValue})`,
displayValue: closestHooks[0],
hasHook: true
};
} else if (closestHooks.length > 1) {
return {
start,
end,
replacement: rawValue,
displayValue: formatSuggestionHooks(closestHooks),
hasHook: true
};
} else {
return {
start,
end,
replacement: rawValue,
displayValue: rawValue,
hasHook: false
};
}
}
// src/rules/v9/no-hardcoded-values/handlers/fontHandler.ts
var handleFontDeclaration = (node, context) => {
const cssProperty = node.property.toLowerCase();
const valueText = context.sourceCode.getText(node.value);
const replacements = [];
forEachFontValue(valueText, (fontValue, positionInfo) => {
if (fontValue && isValidFontValue(fontValue, cssProperty)) {
const replacement = createFontReplacement(fontValue, cssProperty, context, positionInfo);
if (replacement) {
replacements.push(replacement);
}
}
});
handleShorthandAutoFix(node, context, valueText, replacements);
};
function isValidFontValue(fontValue, cssProperty) {
if (cssProperty === "font-size") {
return !!fontValue.unit;
} else if (cssProperty === "font-weight") {
return !fontValue.unit && isKnownFontWeight(fontValue.number);
} else if (cssProperty === "font") {
if (!fontValue.unit && isKnownFontWeight(fontValue.number)) {
return true;
} else if (fontValue.unit) {
return true;
} else {
return false;
}
}
return false;
}
function createFontReplacement(fontValue, cssProperty, context, positionInfo) {
if (!positionInfo?.start) {
return null;
}
const rawValue = fontValue.unit ? `${fontValue.number}${fontValue.unit}` : fontValue.number.toString();
const propToMatch = !fontValue.unit && isKnownFontWeight(fontValue.number) ? resolvePropertyToMatch("font-weight") : resolvePropertyToMatch("font-size");
const closestHooks = getStylingHooksForDensityValue(fontValue, context.valueToStylinghook, propToMatch);
const start = positionInfo.start.offset;
const end = positionInfo.end.offset;
if (closestHooks.length === 1) {
return {
start,
end,
replacement: `var(${closestHooks[0]}, ${rawValue})`,
displayValue: closestHooks[0],
hasHook: true
};
} else if (closestHooks.length > 1) {
return {
start,
end,
replacement: rawValue,
displayValue: formatSuggestionHooks(closestHooks),
hasHook: true
};
} else {
return {
start,
end,
replacement: rawValue,
displayValue: rawValue,
hasHook: false
};
}
}
// src/utils/boxShadowValueParser.ts
var import_css_tree3 = require("@eslint/css-tree");
function isColorValue(node) {
if (!node) return false;
switch (node.type) {
case "Hash":
return true;
// #hex colors
case "Identifier":
return isValidColor(node.name);
case "Function":
return isCssColorFunction(node.name.toLowerCase());
default:
return false;
}
}
function isLengthValue(node) {
if (!node) return false;
switch (node.type) {
case "Dimension":
const dimensionStr = `${node.value}${node.unit}`;
return parseUnitValue(dimensionStr) !== null;
case "Number":
return Number(node.value) === 0;
default:
return false;
}
}
function isInsetKeyword(node) {
return node?.type === "Identifier" && node.name.toLowerCase() === "inset";
}
function extractShadowParts(valueText) {
const shadows = [];
let currentShadow = {
lengthParts: [],
colorParts: [],
inset: false
};
try {
const ast = (0, import_css_tree3.parse)(valueText, { context: "value" });
(0, import_css_tree3.walk)(ast, {
enter(node) {
if (node.type === "Function") {
return this.skip;
}
if (isInsetKeyword(node)) {
currentShadow.inset = true;
} else if (isLengthValue(node)) {
currentShadow.lengthParts.push((0, import_css_tree3.generate)(node));
} else if (isColorValue(node)) {
currentShadow.colorParts.push((0, import_css_tree3.generate)(node));
}
}
});
if (currentShadow.lengthParts.length > 0 || currentShadow.colorParts.length > 0 || currentShadow.inset) {
shadows.push(currentShadow);
}
} catch (error) {
return [];
}
return shadows;
}
function parseBoxShadowValue(value) {
const shadowStrings = value.split(",").map((s) => s.trim());
const allShadows = [];
for (const shadowString of shadowStrings) {
const shadows = extractShadowParts(shadowString);
const parsedShadows = shadows.map((shadow) => {
const shadowValue = {};
const lengthProps = ["offsetX", "offsetY", "blurRadius", "spreadRadius"];
lengthProps.forEach((prop, index) => {
if (shadow.lengthParts.length > index) {
shadowValue[prop] = shadow.lengthParts[index];
}
});
if (shadow.colorParts.length > 0) {
shadowValue.color = shadow.colorParts[0];
}
if (shadow.inset) {
shadowValue.inset = true;
}
return shadowValue;
});
allShadows.push(...parsedShadows);
}
return allShadows;
}
function normalizeLengthValue(value) {
if (!value) return "0px";
if (value === "0") return "0px";
return value;
}
function isBoxShadowMatch(parsedCssValue, parsedValueHook) {
if (parsedCssValue.length !== parsedValueHook.length) {
return false;
}
for (let i = 0; i < parsedCssValue.length; i++) {
const cssShadow = parsedCssValue[i];
const hookShadow = parsedValueHook[i];
if (cssShadow.color !== hookShadow.color || cssShadow.inset !== hookShadow.inset) {
return false;
}
const lengthProps = ["offsetX", "offsetY", "blurRadius", "spreadRadius"];
for (const prop of lengthProps) {
if (normalizeLengthValue(cssShadow[prop]) !== normalizeLengthValue(hookShadow[prop])) {
return false;
}
}
}
return true;
}
// src/rules/v9/no-hardcoded-values/handlers/boxShadowHandler.ts
function toBoxShadowValue(cssValue) {
const parsedCssValue = parseBoxShadowValue(cssValue).filter((shadow) => Object.keys(shadow).length > 0);
if (parsedCssValue.length === 0) {
return null;
}
return parsedCssValue;
}
function shadowValueToHookEntries(supportedStylinghooks) {
return Object.entries(supportedStylinghooks).filter(([key, value]) => {
return value.some((hook) => hook.properties.includes("box-shadow"));
}).map(([key, value]) => {
return [key, value.map((hook) => hook.name)];
});
}
var handleBoxShadowDeclaration = (node, context) => {
const cssProperty = node.property.toLowerCase();
const valueText = context.sourceCode.getText(node.value);
const shadowHooks = shadowValueToHookEntries(context.valueToStylinghook);
const parsedCssValue = toBoxShadowValue(valueText);
if (!parsedCssValue) {
return;
}
for (const [shadow, closestHooks] of shadowHooks) {
const parsedValueHook = toBoxShadowValue(shadow);
if (parsedValueHook && isBoxShadowMatch(parsedCssValue, parsedValueHook)) {
if (closestHooks.length > 0) {
const positionInfo = {
start: { offset: 0, line: 1, column: 1 },
end: { offset: valueText.length, line: 1, column: valueText.length + 1 }
};
const replacement = createBoxShadowReplacement(
valueText,
closestHooks,
context,
positionInfo
);
if (replacement) {
const replacements = [replacement];
handleShorthandAutoFix(node, context, valueText, replacements);
}
}
return;
}
}
};
function createBoxShadowReplacement(originalValue, hooks, context, positionInfo) {
if (!positionInfo?.start) {
return null;
}
const start = positionInfo.start.offset;
const end = positionInfo.end.offset;
if (hooks.length === 1) {
return {
start,
end,
replacement: `var(${hooks[0]}, ${originalValue})`,
displayValue: hooks[0],
hasHook: true
};
} else {
return {
start,
end,
replacement: originalValue,
displayValue: formatSuggestionHooks(hooks),
hasHook: true
};
}
}
// src/utils/rule-utils.ts
function isRuleEnabled(context, ruleName2) {
try {
const rules = context.settings?.sldsRules || {};
if (ruleName2 in rules) {
const ruleConfig2 = rules[ruleName2];
if (Array.isArray(ruleConfig2)) {
return ruleConfig2[0] === true;
} else if (ruleConfig2 !== void 0 && ruleConfig2 !== null) {
return true;
} else if (ruleConfig2 === false) {
return false;
}
}
} catch (error) {
return false;
}
}
// src/rules/v9/no-hardcoded-values/noHardcodedValueRule.ts
function defineNoHardcodedValueRule(config) {
const { ruleConfig: ruleConfig2, ruleName: ruleName2 } = config;
const { type: type2, description: description2, url: url2, messages: messages2 } = ruleConfig2;
return {
meta: {
type: type2,
docs: {
description: description2,
recommended: true,
url: url2
},
fixable: "code",
messages: messages2
},
create(context) {
if (ruleName2 === "no-hardcoded-values-slds1" && isRuleEnabled(context, "@salesforce-ux/slds/no-hardcoded-values-slds2")) {
return {};
}
const handlerContext = {
valueToStylinghook: config.valueToStylinghook,
context,
sourceCode: context.sourceCode
};
const colorOnlySelector = toSelector(colorProperties);
const densityOnlySelector = toSelector(densificationProperties);
const fontDensitySelector = toSelector(fontProperties);
const overlappingProperties = colorProperties.filter((colorProp) => {
return densificationProperties.some((densityProp) => {
if (densityProp === colorProp) {
return true;
}
if (densityProp.includes("*")) {
const regexPattern = new RegExp("^" + densityProp.replace(/\*/g, ".*") + "$");
return regexPattern.test(colorProp);
}
return false;
});
});
const overlappingSet = new Set(overlappingProperties);
const colorOnlyProps = colorProperties.filter((prop) => !overlappingSet.has(prop));
const densityOnlyProps = densificationProperties.filter((prop) => !overlappingSet.has(prop));
const visitors = {};
if (colorOnlyProps.length > 0) {
const colorOnlySelector2 = toSelector(colorOnlyProps);
visitors[colorOnlySelector2] = (node) => {
handleColorDeclaration(node, handlerContext);
};
}
if (densityOnlyProps.length > 0) {
const densityOnlySelector2 = toSelector(densityOnlyProps);
visitors[densityOnlySelector2] = (node) => {
handleDensityDeclaration(node, handlerContext);
};
}
visitors[fontDensitySelector] = (node) => {
handleFontDeclaration(node, handlerContext);
};
visitors['Declaration[property="box-shadow"]'] = (node) => {
handleBoxShadowDeclaration(node, handlerContext);
};
if (overlappingProperties.length > 0) {
const overlappingSelector = toSelector(overlappingProperties);
visitors[overlappingSelector] = (node) => {
handleColorDeclaration(node, handlerContext);
handleDensityDeclaration(node, handlerContext);
};
}
return visitors;
}
};
}
// src/rules/v9/no-hardcoded-values/no-hardcoded-values-slds2.ts
var ruleName = "no-hardcoded-values-slds2";
var ruleConfig = import_rule_messages.default[ruleName];
var { type, description, url, messages } = ruleConfig;
var valueToStylinghook = import_sds_metadata.default.valueToStylingHooksCosmos;
var no_hardcoded_values_slds2_default = defineNoHardcodedValueRule({
ruleConfig,
valueToStylinghook,
ruleName
});
//# sourceMappingURL=no-hardcoded-values-slds2.js.map