Skip to content

Commit 426d4c2

Browse files
jessebeachljharb
authored andcommitted
Fix Flow warnings
1 parent ecec8e4 commit 426d4c2

23 files changed

+144
-79
lines changed

.flowconfig

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
<PROJECT_ROOT>/lib/.*
33
<PROJECT_ROOT>/docs/.*
44
<PROJECT_ROOT>/reports/.*
5+
[options]
6+
suppress_type=$FlowFixMe

__mocks__/IdentifierMock.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
export default function IdentifierMock(ident) {
1+
/**
2+
* @flow
3+
*/
4+
5+
export type IdentifierMockType = {|
6+
type: 'Identifier',
7+
name: string,
8+
|};
9+
10+
export default function IdentifierMock(ident: string): IdentifierMockType {
211
return {
312
type: 'Identifier',
413
name: ident,

__mocks__/JSXAttributeMock.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
/**
2+
* @flow
3+
*/
4+
15
import toAST from 'to-ast'; // eslint-disable-line import/no-extraneous-dependencies
26
import JSXExpressionContainerMock from './JSXExpressionContainerMock';
37

4-
export default function JSXAttributeMock(prop, value, isExpressionContainer = false) {
8+
export type JSXAttributeMockType = {
9+
type: 'JSXAttribute',
10+
name: {
11+
type: 'JSXIdentifier',
12+
name: string,
13+
},
14+
value: mixed,
15+
};
16+
17+
export default function JSXAttributeMock(prop: string, value: mixed, isExpressionContainer?: boolean = false): JSXAttributeMockType {
518
let astValue;
619
if (value && value.type !== undefined) {
720
astValue = value;

__mocks__/JSXElementMock.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22
* @flow
33
*/
44

5-
import JSXAttributeMock from './JSXAttributeMock';
5+
import type { JSXAttributeMockType } from './JSXAttributeMock';
66

7-
export type TJSXElementMock = {
7+
export type JSXElementMockType = {
88
type: 'JSXElement',
99
openingElement: {
1010
type: 'JSXOpeningElement',
1111
name: {
1212
type: 'JSXIdentifier',
1313
name: string,
1414
},
15-
attributes: Array<JSXAttributeMock>,
15+
attributes: Array<JSXAttributeMockType>,
1616
},
1717
children: Array<Node>,
1818
};
1919

2020
export default function JSXElementMock(
2121
tagName: string,
22-
attributes: Array<JSXAttributeMock> = [],
23-
children: Array<Node> = [],
24-
): TJSXElementMock {
22+
attributes: Array<JSXAttributeMockType> = [],
23+
children?: Array<Node> = [],
24+
): JSXElementMockType {
2525
return {
2626
type: 'JSXElement',
2727
openingElement: {

__mocks__/JSXExpressionContainerMock.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
export default function JSXExpressionContainerMock(exp) {
1+
/**
2+
* @flow
3+
*/
4+
5+
export type JSXExpressionContainerMockType = {
6+
type: 'JSXExpressionContainer',
7+
expression: mixed,
8+
}
9+
10+
export default function JSXExpressionContainerMock(exp: mixed): JSXExpressionContainerMockType {
211
return {
312
type: 'JSXExpressionContainer',
413
expression: exp,

__mocks__/JSXSpreadAttributeMock.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
*/
44

55
import IdentifierMock from './IdentifierMock';
6+
import type { IdentifierMockType } from './IdentifierMock';
67

7-
export default function JSXSpreadAttributeMock(identifier: string) {
8+
export type JSXSpreadAttributeMockType = {
9+
type: 'JSXSpreadAttribute',
10+
argument: IdentifierMockType,
11+
};
12+
13+
export default function JSXSpreadAttributeMock(identifier: string): JSXSpreadAttributeMockType {
814
return {
915
type: 'JSXSpreadAttribute',
1016
argument: IdentifierMock(identifier),

__mocks__/JSXTextMock.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
/**
22
* @flow
33
*/
4-
export default function JSXTextMock(value: string) {
4+
5+
export type JSXTextMockType = {|
6+
type: 'JSXText',
7+
value: string,
8+
raw: string,
9+
|};
10+
11+
export default function JSXTextMock(value: string): JSXTextMockType {
512
return {
613
type: 'JSXText',
714
value,

__mocks__/LiteralMock.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
/**
22
* @flow
33
*/
4-
export default function LiteralMock(value: string) {
4+
5+
export type LiteralMockType = {|
6+
type: 'Literal',
7+
value: string,
8+
raw: string,
9+
|};
10+
11+
export default function LiteralMock(value: string): LiteralMockType {
512
return {
613
type: 'Literal',
714
value,

__mocks__/genInteractives.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import includes from 'array-includes';
77
import JSXAttributeMock from './JSXAttributeMock';
88
import JSXElementMock from './JSXElementMock';
99

10-
import type { TJSXElementMock } from './JSXElementMock';
10+
import type { JSXElementMockType } from './JSXElementMock';
11+
import type { JSXAttributeMockType } from './JSXAttributeMock';
1112

1213
const domElements = [...dom.keys()];
1314
const roleNames = [...roles.keys()];
@@ -147,7 +148,7 @@ const nonInteractiveRoles = roleNames
147148
// aria-activedescendant, thus in practice we treat it as a widget.
148149
.filter((role) => !includes(['toolbar'], role));
149150

150-
export function genElementSymbol(openingElement: Object) {
151+
export function genElementSymbol(openingElement: Object): string {
151152
return (
152153
openingElement.name.name + (openingElement.attributes.length > 0
153154
? `${openingElement.attributes
@@ -158,8 +159,8 @@ export function genElementSymbol(openingElement: Object) {
158159
);
159160
}
160161

161-
export function genInteractiveElements(): Array<TJSXElementMock> {
162-
return Object.keys(interactiveElementsMap).map((elementSymbol: string): TJSXElementMock => {
162+
export function genInteractiveElements(): Array<JSXElementMockType> {
163+
return Object.keys(interactiveElementsMap).map((elementSymbol: string): JSXElementMockType => {
163164
const bracketIndex = elementSymbol.indexOf('[');
164165
let name = elementSymbol;
165166
if (bracketIndex > -1) {
@@ -170,15 +171,15 @@ export function genInteractiveElements(): Array<TJSXElementMock> {
170171
});
171172
}
172173

173-
export function genInteractiveRoleElements(): Array<TJSXElementMock> {
174-
return [...interactiveRoles, 'button article', 'fakerole button article'].map((value): TJSXElementMock => JSXElementMock(
174+
export function genInteractiveRoleElements(): Array<JSXElementMockType> {
175+
return [...interactiveRoles, 'button article', 'fakerole button article'].map((value): JSXElementMockType => JSXElementMock(
175176
'div',
176177
[JSXAttributeMock('role', value)],
177178
));
178179
}
179180

180-
export function genNonInteractiveElements(): Array<TJSXElementMock> {
181-
return Object.keys(nonInteractiveElementsMap).map((elementSymbol): TJSXElementMock => {
181+
export function genNonInteractiveElements(): Array<JSXElementMockType> {
182+
return Object.keys(nonInteractiveElementsMap).map((elementSymbol): JSXElementMockType => {
182183
const bracketIndex = elementSymbol.indexOf('[');
183184
let name = elementSymbol;
184185
if (bracketIndex > -1) {
@@ -189,25 +190,25 @@ export function genNonInteractiveElements(): Array<TJSXElementMock> {
189190
});
190191
}
191192

192-
export function genNonInteractiveRoleElements() {
193+
export function genNonInteractiveRoleElements(): Array<JSXElementMockType> {
193194
return [
194195
...nonInteractiveRoles,
195196
'article button',
196197
'fakerole article button',
197198
].map((value) => JSXElementMock('div', [JSXAttributeMock('role', value)]));
198199
}
199200

200-
export function genAbstractRoleElements() {
201+
export function genAbstractRoleElements(): Array<JSXElementMockType> {
201202
return abstractRoles.map((value) => JSXElementMock('div', [JSXAttributeMock('role', value)]));
202203
}
203204

204-
export function genNonAbstractRoleElements() {
205+
export function genNonAbstractRoleElements(): Array<JSXElementMockType> {
205206
return nonAbstractRoles.map((value) => JSXElementMock('div', [JSXAttributeMock('role', value)]));
206207
}
207208

208-
export function genIndeterminantInteractiveElements(): Array<TJSXElementMock> {
209+
export function genIndeterminantInteractiveElements(): Array<JSXElementMockType> {
209210
return Object.keys(indeterminantInteractiveElementsMap).map((name) => {
210-
const attributes = indeterminantInteractiveElementsMap[name].map(({ prop, value }): TJSXElementMock => JSXAttributeMock(prop, value));
211+
const attributes = indeterminantInteractiveElementsMap[name].map(({ prop, value }): JSXAttributeMockType => JSXAttributeMock(prop, value));
211212
return JSXElementMock(name, attributes);
212213
});
213214
}

__tests__/__util__/ruleOptionsMapperFactory.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ type ESLintTestRunnerTestCase = {
99
parserOptions: ?Array<mixed>
1010
};
1111

12-
export default function ruleOptionsMapperFactory(ruleOptions: Array<mixed> = []) {
12+
type RuleOptionsMapperFactoryType = (
13+
params: ESLintTestRunnerTestCase
14+
) => ESLintTestRunnerTestCase;
15+
16+
export default function ruleOptionsMapperFactory(ruleOptions: Array<mixed> = []): RuleOptionsMapperFactoryType {
1317
// eslint-disable-next-line
1418
return ({ code, errors, options, parserOptions }: ESLintTestRunnerTestCase): ESLintTestRunnerTestCase => {
1519
return {

flow/eslint.js

+9
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ export type ESLintContext = {
1010
options: Array<Object>,
1111
report: (ESLintReport) => void,
1212
};
13+
14+
export type ESLintConfig = {
15+
meta?: {[string]: mixed},
16+
create: (context: ESLintContext) => mixed,
17+
}
18+
19+
export type ESLintVisitorSelectorConfig = {
20+
[string]: mixed,
21+
};

src/rules/anchor-is-valid.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import { elementType, getProp, getPropValue } from 'jsx-ast-utils';
1212
import type { JSXOpeningElement } from 'ast-types-flow';
13-
import type { ESLintContext } from '../../flow/eslint';
13+
import type { ESLintConfig, ESLintContext, ESLintVisitorSelectorConfig } from '../../flow/eslint';
1414
import { generateObjSchema, arraySchema, enumArraySchema } from '../util/schemas';
1515

1616
const allAspects = ['noHref', 'invalidHref', 'preferButton'];
@@ -27,16 +27,16 @@ const schema = generateObjSchema({
2727
aspects: enumArraySchema(allAspects, 1),
2828
});
2929

30-
module.exports = {
30+
module.exports = ({
3131
meta: {
3232
docs: {
3333
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/anchor-is-valid.md',
3434
},
3535
schema: [schema],
3636
},
3737

38-
create: (context: ESLintContext) => ({
39-
JSXOpeningElement: (node: JSXOpeningElement) => {
38+
create: (context: ESLintContext): ESLintVisitorSelectorConfig => ({
39+
JSXOpeningElement: (node: JSXOpeningElement): void => {
4040
const { attributes } = node;
4141
const options = context.options[0] || {};
4242
const componentOptions = options.components || [];
@@ -115,4 +115,4 @@ module.exports = {
115115
}
116116
},
117117
}),
118-
};
118+
}: ESLintConfig);

src/rules/control-has-associated-label.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { elementType, getProp, getLiteralPropValue } from 'jsx-ast-utils';
1313
import type { JSXElement } from 'ast-types-flow';
1414
import includes from 'array-includes';
1515
import { generateObjSchema, arraySchema } from '../util/schemas';
16-
import type { ESLintContext } from '../../flow/eslint';
16+
import type { ESLintConfig, ESLintContext, ESLintVisitorSelectorConfig } from '../../flow/eslint';
1717
import isDOMElement from '../util/isDOMElement';
1818
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
1919
import isInteractiveElement from '../util/isInteractiveElement';
@@ -36,13 +36,13 @@ const schema = generateObjSchema({
3636
},
3737
});
3838

39-
module.exports = {
39+
module.exports = ({
4040
meta: {
4141
docs: {},
4242
schema: [schema],
4343
},
4444

45-
create: (context: ESLintContext) => {
45+
create: (context: ESLintContext): ESLintVisitorSelectorConfig => {
4646
const options = context.options[0] || {};
4747
const {
4848
labelAttributes = [],
@@ -53,7 +53,7 @@ module.exports = {
5353

5454
const newIgnoreElements = new Set([...ignoreElements, ...ignoreList]);
5555

56-
const rule = (node: JSXElement) => {
56+
const rule = (node: JSXElement): void => {
5757
const tag = elementType(node.openingElement);
5858
const role = getLiteralPropValue(getProp(node.openingElement.attributes, 'role'));
5959
// Ignore interactive elements that might get their label from a source
@@ -112,4 +112,4 @@ module.exports = {
112112
JSXElement: rule,
113113
};
114114
},
115-
};
115+
}: ESLintConfig);

src/rules/interactive-supports-focus.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from 'jsx-ast-utils';
1818
import type { JSXOpeningElement } from 'ast-types-flow';
1919
import includes from 'array-includes';
20-
import type { ESLintContext } from '../../flow/eslint';
20+
import type { ESLintConfig, ESLintContext, ESLintVisitorSelectorConfig } from '../../flow/eslint';
2121
import {
2222
enumArraySchema,
2323
generateObjSchema,
@@ -47,19 +47,15 @@ const interactiveProps = [
4747
...eventHandlersByType.keyboard,
4848
];
4949

50-
module.exports = {
50+
module.exports = ({
5151
meta: {
5252
docs: {
5353
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/interactive-supports-focus.md',
5454
},
5555
schema: [schema],
5656
},
5757

58-
create: (context: ESLintContext & {
59-
options: {
60-
tabbable: Array<string>
61-
}
62-
}) => ({
58+
create: (context: ESLintContext): ESLintVisitorSelectorConfig => ({
6359
JSXOpeningElement: (node: JSXOpeningElement) => {
6460
const tabbable = (
6561
context.options && context.options[0] && context.options[0].tabbable
@@ -111,4 +107,4 @@ module.exports = {
111107
}
112108
},
113109
}),
114-
};
110+
}: ESLintConfig);

src/rules/label-has-associated-control.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import { getProp, getPropValue, elementType } from 'jsx-ast-utils';
1313
import type { JSXElement } from 'ast-types-flow';
1414
import { generateObjSchema, arraySchema } from '../util/schemas';
15-
import type { ESLintContext } from '../../flow/eslint';
15+
import type { ESLintConfig, ESLintContext, ESLintVisitorSelectorConfig } from '../../flow/eslint';
1616
import mayContainChildComponent from '../util/mayContainChildComponent';
1717
import mayHaveAccessibleLabel from '../util/mayHaveAccessibleLabel';
1818

@@ -41,13 +41,13 @@ const validateId = (node) => {
4141
return htmlForAttr !== false && !!htmlForValue;
4242
};
4343

44-
module.exports = {
44+
module.exports = ({
4545
meta: {
4646
docs: {},
4747
schema: [schema],
4848
},
4949

50-
create: (context: ESLintContext) => {
50+
create: (context: ESLintContext): ESLintVisitorSelectorConfig => {
5151
const options = context.options[0] || {};
5252
const labelComponents = options.labelComponents || [];
5353
const assertType = options.assert || 'either';
@@ -122,4 +122,4 @@ module.exports = {
122122
JSXElement: rule,
123123
};
124124
},
125-
};
125+
}: ESLintConfig);

0 commit comments

Comments
 (0)