@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
50 lines (49 loc) • 1.94 kB
JavaScript
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(function (a) {
return a.type === 'JSXAttribute' && a.name.name === propName;
});
}
var 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: function create(context) {
/**
* Contains a map of imported icon components from any atlaskit icon package.
*/
var isNewIcon = createIsFromImportSourceFor(/^@(atlaskit\/icon|atlaskit\/icon-lab)\/(core|utility)\/*/);
return 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
});
}
});
export default rule;