UNPKG

eslint-plugin-sonarjs

Version:
222 lines (221 loc) • 9.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getElementType = exports.HIDING_ATTRIBUTES = void 0; exports.getFirstConcreteRole = getFirstConcreteRole; exports.isPresentationTable = isPresentationTable; exports.getRole = getRole; exports.hasAccessibleNameAttribute = hasAccessibleNameAttribute; exports.hasAccessibleName = hasAccessibleName; exports.getHiddenState = getHiddenState; exports.getOwnHiddenState = getOwnHiddenState; const aria_query_1 = require("aria-query"); const jsx_ast_utils_x_1 = __importDefault(require("jsx-ast-utils-x")); const ancestor_js_1 = require("./ancestor.js"); const ast_js_1 = require("./ast.js"); const jsx_js_1 = require("./jsx.js"); const { getProp, getLiteralPropValue, getPropValue, elementType } = jsx_ast_utils_x_1.default; /** Attributes that can hide an element from everyone; see {@link getOwnHiddenState}. */ exports.HIDING_ATTRIBUTES = ['aria-hidden', 'hidden', 'style']; const CONCRETE_ROLES = new Set(aria_query_1.roles .entries() .filter(([, definition]) => !definition.abstract) .map(([name]) => name)); /** Returns the first recognized, non-abstract role in an ordered fallback list. */ function getFirstConcreteRole(roleValue) { return roleValue .toLowerCase() .trim() .split(/\s+/) .find(name => CONCRETE_ROLES.has(name)); } function isPresentationTable(context, node) { const DISALLOWED_VALUES = ['presentation', 'none']; const type = (0, exports.getElementType)(context)(node); if (type.toLowerCase() !== 'table') { return false; } const role = getProp(node.attributes, 'role'); if (!role) { return false; } const roleValue = String(getLiteralPropValue(role)); return DISALLOWED_VALUES.includes(roleValue?.toLowerCase()); } function getRole(node) { const roleProp = getProp(node.attributes, 'role'); if (!roleProp) { return undefined; } const roleValue = getLiteralPropValue(roleProp); if (typeof roleValue !== 'string') { return undefined; } return roleValue.toLowerCase(); } /** * Checks whether an accessible-name attribute has a statically non-empty or dynamic value. */ function hasAccessibleNameAttribute(attributes, name) { const attribute = getProp(attributes, name); if (!attribute) { return false; } if (isNonEmptyStringAttribute(attribute) || isPotentiallyNonEmptyTemplateLiteralAttribute(attribute)) { return true; } // Dynamic expressions are unknown statically, but nullish values should not suppress. return getLiteralPropValue(attribute) === null && getPropValue(attribute) != null; } function isNonEmptyStringAttribute(attribute) { if (attribute.value?.type === 'Literal') { return typeof attribute.value.value === 'string' && attribute.value.value.trim() !== ''; } if (attribute.value?.type === 'JSXExpressionContainer' && attribute.value.expression.type === 'Literal') { return (typeof attribute.value.expression.value === 'string' && attribute.value.expression.value.trim() !== ''); } return false; } function isPotentiallyNonEmptyTemplateLiteralAttribute(attribute) { if (attribute.value?.type !== 'JSXExpressionContainer' || attribute.value.expression.type !== 'TemplateLiteral') { return false; } return (attribute.value.expression.expressions.length > 0 || attribute.value.expression.quasis.some(quasi => quasi.value.cooked?.trim() !== '')); } /** * Checks whether an element has a direct-child <title> element with non-empty content. * Independent of whitespace and of the title's position among its siblings. Not specific * to SVG: the caller decides which elements this check applies to. */ function hasTitleChild(node) { const parent = node.parent; if (parent?.type !== 'JSXElement') { return false; } return parent.children.some(child => (0, jsx_js_1.isJsxElementNamed)(child, 'title') && child.children.some(c => (c.type === 'JSXText' && c.value.trim() !== '') || (c.type === 'JSXExpressionContainer' && c.expression.type !== 'JSXEmptyExpression' && !(c.expression.type === 'Literal' && !c.expression.value) && !(c.expression.type === 'Identifier' && c.expression.name === 'undefined')))); } /** * Checks whether an element has an accessible name via aria-labelledby, aria-label, or a * direct-child <title> element (in that precedence order). Id references in aria-labelledby * are not resolved to their target's text, treated purely as a presence check like aria-label. * Not specific to SVG: the caller decides which elements this check applies to. */ function hasAccessibleName(node) { const attributes = node.attributes; return (hasAccessibleNameAttribute(attributes, 'aria-labelledby') || hasAccessibleNameAttribute(attributes, 'aria-label') || hasTitleChild(node)); } const getElementType = (context) => { const { settings } = context; const jsxa11ySettings = settings['jsx-a11y']; const polymorphicPropName = jsxa11ySettings?.polymorphicPropName; const polymorphicAllowList = jsxa11ySettings?.polymorphicAllowList; const componentMap = jsxa11ySettings?.components; return (node) => { const prop = polymorphicPropName ? getProp(node.attributes, polymorphicPropName) : undefined; const polymorphicProp = prop ? getLiteralPropValue(prop) : undefined; let rawType = elementType(node); if (polymorphicProp && (!polymorphicAllowList || polymorphicAllowList.includes(rawType))) { rawType = `${polymorphicProp}`; } if (!componentMap) { return rawType; } return componentMap.hasOwnProperty(rawType) ? componentMap[rawType] : rawType; }; }; exports.getElementType = getElementType; /** * Whether the element or an ancestor is hidden via `aria-hidden`, `hidden`, or `display: none`. * `'unknown'` when none is known to hide it but some value can't be resolved; callers decide how * to treat it. */ function getHiddenState(node, context) { let state = 'visible'; for (const ancestor of (0, ancestor_js_1.ancestorsChain)(node, new Set())) { if (ancestor.type !== 'JSXElement') { continue; } const attributes = ancestor.openingElement.attributes; const ownState = getOwnHiddenState(attributes, context); if (ownState === 'hidden') { return 'hidden'; } if (ownState === 'unknown') { state = 'unknown'; } } return state; } /** * Like {@link getHiddenState}, but for the element's own attributes only. An unresolvable * `aria-hidden` or `hidden` gives `'unknown'`; an unresolvable `style` never hides. */ function getOwnHiddenState(attributes, context) { const states = new Set([ resolveHiddenAttribute(attributes, 'aria-hidden', context, isAriaHiddenValue), // React renders boolean attributes for any truthy value, e.g. `hidden="false"` still hides. resolveHiddenAttribute(attributes, 'hidden', context, Boolean), toHiddenState(isDisplayNone(attributes, context)), ]); if (states.has('hidden')) { return 'hidden'; } return states.has('unknown') ? 'unknown' : 'visible'; } function isAriaHiddenValue(value) { return String(value).trim().toLowerCase() === 'true'; } /** `'visible'` when the attribute is absent, `'unknown'` when its value can't be resolved. */ function resolveHiddenAttribute(attributes, name, context, isHidden) { const attribute = getProp(attributes, name); if (!attribute) { return 'visible'; } const { value } = attribute; if (value === null) { return toHiddenState(isHidden(true)); } if (value.type === 'Literal') { return toHiddenState(isHidden(value.value)); } if (value.type === 'JSXExpressionContainer') { const literal = (0, ast_js_1.getValueOfExpression)(context, value.expression, 'Literal'); if (literal) { return toHiddenState(isHidden(literal.value)); } } return 'unknown'; } function toHiddenState(hidden) { return hidden ? 'hidden' : 'visible'; } function isDisplayNone(attributes, context) { const value = getProp(attributes, 'style')?.value; // String styles are invalid in React, so only object styles are resolved. if (value?.type !== 'JSXExpressionContainer') { return false; } const resolvedStyle = (0, ast_js_1.getValueOfExpression)(context, value.expression, 'ObjectExpression'); const displayProperty = resolvedStyle && (0, ast_js_1.getProperty)(resolvedStyle, 'display', context); const displayValue = displayProperty && (0, ast_js_1.getValueOfExpression)(context, displayProperty.value, 'Literal'); return (typeof displayValue?.value === 'string' && displayValue.value.trim().toLowerCase() === 'none'); }