@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
44 lines (43 loc) • 1.44 kB
JavaScript
import { isNodeOfType } from 'eslint-codemod-utils';
const 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 const getObjectLikeness = node => {
const styleEntries = node.properties.filter(node => isNodeOfType(node, 'Property')).map(({
key,
value
}) => {
if (key.type === 'Identifier') {
return {
key: key.name,
value: value.type === 'Literal' && value.value
};
}
return null;
}).filter(node => Boolean(node));
return countMatchingKeyValues(styleEntries);
};
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export const countMatchingKeyValues = styleEntries => {
const matchingStyleEntries = styleEntries.filter(entry => {
return entry.key in referenceObject;
});
if (styleEntries.length < 5) {
return 0;
}
return matchingStyleEntries.reduce((acc, curr) => 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;
};