UNPKG

@salesforce-ux/eslint-plugin-slds

Version:

ESLint plugin provides custom linting rules specifically built for Salesforce Lightning Design System 2 (SLDS 2 beta)

964 lines (946 loc) 31.1 kB
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 __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); // src/rules/v9/no-hardcoded-values/noHardcodedValueRule.ts var noHardcodedValueRule_exports = {}; __export(noHardcodedValueRule_exports, { defineNoHardcodedValueRule: () => defineNoHardcodedValueRule }); module.exports = __toCommonJS(noHardcodedValueRule_exports); // 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, ruleName) { try { const rules = context.settings?.sldsRules || {}; if (ruleName in rules) { const ruleConfig = rules[ruleName]; if (Array.isArray(ruleConfig)) { return ruleConfig[0] === true; } else if (ruleConfig !== void 0 && ruleConfig !== null) { return true; } else if (ruleConfig === false) { return false; } } } catch (error) { return false; } } // src/rules/v9/no-hardcoded-values/noHardcodedValueRule.ts function defineNoHardcodedValueRule(config) { const { ruleConfig, ruleName } = config; const { type, description, url, messages } = ruleConfig; return { meta: { type, docs: { description, recommended: true, url }, fixable: "code", messages }, create(context) { if (ruleName === "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; } }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { defineNoHardcodedValueRule }); //# sourceMappingURL=noHardcodedValueRule.js.map