UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

62 lines (58 loc) 2.48 kB
/* eslint-disable @repo/internal/react/require-jsdoc */ import { isNodeOfType } from 'eslint-codemod-utils'; import { isPropertyName } from './common'; export const RestrictedProperty = { lint(node, { context, config }) { if (RestrictedProperty._check(node, { context, config })) { let property = 'fontSize, lineHeight, fontWeight or letterSpacing'; if (isNodeOfType(node, 'Identifier')) { property = node.name; } else if (isNodeOfType(node, 'Literal')) { property = String(node.value); } context.report({ node, messageId: property === 'fontWeight' ? 'noRestrictedTypographyPropertiesHeading' : 'noRestrictedTypographyProperties', data: { property } }); } }, _check(node, { config }) { if (!config.patterns.includes('restricted-property')) { return false; } // Prevent font weight being used in combination with heading tokens if (isPropertyName(node, 'fontWeight')) { if (isNodeOfType(node.parent.parent, 'ObjectExpression')) { for (const property of node.parent.parent.properties) { var _property$value$value; // Only looking for heading token on `font` property const isFontProperty = isNodeOfType(property, 'Property') && (isNodeOfType(property.key, 'Literal') && property.key.value === 'font' || isNodeOfType(property.key, 'Identifier') && property.key.name === 'font'); if (!isFontProperty) { continue; } // Checking for heading token string, for example xcss({ font: 'font.heading.medium' }) if (isNodeOfType(property.value, 'Literal') && typeof property.value.value === 'string' && (_property$value$value = property.value.value) !== null && _property$value$value !== void 0 && _property$value$value.startsWith('font.heading')) { return true; } // Checking for wrapped heading token, for example xcss({ font: token('font.heading.medium') }) if (isNodeOfType(property.value, 'CallExpression') && isNodeOfType(property.value.callee, 'Identifier') && property.value.callee.name === 'token' && isNodeOfType(property.value.arguments[0], 'Literal') && typeof property.value.arguments[0].value === 'string' && property.value.arguments[0].value.startsWith('font.heading')) { return true; } } } return false; } return true; } };