UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

77 lines 3.31 kB
import { isNodeOfType } from 'eslint-codemod-utils'; import { createLintRule } from '../utils/create-lint-rule'; import { isImportFromPackage } from '../utils/is-import-from-package'; var elementsAccessibleNameProps = ['label', 'titleId']; var DRAWER_PACKAGE = '@atlaskit/drawer'; var rule = createLintRule({ meta: { name: 'use-drawer-label', type: 'suggestion', docs: { description: 'Encourages to provide accessible name for Atlassian Design System Drawer component.', recommended: true, severity: 'warn' }, messages: { missingLabelProp: 'Missing accessible name. If there is no visible content to associate use `label` prop, otherwise pass id of element to `titleId` prop to be associated as label.', labelPropShouldHaveContents: 'Define string that labels the interactive element.', titleIdShouldHaveValue: '`titleId` should reference the id of element that define accessible name.', noBothPropsUsage: 'Do not include both `titleId` and `label` properties. Use `titleId` if the label text is available in the DOM to reference it, otherwise use `label` to provide accessible name explicitly.' }, hasSuggestions: true }, create: function create(context) { var contextLocalIdentifier = []; return { ImportDeclaration: function ImportDeclaration(node) { if (isImportFromPackage(node.source.value, DRAWER_PACKAGE)) { if (node.specifiers.length) { var defaultImport = node.specifiers.filter(function (spec) { return spec.type === 'ImportDefaultSpecifier'; }); if (defaultImport.length) { var local = defaultImport[0].local; contextLocalIdentifier.push(local.name); } } } }, JSXElement: function JSXElement(node) { if (!isNodeOfType(node, 'JSXElement')) { return; } if (!isNodeOfType(node.openingElement.name, 'JSXIdentifier')) { return; } var name = node.openingElement.name.name; if (contextLocalIdentifier.includes(name)) { var componentLabelProps = node.openingElement.attributes.filter(function (attr) { return isNodeOfType(attr, 'JSXAttribute') && isNodeOfType(attr.name, 'JSXIdentifier') && elementsAccessibleNameProps.includes(attr.name.name); }); if (componentLabelProps.length === 1) { var prop = componentLabelProps[0]; if ('value' in prop && prop.value) { if (isNodeOfType(prop.value, 'Literal') && !prop.value.value || isNodeOfType(prop.value, 'JSXExpressionContainer') && !prop.value.expression) { context.report({ node: prop, messageId: prop.name.name === 'label' ? 'labelPropShouldHaveContents' : 'titleIdShouldHaveValue' }); } } } else if (componentLabelProps.length > 1) { context.report({ node: node.openingElement, messageId: 'noBothPropsUsage' }); } else { context.report({ node: node.openingElement, messageId: 'missingLabelProp' }); } } } }; } }); export default rule;