0.2.0 - Mid migration

This commit is contained in:
Daniel Mason 2022-04-25 14:47:15 +12:00
parent 139e6a915e
commit 7e38fdbd7d
42393 changed files with 5358157 additions and 62 deletions

173
web/node_modules/eslint-plugin-jsx-a11y/lib/index.js generated vendored Normal file
View file

@ -0,0 +1,173 @@
"use strict";
/* eslint-disable global-require */
module.exports = {
rules: {
'accessible-emoji': require('./rules/accessible-emoji'),
'alt-text': require('./rules/alt-text'),
'anchor-has-content': require('./rules/anchor-has-content'),
'anchor-is-valid': require('./rules/anchor-is-valid'),
'aria-activedescendant-has-tabindex': require('./rules/aria-activedescendant-has-tabindex'),
'aria-props': require('./rules/aria-props'),
'aria-proptypes': require('./rules/aria-proptypes'),
'aria-role': require('./rules/aria-role'),
'aria-unsupported-elements': require('./rules/aria-unsupported-elements'),
'autocomplete-valid': require('./rules/autocomplete-valid'),
'click-events-have-key-events': require('./rules/click-events-have-key-events'),
'control-has-associated-label': require('./rules/control-has-associated-label'),
'heading-has-content': require('./rules/heading-has-content'),
'html-has-lang': require('./rules/html-has-lang'),
'iframe-has-title': require('./rules/iframe-has-title'),
'img-redundant-alt': require('./rules/img-redundant-alt'),
'interactive-supports-focus': require('./rules/interactive-supports-focus'),
'label-has-associated-control': require('./rules/label-has-associated-control'),
'label-has-for': require('./rules/label-has-for'),
lang: require('./rules/lang'),
'media-has-caption': require('./rules/media-has-caption'),
'mouse-events-have-key-events': require('./rules/mouse-events-have-key-events'),
'no-access-key': require('./rules/no-access-key'),
'no-autofocus': require('./rules/no-autofocus'),
'no-distracting-elements': require('./rules/no-distracting-elements'),
'no-interactive-element-to-noninteractive-role': require('./rules/no-interactive-element-to-noninteractive-role'),
'no-noninteractive-element-interactions': require('./rules/no-noninteractive-element-interactions'),
'no-noninteractive-element-to-interactive-role': require('./rules/no-noninteractive-element-to-interactive-role'),
'no-noninteractive-tabindex': require('./rules/no-noninteractive-tabindex'),
'no-onchange': require('./rules/no-onchange'),
'no-redundant-roles': require('./rules/no-redundant-roles'),
'no-static-element-interactions': require('./rules/no-static-element-interactions'),
'role-has-required-aria-props': require('./rules/role-has-required-aria-props'),
'role-supports-aria-props': require('./rules/role-supports-aria-props'),
scope: require('./rules/scope'),
'tabindex-no-positive': require('./rules/tabindex-no-positive')
},
configs: {
recommended: {
plugins: ['jsx-a11y'],
parserOptions: {
ecmaFeatures: {
jsx: true
}
},
rules: {
'jsx-a11y/accessible-emoji': 'error',
'jsx-a11y/alt-text': 'error',
'jsx-a11y/anchor-has-content': 'error',
'jsx-a11y/anchor-is-valid': 'error',
'jsx-a11y/aria-activedescendant-has-tabindex': 'error',
'jsx-a11y/aria-props': 'error',
'jsx-a11y/aria-proptypes': 'error',
'jsx-a11y/aria-role': 'error',
'jsx-a11y/aria-unsupported-elements': 'error',
'jsx-a11y/autocomplete-valid': 'error',
'jsx-a11y/click-events-have-key-events': 'error',
'jsx-a11y/control-has-associated-label': ['off', {
ignoreElements: ['audio', 'canvas', 'embed', 'input', 'textarea', 'tr', 'video'],
ignoreRoles: ['grid', 'listbox', 'menu', 'menubar', 'radiogroup', 'row', 'tablist', 'toolbar', 'tree', 'treegrid'],
includeRoles: ['alert', 'dialog']
}],
'jsx-a11y/heading-has-content': 'error',
'jsx-a11y/html-has-lang': 'error',
'jsx-a11y/iframe-has-title': 'error',
'jsx-a11y/img-redundant-alt': 'error',
'jsx-a11y/interactive-supports-focus': ['error', {
tabbable: ['button', 'checkbox', 'link', 'searchbox', 'spinbutton', 'switch', 'textbox']
}],
'jsx-a11y/label-has-associated-control': 'error',
'jsx-a11y/label-has-for': 'off',
'jsx-a11y/media-has-caption': 'error',
'jsx-a11y/mouse-events-have-key-events': 'error',
'jsx-a11y/no-access-key': 'error',
'jsx-a11y/no-autofocus': 'error',
'jsx-a11y/no-distracting-elements': 'error',
'jsx-a11y/no-interactive-element-to-noninteractive-role': ['error', {
tr: ['none', 'presentation']
}],
'jsx-a11y/no-noninteractive-element-interactions': ['error', {
handlers: ['onClick', 'onError', 'onLoad', 'onMouseDown', 'onMouseUp', 'onKeyPress', 'onKeyDown', 'onKeyUp'],
alert: ['onKeyUp', 'onKeyDown', 'onKeyPress'],
body: ['onError', 'onLoad'],
dialog: ['onKeyUp', 'onKeyDown', 'onKeyPress'],
iframe: ['onError', 'onLoad'],
img: ['onError', 'onLoad']
}],
'jsx-a11y/no-noninteractive-element-to-interactive-role': ['error', {
ul: ['listbox', 'menu', 'menubar', 'radiogroup', 'tablist', 'tree', 'treegrid'],
ol: ['listbox', 'menu', 'menubar', 'radiogroup', 'tablist', 'tree', 'treegrid'],
li: ['menuitem', 'option', 'row', 'tab', 'treeitem'],
table: ['grid'],
td: ['gridcell']
}],
'jsx-a11y/no-noninteractive-tabindex': ['error', {
tags: [],
roles: ['tabpanel'],
allowExpressionValues: true
}],
'jsx-a11y/no-onchange': 'error',
'jsx-a11y/no-redundant-roles': 'error',
'jsx-a11y/no-static-element-interactions': ['error', {
allowExpressionValues: true,
handlers: ['onClick', 'onMouseDown', 'onMouseUp', 'onKeyPress', 'onKeyDown', 'onKeyUp']
}],
'jsx-a11y/role-has-required-aria-props': 'error',
'jsx-a11y/role-supports-aria-props': 'error',
'jsx-a11y/scope': 'error',
'jsx-a11y/tabindex-no-positive': 'error'
}
},
strict: {
plugins: ['jsx-a11y'],
parserOptions: {
ecmaFeatures: {
jsx: true
}
},
rules: {
'jsx-a11y/accessible-emoji': 'error',
'jsx-a11y/alt-text': 'error',
'jsx-a11y/anchor-has-content': 'error',
'jsx-a11y/anchor-is-valid': 'error',
'jsx-a11y/aria-activedescendant-has-tabindex': 'error',
'jsx-a11y/aria-props': 'error',
'jsx-a11y/aria-proptypes': 'error',
'jsx-a11y/aria-role': 'error',
'jsx-a11y/aria-unsupported-elements': 'error',
'jsx-a11y/autocomplete-valid': 'error',
'jsx-a11y/click-events-have-key-events': 'error',
'jsx-a11y/control-has-associated-label': ['off', {
ignoreElements: ['audio', 'canvas', 'embed', 'input', 'textarea', 'tr', 'video'],
ignoreRoles: ['grid', 'listbox', 'menu', 'menubar', 'radiogroup', 'row', 'tablist', 'toolbar', 'tree', 'treegrid'],
includeRoles: ['alert', 'dialog']
}],
'jsx-a11y/heading-has-content': 'error',
'jsx-a11y/html-has-lang': 'error',
'jsx-a11y/iframe-has-title': 'error',
'jsx-a11y/img-redundant-alt': 'error',
'jsx-a11y/interactive-supports-focus': ['error', {
tabbable: ['button', 'checkbox', 'link', 'progressbar', 'searchbox', 'slider', 'spinbutton', 'switch', 'textbox']
}],
'jsx-a11y/label-has-for': 'error',
'jsx-a11y/label-has-associated-control': 'error',
'jsx-a11y/media-has-caption': 'error',
'jsx-a11y/mouse-events-have-key-events': 'error',
'jsx-a11y/no-access-key': 'error',
'jsx-a11y/no-autofocus': 'error',
'jsx-a11y/no-distracting-elements': 'error',
'jsx-a11y/no-interactive-element-to-noninteractive-role': 'error',
'jsx-a11y/no-noninteractive-element-interactions': ['error', {
body: ['onError', 'onLoad'],
iframe: ['onError', 'onLoad'],
img: ['onError', 'onLoad']
}],
'jsx-a11y/no-noninteractive-element-to-interactive-role': 'error',
'jsx-a11y/no-noninteractive-tabindex': 'error',
'jsx-a11y/no-onchange': 'error',
'jsx-a11y/no-redundant-roles': 'error',
'jsx-a11y/no-static-element-interactions': 'error',
'jsx-a11y/role-has-required-aria-props': 'error',
'jsx-a11y/role-supports-aria-props': 'error',
'jsx-a11y/scope': 'error',
'jsx-a11y/tabindex-no-positive': 'error'
}
}
}
};

View file

