@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
54 lines • 2.34 kB
JavaScript
import { isNodeOfType } from 'eslint-codemod-utils';
import { createLintRule } from '../utils/create-lint-rule';
const INLINE_IMPORT_SOURCES = new Set(['../src', '@atlaskit/primitives', '@atlaskit/primitives/inline', '@atlaskit/primitives/compiled/inline']);
const separatorAsCombinationNotAllowed = 'The combination of `separator` with `as="li"`, `as="ol"`, or `as="dl"` is not allowed.';
const rule = createLintRule({
meta: {
name: 'no-separator-with-list-elements',
type: 'suggestion',
docs: {
description: 'Warn when the `separator` prop is used with `as="li"`, `as="ol"`, or `as="dl"` in the Inline component.',
recommended: true,
severity: 'warn'
},
messages: {
separatorAsCombinationNotAllowed
}
},
create(context) {
const inlineComponentNames = [];
return {
ImportDeclaration(node) {
if (node.type === 'ImportDeclaration' && INLINE_IMPORT_SOURCES.has(String(node.source.value))) {
node.specifiers.forEach(specifier => {
if (specifier.type === 'ImportDefaultSpecifier') {
inlineComponentNames.push(specifier.local.name);
}
if (specifier.type === 'ImportSpecifier' && 'name' in specifier.imported && specifier.imported.name === 'Inline') {
inlineComponentNames.push(specifier.local.name);
}
});
}
},
JSXElement(node) {
if (!isNodeOfType(node, 'JSXElement') || !isNodeOfType(node.openingElement.name, 'JSXIdentifier')) {
return;
}
const componentName = node.openingElement.name.name;
if (!inlineComponentNames.includes(componentName)) {
return;
}
const inlineProps = node.openingElement.attributes.filter(attr => isNodeOfType(attr, 'JSXAttribute') && isNodeOfType(attr.name, 'JSXIdentifier'));
const separatorProp = inlineProps.find(attr => attr.name.name === 'separator');
const asProp = inlineProps.find(attr => attr.name.name === 'as');
if (separatorProp && asProp && asProp.value && isNodeOfType(asProp.value, 'Literal') && ['li', 'ol', 'dl'].includes(asProp.value.value)) {
context.report({
node: node,
messageId: 'separatorAsCombinationNotAllowed'
});
}
}
};
}
});
export default rule;