@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
55 lines (53 loc) • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getObjectLikeness = exports.countMatchingKeyValues = void 0;
var _eslintCodemodUtils = require("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.
*/
var getObjectLikeness = exports.getObjectLikeness = function getObjectLikeness(node) {
var styleEntries = node.properties.filter(function (node) {
return (0, _eslintCodemodUtils.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
var countMatchingKeyValues = exports.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;
};