@ -0,0 +1,60 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _emojiRegex = _interopRequireDefault(require("emoji-regex"));
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
/**
* @fileoverview Enforce emojis are wrapped in <span> and provide screenreader access.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'Emojis should be wrapped in <span>, have role="img", and have an accessible description with aria-label or aria-labelledby.';
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/accessible-emoji.md'
},
deprecated: true,
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var literalChildValue = node.parent.children.find(function (child) {
return child.type === 'Literal' || child.type === 'JSXText';
});
if (literalChildValue && (0, _emojiRegex["default"])().test(literalChildValue.value)) {
var elementIsHidden = (0, _isHiddenFromScreenReader["default"])((0, _jsxAstUtils.elementType)(node), node.attributes);
if (elementIsHidden) {
return; // emoji is decorative
}
var rolePropValue = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'role'));
var ariaLabelProp = (0, _jsxAstUtils.getProp)(node.attributes, 'aria-label');
var arialLabelledByProp = (0, _jsxAstUtils.getProp)(node.attributes, 'aria-labelledby');
var hasLabel = ariaLabelProp !== undefined || arialLabelledByProp !== undefined;
var isSpan = (0, _jsxAstUtils.elementType)(node) === 'span';
if (hasLabel === false || rolePropValue !== 'img' || isSpan === false) {
context.report({
node,
message: errorMessage
});
}
}
}
};
}
};

View file

@ -0,0 +1,257 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
var _hasAccessibleChild = _interopRequireDefault(require("../util/hasAccessibleChild"));
var _isPresentationRole = _interopRequireDefault(require("../util/isPresentationRole"));
/**
* @fileoverview Enforce all elements that require alternative text have it.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var DEFAULT_ELEMENTS = ['img', 'object', 'area', 'input[type="image"]'];
var schema = (0, _schemas.generateObjSchema)({
elements: _schemas.arraySchema,
img: _schemas.arraySchema,
object: _schemas.arraySchema,
area: _schemas.arraySchema,
'input[type="image"]': _schemas.arraySchema
});
var ariaLabelHasValue = function ariaLabelHasValue(prop) {
var value = (0, _jsxAstUtils.getPropValue)(prop);
if (value === undefined) {
return false;
}
if (typeof value === 'string' && value.length === 0) {
return false;
}
return true;
};
var ruleByElement = {
img(context, node) {
var nodeType = (0, _jsxAstUtils.elementType)(node);
var altProp = (0, _jsxAstUtils.getProp)(node.attributes, 'alt'); // Missing alt prop error.
if (altProp === undefined) {
if ((0, _isPresentationRole["default"])(nodeType, node.attributes)) {
context.report({
node,
message: 'Prefer alt="" over a presentational role. First rule of aria is to not use aria if it can be achieved via native HTML.'
});
return;
} // Check for `aria-label` to provide text alternative
// Don't create an error if the attribute is used correctly. But if it
// isn't, suggest that the developer use `alt` instead.
var ariaLabelProp = (0, _jsxAstUtils.getProp)(node.attributes, 'aria-label');
if (ariaLabelProp !== undefined) {
if (!ariaLabelHasValue(ariaLabelProp)) {
context.report({
node,
message: 'The aria-label attribute must have a value. The alt attribute is preferred over aria-label for images.'
});
}
return;
} // Check for `aria-labelledby` to provide text alternative
// Don't create an error if the attribute is used correctly. But if it
// isn't, suggest that the developer use `alt` instead.
var ariaLabelledbyProp = (0, _jsxAstUtils.getProp)(node.attributes, 'aria-labelledby');
if (ariaLabelledbyProp !== undefined) {
if (!ariaLabelHasValue(ariaLabelledbyProp)) {
context.report({
node,
message: 'The aria-labelledby attribute must have a value. The alt attribute is preferred over aria-labelledby for images.'
});
}
return;
}
context.report({
node,
message: "".concat(nodeType, " elements must have an alt prop, either with meaningful text, or an empty string for decorative images.")
});
return;
} // Check if alt prop is undefined.
var altValue = (0, _jsxAstUtils.getPropValue)(altProp);
var isNullValued = altProp.value === null; // <img alt />
if (altValue && !isNullValued || altValue === '') {
return;
} // Undefined alt prop error.
context.report({
node,
message: "Invalid alt value for ".concat(nodeType, ". Use alt=\"\" for presentational images.")
});
},
object(context, node) {
var ariaLabelProp = (0, _jsxAstUtils.getProp)(node.attributes, 'aria-label');
var arialLabelledByProp = (0, _jsxAstUtils.getProp)(node.attributes, 'aria-labelledby');
var hasLabel = ariaLabelHasValue(ariaLabelProp) || ariaLabelHasValue(arialLabelledByProp);
var titleProp = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'title'));
var hasTitleAttr = !!titleProp;
if (hasLabel || hasTitleAttr || (0, _hasAccessibleChild["default"])(node.parent)) {
return;
}
context.report({
node,
message: 'Embedded <object> elements must have alternative text by providing inner text, aria-label or aria-labelledby props.'
});
},
area(context, node) {
var ariaLabelProp = (0, _jsxAstUtils.getProp)(node.attributes, 'aria-label');
var arialLabelledByProp = (0, _jsxAstUtils.getProp)(node.attributes, 'aria-labelledby');
var hasLabel = ariaLabelHasValue(ariaLabelProp) || ariaLabelHasValue(arialLabelledByProp);
if (hasLabel) {
return;
}
var altProp = (0, _jsxAstUtils.getProp)(node.attributes, 'alt');
if (altProp === undefined) {
context.report({
node,
message: 'Each area of an image map must have a text alternative through the `alt`, `aria-label`, or `aria-labelledby` prop.'
});
return;
}
var altValue = (0, _jsxAstUtils.getPropValue)(altProp);
var isNullValued = altProp.value === null; // <area alt />
if (altValue && !isNullValued || altValue === '') {
return;
}
context.report({
node,
message: 'Each area of an image map must have a text alternative through the `alt`, `aria-label`, or `aria-labelledby` prop.'
});
},
'input[type="image"]': function inputImage(context, node) {
// Only test input[type="image"]
var nodeType = (0, _jsxAstUtils.elementType)(node);
if (nodeType === 'input') {
var typePropValue = (0, _jsxAstUtils.getPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'type'));
if (typePropValue !== 'image') {
return;
}
}
var ariaLabelProp = (0, _jsxAstUtils.getProp)(node.attributes, 'aria-label');
var arialLabelledByProp = (0, _jsxAstUtils.getProp)(node.attributes, 'aria-labelledby');
var hasLabel = ariaLabelHasValue(ariaLabelProp) || ariaLabelHasValue(arialLabelledByProp);
if (hasLabel) {
return;
}
var altProp = (0, _jsxAstUtils.getProp)(node.attributes, 'alt');
if (altProp === undefined) {
context.report({
node,
message: '<input> elements with type="image" must have a text alternative through the `alt`, `aria-label`, or `aria-labelledby` prop.'
});
return;
}
var altValue = (0, _jsxAstUtils.getPropValue)(altProp);
var isNullValued = altProp.value === null; // <area alt />
if (altValue && !isNullValued || altValue === '') {
return;
}
context.report({
node,
message: '<input> elements with type="image" must have a text alternative through the `alt`, `aria-label`, or `aria-labelledby` prop.'
});
}
};
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/alt-text.md'
},
schema: [schema]
},
create: function create(context) {
var _ref;
var options = context.options[0] || {}; // Elements to validate for alt text.
var elementOptions = options.elements || DEFAULT_ELEMENTS; // Get custom components for just the elements that will be tested.
var customComponents = elementOptions.map(function (element) {
return options[element];
}).reduce(function (components, customComponentsForElement) {
return components.concat(customComponentsForElement || []);
}, []);
var typesToValidate = new Set((_ref = []).concat.apply(_ref, [customComponents].concat((0, _toConsumableArray2["default"])(elementOptions))).map(function (type) {
if (type === 'input[type="image"]') {
return 'input';
}
return type;
}));
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var nodeType = (0, _jsxAstUtils.elementType)(node);
if (!typesToValidate.has(nodeType)) {
return;
}
var DOMElement = nodeType;
if (DOMElement === 'input') {
DOMElement = 'input[type="image"]';
} // Map nodeType to the DOM element if we are running this on a custom component.
if (elementOptions.indexOf(DOMElement) === -1) {
DOMElement = elementOptions.find(function (element) {
var customComponentsForElement = options[element] || [];
return customComponentsForElement.indexOf(nodeType) > -1;
});
}
ruleByElement[DOMElement](context, node);
}
};
}
};

View file

@ -0,0 +1,52 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
var _hasAccessibleChild = _interopRequireDefault(require("../util/hasAccessibleChild"));
/**
* @fileoverview Enforce anchor elements to contain accessible content.
* @author Lisa Ring & Niklas Holmberg
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'Anchors must have content and the content must be accessible by a screen reader.';
var schema = (0, _schemas.generateObjSchema)({
components: _schemas.arraySchema
});
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/anchor-has-content.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var options = context.options[0] || {};
var componentOptions = options.components || [];
var typeCheck = ['a'].concat(componentOptions);
var nodeType = (0, _jsxAstUtils.elementType)(node); // Only check anchor elements and custom types.
if (typeCheck.indexOf(nodeType) === -1) {
return;
}
if ((0, _hasAccessibleChild["default"])(node.parent)) {
return;
}
context.report({
node,
message: errorMessage
});
}
};
}
};

View file

@ -0,0 +1,115 @@
"use strict";
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Performs validity check on anchor hrefs. Warns when anchors are used as buttons.
* @author Almero Steyn
*
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var allAspects = ['noHref', 'invalidHref', 'preferButton'];
var preferButtonErrorMessage = 'Anchor used as a button. Anchors are primarily expected to navigate. Use the button element instead. Learn more: https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/anchor-is-valid.md';
var noHrefErrorMessage = 'The href attribute is required for an anchor to be keyboard accessible. Provide a valid, navigable address as the href value. If you cannot provide an href, but still need the element to resemble a link, use a button and change it with appropriate styles. Learn more: https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/anchor-is-valid.md';
var invalidHrefErrorMessage = 'The href attribute requires a valid value to be accessible. Provide a valid, navigable address as the href value. If you cannot provide a valid href, but still need the element to resemble a link, use a button and change it with appropriate styles. Learn more: https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/anchor-is-valid.md';
var schema = (0, _schemas.generateObjSchema)({
components: _schemas.arraySchema,
specialLink: _schemas.arraySchema,
aspects: (0, _schemas.enumArraySchema)(allAspects, 1)
});
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/anchor-is-valid.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var attributes = node.attributes;
var options = context.options[0] || {};
var componentOptions = options.components || [];
var typeCheck = ['a'].concat(componentOptions);
var nodeType = (0, _jsxAstUtils.elementType)(node); // Only check anchor elements and custom types.
if (typeCheck.indexOf(nodeType) === -1) {
return;
} // Set up the rule aspects to check.
var aspects = options.aspects || allAspects; // Create active aspect flag object. Failing checks will only report
// if the related flag is set to true.
var activeAspects = {};
allAspects.forEach(function (aspect) {
activeAspects[aspect] = aspects.indexOf(aspect) !== -1;
});
var propOptions = options.specialLink || [];
var propsToValidate = ['href'].concat(propOptions);
var values = propsToValidate.map(function (prop) {
return (0, _jsxAstUtils.getProp)(node.attributes, prop);
}).map(function (prop) {
return (0, _jsxAstUtils.getPropValue)(prop);
}); // Checks if any actual or custom href prop is provided.
var hasAnyHref = values.filter(function (value) {
return value === undefined || value === null;
}).length !== values.length; // Need to check for spread operator as props can be spread onto the element
// leading to an incorrect validation error.
var hasSpreadOperator = attributes.filter(function (prop) {
return prop.type === 'JSXSpreadAttribute';
}).length > 0;
var onClick = (0, _jsxAstUtils.getProp)(attributes, 'onClick'); // When there is no href at all, specific scenarios apply:
if (!hasAnyHref) {
// If no spread operator is found and no onClick event is present
// it is a link without href.
if (!hasSpreadOperator && activeAspects.noHref && (!onClick || onClick && !activeAspects.preferButton)) {
context.report({
node,
message: noHrefErrorMessage
});
} // If no spread operator is found but an onClick is preset it should be a button.
if (!hasSpreadOperator && onClick && activeAspects.preferButton) {
context.report({
node,
message: preferButtonErrorMessage
});
}
return;
} // Hrefs have been found, now check for validity.
var invalidHrefValues = values.filter(function (value) {
return value !== undefined && value !== null;
}).filter(function (value) {
return typeof value === 'string' && (!value.length || value === '#' || /^\W*?javascript:/.test(value));
});
if (invalidHrefValues.length !== 0) {
// If an onClick is found it should be a button, otherwise it is an invalid link.
if (onClick && activeAspects.preferButton) {
context.report({
node,
message: preferButtonErrorMessage
});
} else if (activeAspects.invalidHref) {
context.report({
node,
message: invalidHrefErrorMessage
});
}
}
}
};
}
};

View file

@ -0,0 +1,69 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
var _getTabIndex = _interopRequireDefault(require("../util/getTabIndex"));
var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
/**
* @fileoverview Enforce elements with aria-activedescendant are tabbable.
* @author Jesse Beach <@jessebeach>
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'An element that manages focus with `aria-activedescendant` must have a tabindex';
var schema = (0, _schemas.generateObjSchema)();
var domElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/aria-activedescendant-has-tabindex.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var attributes = node.attributes;
if ((0, _jsxAstUtils.getProp)(attributes, 'aria-activedescendant') === undefined) {
return;
}
var type = (0, _jsxAstUtils.elementType)(node); // Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
if (domElements.indexOf(type) === -1) {
return;
}
var tabIndex = (0, _getTabIndex["default"])((0, _jsxAstUtils.getProp)(attributes, 'tabIndex')); // If this is an interactive element and the tabindex attribute is not specified,
// or the tabIndex property was not mutated, then the tabIndex
// property will be undefined.
if ((0, _isInteractiveElement["default"])(type, attributes) && tabIndex === undefined) {
return;
}
if (tabIndex >= -1) {
return;
}
context.report({
node,
message: errorMessage
});
}
};
}
};

View file

@ -0,0 +1,63 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
var _getSuggestion = _interopRequireDefault(require("../util/getSuggestion"));
/**
* @fileoverview Enforce all aria-* properties are valid.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var ariaAttributes = (0, _toConsumableArray2["default"])(_ariaQuery.aria.keys());
var errorMessage = function errorMessage(name) {
var suggestions = (0, _getSuggestion["default"])(name, ariaAttributes);
var message = "".concat(name, ": This attribute is an invalid ARIA attribute.");
if (suggestions.length > 0) {
return "".concat(message, " Did you mean to use ").concat(suggestions, "?");
}
return message;
};
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/aria-props.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXAttribute: function JSXAttribute(attribute) {
var name = (0, _jsxAstUtils.propName)(attribute); // `aria` needs to be prefix of property.
if (name.indexOf('aria-') !== 0) {
return;
}
var isValid = ariaAttributes.indexOf(name) > -1;
if (isValid === false) {
context.report({
node: attribute,
message: errorMessage(name)
});
}
}
};
}
};

View file

@ -0,0 +1,124 @@
"use strict";
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce ARIA state and property values are valid.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = function errorMessage(name, type, permittedValues) {
switch (type) {
case 'tristate':
return "The value for ".concat(name, " must be a boolean or the string \"mixed\".");
case 'token':
return "The value for ".concat(name, " must be a single token from the following: ").concat(permittedValues, ".");
case 'tokenlist':
return "The value for ".concat(name, " must be a list of one or more tokens from the following: ").concat(permittedValues, ".");
case 'idlist':
return "The value for ".concat(name, " must be a list of strings that represent DOM element IDs (idlist)");
case 'id':
return "The value for ".concat(name, " must be a string that represents a DOM element ID");
case 'boolean':
case 'string':
case 'integer':
case 'number':
default:
return "The value for ".concat(name, " must be a ").concat(type, ".");
}
};
var validityCheck = function validityCheck(value, expectedType, permittedValues) {
switch (expectedType) {
case 'boolean':
return typeof value === 'boolean';
case 'string':
case 'id':
return typeof value === 'string';
case 'tristate':
return typeof value === 'boolean' || value === 'mixed';
case 'integer':
case 'number':
// Booleans resolve to 0/1 values so hard check that it's not first.
// eslint-disable-next-line no-restricted-globals
return typeof value !== 'boolean' && isNaN(Number(value)) === false;
case 'token':
return permittedValues.indexOf(typeof value === 'string' ? value.toLowerCase() : value) > -1;
case 'idlist':
return typeof value === 'string' && value.split(' ').every(function (token) {
return validityCheck(token, 'id', []);
});
case 'tokenlist':
return typeof value === 'string' && value.split(' ').every(function (token) {
return permittedValues.indexOf(token.toLowerCase()) > -1;
});
default:
return false;
}
};
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
validityCheck,
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/aria-proptypes.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXAttribute: function JSXAttribute(attribute) {
var name = (0, _jsxAstUtils.propName)(attribute);
var normalizedName = name.toLowerCase(); // Not a valid aria-* state or property.
if (normalizedName.indexOf('aria-') !== 0 || _ariaQuery.aria.get(normalizedName) === undefined) {
return;
} // Ignore the attribute if its value is null or undefined.
if ((0, _jsxAstUtils.getPropValue)(attribute) == null) return;
var value = (0, _jsxAstUtils.getLiteralPropValue)(attribute); // Ignore the attribute if its value is not a literal.
if (value === null) {
return;
} // These are the attributes of the property/state to check against.
var attributes = _ariaQuery.aria.get(normalizedName);
var permittedType = attributes.type;
var allowUndefined = attributes.allowUndefined || false;
var permittedValues = attributes.values || [];
var isValid = validityCheck(value, permittedType, permittedValues) || allowUndefined && value === undefined;
if (isValid) {
return;
}
context.report({
node: attribute,
message: errorMessage(name, permittedType, permittedValues)
});
}
};
}
};

View file

@ -0,0 +1,84 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce aria role attribute is valid.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'Elements with ARIA roles must use a valid, non-abstract ARIA role.';
var schema = (0, _schemas.generateObjSchema)({
ignoreNonDOM: {
type: 'boolean',
"default": false
}
});
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/aria-role.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXAttribute: function JSXAttribute(attribute) {
// Determine if ignoreNonDOM is set to true
// If true, then do not run rule.
var options = context.options[0] || {};
var ignoreNonDOM = !!options.ignoreNonDOM;
if (ignoreNonDOM) {
var type = (0, _jsxAstUtils.elementType)(attribute.parent);
if (!_ariaQuery.dom.get(type)) {
return;
}
} // Get prop name
var name = (0, _jsxAstUtils.propName)(attribute).toUpperCase();
if (name !== 'ROLE') {
return;
}
var value = (0, _jsxAstUtils.getLiteralPropValue)(attribute); // If value is undefined, then the role attribute will be dropped in the DOM.
// If value is null, then getLiteralAttributeValue is telling us that the
// value isn't in the form of a literal.
if (value === undefined || value === null) {
return;
}
var values = String(value).split(' ');
var validRoles = (0, _toConsumableArray2["default"])(_ariaQuery.roles.keys()).filter(function (role) {
return _ariaQuery.roles.get(role)["abstract"] === false;
});
var isValid = values.every(function (val) {
return validRoles.indexOf(val) > -1;
});
if (isValid === true) {
return;
}
context.report({
node: attribute,
message: errorMessage
});
}
};
}
};

View file

@ -0,0 +1,63 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce that elements that do not support ARIA roles,
* states and properties do not have those attributes.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = function errorMessage(invalidProp) {
return "This element does not support ARIA roles, states and properties. Try removing the prop '".concat(invalidProp, "'.");
};
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/aria-unsupported-elements.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var nodeType = (0, _jsxAstUtils.elementType)(node);
var nodeAttrs = _ariaQuery.dom.get(nodeType) || {};
var _nodeAttrs$reserved = nodeAttrs.reserved,
isReservedNodeType = _nodeAttrs$reserved === void 0 ? false : _nodeAttrs$reserved; // If it's not reserved, then it can have aria-* roles, states, and properties
if (isReservedNodeType === false) {
return;
}
var invalidAttributes = (0, _toConsumableArray2["default"])(_ariaQuery.aria.keys()).concat('role');
node.attributes.forEach(function (prop) {
if (prop.type === 'JSXSpreadAttribute') {
return;
}
var name = (0, _jsxAstUtils.propName)(prop).toLowerCase();
if (invalidAttributes.indexOf(name) > -1) {
context.report({
node,
message: errorMessage(name)
});
}
});
}
};
}
};

View file

@ -0,0 +1,68 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _axeCore = require("axe-core");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Ensure autocomplete attribute is correct.
* @author Wilco Fiers
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var schema = (0, _schemas.generateObjSchema)({
inputComponents: _schemas.arraySchema
});
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/autocomplete-valid.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var options = context.options[0] || {};
var _options$inputCompone = options.inputComponents,
inputComponents = _options$inputCompone === void 0 ? [] : _options$inputCompone;
var inputTypes = ['input'].concat((0, _toConsumableArray2["default"])(inputComponents));
var elType = (0, _jsxAstUtils.elementType)(node);
var autocomplete = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'autocomplete'));
if (typeof autocomplete !== 'string' || !inputTypes.includes(elType)) {
return;
}
var type = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'type'));
var _runVirtualRule = (0, _axeCore.runVirtualRule)('autocomplete-valid', {
nodeName: 'input',
attributes: {
autocomplete,
// Which autocomplete is valid depends on the input type
type: type === null ? undefined : type
}
}),
violations = _runVirtualRule.violations;
if (violations.length === 0) {
return;
} // Since we only test one rule, with one node, return the message from first (and only) instance of each
context.report({
node,
message: violations[0].nodes[0].all[0].message
});
}
};
}
};

View file

@ -0,0 +1,76 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _schemas = require("../util/schemas");
var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
var _isPresentationRole = _interopRequireDefault(require("../util/isPresentationRole"));
/**
* @fileoverview Enforce a clickable non-interactive element has at least 1 keyboard event listener.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'Visible, non-interactive elements with click handlers must have at least one keyboard listener.';
var schema = (0, _schemas.generateObjSchema)();
var domElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/click-events-have-key-events.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var props = node.attributes;
if ((0, _jsxAstUtils.getProp)(props, 'onclick') === undefined) {
return;
}
var type = (0, _jsxAstUtils.elementType)(node);
var requiredProps = ['onkeydown', 'onkeyup', 'onkeypress'];
if (!(0, _arrayIncludes["default"])(domElements, type)) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
return;
}
if ((0, _isHiddenFromScreenReader["default"])(type, props) || (0, _isPresentationRole["default"])(type, props)) {
return;
}
if ((0, _isInteractiveElement["default"])(type, props)) {
return;
}
if ((0, _jsxAstUtils.hasAnyProp)(props, requiredProps)) {
return;
} // Visible, non-interactive elements with click handlers require one keyboard event listener.
context.report({
node,
message: errorMessage
});
}
};
}
};

View file

@ -0,0 +1,109 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _jsxAstUtils = require("jsx-ast-utils");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _schemas = require("../util/schemas");
var _isDOMElement = _interopRequireDefault(require("../util/isDOMElement"));
var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
var _isInteractiveRole = _interopRequireDefault(require("../util/isInteractiveRole"));
var _mayHaveAccessibleLabel = _interopRequireDefault(require("../util/mayHaveAccessibleLabel"));
/**
* @fileoverview Enforce controls are associated with a text label.
* @author Jesse Beach
*
*
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'A control must be associated with a text label.';
var ignoreList = ['link'];
var schema = (0, _schemas.generateObjSchema)({
labelAttributes: _schemas.arraySchema,
controlComponents: _schemas.arraySchema,
ignoreElements: _schemas.arraySchema,
ignoreRoles: _schemas.arraySchema,
depth: {
description: 'JSX tree depth limit to check for accessible label',
type: 'integer',
minimum: 0
}
});
module.exports = {
meta: {
docs: {},
schema: [schema]
},
create: function create(context) {
var options = context.options[0] || {};
var _options$labelAttribu = options.labelAttributes,
labelAttributes = _options$labelAttribu === void 0 ? [] : _options$labelAttribu,
_options$controlCompo = options.controlComponents,
controlComponents = _options$controlCompo === void 0 ? [] : _options$controlCompo,
_options$ignoreElemen = options.ignoreElements,
ignoreElements = _options$ignoreElemen === void 0 ? [] : _options$ignoreElemen,
_options$ignoreRoles = options.ignoreRoles,
ignoreRoles = _options$ignoreRoles === void 0 ? [] : _options$ignoreRoles;
var newIgnoreElements = new Set([].concat((0, _toConsumableArray2["default"])(ignoreElements), ignoreList));
var rule = function rule(node) {
var tag = (0, _jsxAstUtils.elementType)(node.openingElement);
var role = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.openingElement.attributes, 'role')); // Ignore interactive elements that might get their label from a source
// that cannot be discerned from static analysis, like
// <label><input />Save</label>
if (newIgnoreElements.has(tag)) {
return;
} // Ignore roles that are "interactive" but should not require a label.
if ((0, _arrayIncludes["default"])(ignoreRoles, role)) {
return;
}
var props = node.openingElement.attributes;
var nodeIsDOMElement = (0, _isDOMElement["default"])(tag);
var nodeIsHiddenFromScreenReader = (0, _isHiddenFromScreenReader["default"])(tag, props);
var nodeIsInteractiveElement = (0, _isInteractiveElement["default"])(tag, props);
var nodeIsInteractiveRole = (0, _isInteractiveRole["default"])(tag, props);
var nodeIsControlComponent = controlComponents.indexOf(tag) > -1;
if (nodeIsHiddenFromScreenReader) {
return;
}
var hasAccessibleLabel = true;
if (nodeIsInteractiveElement || nodeIsDOMElement && nodeIsInteractiveRole || nodeIsControlComponent) {
// Prevent crazy recursion.
var recursionDepth = Math.min(options.depth === undefined ? 2 : options.depth, 25);
hasAccessibleLabel = (0, _mayHaveAccessibleLabel["default"])(node, recursionDepth, labelAttributes);
}
if (!hasAccessibleLabel) {
context.report({
node: node.openingElement,
message: errorMessage
});
}
}; // Create visitor selectors.
return {
JSXElement: rule
};
}
};

View file

@ -0,0 +1,59 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
var _hasAccessibleChild = _interopRequireDefault(require("../util/hasAccessibleChild"));
var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
/**
* @fileoverview Enforce heading (h1, h2, etc) elements contain accessible content.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'Headings must have content and the content must be accessible by a screen reader.';
var headings = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
var schema = (0, _schemas.generateObjSchema)({
components: _schemas.arraySchema
});
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/heading-has-content.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var options = context.options[0] || {};
var componentOptions = options.components || [];
var typeCheck = headings.concat(componentOptions);
var nodeType = (0, _jsxAstUtils.elementType)(node); // Only check 'h*' elements and custom types.
if (typeCheck.indexOf(nodeType) === -1) {
return;
}
if ((0, _hasAccessibleChild["default"])(node.parent)) {
return;
}
if ((0, _isHiddenFromScreenReader["default"])(nodeType, node.attributes)) {
return;
}
context.report({
node,
message: errorMessage
});
}
};
}
};

View file

@ -0,0 +1,45 @@
"use strict";
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce html element has lang prop.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = '<html> elements must have the lang prop.';
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/html-has-lang.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var type = (0, _jsxAstUtils.elementType)(node);
if (type && type !== 'html') {
return;
}
var lang = (0, _jsxAstUtils.getPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'lang'));
if (lang) {
return;
}
context.report({
node,
message: errorMessage
});
}
};
}
};

View file

@ -0,0 +1,45 @@
"use strict";
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce iframe elements have a title attribute.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = '<iframe> elements must have a unique title property.';
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/iframe-has-title.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var type = (0, _jsxAstUtils.elementType)(node);
if (type && type !== 'iframe') {
return;
}
var title = (0, _jsxAstUtils.getPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'title'));
if (title && typeof title === 'string') {
return;
}
context.report({
node,
message: errorMessage
});
}
};
}
};

View file

@ -0,0 +1,68 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
/**
* @fileoverview Enforce img alt attribute does not have the word image, picture, or photo.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var REDUNDANT_WORDS = ['image', 'photo', 'picture'];
var errorMessage = 'Redundant alt attribute. Screen-readers already announce `img` tags as an image. You dont need to use the words `image`, `photo,` or `picture` (or any specified custom words) in the alt prop.';
var schema = (0, _schemas.generateObjSchema)({
components: _schemas.arraySchema,
words: _schemas.arraySchema
});
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/img-redundant-alt.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var options = context.options[0] || {};
var componentOptions = options.components || [];
var typesToValidate = ['img'].concat(componentOptions);
var nodeType = (0, _jsxAstUtils.elementType)(node); // Only check 'label' elements and custom types.
if (typesToValidate.indexOf(nodeType) === -1) {
return;
}
var altProp = (0, _jsxAstUtils.getProp)(node.attributes, 'alt'); // Return if alt prop is not present.
if (altProp === undefined) {
return;
}
var value = (0, _jsxAstUtils.getLiteralPropValue)(altProp);
var isVisible = (0, _isHiddenFromScreenReader["default"])(nodeType, node.attributes) === false;
var _options$words = options.words,
words = _options$words === void 0 ? [] : _options$words;
var redundantWords = REDUNDANT_WORDS.concat(words);
if (typeof value === 'string' && isVisible) {
var hasRedundancy = new RegExp("(?!{)\\b(".concat(redundantWords.join('|'), ")\\b(?!})"), 'i').test(value);
if (hasRedundancy === true) {
context.report({
node,
message: errorMessage
});
}
}
}
};
}
};

View file

@ -0,0 +1,99 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _schemas = require("../util/schemas");
var _isDisabledElement = _interopRequireDefault(require("../util/isDisabledElement"));
var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
var _isInteractiveRole = _interopRequireDefault(require("../util/isInteractiveRole"));
var _isNonInteractiveElement = _interopRequireDefault(require("../util/isNonInteractiveElement"));
var _isNonInteractiveRole = _interopRequireDefault(require("../util/isNonInteractiveRole"));
var _isPresentationRole = _interopRequireDefault(require("../util/isPresentationRole"));
var _getTabIndex = _interopRequireDefault(require("../util/getTabIndex"));
/**
* @fileoverview Enforce that elements with onClick handlers must be tabbable.
* @author Ethan Cohen
*
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var schema = (0, _schemas.generateObjSchema)({
tabbable: (0, _schemas.enumArraySchema)((0, _toConsumableArray2["default"])(_ariaQuery.roles.keys()).filter(function (name) {
return !_ariaQuery.roles.get(name)["abstract"];
}).filter(function (name) {
return _ariaQuery.roles.get(name).superClass.some(function (klasses) {
return (0, _arrayIncludes["default"])(klasses, 'widget');
});
}))
});
var domElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
var interactiveProps = [].concat((0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.mouse), (0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.keyboard));
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/interactive-supports-focus.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var tabbable = context.options && context.options[0] && context.options[0].tabbable || [];
var attributes = node.attributes;
var type = (0, _jsxAstUtils.elementType)(node);
var hasInteractiveProps = (0, _jsxAstUtils.hasAnyProp)(attributes, interactiveProps);
var hasTabindex = (0, _getTabIndex["default"])((0, _jsxAstUtils.getProp)(attributes, 'tabIndex')) !== undefined;
if (!(0, _arrayIncludes["default"])(domElements, type)) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
return;
}
if (!hasInteractiveProps || (0, _isDisabledElement["default"])(attributes) || (0, _isHiddenFromScreenReader["default"])(type, attributes) || (0, _isPresentationRole["default"])(type, attributes)) {
// Presentation is an intentional signal from the author that this
// element is not meant to be perceivable. For example, a click screen
// to close a dialog .
return;
}
if (hasInteractiveProps && (0, _isInteractiveRole["default"])(type, attributes) && !(0, _isInteractiveElement["default"])(type, attributes) && !(0, _isNonInteractiveElement["default"])(type, attributes) && !(0, _isNonInteractiveRole["default"])(type, attributes) && !hasTabindex) {
var role = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(attributes, 'role'));
if ((0, _arrayIncludes["default"])(tabbable, role)) {
// Always tabbable, tabIndex = 0
context.report({
node,
message: "Elements with the '".concat(role, "' interactive role must be tabbable.")
});
} else {
// Focusable, tabIndex = -1 or 0
context.report({
node,
message: "Elements with the '".concat(role, "' interactive role must be focusable.")
});
}
}
}
};
}
};

View file

@ -0,0 +1,118 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
var _mayContainChildComponent = _interopRequireDefault(require("../util/mayContainChildComponent"));
var _mayHaveAccessibleLabel = _interopRequireDefault(require("../util/mayHaveAccessibleLabel"));
/**
* @fileoverview Enforce label tags have an associated control.
* @author Jesse Beach
*
*
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'A form label must be associated with a control.';
var schema = (0, _schemas.generateObjSchema)({
labelComponents: _schemas.arraySchema,
labelAttributes: _schemas.arraySchema,
controlComponents: _schemas.arraySchema,
assert: {
description: 'Assert that the label has htmlFor, a nested label, both or either',
type: 'string',
"enum": ['htmlFor', 'nesting', 'both', 'either']
},
depth: {
description: 'JSX tree depth limit to check for accessible label',
type: 'integer',
minimum: 0
}
});
var validateId = function validateId(node) {
var htmlForAttr = (0, _jsxAstUtils.getProp)(node.attributes, 'htmlFor');
var htmlForValue = (0, _jsxAstUtils.getPropValue)(htmlForAttr);
return htmlForAttr !== false && !!htmlForValue;
};
module.exports = {
meta: {
docs: {},
schema: [schema]
},
create: function create(context) {
var options = context.options[0] || {};
var labelComponents = options.labelComponents || [];
var assertType = options.assert || 'either';
var componentNames = ['label'].concat(labelComponents);
var rule = function rule(node) {
if (componentNames.indexOf((0, _jsxAstUtils.elementType)(node.openingElement)) === -1) {
return;
}
var controlComponents = ['input', 'meter', 'output', 'progress', 'select', 'textarea'].concat(options.controlComponents || []); // Prevent crazy recursion.
var recursionDepth = Math.min(options.depth === undefined ? 2 : options.depth, 25);
var hasLabelId = validateId(node.openingElement); // Check for multiple control components.
var hasNestedControl = controlComponents.some(function (name) {
return (0, _mayContainChildComponent["default"])(node, name, recursionDepth);
});
var hasAccessibleLabel = (0, _mayHaveAccessibleLabel["default"])(node, recursionDepth, options.labelAttributes);
if (hasAccessibleLabel) {
switch (assertType) {
case 'htmlFor':
if (hasLabelId) {
return;
}
break;
case 'nesting':
if (hasNestedControl) {
return;
}
break;
case 'both':
if (hasLabelId && hasNestedControl) {
return;
}
break;
case 'either':
if (hasLabelId || hasNestedControl) {
return;
}
break;
default:
break;
}
} // htmlFor case
context.report({
node: node.openingElement,
message: errorMessage
});
}; // Create visitor selectors.
return {
JSXElement: rule
};
}
};

View file

@ -0,0 +1,153 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
var _hasAccessibleChild = _interopRequireDefault(require("../util/hasAccessibleChild"));
/**
* @fileoverview Enforce label tags have htmlFor attribute.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var enumValues = ['nesting', 'id'];
var schema = {
type: 'object',
properties: {
components: _schemas.arraySchema,
required: {
oneOf: [{
type: 'string',
"enum": enumValues
}, (0, _schemas.generateObjSchema)({
some: (0, _schemas.enumArraySchema)(enumValues)
}, ['some']), (0, _schemas.generateObjSchema)({
every: (0, _schemas.enumArraySchema)(enumValues)
}, ['every'])]
},
allowChildren: {
type: 'boolean'
}
}
}; // Breadth-first search, assuming that HTML for forms is shallow.
function validateNesting(node) {
var queue = (0, _toConsumableArray2["default"])(node.parent.children);
var child;
var opener;
while (queue.length) {
child = queue.shift();
opener = child.openingElement;
if (child.type === 'JSXElement' && opener && (opener.name.name === 'input' || opener.name.name === 'textarea' || opener.name.name === 'select')) {
return true;
}
if (child.children) {
queue = queue.concat(child.children);
}
}
return false;
}
var validateId = function validateId(node) {
var htmlForAttr = (0, _jsxAstUtils.getProp)(node.attributes, 'htmlFor');
var htmlForValue = (0, _jsxAstUtils.getPropValue)(htmlForAttr);
return htmlForAttr !== false && !!htmlForValue;
};
var validate = function validate(node, required, allowChildren) {
if (allowChildren === true) {
return (0, _hasAccessibleChild["default"])(node.parent);
}
if (required === 'nesting') {
return validateNesting(node);
}
return validateId(node);
};
var getValidityStatus = function getValidityStatus(node, required, allowChildren) {
if (Array.isArray(required.some)) {
var _isValid = required.some.some(function (rule) {
return validate(node, rule, allowChildren);
});
var _message = !_isValid ? "Form label must have ANY of the following types of associated control: ".concat(required.some.join(', ')) : null;
return {
isValid: _isValid,
message: _message
};
}
if (Array.isArray(required.every)) {
var _isValid2 = required.every.every(function (rule) {
return validate(node, rule, allowChildren);
});
var _message2 = !_isValid2 ? "Form label must have ALL of the following types of associated control: ".concat(required.every.join(', ')) : null;
return {
isValid: _isValid2,
message: _message2
};
}
var isValid = validate(node, required, allowChildren);
var message = !isValid ? "Form label must have the following type of associated control: ".concat(required) : null;
return {
isValid,
message
};
};
module.exports = {
meta: {
deprecated: true,
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/label-has-for.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var options = context.options[0] || {};
var componentOptions = options.components || [];
var typesToValidate = ['label'].concat(componentOptions);
var nodeType = (0, _jsxAstUtils.elementType)(node); // Only check 'label' elements and custom types.
if (typesToValidate.indexOf(nodeType) === -1) {
return;
}
var required = options.required || {
every: ['nesting', 'id']
};
var allowChildren = options.allowChildren || false;
var _getValidityStatus = getValidityStatus(node, required, allowChildren),
isValid = _getValidityStatus.isValid,
message = _getValidityStatus.message;
if (!isValid) {
context.report({
node,
message
});
}
}
};
}
};

View file

@ -0,0 +1,68 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _jsxAstUtils = require("jsx-ast-utils");
var _languageTags = _interopRequireDefault(require("language-tags"));
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce lang attribute has a valid value.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'lang attribute must have a valid value.';
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/lang.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXAttribute: function JSXAttribute(node) {
var name = (0, _jsxAstUtils.propName)(node);
if (name && name.toUpperCase() !== 'LANG') {
return;
}
var parent = node.parent;
var type = (0, _jsxAstUtils.elementType)(parent);
if (type && type !== 'html') {
return;
}
var value = (0, _jsxAstUtils.getLiteralPropValue)(node); // Don't check identifiers
if (value === null) {
return;
}
if (value === undefined) {
context.report({
node,
message: errorMessage
});
return;
}
if (_languageTags["default"].check(value)) {
return;
}
context.report({
node,
message: errorMessage
});
}
};
}
};

View file

@ -0,0 +1,98 @@
"use strict";
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview <audio> and <video> elements must have a <track> for captions.
* @author Ethan Cohen
*
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'Media elements such as <audio> and <video> must have a <track> for captions.';
var MEDIA_TYPES = ['audio', 'video'];
var schema = (0, _schemas.generateObjSchema)({
audio: _schemas.arraySchema,
video: _schemas.arraySchema,
track: _schemas.arraySchema
});
var isMediaType = function isMediaType(context, type) {
var options = context.options[0] || {};
return MEDIA_TYPES.map(function (mediaType) {
return options[mediaType];
}).reduce(function (types, customComponent) {
return types.concat(customComponent);
}, MEDIA_TYPES).some(function (typeToCheck) {
return typeToCheck === type;
});
};
var isTrackType = function isTrackType(context, type) {
var options = context.options[0] || {};
return ['track'].concat(options.track || []).some(function (typeToCheck) {
return typeToCheck === type;
});
};
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/media-has-caption.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXElement: function JSXElement(node) {
var element = node.openingElement;
var type = (0, _jsxAstUtils.elementType)(element);
if (!isMediaType(context, type)) {
return;
}
var mutedProp = (0, _jsxAstUtils.getProp)(element.attributes, 'muted');
var mutedPropVal = (0, _jsxAstUtils.getLiteralPropValue)(mutedProp);
if (mutedPropVal === true) {
return;
} // $FlowFixMe https://github.com/facebook/flow/issues/1414
var trackChildren = node.children.filter(function (child) {
if (child.type !== 'JSXElement') {
return false;
} // $FlowFixMe https://github.com/facebook/flow/issues/1414
return isTrackType(context, (0, _jsxAstUtils.elementType)(child.openingElement));
});
if (trackChildren.length === 0) {
context.report({
node: element,
message: errorMessage
});
return;
}
var hasCaption = trackChildren.some(function (track) {
var kindProp = (0, _jsxAstUtils.getProp)(track.openingElement.attributes, 'kind');
var kindPropValue = (0, _jsxAstUtils.getLiteralPropValue)(kindProp) || '';
return kindPropValue.toLowerCase() === 'captions';
});
if (!hasCaption) {
context.report({
node: element,
message: errorMessage
});
}
}
};
}
};

View file

@ -0,0 +1,71 @@
"use strict";
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce onmouseover/onmouseout are
* accompanied by onfocus/onblur.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var mouseOverErrorMessage = 'onMouseOver must be accompanied by onFocus for accessibility.';
var mouseOutErrorMessage = 'onMouseOut must be accompanied by onBlur for accessibility.';
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/mouse-events-have-key-events.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var name = node.name.name;
if (!_ariaQuery.dom.get(name)) {
return;
}
var attributes = node.attributes; // Check onmouseover / onfocus pairing.
var onMouseOver = (0, _jsxAstUtils.getProp)(attributes, 'onMouseOver');
var onMouseOverValue = (0, _jsxAstUtils.getPropValue)(onMouseOver);
if (onMouseOver && onMouseOverValue != null) {
var hasOnFocus = (0, _jsxAstUtils.getProp)(attributes, 'onFocus');
var onFocusValue = (0, _jsxAstUtils.getPropValue)(hasOnFocus);
if (hasOnFocus === false || onFocusValue === null || onFocusValue === undefined) {
context.report({
node,
message: mouseOverErrorMessage
});
}
} // Checkout onmouseout / onblur pairing
var onMouseOut = (0, _jsxAstUtils.getProp)(attributes, 'onMouseOut');
var onMouseOutValue = (0, _jsxAstUtils.getPropValue)(onMouseOut);
if (onMouseOut && onMouseOutValue != null) {
var hasOnBlur = (0, _jsxAstUtils.getProp)(attributes, 'onBlur');
var onBlurValue = (0, _jsxAstUtils.getPropValue)(hasOnBlur);
if (hasOnBlur === false || onBlurValue === null || onBlurValue === undefined) {
context.report({
node,
message: mouseOutErrorMessage
});
}
}
}
};
}
};

View file

@ -0,0 +1,38 @@
"use strict";
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce no accesskey attribute on element.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'No access key attribute allowed. Inconsistencies between keyboard shortcuts and keyboard comments used by screenreader and keyboard only users create a11y complications.';
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-access-key.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var accessKey = (0, _jsxAstUtils.getProp)(node.attributes, 'accesskey');
var accessKeyValue = (0, _jsxAstUtils.getPropValue)(accessKey);
if (accessKey && accessKeyValue) {
context.report({
node,
message: errorMessage
});
}
}
};
}
};

View file

@ -0,0 +1,56 @@
"use strict";
var _jsxAstUtils = require("jsx-ast-utils");
var _ariaQuery = require("aria-query");
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce autoFocus prop is not used.
* @author Ethan Cohen <@evcohen>
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'The autoFocus prop should not be used, as it can reduce usability and accessibility for users.';
var schema = (0, _schemas.generateObjSchema)({
ignoreNonDOM: {
type: 'boolean',
"default": false
}
});
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-autofocus.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXAttribute: function JSXAttribute(attribute) {
// Determine if ignoreNonDOM is set to true
// If true, then do not run rule.
var options = context.options[0] || {};
var ignoreNonDOM = !!options.ignoreNonDOM;
if (ignoreNonDOM) {
var type = (0, _jsxAstUtils.elementType)(attribute.parent);
if (!_ariaQuery.dom.get(type)) {
return;
}
} // Don't normalize, since React only recognizes autoFocus on low-level DOM elements.
if ((0, _jsxAstUtils.propName)(attribute) === 'autoFocus') {
context.report({
node: attribute,
message: errorMessage
});
}
}
};
}
};

View file

@ -0,0 +1,48 @@
"use strict";
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce distracting elements are not used.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = function errorMessage(element) {
return "Do not use <".concat(element, "> elements as they can create visual accessibility issues and are deprecated.");
};
var DEFAULT_ELEMENTS = ['marquee', 'blink'];
var schema = (0, _schemas.generateObjSchema)({
elements: (0, _schemas.enumArraySchema)(DEFAULT_ELEMENTS)
});
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-distracting-elements.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var options = context.options[0] || {};
var elementOptions = options.elements || DEFAULT_ELEMENTS;
var type = (0, _jsxAstUtils.elementType)(node);
var distractingElement = elementOptions.find(function (element) {
return type === element;
});
if (distractingElement) {
context.report({
node,
message: errorMessage(distractingElement)
});
}
}
};
}
};

View file

@ -0,0 +1,87 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _has = _interopRequireDefault(require("has"));
var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
var _isNonInteractiveRole = _interopRequireDefault(require("../util/isNonInteractiveRole"));
var _isPresentationRole = _interopRequireDefault(require("../util/isPresentationRole"));
/**
* @fileoverview Disallow inherently interactive elements to be assigned
* non-interactive roles.
* @author Jesse Beach
*
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'Interactive elements should not be assigned non-interactive roles.';
var domElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-interactive-element-to-noninteractive-role.md'
},
schema: [{
type: 'object',
additionalProperties: {
type: 'array',
items: {
type: 'string'
},
uniqueItems: true
}
}]
},
create: function create(context) {
var options = context.options;
return {
JSXAttribute: function JSXAttribute(attribute) {
var attributeName = (0, _jsxAstUtils.propName)(attribute); // $FlowFixMe: [TODO] Mark propName as a JSXIdentifier, not a string.
if (attributeName !== 'role') {
return;
}
var node = attribute.parent;
var attributes = node.attributes;
var type = (0, _jsxAstUtils.elementType)(node);
var role = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'role'));
if (!(0, _arrayIncludes["default"])(domElements, type)) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
return;
} // Allow overrides from rule configuration for specific elements and
// roles.
var allowedRoles = options[0] || {};
if ((0, _has["default"])(allowedRoles, type) && (0, _arrayIncludes["default"])(allowedRoles[type], role)) {
return;
}
if ((0, _isInteractiveElement["default"])(type, attributes) && ((0, _isNonInteractiveRole["default"])(type, attributes) || (0, _isPresentationRole["default"])(type, attributes))) {
// Visible, non-interactive elements should not have an interactive handler.
context.report({
node: attribute,
message: errorMessage
});
}
}
};
}
};

View file

@ -0,0 +1,97 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _has = _interopRequireDefault(require("has"));
var _schemas = require("../util/schemas");
var _isAbstractRole = _interopRequireDefault(require("../util/isAbstractRole"));
var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
var _isInteractiveRole = _interopRequireDefault(require("../util/isInteractiveRole"));
var _isNonInteractiveElement = _interopRequireDefault(require("../util/isNonInteractiveElement"));
var _isNonInteractiveRole = _interopRequireDefault(require("../util/isNonInteractiveRole"));
var _isPresentationRole = _interopRequireDefault(require("../util/isPresentationRole"));
/**
* @fileoverview Enforce non-interactive elements have no interactive handlers.
* @author Jese Beach
*
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'Non-interactive elements should not be assigned mouse or keyboard event listeners.';
var domElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
var defaultInteractiveProps = [].concat((0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.focus), (0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.image), (0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.keyboard), (0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.mouse));
var schema = (0, _schemas.generateObjSchema)({
handlers: _schemas.arraySchema
});
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-noninteractive-element-interactions.md'
},
schema: [schema]
},
create: function create(context) {
var options = context.options;
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var attributes = node.attributes;
var type = (0, _jsxAstUtils.elementType)(node);
var config = options[0] || {};
var interactiveProps = config.handlers || defaultInteractiveProps; // Allow overrides from rule configuration for specific elements and roles.
if ((0, _has["default"])(config, type)) {
attributes = attributes.filter(function (attr) {
return attr.type !== 'JSXSpreadAttribute' && !(0, _arrayIncludes["default"])(config[type], (0, _jsxAstUtils.propName)(attr));
});
}
var hasInteractiveProps = interactiveProps.some(function (prop) {
return (0, _jsxAstUtils.hasProp)(attributes, prop) && (0, _jsxAstUtils.getPropValue)((0, _jsxAstUtils.getProp)(attributes, prop)) != null;
});
if (!(0, _arrayIncludes["default"])(domElements, type)) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
return;
}
if (!hasInteractiveProps || (0, _isHiddenFromScreenReader["default"])(type, attributes) || (0, _isPresentationRole["default"])(type, attributes)) {
// Presentation is an intentional signal from the author that this
// element is not meant to be perceivable. For example, a click screen
// to close a dialog .
return;
}
if ((0, _isInteractiveElement["default"])(type, attributes) || (0, _isInteractiveRole["default"])(type, attributes) || !(0, _isNonInteractiveElement["default"])(type, attributes) && !(0, _isNonInteractiveRole["default"])(type, attributes) || (0, _isAbstractRole["default"])(type, attributes)) {
// This rule has no opinion about abtract roles.
return;
} // Visible, non-interactive elements should not have an interactive handler.
context.report({
node,
message: errorMessage
});
}
};
}
};

View file

@ -0,0 +1,86 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _has = _interopRequireDefault(require("has"));
var _getExplicitRole = _interopRequireDefault(require("../util/getExplicitRole"));
var _isNonInteractiveElement = _interopRequireDefault(require("../util/isNonInteractiveElement"));
var _isInteractiveRole = _interopRequireDefault(require("../util/isInteractiveRole"));
/**
* @fileoverview Disallow inherently non-interactive elements to be assigned
* interactive roles.
* @author Jesse Beach
*
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'Non-interactive elements should not be assigned interactive roles.';
var domElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-noninteractive-element-to-interactive-role.md'
},
schema: [{
type: 'object',
additionalProperties: {
type: 'array',
items: {
type: 'string'
},
uniqueItems: true
}
}]
},
create: function create(context) {
var options = context.options;
return {
JSXAttribute: function JSXAttribute(attribute) {
var attributeName = (0, _jsxAstUtils.propName)(attribute); // $FlowFixMe: [TODO] Mark propName as a JSXIdentifier, not a string.
if (attributeName !== 'role') {
return;
}
var node = attribute.parent;
var attributes = node.attributes;
var type = (0, _jsxAstUtils.elementType)(node);
var role = (0, _getExplicitRole["default"])(type, node.attributes);
if (!(0, _arrayIncludes["default"])(domElements, type)) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
return;
} // Allow overrides from rule configuration for specific elements and
// roles.
var allowedRoles = options[0] || {};
if ((0, _has["default"])(allowedRoles, type) && (0, _arrayIncludes["default"])(allowedRoles[type], role)) {
return;
}
if ((0, _isNonInteractiveElement["default"])(type, attributes) && (0, _isInteractiveRole["default"])(type, attributes)) {
context.report({
node: attribute,
message: errorMessage
});
}
}
};
}
};

View file

@ -0,0 +1,95 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
var _isInteractiveRole = _interopRequireDefault(require("../util/isInteractiveRole"));
var _isNonLiteralProperty = _interopRequireDefault(require("../util/isNonLiteralProperty"));
var _schemas = require("../util/schemas");
var _getTabIndex = _interopRequireDefault(require("../util/getTabIndex"));
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
var errorMessage = '`tabIndex` should only be declared on interactive elements.';
var schema = (0, _schemas.generateObjSchema)({
roles: _objectSpread(_objectSpread({}, _schemas.arraySchema), {}, {
description: 'An array of ARIA roles'
}),
tags: _objectSpread(_objectSpread({}, _schemas.arraySchema), {}, {
description: 'An array of HTML tag names'
})
});
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-noninteractive-tabindex.md'
},
schema: [schema]
},
create: function create(context) {
var options = context.options;
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var type = (0, _jsxAstUtils.elementType)(node);
var attributes = node.attributes;
var tabIndexProp = (0, _jsxAstUtils.getProp)(attributes, 'tabIndex');
var tabIndex = (0, _getTabIndex["default"])(tabIndexProp); // Early return;
if (typeof tabIndex === 'undefined') {
return;
}
var role = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'role'));
if (!_ariaQuery.dom.has(type)) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
return;
} // Allow for configuration overrides.
var _ref = options[0] || {},
tags = _ref.tags,
roles = _ref.roles,
allowExpressionValues = _ref.allowExpressionValues;
if (tags && (0, _arrayIncludes["default"])(tags, type)) {
return;
}
if (roles && (0, _arrayIncludes["default"])(roles, role)) {
return;
}
if (allowExpressionValues === true && (0, _isNonLiteralProperty["default"])(attributes, 'role')) {
return;
}
if ((0, _isInteractiveElement["default"])(type, attributes) || (0, _isInteractiveRole["default"])(type, attributes)) {
return;
}
if (tabIndex >= 0) {
context.report({
node: tabIndexProp,
message: errorMessage
});
}
}
};
}
};

View file

@ -0,0 +1,45 @@
"use strict";
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce usage of onBlur over onChange for accessibility.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'onBlur must be used instead of onchange, unless absolutely necessary and it causes no negative consequences for keyboard only or screen reader users.';
var applicableTypes = ['select', 'option'];
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-onchange.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var nodeType = (0, _jsxAstUtils.elementType)(node);
if (applicableTypes.indexOf(nodeType) === -1) {
return;
}
var onChange = (0, _jsxAstUtils.getProp)(node.attributes, 'onChange');
var hasOnBlur = (0, _jsxAstUtils.getProp)(node.attributes, 'onBlur') !== undefined;
if (onChange && !hasOnBlur) {
context.report({
node,
message: errorMessage
});
}
}
};
}
};

View file

@ -0,0 +1,81 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _jsxAstUtils = require("jsx-ast-utils");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _has = _interopRequireDefault(require("has"));
var _getExplicitRole = _interopRequireDefault(require("../util/getExplicitRole"));
var _getImplicitRole = _interopRequireDefault(require("../util/getImplicitRole"));
/**
* @fileoverview Enforce explicit role property is not the
* same as implicit/default role property on element.
* @author Ethan Cohen <@evcohen>
*
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = function errorMessage(element, implicitRole) {
return "The element ".concat(element, " has an implicit role of ").concat(implicitRole, ". Defining this explicitly is redundant and should be avoided.");
};
var DEFAULT_ROLE_EXCEPTIONS = {
nav: ['navigation']
};
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-redundant-roles.md'
},
schema: [{
type: 'object',
additionalProperties: {
type: 'array',
items: {
type: 'string'
},
uniqueItems: true
}
}]
},
create: function create(context) {
var options = context.options;
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var type = (0, _jsxAstUtils.elementType)(node);
var implicitRole = (0, _getImplicitRole["default"])(type, node.attributes);
var explicitRole = (0, _getExplicitRole["default"])(type, node.attributes);
if (!implicitRole || !explicitRole) {
return;
}
if (implicitRole === explicitRole) {
var allowedRedundantRoles = options[0] || {};
var redundantRolesForElement;
if ((0, _has["default"])(allowedRedundantRoles, type)) {
redundantRolesForElement = allowedRedundantRoles[type];
} else {
redundantRolesForElement = DEFAULT_ROLE_EXCEPTIONS[type] || [];
}
if ((0, _arrayIncludes["default"])(redundantRolesForElement, implicitRole)) {
return;
}
context.report({
node,
message: errorMessage(type, implicitRole.toLowerCase())
});
}
}
};
}
};

View file

@ -0,0 +1,99 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _schemas = require("../util/schemas");
var _isAbstractRole = _interopRequireDefault(require("../util/isAbstractRole"));
var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
var _isInteractiveRole = _interopRequireDefault(require("../util/isInteractiveRole"));
var _isNonInteractiveElement = _interopRequireDefault(require("../util/isNonInteractiveElement"));
var _isNonInteractiveRole = _interopRequireDefault(require("../util/isNonInteractiveRole"));
var _isNonLiteralProperty = _interopRequireDefault(require("../util/isNonLiteralProperty"));
var _isPresentationRole = _interopRequireDefault(require("../util/isPresentationRole"));
/**
* @fileoverview Enforce static elements have no interactive handlers.
* @author Ethan Cohen
*
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'Static HTML elements with event handlers require a role.';
var domElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
var defaultInteractiveProps = [].concat((0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.focus), (0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.keyboard), (0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.mouse));
var schema = (0, _schemas.generateObjSchema)({
handlers: _schemas.arraySchema
});
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-static-element-interactions.md'
},
schema: [schema]
},
create: function create(context) {
var options = context.options;
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var attributes = node.attributes;
var type = (0, _jsxAstUtils.elementType)(node);
var _ref = options[0] || {},
allowExpressionValues = _ref.allowExpressionValues,
_ref$handlers = _ref.handlers,
handlers = _ref$handlers === void 0 ? defaultInteractiveProps : _ref$handlers;
var hasInteractiveProps = handlers.some(function (prop) {
return (0, _jsxAstUtils.hasProp)(attributes, prop) && (0, _jsxAstUtils.getPropValue)((0, _jsxAstUtils.getProp)(attributes, prop)) != null;
});
if (!(0, _arrayIncludes["default"])(domElements, type)) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
return;
}
if (!hasInteractiveProps || (0, _isHiddenFromScreenReader["default"])(type, attributes) || (0, _isPresentationRole["default"])(type, attributes)) {
// Presentation is an intentional signal from the author that this
// element is not meant to be perceivable. For example, a click screen
// to close a dialog .
return;
}
if ((0, _isInteractiveElement["default"])(type, attributes) || (0, _isInteractiveRole["default"])(type, attributes) || (0, _isNonInteractiveElement["default"])(type, attributes) || (0, _isNonInteractiveRole["default"])(type, attributes) || (0, _isAbstractRole["default"])(type, attributes)) {
// This rule has no opinion about abstract roles.
return;
}
if (allowExpressionValues === true && (0, _isNonLiteralProperty["default"])(attributes, 'role')) {
// This rule has no opinion about non-literal roles.
return;
} // Visible, non-interactive elements should not have an interactive handler.
context.report({
node,
message: errorMessage
});
}
};
}
};

View file

@ -0,0 +1,92 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
var _isSemanticRoleElement = _interopRequireDefault(require("../util/isSemanticRoleElement"));
/**
* @fileoverview Enforce that elements with ARIA roles must
* have all required attributes for that role.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = function errorMessage(role, requiredProps) {
return "Elements with the ARIA role \"".concat(role, "\" must have the following attributes defined: ").concat(String(requiredProps).toLowerCase());
};
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/role-has-required-aria-props.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXAttribute: function JSXAttribute(attribute) {
var name = (0, _jsxAstUtils.propName)(attribute).toLowerCase();
if (name !== 'role') {
return;
}
var type = (0, _jsxAstUtils.elementType)(attribute.parent);
if (!_ariaQuery.dom.get(type)) {
return;
}
var roleAttrValue = (0, _jsxAstUtils.getLiteralPropValue)(attribute);
var attributes = attribute.parent.attributes; // If value is undefined, then the role attribute will be dropped in the DOM.
// If value is null, then getLiteralAttributeValue is telling us
// that the value isn't in the form of a literal.
if (roleAttrValue === undefined || roleAttrValue === null) {
return;
}
var normalizedValues = String(roleAttrValue).toLowerCase().split(' ');
var validRoles = normalizedValues.filter(function (val) {
return (0, _toConsumableArray2["default"])(_ariaQuery.roles.keys()).indexOf(val) > -1;
}); // Check semantic DOM elements
// For example, <input type="checkbox" role="switch" />
if ((0, _isSemanticRoleElement["default"])(type, attributes)) {
return;
} // Check arbitrary DOM elements
validRoles.forEach(function (role) {
var _roles$get = _ariaQuery.roles.get(role),
requiredPropKeyValues = _roles$get.requiredProps;
var requiredProps = Object.keys(requiredPropKeyValues);
if (requiredProps.length > 0) {
var hasRequiredProps = requiredProps.every(function (prop) {
return (0, _jsxAstUtils.getProp)(attribute.parent.attributes, prop);
});
if (hasRequiredProps === false) {
context.report({
node: attribute,
message: errorMessage(role.toLowerCase(), requiredProps)
});
}
}
});
}
};
}
};

View file

@ -0,0 +1,79 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
var _getImplicitRole = _interopRequireDefault(require("../util/getImplicitRole"));
/**
* @fileoverview Enforce that elements with explicit or implicit roles defined contain only
* `aria-*` properties supported by that `role`.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = function errorMessage(attr, role, tag, isImplicit) {
if (isImplicit) {
return "The attribute ".concat(attr, " is not supported by the role ").concat(role, ". This role is implicit on the element ").concat(tag, ".");
}
return "The attribute ".concat(attr, " is not supported by the role ").concat(role, ".");
};
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/role-supports-aria-props.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
// If role is not explicitly defined, then try and get its implicit role.
var type = (0, _jsxAstUtils.elementType)(node);
var role = (0, _jsxAstUtils.getProp)(node.attributes, 'role');
var roleValue = role ? (0, _jsxAstUtils.getLiteralPropValue)(role) : (0, _getImplicitRole["default"])(type, node.attributes);
var isImplicit = roleValue && role === undefined; // If there is no explicit or implicit role, then assume that the element
// can handle the global set of aria-* properties.
// This actually isn't true - should fix in future release.
if (typeof roleValue !== 'string' || _ariaQuery.roles.get(roleValue) === undefined) {
return;
} // Make sure it has no aria-* properties defined outside of its property set.
var _roles$get = _ariaQuery.roles.get(roleValue),
propKeyValues = _roles$get.props;
var propertySet = Object.keys(propKeyValues);
var invalidAriaPropsForRole = (0, _toConsumableArray2["default"])(_ariaQuery.aria.keys()).filter(function (attribute) {
return propertySet.indexOf(attribute) === -1;
});
node.attributes.forEach(function (prop) {
// Ignore the attribute if its value is null or undefined.
if ((0, _jsxAstUtils.getPropValue)(prop) == null) return; // Ignore the attribute if it's a spread.
if (prop.type === 'JSXSpreadAttribute') return;
var name = (0, _jsxAstUtils.propName)(prop);
if (invalidAriaPropsForRole.indexOf(name) > -1) {
context.report({
node,
message: errorMessage(name, roleValue, type, isImplicit)
});
}
});
}
};
}
};

View file

@ -0,0 +1,53 @@
"use strict";
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce scope prop is only used on <th> elements.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'The scope prop can only be used on <th> elements.';
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/scope.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXAttribute: function JSXAttribute(node) {
var name = (0, _jsxAstUtils.propName)(node);
if (name && name.toUpperCase() !== 'SCOPE') {
return;
}
var parent = node.parent;
var tagName = (0, _jsxAstUtils.elementType)(parent); // Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
if (!_ariaQuery.dom.has(tagName)) {
return;
}
if (tagName && tagName.toUpperCase() === 'TH') {
return;
}
context.report({
node,
message: errorMessage
});
}
};
}
};

View file

@ -0,0 +1,46 @@
"use strict";
var _jsxAstUtils = require("jsx-ast-utils");
var _schemas = require("../util/schemas");
/**
* @fileoverview Enforce tabIndex value is not greater than zero.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'Avoid positive integer values for tabIndex.';
var schema = (0, _schemas.generateObjSchema)();
module.exports = {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/tabindex-no-positive.md'
},
schema: [schema]
},
create: function create(context) {
return {
JSXAttribute: function JSXAttribute(attribute) {
var name = (0, _jsxAstUtils.propName)(attribute).toUpperCase(); // Check if tabIndex is the attribute
if (name !== 'TABINDEX') {
return;
} // Only check literals because we can't infer values from certain expressions.
var value = Number((0, _jsxAstUtils.getLiteralPropValue)(attribute)); // eslint-disable-next-line no-restricted-globals
if (isNaN(value) || value <= 0) {
return;
}
context.report({
node: attribute,
message: errorMessage
});
}
};
}
};

View file

@ -0,0 +1,40 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns true if all items in baseAttributes are found in attributes. Always
* returns true if baseAttributes is empty.
*/
function attributesComparator() {
var baseAttributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
return baseAttributes.every(function (baseAttr) {
return attributes.some(function (attribute) {
// Guard against non-JSXAttribute nodes like JSXSpreadAttribute
if (attribute.type !== 'JSXAttribute') {
return false;
} // Attribute matches.
if (baseAttr.name !== (0, _jsxAstUtils.propName)(attribute)) {
return false;
} // Value exists and does not match.
if (baseAttr.value && baseAttr.value !== (0, _jsxAstUtils.getLiteralPropValue)(attribute)) {
return false;
}
return true;
});
});
}
var _default = attributesComparator;
exports["default"] = _default;

