-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for accessibilityHint (#74)
- Loading branch information
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* eslint-env jest */ | ||
/** | ||
* @fileoverview An accessibility hint helps users understand what will happen when they perform an action on the accessibility element when that result is not apparent from the accessibility label. | ||
* @author JP Driver | ||
*/ | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Requirements | ||
// ----------------------------------------------------------------------------- | ||
|
||
import { RuleTester } from 'eslint'; | ||
import parserOptionsMapper from '../../__util__/parserOptionsMapper'; | ||
import rule from '../../../src/rules/has-accessibility-hint'; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Tests | ||
// ----------------------------------------------------------------------------- | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
const expectedError = { | ||
message: 'has accessibilityLabel prop but no accessibilityHint', | ||
type: 'JSXOpeningElement' | ||
}; | ||
|
||
ruleTester.run('has-accessibility-hint', rule, { | ||
valid: [ | ||
{ | ||
code: `<TouchableOpacity />` | ||
}, | ||
{ | ||
code: `<TouchableOpacity | ||
accessibilityHint="Navigates to the previous screen" | ||
/>` | ||
}, | ||
{ | ||
code: `<TouchableOpacity | ||
accessibilityLabel="Go back" | ||
accessibilityHint="Navigates to the previous screen" | ||
/>` | ||
} | ||
].map(parserOptionsMapper), | ||
invalid: [ | ||
{ | ||
code: `<TouchableOpacity | ||
accessibilityLabel="Go back" | ||
/>`, | ||
errors: [expectedError] | ||
} | ||
].map(parserOptionsMapper) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# has-accessibility-hint | ||
|
||
An accessibility hint helps users understand what will happen when they perform an action on the accessibility element when that result is not apparent from the accessibility label. | ||
|
||
### References | ||
|
||
1. [React Native Docs - accessibilityHint (iOS, Android)](https://facebook.github.io/react-native/docs/accessibility#accessibilityhint-ios-android) | ||
|
||
## Rule details | ||
|
||
This rule takes no arguments. | ||
|
||
### Succeed | ||
```jsx | ||
<TouchableOpacity /> | ||
<TouchableOpacity accessibilityHint="Navigates to the previous screen" /> | ||
<TouchableOpacity accessibilityHint="Navigates to the previous screen" accessibilityLabel="Go back" /> | ||
``` | ||
|
||
### Fail | ||
```jsx | ||
<TouchableOpacity accessibilityLabel="Go back" /> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @fileoverview An accessibility hint helps users understand what will happen when they perform an action on the accessibility element when that result is not apparent from the accessibility label. | ||
* @author JP Driver | ||
* @flow | ||
*/ | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Rule Definition | ||
// ---------------------------------------------------------------------------- | ||
|
||
import { hasProp } from 'jsx-ast-utils'; | ||
import type { JSXOpeningElement } from 'ast-types-flow'; | ||
import type { ESLintContext } from '../../flow/eslint'; | ||
import { generateObjSchema } from '../util/schemas'; | ||
|
||
const errorMessage = ''; | ||
|
||
const schema = generateObjSchema(); | ||
|
||
module.exports = { | ||
meta: { | ||
docs: {}, | ||
schema: [schema] | ||
}, | ||
|
||
create: (context: ESLintContext) => ({ | ||
JSXOpeningElement: (node: JSXOpeningElement) => { | ||
if ( | ||
hasProp(node.attributes, 'accessibilityLabel') && | ||
!hasProp(node.attributes, 'accessibilityHint') | ||
) { | ||
context.report({ | ||
node, | ||
message: 'has accessibilityLabel prop but no accessibilityHint' | ||
}); | ||
} | ||
} | ||
}) | ||
}; |