UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

64 lines 2.47 kB
import { isNodeOfType } from 'eslint-codemod-utils'; import { createLintRule } from '../utils/create-lint-rule'; import { isImportFromPackage } from '../utils/is-import-from-package'; export var headingLevelRequiredSuggestionText = 'Add a `headingLevel` that is of a contextually relevant level.'; var SECTION_MESSAGE_PACKAGE = '@atlaskit/section-message'; var rule = createLintRule({ meta: { name: 'use-heading-level-in-section-message', type: 'suggestion', fixable: 'code', docs: { description: 'The `SectionMessage` component in `@atlaskit/section-message` needs to be the correct level within the document flow. This is not something that can be automated and requires contextual knowledge of what is present in the experience.', recommended: true, severity: 'warn' }, messages: { headingLevelRequired: headingLevelRequiredSuggestionText } }, create: function create(context) { var sectionMessageImportName; return { ImportDeclaration: function ImportDeclaration(node) { if (!isImportFromPackage(node.source.value, SECTION_MESSAGE_PACKAGE)) { return; } node.specifiers.forEach(function (spec) { if (isNodeOfType(spec, 'ImportDefaultSpecifier')) { sectionMessageImportName = spec.local.name; } }); }, JSXElement: function JSXElement(node) { if (!isNodeOfType(node, 'JSXElement')) { return; } if (!isNodeOfType(node.openingElement.name, 'JSXIdentifier')) { return; } if (node.openingElement.name.name === sectionMessageImportName) { // and if `title` exists and `headingLevel` prop does not exist var sectionMessageProps = node.openingElement.attributes.filter(function (attr) { return isNodeOfType(attr, 'JSXAttribute'); }).filter(function (attr) { return attr.name.type === 'JSXIdentifier'; }); var title = sectionMessageProps.find(function (attr) { return attr.name.name === 'title'; }); var headingLevel = sectionMessageProps.find(function (attr) { return attr.name.name === 'headingLevel'; }); if (title && !headingLevel) { context.report({ node: node, messageId: 'headingLevelRequired' }); } } } }; } }); export default rule;