Skip to content

Commit 47b1306

Browse files
committed
Add linting to all directories and fix errors.
1 parent bf0ac6a commit 47b1306

File tree

6 files changed

+125
-190
lines changed

6 files changed

+125
-190
lines changed

__mocks__/IdentifierMock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export default function IdentifierMock(ident) {
33
type: 'Identifier',
44
name: ident,
55
};
6-
};
6+
}

__mocks__/JSXAttributeMock.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1+
import toAST from 'to-ast'; // eslint-disable-line import/no-extraneous-dependencies
12
import JSXExpressionContainerMock from './JSXExpressionContainerMock';
2-
import toAST from 'to-ast';
33

4-
export default function JSXAttributeMock (
5-
prop,
6-
value,
7-
isExpressionContainer = false
8-
) {
4+
export default function JSXAttributeMock(prop, value, isExpressionContainer = false) {
95
let astValue;
106
if (value && value.type !== undefined) {
117
astValue = value;
128
} else {
139
astValue = toAST(value);
1410
}
1511
let attributeValue = astValue;
16-
if (
17-
isExpressionContainer
18-
|| astValue.type !== 'Literal'
19-
) {
20-
attributeValue = JSXExpressionContainerMock(
21-
astValue
22-
);
12+
if (isExpressionContainer || astValue.type !== 'Literal') {
13+
attributeValue = JSXExpressionContainerMock(astValue);
2314
}
2415

2516
return {

__mocks__/genInteractives.js

Lines changed: 76 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
* @flow
33
*/
44

5-
import {
6-
dom,
7-
roles,
8-
} from 'aria-query';
5+
import { dom, roles } from 'aria-query';
96
import JSXAttributeMock from './JSXAttributeMock';
107
import JSXElementMock from './JSXElementMock';
118

@@ -21,28 +18,27 @@ const interactiveElementsMap = {
2118
embed: [],
2219
link: [],
2320
input: [],
24-
'input[type=\"button\"]': [{ prop: 'type', value: 'button' }],
25-
'input[type=\"checkbox\"]': [{ prop: 'type', value: 'checkbox' }],
26-
'input[type=\"color\"]': [{ prop: 'type', value: 'color' }],
27-
'input[type=\"date\"]': [{ prop: 'type', value: 'date' }],
28-
'input[type=\"datetime\"]': [{ prop: 'type', value: 'datetime' }],
29-
'input[type=\"datetime\"]': [{ prop: 'type', value: 'datetime' }],
30-
'input[type=\"email\"]': [{ prop: 'type', value: 'email' }],
31-
'input[type=\"file\"]': [{ prop: 'type', value: 'file' }],
32-
'input[type=\"image\"]': [{ prop: 'type', value: 'image' }],
33-
'input[type=\"month\"]': [{ prop: 'type', value: 'month' }],
34-
'input[type=\"number\"]': [{ prop: 'type', value: 'number' }],
35-
'input[type=\"password\"]': [{ prop: 'type', value: 'password' }],
36-
'input[type=\"radio\"]': [{ prop: 'type', value: 'radio' }],
37-
'input[type=\"range\"]': [{ prop: 'type', value: 'range' }],
38-
'input[type=\"reset\"]': [{ prop: 'type', value: 'reset' }],
39-
'input[type=\"search\"]': [{ prop: 'type', value: 'search' }],
40-
'input[type=\"submit\"]': [{ prop: 'type', value: 'submit' }],
41-
'input[type=\"tel\"]': [{ prop: 'type', value: 'tel' }],
42-
'input[type=\"text\"]': [{ prop: 'type', value: 'text' }],
43-
'input[type=\"time\"]': [{ prop: 'type', value: 'time' }],
44-
'input[type=\"url\"]': [{ prop: 'type', value: 'url' }],
45-
'input[type=\"week\"]': [{ prop: 'type', value: 'week' }],
21+
'input[type="button"]': [{ prop: 'type', value: 'button' }],
22+
'input[type="checkbox"]': [{ prop: 'type', value: 'checkbox' }],
23+
'input[type="color"]': [{ prop: 'type', value: 'color' }],
24+
'input[type="date"]': [{ prop: 'type', value: 'date' }],
25+
'input[type="datetime"]': [{ prop: 'type', value: 'datetime' }],
26+
'input[type="email"]': [{ prop: 'type', value: 'email' }],
27+
'input[type="file"]': [{ prop: 'type', value: 'file' }],
28+
'input[type="image"]': [{ prop: 'type', value: 'image' }],
29+
'input[type="month"]': [{ prop: 'type', value: 'month' }],
30+
'input[type="number"]': [{ prop: 'type', value: 'number' }],
31+
'input[type="password"]': [{ prop: 'type', value: 'password' }],
32+
'input[type="radio"]': [{ prop: 'type', value: 'radio' }],
33+
'input[type="range"]': [{ prop: 'type', value: 'range' }],
34+
'input[type="reset"]': [{ prop: 'type', value: 'reset' }],
35+
'input[type="search"]': [{ prop: 'type', value: 'search' }],
36+
'input[type="submit"]': [{ prop: 'type', value: 'submit' }],
37+
'input[type="tel"]': [{ prop: 'type', value: 'tel' }],
38+
'input[type="text"]': [{ prop: 'type', value: 'text' }],
39+
'input[type="time"]': [{ prop: 'type', value: 'time' }],
40+
'input[type="url"]': [{ prop: 'type', value: 'url' }],
41+
'input[type="week"]': [{ prop: 'type', value: 'week' }],
4642
menuitem: [],
4743
option: [],
4844
select: [],
@@ -107,29 +103,21 @@ const nonInteractiveElementsMap = {
107103
ul: [],
108104
};
109105

110-
const indeterminantInteractiveElementsMap = domElements
111-
.reduce(
112-
(
113-
accumulator: {[key: string]: Array<any>},
114-
name: string,
115-
): {[key: string]: Array<any>} => {
116-
accumulator[name] = [];
117-
return accumulator;
118-
},
119-
{},
120-
);
106+
const indeterminantInteractiveElementsMap = domElements.reduce(
107+
(accumulator: { [key: string]: Array<any> }, name: string): { [key: string]: Array<any> } => ({
108+
...accumulator,
109+
[name]: [],
110+
}),
111+
{},
112+
);
121113

122114
Object.keys(interactiveElementsMap)
123115
.concat(Object.keys(nonInteractiveElementsMap))
124-
.forEach(
125-
(name: string) => delete indeterminantInteractiveElementsMap[name],
126-
);
116+
.forEach((name: string) => delete indeterminantInteractiveElementsMap[name]);
127117

128-
const abstractRoles = roleNames
129-
.filter(role => roles.get(role).abstract);
118+
const abstractRoles = roleNames.filter(role => roles.get(role).abstract);
130119

131-
const nonAbstractRoles = roleNames
132-
.filter(role => !roles.get(role).abstract);
120+
const nonAbstractRoles = roleNames.filter(role => !roles.get(role).abstract);
133121

134122
const interactiveRoles = []
135123
.concat(
@@ -139,107 +127,79 @@ const interactiveRoles = []
139127
'toolbar',
140128
)
141129
.filter(role => !roles.get(role).abstract)
142-
.filter(role => roles.get(role).superClass.some(
143-
klasses => klasses.includes('widget')),
144-
);
130+
.filter(role => roles.get(role).superClass.some(klasses => klasses.includes('widget')));
145131

146132
const nonInteractiveRoles = roleNames
147133
.filter(role => !roles.get(role).abstract)
148-
.filter(role => !roles.get(role).superClass.some(
149-
klasses => klasses.includes('widget')),
150-
)
134+
.filter(role => !roles.get(role).superClass.some(klasses => klasses.includes('widget')))
151135
// 'toolbar' does not descend from widget, but it does support
152136
// aria-activedescendant, thus in practice we treat it as a widget.
153137
.filter(role => !['toolbar'].includes(role));
154138

155-
export function genElementSymbol(
156-
openingElement: Object,
157-
) {
158-
return openingElement.name.name + (
159-
(openingElement.attributes.length > 0)
160-
? `${openingElement.attributes.map(
161-
attr => `[${attr.name.name}=\"${attr.value.value}\"]`).join('')
162-
}`
163-
: ''
139+
export function genElementSymbol(openingElement: Object) {
140+
return (
141+
openingElement.name.name +
142+
(openingElement.attributes.length > 0
143+
? `${openingElement.attributes
144+
.map(attr => `[${attr.name.name}="${attr.value.value}"]`)
145+
.join('')}`
146+
: '')
164147
);
165148
}
166149

167150
export function genInteractiveElements() {
168-
return Object.keys(interactiveElementsMap)
169-
.map((elementSymbol) => {
170-
const bracketIndex = elementSymbol.indexOf('[');
171-
let name = elementSymbol;
172-
if (bracketIndex > -1) {
173-
name = elementSymbol.slice(0, bracketIndex);
174-
}
175-
const attributes = interactiveElementsMap[elementSymbol].map(
176-
({ prop, value }) => JSXAttributeMock(prop, value),
177-
);
178-
return JSXElementMock(name, attributes);
179-
});
151+
return Object.keys(interactiveElementsMap).map((elementSymbol) => {
152+
const bracketIndex = elementSymbol.indexOf('[');
153+
let name = elementSymbol;
154+
if (bracketIndex > -1) {
155+
name = elementSymbol.slice(0, bracketIndex);
156+
}
157+
const attributes = interactiveElementsMap[elementSymbol].map(({ prop, value }) =>
158+
JSXAttributeMock(prop, value),
159+
);
160+
return JSXElementMock(name, attributes);
161+
});
180162
}
181163

182164
export function genInteractiveRoleElements() {
183-
return [
184-
...interactiveRoles,
185-
'button article',
186-
'fakerole button article',
187-
].map(
188-
value => JSXElementMock('div', [
189-
JSXAttributeMock('role', value),
190-
]),
165+
return [...interactiveRoles, 'button article', 'fakerole button article'].map(value =>
166+
JSXElementMock('div', [JSXAttributeMock('role', value)]),
191167
);
192168
}
193169

194170
export function genNonInteractiveElements() {
195-
return Object.keys(nonInteractiveElementsMap)
196-
.map((elementSymbol) => {
197-
const bracketIndex = elementSymbol.indexOf('[');
198-
let name = elementSymbol;
199-
if (bracketIndex > -1) {
200-
name = elementSymbol.slice(0, bracketIndex);
201-
}
202-
const attributes = nonInteractiveElementsMap[elementSymbol].map(
203-
({ prop, value }) => JSXAttributeMock(prop, value),
204-
);
205-
return JSXElementMock(name, attributes);
206-
});
171+
return Object.keys(nonInteractiveElementsMap).map((elementSymbol) => {
172+
const bracketIndex = elementSymbol.indexOf('[');
173+
let name = elementSymbol;
174+
if (bracketIndex > -1) {
175+
name = elementSymbol.slice(0, bracketIndex);
176+
}
177+
const attributes = nonInteractiveElementsMap[elementSymbol].map(({ prop, value }) =>
178+
JSXAttributeMock(prop, value),
179+
);
180+
return JSXElementMock(name, attributes);
181+
});
207182
}
208183

209184
export function genNonInteractiveRoleElements() {
210-
return [
211-
...nonInteractiveRoles,
212-
'article button',
213-
'fakerole article button',
214-
].map(
215-
value => JSXElementMock('div', [
216-
JSXAttributeMock('role', value),
217-
]),
185+
return [...nonInteractiveRoles, 'article button', 'fakerole article button'].map(value =>
186+
JSXElementMock('div', [JSXAttributeMock('role', value)]),
218187
);
219188
}
220189

221190
export function genAbstractRoleElements() {
222-
return abstractRoles.map(
223-
value => JSXElementMock('div', [
224-
JSXAttributeMock('role', value),
225-
]),
226-
);
191+
return abstractRoles.map(value => JSXElementMock('div', [JSXAttributeMock('role', value)]));
227192
}
228193

229194
export function genNonAbstractRoleElements() {
230-
return nonAbstractRoles.map(
231-
value => JSXElementMock('div', [
232-
JSXAttributeMock('role', value),
233-
]),
234-
);
195+
return nonAbstractRoles.map(value => JSXElementMock('div', [JSXAttributeMock('role', value)]));
235196
}
236197

237198
export function genIndeterminantInteractiveElements() {
238-
return Object.keys(indeterminantInteractiveElementsMap)
239-
.map((name) => {
240-
const attributes = indeterminantInteractiveElementsMap[name].map(
241-
({ prop, value }) => JSXAttributeMock(prop, value),
242-
);
243-
return JSXElementMock(name, attributes);
244-
});
199+
return Object.keys(indeterminantInteractiveElementsMap).map((name) => {
200+
const attributes = indeterminantInteractiveElementsMap[name].map(({ prop, value }) =>
201+
JSXAttributeMock(prop, value),
202+
);
203+
return JSXElementMock(name, attributes);
204+
});
245205
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"prepublish": "npm run lint && npm run flow && npm run test && npm run build",
2222
"coveralls": "cat ./reports/lcov.info | coveralls",
2323
"flow": "if [ ! -e ./.flowconfig ]; then echo \"Could not find .flowconfig\"; else flow; test $? -eq 0 -o $? -eq 2; fi",
24-
"lint": "eslint --config .eslintrc src __tests__",
24+
"lint": "eslint --config .eslintrc src __tests__ __mocks__ scripts",
2525
"lint:fix": "npm run lint -- --fix",
2626
"pretest": "npm run lint:fix && npm run flow",
2727
"test": "jest --coverage __tests__/**/*",

scripts/addRuleToIndex.js

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ export const parser = 'flow';
33
export default function transformer(file, api, options) {
44
const j = api.jscodeshift;
55
const s = j(file.source);
6-
const {
7-
ruleName,
8-
rulePath,
9-
} = options || {};
6+
const { ruleName, rulePath } = options || {};
107

118
const nameSort = (a, b) => {
12-
const aName = (a.key.type === 'Literal') ? a.key.value : a.key.name;
13-
const bName = (b.key.type === 'Literal') ? b.key.value : b.key.name;
9+
const aName = a.key.type === 'Literal' ? a.key.value : a.key.name;
10+
const bName = b.key.type === 'Literal' ? b.key.value : b.key.name;
1411
if (aName < bName) {
1512
return -1;
1613
}
@@ -22,40 +19,32 @@ export default function transformer(file, api, options) {
2219

2320
let changesMade = 0;
2421

25-
const rulePathInSrc = './' + rulePath.match(/src\/(.*)\.js/)[1];
26-
27-
changesMade += s.find(j.Identifier, {
28-
name: 'rules'
29-
}).forEach((path, index) => {
30-
// Add rule path.
31-
if (index === 0) {
32-
path.parentPath.value.value.properties.unshift(
33-
j.property(
34-
'init',
35-
j.literal(ruleName),
36-
j.callExpression(
37-
j.identifier('require'),
38-
[
39-
j.literal(rulePathInSrc),
40-
]
22+
const rulePathInSrc = `./${rulePath.match(/src\/(.*)\.js/)[1]}`;
23+
24+
changesMade += s
25+
.find(j.Identifier, {
26+
name: 'rules',
27+
})
28+
.forEach((path, index) => {
29+
// Add rule path.
30+
if (index === 0) {
31+
path.parentPath.value.value.properties.unshift(
32+
j.property(
33+
'init',
34+
j.literal(ruleName),
35+
j.callExpression(j.identifier('require'), [j.literal(rulePathInSrc)]),
4136
),
42-
)
43-
);
44-
path.parentPath.value.value.properties.sort(nameSort);
45-
}
46-
// Set default reporting to error.
47-
if (index === 1) {
48-
path.parentPath.value.value.properties.unshift(
49-
j.property(
50-
'init',
51-
j.literal('jsx-a11y/' + ruleName),
52-
j.literal('error'),
53-
)
54-
);
55-
path.parentPath.value.value.properties.sort(nameSort);
56-
}
57-
}).length;
58-
37+
);
38+
path.parentPath.value.value.properties.sort(nameSort);
39+
}
40+
// Set default reporting to error.
41+
if (index === 1) {
42+
path.parentPath.value.value.properties.unshift(
43+
j.property('init', j.literal(`jsx-a11y/${ruleName}`), j.literal('error')),
44+
);
45+
path.parentPath.value.value.properties.sort(nameSort);
46+
}
47+
}).length;
5948

6049
if (changesMade === 0) {
6150
return null;

0 commit comments

Comments
 (0)