UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

59 lines 2.45 kB
import { isNodeOfType } from 'eslint-codemod-utils'; import { createLintRule } from '../utils/create-lint-rule'; export const headingLevelRequiredSuggestionText = 'Add a `headingLevel` that is of a contextually relevant level.'; const ONBOARDING_PACKAGE = '@atlaskit/onboarding'; const SPOTLIGHT_CARD_IMPORT_SOURCE = '@atlaskit/onboarding/spotlight-card'; const rule = createLintRule({ meta: { name: 'use-heading-level-in-spotlight-card', type: 'suggestion', fixable: 'code', docs: { description: 'Inform developers of eventual requirement of `headingLevel` prop in `SpotlightCard` component. The heading level should be the appropriate level according to the surrounding context.', recommended: true, severity: 'warn' }, messages: { headingLevelRequired: headingLevelRequiredSuggestionText } }, create(context) { const spotlightCardImportNames = []; return { ImportDeclaration(node) { if (!isNodeOfType(node, 'ImportDeclaration')) { return; } node.specifiers.forEach(spec => { if (node.source.value === ONBOARDING_PACKAGE && isNodeOfType(spec, 'ImportSpecifier') && spec.imported.name === 'SpotlightCard') { spotlightCardImportNames.push(spec.local.name); } if (node.source.value === SPOTLIGHT_CARD_IMPORT_SOURCE && isNodeOfType(spec, 'ImportDefaultSpecifier')) { spotlightCardImportNames.push(spec.local.name); } }); }, JSXElement(node) { if (!isNodeOfType(node, 'JSXElement')) { return; } if (!isNodeOfType(node.openingElement.name, 'JSXIdentifier')) { return; } if (spotlightCardImportNames.includes(node.openingElement.name.name)) { // and if `heading` exists and `headingLevel` prop does not exist const spotlightCardProps = node.openingElement.attributes.filter(attr => isNodeOfType(attr, 'JSXAttribute')).filter(attr => attr.name.type === 'JSXIdentifier'); const heading = spotlightCardProps.find(attr => attr.name.name === 'heading'); const headingLevel = spotlightCardProps.find(attr => attr.name.name === 'headingLevel'); if (heading && !headingLevel) { context.report({ node: node, messageId: 'headingLevelRequired' }); } } } }; } }); export default rule;