UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

66 lines (65 loc) 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _eslintCodemodUtils = require("eslint-codemod-utils"); var _createLintRule = require("../utils/create-lint-rule"); var INLINE_IMPORT_SOURCES = new Set(['../src', '@atlaskit/primitives', '@atlaskit/primitives/inline', '@atlaskit/primitives/compiled/inline']); var separatorAsCombinationNotAllowed = 'The combination of `separator` with `as="li"`, `as="ol"`, or `as="dl"` is not allowed.'; var rule = (0, _createLintRule.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: separatorAsCombinationNotAllowed } }, create: function create(context) { var inlineComponentNames = []; return { ImportDeclaration: function ImportDeclaration(node) { if (node.type === 'ImportDeclaration' && INLINE_IMPORT_SOURCES.has(String(node.source.value))) { node.specifiers.forEach(function (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: function JSXElement(node) { if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXElement') || !(0, _eslintCodemodUtils.isNodeOfType)(node.openingElement.name, 'JSXIdentifier')) { return; } var componentName = node.openingElement.name.name; if (!inlineComponentNames.includes(componentName)) { return; } var inlineProps = node.openingElement.attributes.filter(function (attr) { return (0, _eslintCodemodUtils.isNodeOfType)(attr, 'JSXAttribute') && (0, _eslintCodemodUtils.isNodeOfType)(attr.name, 'JSXIdentifier'); }); var separatorProp = inlineProps.find(function (attr) { return attr.name.name === 'separator'; }); var asProp = inlineProps.find(function (attr) { return attr.name.name === 'as'; }); if (separatorProp && asProp && asProp.value && (0, _eslintCodemodUtils.isNodeOfType)(asProp.value, 'Literal') && ['li', 'ol', 'dl'].includes(asProp.value.value)) { context.report({ node: node, messageId: 'separatorAsCombinationNotAllowed' }); } } }; } }); var _default = exports.default = rule;