View file

@ -0,0 +1,22 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getComputedRole;
var _getExplicitRole = _interopRequireDefault(require("./getExplicitRole"));
var _getImplicitRole = _interopRequireDefault(require("./getImplicitRole"));
/**
* Returns an element's computed role, which is
*
* 1. The valid value of its explicit role attribute; or
* 2. The implicit value of its tag.
*/
function getComputedRole(tag, attributes) {
return (0, _getExplicitRole["default"])(tag, attributes) || (0, _getImplicitRole["default"])(tag, attributes);
}

View file

@ -0,0 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getExplicitRole;
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns an element's computed role, which is
*
* 1. The valid value of its explicit role attribute; or
* 2. The implicit value of its tag.
*/
function getExplicitRole(tag, attributes) {
var explicitRole = function toLowerCase(role) {
if (typeof role === 'string') {
return role.toLowerCase();
}
return null;
}((0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(attributes, 'role')));
if (_ariaQuery.roles.has(explicitRole)) {
return explicitRole;
}
return null;
}

View file

@ -0,0 +1,30 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRole;
var _ariaQuery = require("aria-query");
var _implicitRoles = _interopRequireDefault(require("./implicitRoles"));
/**
* Returns an element's implicit role given its attributes and type.
* Some elements only have an implicit role when certain props are defined.
*/
function getImplicitRole(type, attributes) {
var implicitRole;
if (_implicitRoles["default"][type]) {
implicitRole = _implicitRoles["default"][type](attributes);
}
if (_ariaQuery.roles.has(implicitRole)) {
return implicitRole;
}
return null;
}

