UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

49 lines (48 loc) 1.6 kB
import { isNodeOfType } from 'eslint-codemod-utils'; var referenceObject = { width: '1px', height: '1px', padding: '0', position: 'absolute', border: '0', clip: 'rect(1px, 1px, 1px, 1px)', overflow: 'hidden', whiteSpace: 'nowrap' }; /** * Given a node, translate the node into css key-value pairs and * compare the output to the reference styles required to make a * visually hidden element. * * @returns {number} A fraction between 0-1 depending on the object's likeness. */ export var getObjectLikeness = function getObjectLikeness(node) { var styleEntries = node.properties.filter(function (node) { return isNodeOfType(node, 'Property'); }).map(function (_ref) { var key = _ref.key, value = _ref.value; if (key.type === 'Identifier') { return { key: key.name, value: value.type === 'Literal' && value.value }; } return null; }).filter(function (node) { return Boolean(node); }); return countMatchingKeyValues(styleEntries); }; // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports export var countMatchingKeyValues = function countMatchingKeyValues(styleEntries) { var matchingStyleEntries = styleEntries.filter(function (entry) { return entry.key in referenceObject; }); if (styleEntries.length < 5) { return 0; } return matchingStyleEntries.reduce(function (acc, curr) { return acc + (referenceObject[curr === null || curr === void 0 ? void 0 : curr.key] === (curr === null || curr === void 0 ? void 0 : curr.value) ? 1.5 : 0.75); }, 0) / styleEntries.length; };