@salesforce-ux/eslint-plugin-slds
Version:
ESLint plugin provides custom linting rules specifically built for Salesforce Lightning Design System 2 (SLDS 2 beta)
84 lines (80 loc) • 3.02 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/utils/styling-hook-utils.ts
var styling_hook_utils_exports = {};
__export(styling_hook_utils_exports, {
getStylingHooksForDensityValue: () => getStylingHooksForDensityValue
});
module.exports = __toCommonJS(styling_hook_utils_exports);
// 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/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;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getStylingHooksForDensityValue
});
//# sourceMappingURL=styling-hook-utils.js.map