View file

@ -0,0 +1,35 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getSuggestion;
var _damerauLevenshtein = _interopRequireDefault(require("damerau-levenshtein"));
// Minimum edit distance to be considered a good suggestion.
var THRESHOLD = 2;
/**
* Returns an array of suggestions given a word and a dictionary and limit of suggestions
* to return.
*/
function getSuggestion(word) {
var dictionary = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var limit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2;
var distances = dictionary.reduce(function (suggestions, dictionaryWord) {
var distance = (0, _damerauLevenshtein["default"])(word.toUpperCase(), dictionaryWord.toUpperCase());
var steps = distance.steps;
suggestions[dictionaryWord] = steps; // eslint-disable-line
return suggestions;
}, {});
return Object.keys(distances).filter(function (suggestion) {
return distances[suggestion] <= THRESHOLD;
}).sort(function (a, b) {
return distances[a] - distances[b];
}) // Sort by distance
.slice(0, limit);
}

View file

@ -0,0 +1,37 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getTabIndex;
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns the tabIndex value.
*/
function getTabIndex(tabIndex) {
var literalValue = (0, _jsxAstUtils.getLiteralPropValue)(tabIndex); // String and number values.
if (['string', 'number'].indexOf(typeof literalValue) > -1) {
// Empty string will convert to zero, so check for it explicity.
if (typeof literalValue === 'string' && literalValue.length === 0) {
return undefined;
}
var value = Number(literalValue);
if (Number.isNaN(value)) {
return undefined;
}
return Number.isInteger(value) ? value : undefined;
} // Booleans are not valid values, return undefined.
if (literalValue === true || literalValue === false) {
return undefined;
}
return (0, _jsxAstUtils.getPropValue)(tabIndex);
}

