@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
70 lines (69 loc) • 2.83 kB
JavaScript
;
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 _isImportFromPackage = require("../utils/is-import-from-package");
var headingLevelRequiredSuggestionText = exports.headingLevelRequiredSuggestionText = 'Add a `headingLevel` that is of a contextually relevant level.';
var SECTION_MESSAGE_PACKAGE = '@atlaskit/section-message';
var rule = (0, _createLintRule.createLintRule)({
meta: {
name: 'use-heading-level-in-section-message',
type: 'suggestion',
fixable: 'code',
docs: {
description: 'The `SectionMessage` component in `@atlaskit/section-message` needs to be the correct level within the document flow. This is not something that can be automated and requires contextual knowledge of what is present in the experience.',
recommended: true,
severity: 'warn'
},
messages: {
headingLevelRequired: headingLevelRequiredSuggestionText
}
},
create: function create(context) {
var sectionMessageImportName;
return {
ImportDeclaration: function ImportDeclaration(node) {
if (!(0, _isImportFromPackage.isImportFromPackage)(node.source.value, SECTION_MESSAGE_PACKAGE)) {
return;
}
node.specifiers.forEach(function (spec) {
if ((0, _eslintCodemodUtils.isNodeOfType)(spec, 'ImportDefaultSpecifier')) {
sectionMessageImportName = spec.local.name;
}
});
},
JSXElement: function JSXElement(node) {
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXElement')) {
return;
}
if (!(0, _eslintCodemodUtils.isNodeOfType)(node.openingElement.name, 'JSXIdentifier')) {
return;
}
if (node.openingElement.name.name === sectionMessageImportName) {
// and if `title` exists and `headingLevel` prop does not exist
var sectionMessageProps = node.openingElement.attributes.filter(function (attr) {
return (0, _eslintCodemodUtils.isNodeOfType)(attr, 'JSXAttribute');
}).filter(function (attr) {
return attr.name.type === 'JSXIdentifier';
});
var title = sectionMessageProps.find(function (attr) {
return attr.name.name === 'title';
});
var headingLevel = sectionMessageProps.find(function (attr) {
return attr.name.name === 'headingLevel';
});
if (title && !headingLevel) {
context.report({
node: node,
messageId: 'headingLevelRequired'
});
}
}
}
};
}
});
var _default = exports.default = rule;