Skip to content

Rules: add pressable-has-accessibility-role #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions __tests__/src/rules/pressable-has-accessibility-role-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @fileoverview Forbid "pressable" element without an explicit "accessibilityRole" attribute rule.
* @author forxtu
*/

// -----------------------------------------------------------------------------
// Requirements
// -----------------------------------------------------------------------------

import { RuleTester } from 'eslint';
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
import rule from '../../../src/rules/pressable-has-accessibility-role';

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

const ruleTester = new RuleTester();

const expectedError = {
message:
'Missing an explicit "accessibilityRole" attribute for pressable element',
type: 'JSXOpeningElement',
};

ruleTester.run('pressable-has-accessibility-role', rule, {
valid: [
{
code: `<Pressable onPress={() => {}} accessibilityRole="button"></Pressable>`,
},
{
code: `<Text onPress={() => {}} accessibilityRole="link"></Text>`,
},
].map(parserOptionsMapper),
invalid: [
{
code: `<Pressable onPress={() => {}}></Pressable>`,
errors: [expectedError],
},
{
code: `<Text onPress={() => {}}></Text>`,
errors: [expectedError],
},
].map(parserOptionsMapper),
});
33 changes: 33 additions & 0 deletions docs/rules/pressable-has-accessibility-role.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# pressable-has-accessibility-role

Forbid "pressable" element without an explicit "accessibilityRole" attribute. By pressable element we mean an element that has "onPress" attribute.

### References

1. [React Native Docs - accessibilityRole](https://reactnative.dev/docs/accessibility)

## Rule Details

This rule aims to improve accessibility enforcing developers to add explicit "accessibilityRole" attribute to the pressable elements.

### Options

- "off" or 0 - turn the rule off
- "warn" or 1 - turn the rule on as a warning (doesn't affect exit code)
- "error" or 2 - turn the rule on as an error (exit code is 1 when triggered)

### Succeed

```js
<Pressable onPress={() => {}} accessibilityRole="button"></Pressable>
```

### Fail

```js
<Pressable onPress={() => {}}></Pressable>
```

## When Not To Use It

If you don't need additional accessibility for pressable elements.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const basicRules = {
'react-native-a11y/has-valid-accessibility-traits': 'error',
'react-native-a11y/has-valid-accessibility-value': 'error',
'react-native-a11y/no-nested-touchables': 'error',
'react-native-a11y/pressable-has-accessibility-role': 'error',
};

const iOSRules = {
Expand All @@ -46,6 +47,7 @@ module.exports = {
'has-valid-accessibility-value': require('./rules/has-valid-accessibility-value'),
'has-valid-important-for-accessibility': require('./rules/has-valid-important-for-accessibility'),
'no-nested-touchables': require('./rules/no-nested-touchables'),
'pressable-has-accessibility-role': require('./rules/pressable-has-accessibility-role'),
},
configs: {
basic: {
Expand Down
54 changes: 54 additions & 0 deletions src/rules/pressable-has-accessibility-role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @fileoverview Forbid "pressable" element without an explicit "accessibilityRole" attribute.
* @author forxtu
* @flow
*/

import { hasProp } from 'jsx-ast-utils';

// Utils
import { generateObjSchema } from '../util/schemas';

// Types
import type { JSXOpeningElement } from 'ast-types-flow';
import type { ESLintContext } from '../../flow/eslint';

// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------

const errorMessage =
'Missing an explicit "accessibilityRole" attribute for pressable element';

const schema = generateObjSchema();

module.exports = {
meta: {
docs: {
description:
'Forbid "pressable" element without an explicit "accessibilityRole" attribute',
category: 'Possible Errors',
recommended: true,
url:
'https://github.com/FormidableLabs/eslint-plugin-react-native-a11y/tree/master/docs/rules/pressable-has-accessibility-role.md',
},
fixable: null,
schema: [schema],
},
create: (context: ESLintContext) => ({
JSXOpeningElement: (node: JSXOpeningElement) => {
const hasOnPressAttr = hasProp(node.attributes, 'onPress');
const hasAccessibilityRoleAttr = hasProp(
node.attributes,
'accessibilityRole'
);

if (hasOnPressAttr && !hasAccessibilityRoleAttr) {
context.report({
node,
message: errorMessage,
});
}
},
}),
};