@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
67 lines • 2.67 kB
JavaScript
import { isNodeOfType } from 'eslint-codemod-utils';
import { createLintRule } from '../utils/create-lint-rule';
export var headingLevelRequiredSuggestionText = 'Add a `headingLevel` that is of a contextually relevant level.';
var ONBOARDING_PACKAGE = '@atlaskit/onboarding';
var SPOTLIGHT_CARD_IMPORT_SOURCE = '@atlaskit/onboarding/spotlight-card';
var rule = createLintRule({
meta: {
name: 'use-heading-level-in-spotlight-card',
type: 'suggestion',
fixable: 'code',
docs: {
description: 'Inform developers of eventual requirement of `headingLevel` prop in `SpotlightCard` component. The heading level should be the appropriate level according to the surrounding context.',
recommended: true,
severity: 'warn'
},
messages: {
headingLevelRequired: headingLevelRequiredSuggestionText
}
},
create: function create(context) {
var spotlightCardImportNames = [];
return {
ImportDeclaration: function ImportDeclaration(node) {
if (!isNodeOfType(node, 'ImportDeclaration')) {
return;
}
node.specifiers.forEach(function (spec) {
if (node.source.value === ONBOARDING_PACKAGE && isNodeOfType(spec, 'ImportSpecifier') && spec.imported.name === 'SpotlightCard') {
spotlightCardImportNames.push(spec.local.name);
}
if (node.source.value === SPOTLIGHT_CARD_IMPORT_SOURCE && isNodeOfType(spec, 'ImportDefaultSpecifier')) {
spotlightCardImportNames.push(spec.local.name);
}
});
},
JSXElement: function JSXElement(node) {
if (!isNodeOfType(node, 'JSXElement')) {
return;
}
if (!isNodeOfType(node.openingElement.name, 'JSXIdentifier')) {
return;
}
if (spotlightCardImportNames.includes(node.openingElement.name.name)) {
// and if `heading` exists and `headingLevel` prop does not exist
var spotlightCardProps = node.openingElement.attributes.filter(function (attr) {
return isNodeOfType(attr, 'JSXAttribute');
}).filter(function (attr) {
return attr.name.type === 'JSXIdentifier';
});
var heading = spotlightCardProps.find(function (attr) {
return attr.name.name === 'heading';
});
var headingLevel = spotlightCardProps.find(function (attr) {
return attr.name.name === 'headingLevel';
});
if (heading && !headingLevel) {
context.report({
node: node,
messageId: 'headingLevelRequired'
});
}
}
}
};
}
});
export default rule;