@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
97 lines (96 loc) • 4.67 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 elementsAccessibleNameProps = ['label', 'titleId'];
var POPUP_PACKAGE = '@atlaskit/popup';
var POPUP_IMPORT_SOURCE = '@atlaskit/popup/popup';
var ENTRY_POINT_POPUP_PACKAGE = '@atlassian/entry-points/popup-trigger';
var rule = (0, _createLintRule.createLintRule)({
meta: {
name: 'use-popup-label',
type: 'suggestion',
docs: {
description: 'Encourages to provide accessible name for Atlassian Design System Popup component.',
recommended: true,
severity: 'warn'
},
messages: {
missingLabelProp: 'Missing accessible name. If there is no visible content to associate use `label` prop, otherwise pass id of element to `titleId` prop to be associated as label.',
labelPropShouldHaveContents: 'Define string that labels the interactive element.',
titleIdShouldHaveValue: '`titleId` should reference the id of element that define accessible name.',
noBothPropsUsage: 'Do not include both `titleId` and `label` properties. Use `titleId` if the label text is available in the DOM to reference it, otherwise use `label` to provide accessible name explicitly.'
},
hasSuggestions: true
},
create: function create(context) {
var contextLocalIdentifier = [];
return {
ImportDeclaration: function ImportDeclaration(node) {
if (node.source.value === POPUP_PACKAGE || node.source.value === POPUP_IMPORT_SOURCE || node.source.value === ENTRY_POINT_POPUP_PACKAGE) {
if (node.specifiers.length) {
node.specifiers.forEach(function (spec) {
if (spec.type === 'ImportDefaultSpecifier' && node.source.value !== ENTRY_POINT_POPUP_PACKAGE) {
contextLocalIdentifier.push(spec.local.name);
}
if (spec.type === 'ImportSpecifier' && 'name' in spec.imported) {
if (node.source.value === POPUP_IMPORT_SOURCE && spec.imported.name === 'Popup') {
contextLocalIdentifier.push(spec.local.name);
}
if (node.source.value === ENTRY_POINT_POPUP_PACKAGE && spec.imported.name === 'PopupTrigger') {
contextLocalIdentifier.push(spec.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 componentRoleDialogProp = node.openingElement.attributes.find(function (attr) {
return (0, _eslintCodemodUtils.isNodeOfType)(attr, 'JSXAttribute') && (0, _eslintCodemodUtils.isNodeOfType)(attr.name, 'JSXIdentifier') && attr.value && (0, _eslintCodemodUtils.isNodeOfType)(attr.value, 'Literal') && attr.name.name === 'role' && attr.value.value === 'dialog';
});
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 {
if (componentRoleDialogProp) {
context.report({
node: node.openingElement,
messageId: 'missingLabelProp'
});
} else {
return;
}
}
}
}
};
}
});
var _default = exports.default = rule;