|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import type { JSXOpeningElement } from 'ast-types-flow'; |
8 | | -import { getProp, getPropValue, hasProp } from 'jsx-ast-utils'; |
| 8 | +import { getProp, getPropValue, hasEveryProp, hasProp } from 'jsx-ast-utils'; |
9 | 9 | import { generateObjSchema } from '../util/schemas'; |
10 | 10 | import type { ESLintContext } from '../../flow/eslint'; |
11 | 11 |
|
@@ -36,51 +36,67 @@ module.exports = { |
36 | 36 | message, |
37 | 37 | }); |
38 | 38 |
|
39 | | - if (hasProp(node.attributes, 'accessibilityActions')) { |
40 | | - if ( |
41 | | - typeof getPropValue( |
42 | | - getProp(node.attributes, 'onAccessibilityAction') |
43 | | - ) !== 'function' |
44 | | - ) { |
45 | | - error( |
46 | | - 'accessibilityActions: has accessibilityActions but onAccessibilityAction is not a function' |
47 | | - ); |
| 39 | + if ( |
| 40 | + hasEveryProp(node.attributes, [ |
| 41 | + 'accessibilityActions', |
| 42 | + 'onAccessibilityAction', |
| 43 | + ]) |
| 44 | + ) { |
| 45 | + const handlerProp = getProp(node.attributes, 'onAccessibilityAction'); |
| 46 | + const handlerPropType = handlerProp.value.expression.type; |
| 47 | + // CallExpressions are always assumed valid |
| 48 | + if (handlerPropType !== 'CallExpression') { |
| 49 | + const handlerPropValue = getPropValue(handlerProp); |
| 50 | + if (typeof handlerPropValue !== 'function') { |
| 51 | + error( |
| 52 | + 'accessibilityActions: has accessibilityActions but onAccessibilityAction is not a function' |
| 53 | + ); |
| 54 | + } |
48 | 55 | } |
49 | 56 |
|
50 | | - const attrValue = getPropValue( |
51 | | - getProp(node.attributes, 'accessibilityActions') |
52 | | - ); |
| 57 | + const actionsProp = getProp(node.attributes, 'accessibilityActions'); |
| 58 | + const actionsPropType = actionsProp.value.expression.type; |
| 59 | + // CallExpressions are always assumed valid |
| 60 | + if (actionsPropType !== 'CallExpression') { |
| 61 | + const attrValue = getPropValue(actionsProp); |
53 | 62 |
|
54 | | - if (!Array.isArray(attrValue)) { |
55 | | - error('accessibilityActions: value must be an Array'); |
56 | | - } else if (attrValue.length === 0) { |
57 | | - error('accessibilityActions: Array cannot be empty'); |
58 | | - } else { |
59 | | - attrValue.forEach((action) => { |
60 | | - if (!action.name) { |
61 | | - error('accessibilityActions: action missing name'); |
62 | | - } else if ( |
63 | | - standardActions.indexOf(action.name) < 0 && |
64 | | - !action.label |
65 | | - ) { |
66 | | - error( |
67 | | - `accessibilityActions: custom action "${action.name}" missing label` |
68 | | - ); |
69 | | - } |
70 | | - if ( |
71 | | - Object.keys(action).filter((f) => f !== 'name' && f !== 'label') |
72 | | - .length > 0 |
73 | | - ) { |
74 | | - error( |
75 | | - `accessibilityActions: action "${action.name}" contains unrecognised keys` |
76 | | - ); |
77 | | - } |
78 | | - }); |
| 63 | + if (!Array.isArray(attrValue)) { |
| 64 | + error('accessibilityActions: value must be an Array'); |
| 65 | + } else if (attrValue.length === 0) { |
| 66 | + error('accessibilityActions: Array cannot be empty'); |
| 67 | + } else { |
| 68 | + attrValue.forEach((action) => { |
| 69 | + if (!action.name) { |
| 70 | + error('accessibilityActions: action missing name'); |
| 71 | + } else if ( |
| 72 | + standardActions.indexOf(action.name) < 0 && |
| 73 | + !action.label |
| 74 | + ) { |
| 75 | + error( |
| 76 | + `accessibilityActions: custom action "${action.name}" missing label` |
| 77 | + ); |
| 78 | + } |
| 79 | + if ( |
| 80 | + Object.keys(action).filter((f) => f !== 'name' && f !== 'label') |
| 81 | + .length > 0 |
| 82 | + ) { |
| 83 | + error( |
| 84 | + `accessibilityActions: action "${action.name}" contains unrecognised keys` |
| 85 | + ); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | + } else { |
| 91 | + if (hasProp(node.attributes, 'accessibilityActions')) { |
| 92 | + error( |
| 93 | + 'accessibilityActions: has accessibilityActions but onAccessibilityAction is not a function' |
| 94 | + ); |
| 95 | + } else if (hasProp(node.attributes, 'onAccessibilityAction')) { |
| 96 | + error( |
| 97 | + 'accessibilityActions: has onAccessibilityAction function but no accessibilityActions Array' |
| 98 | + ); |
79 | 99 | } |
80 | | - } else if (hasProp(node.attributes, 'onAccessibilityAction')) { |
81 | | - error( |
82 | | - 'accessibilityActions: has onAccessibilityAction function but no accessibilityActions Array' |
83 | | - ); |
84 | 100 | } |
85 | 101 | }, |
86 | 102 | }), |
|
0 commit comments