UNPKG

@salesforce-ux/eslint-plugin-slds

Version:

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

161 lines (157 loc) 6.03 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/utils/color-lib-utils.ts var color_lib_utils_exports = {}; __export(color_lib_utils_exports, { convertToHex: () => convertToHex, extractColorValue: () => extractColorValue, findClosestColorHook: () => findClosestColorHook, isHardCodedColor: () => isHardCodedColor, isHexCode: () => isHexCode, isValidColor: () => isValidColor }); module.exports = __toCommonJS(color_lib_utils_exports); 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 isCssColorFunction(value) { return RGB_COLOR_FUNCTIONS.includes(value); } // src/utils/color-lib-utils.ts var LAB_THRESHOLD = 25; var isHardCodedColor = (color) => { const colorRegex = /\b(rgb|rgba)\((\s*\d{1,3}\s*,\s*){2,3}\s*(0|1|0?\.\d+)\s*\)|#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\b|[a-zA-Z]+/g; return colorRegex.test(color); }; 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; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { convertToHex, extractColorValue, findClosestColorHook, isHardCodedColor, isHexCode, isValidColor }); //# sourceMappingURL=color-lib-utils.js.map