UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

73 lines (72 loc) 3.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.headingLevelRequiredSuggestionText = exports.default = void 0; var _eslintCodemodUtils = require("eslint-codemod-utils"); var _createLintRule = require("../utils/create-lint-rule"); var headingLevelRequiredSuggestionText = exports.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 = (0, _createLintRule.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 (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'ImportDeclaration')) { return; } node.specifiers.forEach(function (spec) { if (node.source.value === ONBOARDING_PACKAGE && (0, _eslintCodemodUtils.isNodeOfType)(spec, 'ImportSpecifier') && spec.imported.name === 'SpotlightCard') { spotlightCardImportNames.push(spec.local.name); } if (node.source.value === SPOTLIGHT_CARD_IMPORT_SOURCE && (0, _eslintCodemodUtils.isNodeOfType)(spec, 'ImportDefaultSpecifier')) { spotlightCardImportNames.push(spec.local.name); } }); }, JSXElement: function JSXElement(node) { if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXElement')) { return; } if (!(0, _eslintCodemodUtils.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 (0, _eslintCodemodUtils.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' }); } } } }; } }); var _default = exports.default = rule;