View file

@ -0,0 +1,35 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = hasAccessibleChild;
var _jsxAstUtils = require("jsx-ast-utils");
var _isHiddenFromScreenReader = _interopRequireDefault(require("./isHiddenFromScreenReader"));
function hasAccessibleChild(node) {
return node.children.some(function (child) {
switch (child.type) {
case 'Literal':
case 'JSXText':
return Boolean(child.value);
case 'JSXElement':
return !(0, _isHiddenFromScreenReader["default"])((0, _jsxAstUtils.elementType)(child.openingElement), child.openingElement.attributes);
case 'JSXExpressionContainer':
if (child.expression.type === 'Identifier') {
return child.expression.name !== 'undefined';
}
return true;
default:
return false;
}
}) || (0, _jsxAstUtils.hasAnyProp)(node.openingElement.attributes, ['dangerouslySetInnerHTML', 'children']);
}

View file

@ -0,0 +1,19 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForAnchor;
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns the implicit role for an anchor tag.
*/
function getImplicitRoleForAnchor(attributes) {
if ((0, _jsxAstUtils.getProp)(attributes, 'href')) {
return 'link';
}
return '';
}

View file

@ -0,0 +1,19 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForArea;
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns the implicit role for an area tag.
*/
function getImplicitRoleForArea(attributes) {
if ((0, _jsxAstUtils.getProp)(attributes, 'href')) {
return 'link';
}
return '';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForArticle;
/**
* Returns the implicit role for an article tag.
*/
function getImplicitRoleForArticle() {
return 'article';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForAside;
/**
* Returns the implicit role for an aside tag.
*/
function getImplicitRoleForAside() {
return 'complementary';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForBody;
/**
* Returns the implicit role for a body tag.
*/
function getImplicitRoleForBody() {
return 'document';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForButton;
/**
* Returns the implicit role for a button tag.
*/
function getImplicitRoleForButton() {
return 'button';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForDatalist;
/**
* Returns the implicit role for a datalist tag.
*/
function getImplicitRoleForDatalist() {
return 'listbox';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForDetails;
/**
* Returns the implicit role for a details tag.
*/
function getImplicitRoleForDetails() {
return 'group';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForDialog;
/**
* Returns the implicit role for a dialog tag.
*/
function getImplicitRoleForDialog() {
return 'dialog';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForDl;
/**
* Returns the implicit role for a dl tag.
*/
function getImplicitRoleForDl() {
return 'list';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForForm;
/**
* Returns the implicit role for a form tag.
*/
function getImplicitRoleForForm() {
return 'form';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForH1;
/**
* Returns the implicit role for an h1 tag.
*/
function getImplicitRoleForH1() {
return 'heading';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForH2;
/**
* Returns the implicit role for an h2 tag.
*/
function getImplicitRoleForH2() {
return 'heading';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForH3;
/**
* Returns the implicit role for an h3 tag.
*/
function getImplicitRoleForH3() {
return 'heading';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForH4;
/**
* Returns the implicit role for an h4 tag.
*/
function getImplicitRoleForH4() {
return 'heading';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForH5;
/**
* Returns the implicit role for an h5 tag.
*/
function getImplicitRoleForH5() {
return 'heading';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForH6;
/**
* Returns the implicit role for an h6tag.
*/
function getImplicitRoleForH6() {
return 'heading';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForHr;
/**
* Returns the implicit role for an hr tag.
*/
function getImplicitRoleForHr() {
return 'separator';
}

View file

@ -0,0 +1,21 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForImg;
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns the implicit role for an img tag.
*/
function getImplicitRoleForImg(attributes) {
var alt = (0, _jsxAstUtils.getProp)(attributes, 'alt');
if (alt && (0, _jsxAstUtils.getLiteralPropValue)(alt) === '') {
return '';
}
return 'img';
}

View file

@ -0,0 +1,123 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _a = _interopRequireDefault(require("./a"));
var _area = _interopRequireDefault(require("./area"));
var _article = _interopRequireDefault(require("./article"));
var _aside = _interopRequireDefault(require("./aside"));
var _body = _interopRequireDefault(require("./body"));
var _button = _interopRequireDefault(require("./button"));
var _datalist = _interopRequireDefault(require("./datalist"));
var _details = _interopRequireDefault(require("./details"));
var _dialog = _interopRequireDefault(require("./dialog"));
var _dl = _interopRequireDefault(require("./dl"));
var _form = _interopRequireDefault(require("./form"));
var _h = _interopRequireDefault(require("./h1"));
var _h2 = _interopRequireDefault(require("./h2"));
var _h3 = _interopRequireDefault(require("./h3"));
var _h4 = _interopRequireDefault(require("./h4"));
var _h5 = _interopRequireDefault(require("./h5"));
var _h6 = _interopRequireDefault(require("./h6"));
var _hr = _interopRequireDefault(require("./hr"));
var _img = _interopRequireDefault(require("./img"));
var _input = _interopRequireDefault(require("./input"));
var _li = _interopRequireDefault(require("./li"));
var _link = _interopRequireDefault(require("./link"));
var _menu = _interopRequireDefault(require("./menu"));
var _menuitem = _interopRequireDefault(require("./menuitem"));
var _meter = _interopRequireDefault(require("./meter"));
var _nav = _interopRequireDefault(require("./nav"));
var _ol = _interopRequireDefault(require("./ol"));
var _option = _interopRequireDefault(require("./option"));
var _output = _interopRequireDefault(require("./output"));
var _progress = _interopRequireDefault(require("./progress"));
var _section = _interopRequireDefault(require("./section"));
var _select = _interopRequireDefault(require("./select"));
var _tbody = _interopRequireDefault(require("./tbody"));
var _textarea = _interopRequireDefault(require("./textarea"));
var _tfoot = _interopRequireDefault(require("./tfoot"));
var _thead = _interopRequireDefault(require("./thead"));
var _ul = _interopRequireDefault(require("./ul"));
var _default = {
a: _a["default"],
area: _area["default"],
article: _article["default"],
aside: _aside["default"],
body: _body["default"],
button: _button["default"],
datalist: _datalist["default"],
details: _details["default"],
dialog: _dialog["default"],
dl: _dl["default"],
form: _form["default"],
h1: _h["default"],
h2: _h2["default"],
h3: _h3["default"],
h4: _h4["default"],
h5: _h5["default"],
h6: _h6["default"],
hr: _hr["default"],
img: _img["default"],
input: _input["default"],
li: _li["default"],
link: _link["default"],
menu: _menu["default"],
menuitem: _menuitem["default"],
meter: _meter["default"],
nav: _nav["default"],
ol: _ol["default"],
option: _option["default"],
output: _output["default"],
progress: _progress["default"],
section: _section["default"],
select: _select["default"],
tbody: _tbody["default"],
textarea: _textarea["default"],
tfoot: _tfoot["default"],
thead: _thead["default"],
ul: _ul["default"]
};
exports["default"] = _default;

View file

@ -0,0 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForInput;
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns the implicit role for an input tag.
*/
function getImplicitRoleForInput(attributes) {
var type = (0, _jsxAstUtils.getProp)(attributes, 'type');
if (type) {
var value = (0, _jsxAstUtils.getLiteralPropValue)(type) || '';
switch (typeof value === 'string' && value.toUpperCase()) {
case 'BUTTON':
case 'IMAGE':
case 'RESET':
case 'SUBMIT':
return 'button';
case 'CHECKBOX':
return 'checkbox';
case 'RADIO':
return 'radio';
case 'RANGE':
return 'slider';
case 'EMAIL':
case 'PASSWORD':
case 'SEARCH': // with [list] selector it's combobox
case 'TEL': // with [list] selector it's combobox
case 'URL': // with [list] selector it's combobox
default:
return 'textbox';
}
}
return 'textbox';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForLi;
/**
* Returns the implicit role for an li tag.
*/
function getImplicitRoleForLi() {
return 'listitem';
}

View file

@ -0,0 +1,19 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForLink;
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns the implicit role for a link tag.
*/
function getImplicitRoleForLink(attributes) {
if ((0, _jsxAstUtils.getProp)(attributes, 'href')) {
return 'link';
}
return '';
}

View file

@ -0,0 +1,22 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForMenu;
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns the implicit role for a menu tag.
*/
function getImplicitRoleForMenu(attributes) {
var type = (0, _jsxAstUtils.getProp)(attributes, 'type');
if (type) {
var value = (0, _jsxAstUtils.getLiteralPropValue)(type);
return value && value.toUpperCase() === 'TOOLBAR' ? 'toolbar' : '';
}
return '';
}

View file

@ -0,0 +1,35 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForMenuitem;
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns the implicit role for a menuitem tag.
*/
function getImplicitRoleForMenuitem(attributes) {
var type = (0, _jsxAstUtils.getProp)(attributes, 'type');
if (type) {
var value = (0, _jsxAstUtils.getLiteralPropValue)(type) || '';
switch (typeof value === 'string' && value.toUpperCase()) {
case 'COMMAND':
return 'menuitem';
case 'CHECKBOX':
return 'menuitemcheckbox';
case 'RADIO':
return 'menuitemradio';
default:
return '';
}
}
return '';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForMeter;
/**
* Returns the implicit role for a meter tag.
*/
function getImplicitRoleForMeter() {
return 'progressbar';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForNav;
/**
* Returns the implicit role for a nav tag.
*/
function getImplicitRoleForNav() {
return 'navigation';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForOl;
/**
* Returns the implicit role for an ol tag.
*/
function getImplicitRoleForOl() {
return 'list';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForOption;
/**
* Returns the implicit role for an option tag.
*/
function getImplicitRoleForOption() {
return 'option';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForOutput;
/**
* Returns the implicit role for an output tag.
*/
function getImplicitRoleForOutput() {
return 'status';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForProgress;
/**
* Returns the implicit role for a progress tag.
*/
function getImplicitRoleForProgress() {
return 'progressbar';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForSection;
/**
* Returns the implicit role for a section tag.
*/
function getImplicitRoleForSection() {
return 'region';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForSelect;
/**
* Returns the implicit role for a select tag.
*/
function getImplicitRoleForSelect() {
return 'listbox';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForTbody;
/**
* Returns the implicit role for a tbody tag.
*/
function getImplicitRoleForTbody() {
return 'rowgroup';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForTextarea;
/**
* Returns the implicit role for a textarea tag.
*/
function getImplicitRoleForTextarea() {
return 'textbox';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForTfoot;
/**
* Returns the implicit role for a tfoot tag.
*/
function getImplicitRoleForTfoot() {
return 'rowgroup';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForThead;
/**
* Returns the implicit role for a thead tag.
*/
function getImplicitRoleForThead() {
return 'rowgroup';
}

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = getImplicitRoleForUl;
/**
* Returns the implicit role for a ul tag.
*/
function getImplicitRoleForUl() {
return 'list';
}

View file

@ -0,0 +1,33 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var abstractRoles = new Set((0, _toConsumableArray2["default"])(_ariaQuery.roles.keys()).filter(function (role) {
return _ariaQuery.roles.get(role)["abstract"];
}));
var DOMElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
var isAbstractRole = function isAbstractRole(tagName, attributes) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
if (DOMElements.indexOf(tagName) === -1) {
return false;
}
var role = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(attributes, 'role'));
return abstractRoles.has(role);
};
var _default = isAbstractRole;
exports["default"] = _default;

View file

@ -0,0 +1,26 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var domElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
/**
* Returns boolean indicating whether the given element is a DOM element.
*/
var isDOMElement = function isDOMElement(tagName) {
return (0, _arrayIncludes["default"])(domElements, tagName);
};
var _default = isDOMElement;
exports["default"] = _default;

View file

@ -0,0 +1,30 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _jsxAstUtils = require("jsx-ast-utils");
var isDisabledElement = function isDisabledElement(attributes) {
var disabledAttr = (0, _jsxAstUtils.getProp)(attributes, 'disabled');
var disabledAttrValue = (0, _jsxAstUtils.getPropValue)(disabledAttr);
var isHTML5Disabled = disabledAttr && disabledAttrValue !== undefined;
if (isHTML5Disabled) {
return true;
}
var ariaDisabledAttr = (0, _jsxAstUtils.getProp)(attributes, 'aria-disabled');
var ariaDisabledAttrValue = (0, _jsxAstUtils.getLiteralPropValue)(ariaDisabledAttr);
if (ariaDisabledAttr && ariaDisabledAttrValue !== undefined && ariaDisabledAttrValue === true) {
return true;
}
return false;
};
var _default = isDisabledElement;
exports["default"] = _default;

View file

@ -0,0 +1,31 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns boolean indicating that the aria-hidden prop
* is present or the value is true. Will also return true if
* there is an input with type='hidden'.
*
* <div aria-hidden /> is equivalent to the DOM as <div aria-hidden=true />.
*/
var isHiddenFromScreenReader = function isHiddenFromScreenReader(type, attributes) {
if (type.toUpperCase() === 'INPUT') {
var hidden = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(attributes, 'type'));
if (hidden && hidden.toUpperCase() === 'HIDDEN') {
return true;
}
}
var ariaHidden = (0, _jsxAstUtils.getPropValue)((0, _jsxAstUtils.getProp)(attributes, 'aria-hidden'));
return ariaHidden === true;
};
var _default = isHiddenFromScreenReader;
exports["default"] = _default;

View file

@ -0,0 +1,139 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _axobjectQuery = require("axobject-query");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _attributesComparator = _interopRequireDefault(require("./attributesComparator"));
var domKeys = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
var roleKeys = (0, _toConsumableArray2["default"])(_ariaQuery.roles.keys());
var elementRoleEntries = (0, _toConsumableArray2["default"])(_ariaQuery.elementRoles);
var nonInteractiveRoles = new Set(roleKeys.filter(function (name) {
var role = _ariaQuery.roles.get(name);
return !role["abstract"] // 'toolbar' does not descend from widget, but it does support
// aria-activedescendant, thus in practice we treat it as a widget.
&& name !== 'toolbar' && !role.superClass.some(function (classes) {
return (0, _arrayIncludes["default"])(classes, 'widget');
});
}).concat( // The `progressbar` is descended from `widget`, but in practice, its
// value is always `readonly`, so we treat it as a non-interactive role.
'progressbar'));
var interactiveRoles = new Set(roleKeys.filter(function (name) {
var role = _ariaQuery.roles.get(name);
return !role["abstract"] // The `progressbar` is descended from `widget`, but in practice, its
// value is always `readonly`, so we treat it as a non-interactive role.
&& name !== 'progressbar' && role.superClass.some(function (classes) {
return (0, _arrayIncludes["default"])(classes, 'widget');
});
}).concat( // 'toolbar' does not descend from widget, but it does support
// aria-activedescendant, thus in practice we treat it as a widget.
'toolbar'));
var nonInteractiveElementRoleSchemas = elementRoleEntries.reduce(function (accumulator, _ref) {
var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
elementSchema = _ref2[0],
roleSet = _ref2[1];
if ((0, _toConsumableArray2["default"])(roleSet).every(function (role) {
return nonInteractiveRoles.has(role);
})) {
accumulator.push(elementSchema);
}
return accumulator;
}, []);
var interactiveElementRoleSchemas = elementRoleEntries.reduce(function (accumulator, _ref3) {
var _ref4 = (0, _slicedToArray2["default"])(_ref3, 2),
elementSchema = _ref4[0],
roleSet = _ref4[1];
if ((0, _toConsumableArray2["default"])(roleSet).some(function (role) {
return interactiveRoles.has(role);
})) {
accumulator.push(elementSchema);
}
return accumulator;
}, []);
var interactiveAXObjects = new Set((0, _toConsumableArray2["default"])(_axobjectQuery.AXObjects.keys()).filter(function (name) {
return _axobjectQuery.AXObjects.get(name).type === 'widget';
}));
var interactiveElementAXObjectSchemas = (0, _toConsumableArray2["default"])(_axobjectQuery.elementAXObjects).reduce(function (accumulator, _ref5) {
var _ref6 = (0, _slicedToArray2["default"])(_ref5, 2),
elementSchema = _ref6[0],
AXObjectSet = _ref6[1];
if ((0, _toConsumableArray2["default"])(AXObjectSet).every(function (role) {
return interactiveAXObjects.has(role);
})) {
accumulator.push(elementSchema);
}
return accumulator;
}, []);
function checkIsInteractiveElement(tagName, attributes) {
function elementSchemaMatcher(elementSchema) {
return tagName === elementSchema.name && (0, _attributesComparator["default"])(elementSchema.attributes, attributes);
} // Check in elementRoles for inherent interactive role associations for
// this element.
var isInherentInteractiveElement = interactiveElementRoleSchemas.some(elementSchemaMatcher);
if (isInherentInteractiveElement) {
return true;
} // Check in elementRoles for inherent non-interactive role associations for
// this element.
var isInherentNonInteractiveElement = nonInteractiveElementRoleSchemas.some(elementSchemaMatcher);
if (isInherentNonInteractiveElement) {
return false;
} // Check in elementAXObjects for AX Tree associations for this element.
var isInteractiveAXElement = interactiveElementAXObjectSchemas.some(elementSchemaMatcher);
if (isInteractiveAXElement) {
return true;
}
return false;
}
/**
* Returns boolean indicating whether the given element is
* interactive on the DOM or not. Usually used when an element
* has a dynamic handler on it and we need to discern whether or not
* it's intention is to be interacted with on the DOM.
*/
var isInteractiveElement = function isInteractiveElement(tagName, attributes) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
if (!(0, _arrayIncludes["default"])(domKeys, tagName)) {
return false;
}
return checkIsInteractiveElement(tagName, attributes);
};
var _default = isInteractiveElement;
exports["default"] = _default;

View file

@ -0,0 +1,69 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var roles = (0, _toConsumableArray2["default"])(_ariaQuery.roles.keys());
var interactiveRoles = roles.filter(function (name) {
return !_ariaQuery.roles.get(name)["abstract"];
}).filter(function (name) {
return _ariaQuery.roles.get(name).superClass.some(function (klasses) {
return (0, _arrayIncludes["default"])(klasses, 'widget');
});
}); // 'toolbar' does not descend from widget, but it does support
// aria-activedescendant, thus in practice we treat it as a widget.
interactiveRoles.push('toolbar');
/**
* Returns boolean indicating whether the given element has a role
* that is associated with an interactive component. Used when an element
* has a dynamic handler on it and we need to discern whether or not
* its intention is to be interacted with in the DOM.
*
* isInteractiveRole is a Logical Disjunction:
* https://en.wikipedia.org/wiki/Logical_disjunction
* The JSX element does not have a tagName or it has a tagName and a role
* attribute with a value in the set of non-interactive roles.
*/
var isInteractiveRole = function isInteractiveRole(tagName, attributes) {
var value = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(attributes, 'role')); // If value is undefined, then the role attribute will be dropped in the DOM.
// If value is null, then getLiteralAttributeValue is telling us that the
// value isn't in the form of a literal
if (value === undefined || value === null) {
return false;
}
var isInteractive = false;
var normalizedValues = String(value).toLowerCase().split(' ');
var validRoles = normalizedValues.reduce(function (accumulator, name) {
if ((0, _arrayIncludes["default"])(roles, name)) {
accumulator.push(name);
}
return accumulator;
}, []);
if (validRoles.length > 0) {
// The first role value is a series takes precedence.
isInteractive = (0, _arrayIncludes["default"])(interactiveRoles, validRoles[0]);
}
return isInteractive;
};
var _default = isInteractiveRole;
exports["default"] = _default;

View file

@ -0,0 +1,152 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _axobjectQuery = require("axobject-query");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _attributesComparator = _interopRequireDefault(require("./attributesComparator"));
var roleKeys = (0, _toConsumableArray2["default"])(_ariaQuery.roles.keys());
var elementRoleEntries = (0, _toConsumableArray2["default"])(_ariaQuery.elementRoles);
var nonInteractiveRoles = new Set(roleKeys.filter(function (name) {
var role = _ariaQuery.roles.get(name);
return !role["abstract"] // 'toolbar' does not descend from widget, but it does support
// aria-activedescendant, thus in practice we treat it as a widget.
&& name !== 'toolbar' // This role is meant to have no semantic value.
// @see https://www.w3.org/TR/wai-aria-1.2/#generic
&& name !== 'generic' && !role.superClass.some(function (classes) {
return (0, _arrayIncludes["default"])(classes, 'widget');
});
}).concat( // The `progressbar` is descended from `widget`, but in practice, its
// value is always `readonly`, so we treat it as a non-interactive role.
'progressbar'));
var interactiveRoles = new Set(roleKeys.filter(function (name) {
var role = _ariaQuery.roles.get(name);
return !role["abstract"] // The `progressbar` is descended from `widget`, but in practice, its
// value is always `readonly`, so we treat it as a non-interactive role.
&& name !== 'progressbar' // This role is meant to have no semantic value.
// @see https://www.w3.org/TR/wai-aria-1.2/#generic
&& name !== 'generic' && role.superClass.some(function (classes) {
return (0, _arrayIncludes["default"])(classes, 'widget');
});
}).concat( // 'toolbar' does not descend from widget, but it does support
// aria-activedescendant, thus in practice we treat it as a widget.
'toolbar'));
var nonInteractiveElementRoleSchemas = elementRoleEntries.reduce(function (accumulator, _ref) {
var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
elementSchema = _ref2[0],
roleSet = _ref2[1];
if ((0, _toConsumableArray2["default"])(roleSet).every(function (role) {
return nonInteractiveRoles.has(role);
})) {
accumulator.push(elementSchema);
}
return accumulator;
}, []);
var interactiveElementRoleSchemas = elementRoleEntries.reduce(function (accumulator, _ref3) {
var _ref4 = (0, _slicedToArray2["default"])(_ref3, 2),
elementSchema = _ref4[0],
roleSet = _ref4[1];
if ((0, _toConsumableArray2["default"])(roleSet).some(function (role) {
return interactiveRoles.has(role);
})) {
accumulator.push(elementSchema);
}
return accumulator;
}, []);
var nonInteractiveAXObjects = new Set((0, _toConsumableArray2["default"])(_axobjectQuery.AXObjects.keys()).filter(function (name) {
return (0, _arrayIncludes["default"])(['window', 'structure'], _axobjectQuery.AXObjects.get(name).type);
}));
var nonInteractiveElementAXObjectSchemas = (0, _toConsumableArray2["default"])(_axobjectQuery.elementAXObjects).reduce(function (accumulator, _ref5) {
var _ref6 = (0, _slicedToArray2["default"])(_ref5, 2),
elementSchema = _ref6[0],
AXObjectSet = _ref6[1];
if ((0, _toConsumableArray2["default"])(AXObjectSet).every(function (role) {
return nonInteractiveAXObjects.has(role);
})) {
accumulator.push(elementSchema);
}
return accumulator;
}, []);
function checkIsNonInteractiveElement(tagName, attributes) {
function elementSchemaMatcher(elementSchema) {
return tagName === elementSchema.name && (0, _attributesComparator["default"])(elementSchema.attributes, attributes);
} // Check in elementRoles for inherent non-interactive role associations for
// this element.
var isInherentNonInteractiveElement = nonInteractiveElementRoleSchemas.some(elementSchemaMatcher);
if (isInherentNonInteractiveElement) {
return true;
} // Check in elementRoles for inherent interactive role associations for
// this element.
var isInherentInteractiveElement = interactiveElementRoleSchemas.some(elementSchemaMatcher);
if (isInherentInteractiveElement) {
return false;
} // Check in elementAXObjects for AX Tree associations for this element.
var isNonInteractiveAXElement = nonInteractiveElementAXObjectSchemas.some(elementSchemaMatcher);
if (isNonInteractiveAXElement) {
return true;
}
return false;
}
/**
* Returns boolean indicating whether the given element is a non-interactive
* element. If the element has either a non-interactive role assigned or it
* is an element with an inherently non-interactive role, then this utility
* returns true. Elements that lack either an explicitly assigned role or
* an inherent role are not considered. For those, this utility returns false
* because a positive determination of interactiveness cannot be determined.
*/
var isNonInteractiveElement = function isNonInteractiveElement(tagName, attributes) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
if (!_ariaQuery.dom.has(tagName)) {
return false;
} // <header> elements do not technically have semantics, unless the
// element is a direct descendant of <body>, and this plugin cannot
// reliably test that.
// @see https://www.w3.org/TR/wai-aria-practices/examples/landmarks/banner.html
if (tagName === 'header') {
return false;
}
return checkIsNonInteractiveElement(tagName, attributes);
};
var _default = isNonInteractiveElement;
exports["default"] = _default;

View file

@ -0,0 +1,71 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var roles = (0, _toConsumableArray2["default"])(_ariaQuery.roles.keys());
var nonInteractiveRoles = roles.filter(function (name) {
return !_ariaQuery.roles.get(name)["abstract"];
}).filter(function (name) {
return !_ariaQuery.roles.get(name).superClass.some(function (klasses) {
return (0, _arrayIncludes["default"])(klasses, 'widget');
});
});
/**
* Returns boolean indicating whether the given element has a role
* that is associated with a non-interactive component. Non-interactive roles
* include `listitem`, `article`, or `dialog`. These are roles that indicate
* for the most part containers.
*
* Elements with these roles should not respond or handle user interactions.
* For example, an `onClick` handler should not be assigned to an element with
* the role `listitem`. An element inside the `listitem`, like a button or a
* link, should handle the click.
*
* This utility returns true for elements that are assigned a non-interactive
* role. It will return false for elements that do not have a role. So whereas
* a `div` might be considered non-interactive, for the purpose of this utility,
* it is considered neither interactive nor non-interactive -- a determination
* cannot be made in this case and false is returned.
*/
var isNonInteractiveRole = function isNonInteractiveRole(tagName, attributes) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
if (!_ariaQuery.dom.has(tagName)) {
return false;
}
var role = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(attributes, 'role'));
var isNonInteractive = false;
var normalizedValues = String(role).toLowerCase().split(' ');
var validRoles = normalizedValues.reduce(function (accumulator, name) {
if ((0, _arrayIncludes["default"])(roles, name)) {
accumulator.push(name);
}
return accumulator;
}, []);
if (validRoles.length > 0) {
// The first role value is a series takes precedence.
isNonInteractive = (0, _arrayIncludes["default"])(nonInteractiveRoles, validRoles[0]);
}
return isNonInteractive;
};
var _default = isNonInteractiveRole;
exports["default"] = _default;

View file

@ -0,0 +1,34 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns boolean indicating whether the given element has been specified with
* an AST node with a non-literal type.
*
* Returns true if the elements has a role and its value is not of a type Literal.
* Otherwise returns false.
*/
var isNonLiteralProperty = function isNonLiteralProperty(attributes, propName) {
var prop = (0, _jsxAstUtils.getProp)(attributes, propName);
if (!prop) return false;
var propValue = prop.value;
if (!propValue) return false;
if (propValue.type === 'Literal') return false;
if (propValue.type === 'JSXExpressionContainer') {
var expression = propValue.expression;
if (expression.type === 'Identifier' && expression.name === 'undefined') return false;
if (expression.type === 'JSXText') return false;
}
return true;
};
var _default = isNonLiteralProperty;
exports["default"] = _default;

View file

@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _jsxAstUtils = require("jsx-ast-utils");
var presentationRoles = new Set(['presentation', 'none']);
var isPresentationRole = function isPresentationRole(tagName, attributes) {
return presentationRoles.has((0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(attributes, 'role')));
};
var _default = isPresentationRole;
exports["default"] = _default;

View file

@ -0,0 +1,68 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _axobjectQuery = require("axobject-query");
var _jsxAstUtils = require("jsx-ast-utils");
var isSemanticRoleElement = function isSemanticRoleElement(elementType, attributes) {
var roleAttr = (0, _jsxAstUtils.getProp)(attributes, 'role');
var res = false;
var roleAttrValue = (0, _jsxAstUtils.getLiteralPropValue)(roleAttr);
_axobjectQuery.elementAXObjects.forEach(function (axObjects, concept) {
if (res) {
return;
}
if (concept.name === elementType && (concept.attributes || []).every(function (cAttr) {
return attributes.some(function (attr) {
if (!attr.type || attr.type !== 'JSXAttribute') {
return false;
}
var namesMatch = cAttr.name === (0, _jsxAstUtils.propName)(attr);
var valuesMatch;
if (cAttr.value !== undefined) {
valuesMatch = cAttr.value === (0, _jsxAstUtils.getLiteralPropValue)(attr);
}
if (!namesMatch) {
return false;
}
return namesMatch && valuesMatch !== undefined ? valuesMatch : true;
});
})) {
axObjects.forEach(function (name) {
if (res) {
return;
}
var roles = _axobjectQuery.AXObjectRoles.get(name);
if (roles) {
roles.forEach(function (role) {
if (res === true) {
return;
}
if (role.name === roleAttrValue) {
res = true;
}
});
}
});
}
});
return res;
};
var _default = isSemanticRoleElement;
exports["default"] = _default;

View file

@ -0,0 +1,53 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = mayContainChildComponent;
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns true if it can positively determine that the element lacks an
* accessible label. If no determination is possible, it returns false. Treat
* false as an unknown value. The element might still have an accessible label,
* but this module cannot determine it positively.
*
*
*/
function mayContainChildComponent(root, componentName) {
var maxDepth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
function traverseChildren(node, depth) {
// Bail when maxDepth is exceeded.
if (depth > maxDepth) {
return false;
}
if (node.children) {
/* $FlowFixMe */
for (var i = 0; i < node.children.length; i += 1) {
/* $FlowFixMe */
var childNode = node.children[i]; // Assume an expression container renders a label. It is the best we can
// do in this case.
if (childNode.type === 'JSXExpressionContainer') {
return true;
} // Check for comonents with the provided name.
if (childNode.type === 'JSXElement' && childNode.openingElement && (0, _jsxAstUtils.elementType)(childNode.openingElement) === componentName) {
return true;
}
if (traverseChildren(childNode, depth + 1)) {
return true;
}
}
}
return false;
}
return traverseChildren(root, 1);
}

View file

@ -0,0 +1,93 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = mayHaveAccessibleLabel;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _jsxAstUtils = require("jsx-ast-utils");
/**
* Returns true if a labelling element is found or if it cannot determine if
* a label is present because of expression containers or spread attributes.
* A false return value means that the node definitely does not have a label,
* but a true return return value means that the node may or may not have a
* label.
*
*
*/
function hasLabellingProp(openingElement) {
var additionalLabellingProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var labellingProps = ['alt', // Assume alt is used correctly on an image
'aria-label', 'aria-labelledby'].concat((0, _toConsumableArray2["default"])(additionalLabellingProps));
return openingElement.attributes.some(function (attribute) {
// We must assume that a spread value contains a labelling prop.
if (attribute.type !== 'JSXAttribute') {
return true;
} // Attribute matches.
if ((0, _arrayIncludes["default"])(labellingProps, (0, _jsxAstUtils.propName)(attribute)) && !!(0, _jsxAstUtils.getPropValue)(attribute)) {
return true;
}
return false;
});
}
function mayHaveAccessibleLabel(root) {
var maxDepth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
var additionalLabellingProps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
function checkElement(node, depth) {
// Bail when maxDepth is exceeded.
if (depth > maxDepth) {
return false;
} // Check for literal text.
if (node.type === 'Literal' && !!node.value) {
return true;
} // Assume an expression container renders a label. It is the best we can
// do in this case.
if (node.type === 'JSXExpressionContainer') {
return true;
} // Check for JSXText.
// $FlowFixMe Remove after updating ast-types-flow
if (node.type === 'JSXText' && !!node.value) {
return true;
} // Check for labelling props.
if (node.openingElement
/* $FlowFixMe */
&& hasLabellingProp(node.openingElement, additionalLabellingProps)) {
return true;
} // Recurse into the child element nodes.
if (node.children) {
/* $FlowFixMe */
for (var i = 0; i < node.children.length; i += 1) {
/* $FlowFixMe */
if (checkElement(node.children[i], depth + 1)) {
return true;
}
}
}
return false;
}
return checkElement(root, 0);
}

View file

@ -0,0 +1,62 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.generateObjSchema = exports.enumArraySchema = exports.arraySchema = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
/**
* JSON schema to accept an array of unique strings
*/
var arraySchema = {
type: 'array',
items: {
type: 'string'
},
uniqueItems: true,
additionalItems: false
};
/**
* JSON schema to accept an array of unique strings from an enumerated list.
*/
exports.arraySchema = arraySchema;
var enumArraySchema = function enumArraySchema() {
var enumeratedList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var minItems = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return _objectSpread(_objectSpread({}, arraySchema), {}, {
items: {
type: 'string',
"enum": enumeratedList
},
minItems
});
};
/**
* Factory function to generate an object schema
* with specified properties object
*/
exports.enumArraySchema = enumArraySchema;
var generateObjSchema = function generateObjSchema() {
var properties = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var required = arguments.length > 1 ? arguments[1] : undefined;
return {
type: 'object',
properties,
required
};
};
exports.generateObjSchema = generateObjSchema;