UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

55 lines (54 loc) 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _eslintCodemodUtils = require("eslint-codemod-utils"); var _isFromImportSource = require("../../common/is-from-import-source"); var _createLintRule = require("../utils/create-lint-rule"); var _errorBoundary = require("../utils/error-boundary"); /** * Returns if the node is a JSXElement with a prop that matches the given name. */ function hasProp(node, propName) { return (0, _eslintCodemodUtils.isNodeOfType)(node.openingElement, 'JSXOpeningElement') && node.openingElement.attributes.some(function (a) { return a.type === 'JSXAttribute' && a.name.name === propName; }); } var rule = (0, _createLintRule.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: function create(context) { /** * Contains a map of imported icon components from any atlaskit icon package. */ var isNewIcon = (0, _isFromImportSource.createIsFromImportSourceFor)(/^@(atlaskit\/icon|atlaskit\/icon-lab)\/(core|utility)\/*/); return (0, _errorBoundary.errorBoundary)({ JSXElement: function JSXElement(node) { var _isNewIcon$getImportS; if (!isNewIcon(node) || hasProp(node, 'color')) { return; } var importSource = (_isNewIcon$getImportS = isNewIcon.getImportSource(node)) !== null && _isNewIcon$getImportS !== void 0 ? _isNewIcon$getImportS : ''; context.report({ node: node.openingElement, messageId: 'missingColorProp', data: { importSource: importSource } }); }, ImportDeclaration: isNewIcon.importDeclarationHook }); } }); var _default = exports.default = rule;