-
Notifications
You must be signed in to change notification settings - Fork 26
update accessibilityState rule for RN v0.61+ #60
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,73 @@ | ||
| /* eslint-env jest */ | ||
| /** | ||
| * @fileoverview Describes the current state of a component to the user of an assistive technology. | ||
| * @author JP Driver | ||
| */ | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
| // Requirements | ||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| import { RuleTester } from 'eslint'; | ||
| import parserOptionsMapper from '../../__util__/parserOptionsMapper'; | ||
| import rule from '../../../src/rules/has-valid-accessibility-state'; | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
| // Tests | ||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| const ruleTester = new RuleTester(); | ||
|
|
||
| const propMustBeAnObject = { | ||
| message: 'accessibilityState must be an object', | ||
| type: 'JSXAttribute' | ||
| }; | ||
|
|
||
| const invalidObjectKey = key => ({ | ||
| message: `accessibilityState object: "${key}" is not a valid key`, | ||
| type: 'JSXAttribute' | ||
| }); | ||
|
|
||
| const valueMustBeBoolean = key => ({ | ||
| message: `accessibilityState object: "${key}" value is not a boolean`, | ||
| type: 'JSXAttribute' | ||
| }); | ||
|
|
||
| const checkedMustBeBooleanOrMixed = { | ||
| message: `accessibilityState object: "checked" value is not either a boolean or 'mixed'`, | ||
| type: 'JSXAttribute' | ||
| }; | ||
|
|
||
| ruleTester.run('has-valid-accessibility-state', rule, { | ||
| valid: [ | ||
| { code: '<TouchableOpacity accessibilityState={{ disabled: true }} />;' }, | ||
| { code: '<TouchableOpacity accessibilityState={{ checked: true }} />;' }, | ||
| { code: '<TouchableOpacity accessibilityState={{ checked: "mixed" }} />;' } | ||
| ].map(parserOptionsMapper), | ||
| invalid: [ | ||
| { | ||
| code: '<TouchableOpacity accessibilityState="disabled" />', | ||
| errors: [propMustBeAnObject] | ||
| }, | ||
| { | ||
| code: '<TouchableOpacity accessibilityState={["disabled"]} />', | ||
| errors: [propMustBeAnObject] | ||
| }, | ||
| { | ||
| code: '<TouchableOpacity accessibilityState={{ disabled: "yes" }} />', | ||
| errors: [valueMustBeBoolean('disabled')] | ||
| }, | ||
| { | ||
| code: '<TouchableOpacity accessibilityState={{ checked: "yes" }} />', | ||
| errors: [checkedMustBeBooleanOrMixed] | ||
| }, | ||
| { | ||
| code: '<TouchableOpacity accessibilityState={{ foo: true }} />', | ||
| errors: [invalidObjectKey('foo')] | ||
| }, | ||
| { | ||
| code: '<TouchableOpacity accessibilityState={{ foo: "yes" }} />', | ||
| errors: [invalidObjectKey('foo')] | ||
| } | ||
| ].map(parserOptionsMapper) | ||
| }); | ||
This file contains hidden or 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,42 @@ | ||
| # has-valid-accessibility-state | ||
|
|
||
| Describes the current state of a component to the user of an assistive technology. | ||
|
|
||
| ## `accessibilityState` is an object. It contains the following fields: | ||
|
|
||
| NAME|TYPE|REQUIRED | ||
| -|-|- | ||
| disabled|boolean|No | ||
| selecte|boolean|No | ||
| checked|boolean or 'mixed'|No | ||
| busy|boolean|No | ||
| expanded|boolean|No | ||
|
|
||
| ### References | ||
|
|
||
| 1. [React Native Docs - accessibilityState (iOS, Android)](https://facebook.github.io/react-native/docs/accessibility#accessibilitystate-ios-android) | ||
|
|
||
| ## Rule details | ||
|
|
||
| This rule takes no arguments. | ||
|
|
||
| ### Succeed | ||
|
|
||
| ```jsx | ||
| <TouchableOpacity accessibilityState={{ disabled: true }} /> | ||
| <TouchableOpacity accessibilityState={{ checked: true }} /> | ||
| <TouchableOpacity accessibilityState={{ checked: "mixed" }} /> | ||
| ``` | ||
|
|
||
| ### Fail | ||
|
|
||
| ```jsx | ||
| <TouchableOpacity accessibilityState="disabled" /> | ||
| <TouchableOpacity accessibilityState={["disabled"]} /> | ||
| <TouchableOpacity accessibilityState={{ disabled: "yes" }} /> | ||
| <TouchableOpacity accessibilityState={{ checked: "yes" }} /> | ||
| <TouchableOpacity accessibilityState={{ foo: true }} /> | ||
| <TouchableOpacity accessibilityState={{ foo: "yes" }} /> | ||
| ``` | ||
|
|
||
| *Note*: This plugin previously defined a rule with this name which is now `has-valid-accessibility-states` (see [#42](https://github.com/FormidableLabs/eslint-plugin-react-native-a11y/pull/42)). React Native v0.61 introduced a new `accessibilityState` prop (see [commit](https://github.com/facebook/react-native/commit/099be9b35634851b178e990c47358c2129c0dd7d)) -- which is now covered by this rule. |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,40 +1,60 @@ | ||
| /** | ||
| * @fileoverview Used to tell Talkback or Voiceover the state a UI Element is in | ||
| * @author Jen Luker | ||
| * @fileoverview Describes the current state of a component to the user of an assistive technology. | ||
| * @author JP Driver | ||
| * @flow | ||
| */ | ||
|
|
||
| import createValidPropRule from '../factory/valid-prop'; | ||
| import type { JSXAttribute } from 'ast-types-flow'; | ||
| import { elementType, getPropValue, getLiteralPropValue } from 'jsx-ast-utils'; | ||
| import { generateObjSchema } from '../util/schemas'; | ||
| import type { ESLintContext } from '../../flow/eslint'; | ||
|
|
||
| // ---------------------------------------------------------------------------- | ||
| // Rule Definition | ||
| // ---------------------------------------------------------------------------- | ||
|
|
||
| const errorMessage = | ||
| 'accessibilityStates must be one or both of the defined values'; | ||
| const validKeys = ['disabled', 'selected', 'checked', 'busy', 'expanded']; | ||
|
|
||
| const validValues = ['selected', 'disabled']; | ||
| module.exports = { | ||
| meta: { | ||
| docs: {}, | ||
| schema: [generateObjSchema()] | ||
| }, | ||
|
|
||
| let deprecationHasBeenWarned = false; | ||
| create: (context: ESLintContext) => ({ | ||
| JSXAttribute: (node: JSXAttribute) => { | ||
| const attrName = elementType(node); | ||
| if (attrName === 'accessibilityState') { | ||
| const attrValue = getPropValue(node); | ||
| const test = getLiteralPropValue(node); | ||
|
|
||
| const rule = createValidPropRule( | ||
| 'accessibilityStates', | ||
| validValues, | ||
| errorMessage, | ||
| { | ||
| deprecated: true | ||
| }, | ||
| { | ||
| Program: () => { | ||
| if (deprecationHasBeenWarned) return; | ||
| // eslint-disable-next-line no-console | ||
| console.log( | ||
| 'The react-native-a11y/has-valid-accessibility-state rule is deprecated. ' + | ||
| 'Please use the react-native-a11y/has-valid-accessibility-states rule instead.' | ||
| ); | ||
| deprecationHasBeenWarned = true; | ||
| } | ||
| } | ||
| ); | ||
| const error = message => | ||
| context.report({ | ||
| node, | ||
| message | ||
| }); | ||
|
|
||
| module.exports = rule; | ||
| if (typeof attrValue !== 'object' || Array.isArray(attrValue)) { | ||
| error('accessibilityState must be an object'); | ||
| } else { | ||
| Object.entries(attrValue).map(([key, value]) => { | ||
| if (validKeys.indexOf(key) < 0) { | ||
| error(`accessibilityState object: "${key}" is not a valid key`); | ||
| } else if (key !== 'checked' && typeof value !== 'boolean') { | ||
| error( | ||
| `accessibilityState object: "${key}" value is not a boolean` | ||
| ); | ||
| } else if ( | ||
| key === 'checked' && | ||
jpdriver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| !(typeof value === 'boolean' || value === 'mixed') | ||
| ) { | ||
| error( | ||
| `accessibilityState object: "checked" value is not either a boolean or 'mixed'` | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.