@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
83 lines (82 loc) • 3.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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 elementsAccessibleNameProps = ['label', 'titleId'];
var TAG_GROUP_PACKAGE = '@atlaskit/tag-group';
var rule = (0, _createLintRule.createLintRule)({
meta: {
name: 'use-tag-group-label',
type: 'suggestion',
docs: {
description: 'Ensures tag groups are described to assistive technology by a direct label or by another element.',
recommended: true,
severity: 'warn'
},
messages: {
missingLabelProp: 'Give TagGroup an accessible name: set titleId to the id of its visible heading, or set label to a concise description of the group of tags when no heading exists.',
labelPropShouldHaveContents: 'Replace the empty TagGroup label with text describing the group of tags; if a visible heading already provides that name, use titleId to reference its id instead.',
titleIdShouldHaveValue: 'Set TagGroup.titleId to the non-empty id of a rendered element containing its accessible name; ensure that element exists while the group of tags is displayed.',
noBothPropsUsage: 'Keep one naming prop on TagGroup: titleId when referencing a visible heading, otherwise label. Remove the other prop without deleting the intended accessible name.'
},
hasSuggestions: true
},
create: function create(context) {
var contextLocalIdentifier = [];
return {
ImportDeclaration: function ImportDeclaration(node) {
if ((0, _isImportFromPackage.isImportFromPackage)(node.source.value, TAG_GROUP_PACKAGE)) {
if (node.specifiers.length) {
var defaultImport = node.specifiers.filter(function (spec) {
return spec.type === 'ImportDefaultSpecifier';
});
if (defaultImport.length) {
var local = defaultImport[0].local;
contextLocalIdentifier.push(local.name);
}
}
}
},
JSXElement: function JSXElement(node) {
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXElement')) {
return;
}
if (!(0, _eslintCodemodUtils.isNodeOfType)(node.openingElement.name, 'JSXIdentifier')) {
return;
}
var name = node.openingElement.name.name;
if (contextLocalIdentifier.includes(name)) {
var componentLabelProps = node.openingElement.attributes.filter(function (attr) {
return (0, _eslintCodemodUtils.isNodeOfType)(attr, 'JSXAttribute') && (0, _eslintCodemodUtils.isNodeOfType)(attr.name, 'JSXIdentifier') && elementsAccessibleNameProps.includes(attr.name.name);
});
if (componentLabelProps.length === 1) {
var prop = componentLabelProps[0];
if ('value' in prop && prop.value) {
if ((0, _eslintCodemodUtils.isNodeOfType)(prop.value, 'Literal') && !prop.value.value || (0, _eslintCodemodUtils.isNodeOfType)(prop.value, 'JSXExpressionContainer') && !prop.value.expression) {
context.report({
node: prop,
messageId: prop.name.name === 'label' ? 'labelPropShouldHaveContents' : 'titleIdShouldHaveValue'
});
}
}
} else if (componentLabelProps.length > 1) {
context.report({
node: node.openingElement,
messageId: 'noBothPropsUsage'
});
} else {
context.report({
node: node.openingElement,
messageId: 'missingLabelProp'
});
}
}
}
};
}
});
var _default = exports.default = rule;