UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

60 lines 2.55 kB
import { isNodeOfType } from 'eslint-codemod-utils'; import { createLintRule } from '../utils/create-lint-rule'; var INLINE_IMPORT_SOURCES = new Set(['../src', '@atlaskit/primitives', '@atlaskit/primitives/inline', '@atlaskit/primitives/compiled/inline']); var separatorAsCombinationNotAllowed = 'The combination of `separator` with `as="li"`, `as="ol"`, or `as="dl"` is not allowed.'; var rule = createLintRule({ meta: { name: 'no-separator-with-list-elements', type: 'suggestion', docs: { description: 'Warn when the `separator` prop is used with `as="li"`, `as="ol"`, or `as="dl"` in the Inline component.', recommended: true, severity: 'warn' }, messages: { separatorAsCombinationNotAllowed: separatorAsCombinationNotAllowed } }, create: function create(context) { var inlineComponentNames = []; return { ImportDeclaration: function ImportDeclaration(node) { if (node.type === 'ImportDeclaration' && INLINE_IMPORT_SOURCES.has(String(node.source.value))) { node.specifiers.forEach(function (specifier) { if (specifier.type === 'ImportDefaultSpecifier') { inlineComponentNames.push(specifier.local.name); } if (specifier.type === 'ImportSpecifier' && 'name' in specifier.imported && specifier.imported.name === 'Inline') { inlineComponentNames.push(specifier.local.name); } }); } }, JSXElement: function JSXElement(node) { if (!isNodeOfType(node, 'JSXElement') || !isNodeOfType(node.openingElement.name, 'JSXIdentifier')) { return; } var componentName = node.openingElement.name.name; if (!inlineComponentNames.includes(componentName)) { return; } var inlineProps = node.openingElement.attributes.filter(function (attr) { return isNodeOfType(attr, 'JSXAttribute') && isNodeOfType(attr.name, 'JSXIdentifier'); }); var separatorProp = inlineProps.find(function (attr) { return attr.name.name === 'separator'; }); var asProp = inlineProps.find(function (attr) { return attr.name.name === 'as'; }); if (separatorProp && asProp && asProp.value && isNodeOfType(asProp.value, 'Literal') && ['li', 'ol', 'dl'].includes(asProp.value.value)) { context.report({ node: node, messageId: 'separatorAsCombinationNotAllowed' }); } } }; } }); export default rule;