UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

48 lines (47 loc) 1.86 kB
import { isNodeOfType } from 'eslint-codemod-utils'; import { createIsFromImportSourceFor } from '../../common/is-from-import-source'; import { createLintRule } from '../utils/create-lint-rule'; import { errorBoundary } from '../utils/error-boundary'; /** * Returns if the node is a JSXElement with a prop that matches the given name. */ function hasProp(node, propName) { return isNodeOfType(node.openingElement, 'JSXOpeningElement') && node.openingElement.attributes.some(a => a.type === 'JSXAttribute' && a.name.name === propName); } const rule = createLintRule({ meta: { name: 'ensure-icon-color', docs: { description: 'Enforces that upcoming icon components have a color prop set, to enable a migration of the default value.', recommended: false, severity: 'error' }, messages: { missingColorProp: 'The default value of the `color` prop is about to change. To assist in the migration, the color prop must be set on new icons from `@atlaskit/icon/(core|utility)`.' } }, create(context) { /** * Contains a map of imported icon components from any atlaskit icon package. */ const isNewIcon = createIsFromImportSourceFor(/^@(atlaskit\/icon|atlaskit\/icon-lab)\/(core|utility)\/*/); return errorBoundary({ JSXElement(node) { var _isNewIcon$getImportS; if (!isNewIcon(node) || hasProp(node, 'color')) { return; } const importSource = (_isNewIcon$getImportS = isNewIcon.getImportSource(node)) !== null && _isNewIcon$getImportS !== void 0 ? _isNewIcon$getImportS : ''; context.report({ node: node.openingElement, messageId: 'missingColorProp', data: { importSource } }); }, ImportDeclaration: isNewIcon.importDeclarationHook }); } }); export default rule;