Skip to content

Commit 410b329

Browse files
committed
[fix] - Convert some arrow syntax functions to function syntax and add better documentation.
I decided to convert *all* of the util functions from arrow syntax to function syntax for consistency and readability. This is where all of the shared logic really lies and it helps with grokking. Since we are already at one function per file, it doesn’t make the code more readable to keep inline. Coverage goes down because more lines were added :/
1 parent fd542f6 commit 410b329

18 files changed

+222
-64
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"build": "rimraf lib && babel src --out-dir lib",
2121
"prepublish": "npm run lint && npm run test && npm run build",
2222
"coveralls": "cat ./reports/coverage/lcov.info | coveralls",
23-
"lint": "eslint --config .eslintrc.js src tests",
23+
"lint": "eslint --config .eslintrc.js src/**/*.js tests/**/*.js",
2424
"pretest": "npm run lint",
2525
"test": "istanbul cover --dir reports/coverage node_modules/mocha/bin/_mocha tests/**/*.js -- --compilers js:babel-core/register --reporter dot"
2626
},

src/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ module.exports = {
2424
}
2525
},
2626
rules: {
27-
"jsx-a11y/img-uses-alt": 2,
28-
"jsx-a11y/redundant-alt": 2,
29-
"jsx-a11y/onclick-uses-role": 2,
30-
"jsx-a11y/mouse-events-map-to-key-events": 2,
31-
"jsx-a11y/use-onblur-not-onchange": 2,
32-
"jsx-a11y/no-access-key": 2,
33-
"jsx-a11y/label-uses-for": 2,
34-
"jsx-a11y/no-hash-href": 2,
35-
"jsx-a11y/valid-aria-role": 2,
36-
"jsx-a11y/valid-aria-proptypes": 2,
37-
"jsx-a11y/no-invalid-aria": 2,
38-
"jsx-a11y/role-requires-aria": 2,
39-
"jsx-a11y/no-unsupported-elements-use-aria": 2
27+
'jsx-a11y/img-uses-alt': 2,
28+
'jsx-a11y/redundant-alt': 2,
29+
'jsx-a11y/onclick-uses-role': 2,
30+
'jsx-a11y/mouse-events-map-to-key-events': 2,
31+
'jsx-a11y/use-onblur-not-onchange': 2,
32+
'jsx-a11y/no-access-key': 2,
33+
'jsx-a11y/label-uses-for': 2,
34+
'jsx-a11y/no-hash-href': 2,
35+
'jsx-a11y/valid-aria-role': 2,
36+
'jsx-a11y/valid-aria-proptypes': 2,
37+
'jsx-a11y/no-invalid-aria': 2,
38+
'jsx-a11y/role-requires-aria': 2,
39+
'jsx-a11y/no-unsupported-elements-use-aria': 2
4040
}
4141
}
4242
}

src/util/getAttributeValue.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
'use strict';
22

3-
import { getValue, getLiteralValue } from './values';
3+
import {
4+
default as getValue,
5+
getLiteralValue
6+
} from './values';
47

5-
const extract = (attribute, extractor) => {
8+
9+
10+
const extractValue = (attribute, extractor) => {
611
if (attribute.type === 'JSXAttribute') {
712
if (attribute.value === null) {
813
// Null valued attributes imply truthiness.
@@ -27,8 +32,22 @@ const extract = (attribute, extractor) => {
2732
*
2833
* @param attribute - The JSXAttribute collected by AST parser.
2934
*/
30-
const getAttributeValue = attribute => extract(attribute, getValue);
35+
export default function getAttributeValue(attribute) {
36+
return extractValue(attribute, getValue);
37+
}
3138

32-
export const getLiteralAttributeValue = attribute => extract(attribute, getLiteralValue);
39+
/**
40+
* Returns the value of a given attribute.
41+
* Different types of attributes have their associated
42+
* values in different properties on the object.
43+
*
44+
* This function should return a value only if we can extract
45+
* a literal value from its attribute (i.e. values that have generic
46+
* types in JavaScript - strings, numbers, booleans, etc.)
47+
*
48+
* @param attribute - The JSXAttribute collected by AST parser.
49+
*/
50+
export const getLiteralAttributeValue = function getLiteralAttributeValue(attribute) {
51+
return extractValue(attribute, getLiteralValue);
52+
};
3353

34-
export default getAttributeValue;

src/util/getNodeType.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
'use strict';
22

3-
const getNodeType = node => {
3+
/**
4+
* Returns the tagName associated with a JSXElement.
5+
*/
6+
export default function getNodeType(node) {
47
const { name } = node;
58

69
if (name.type === 'JSXMemberExpression') {
@@ -9,7 +12,5 @@ const getNodeType = node => {
912
}
1013

1114
return node.name.name;
12-
};
13-
14-
export default getNodeType;
15+
}
1516

src/util/hasAttribute.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
43
/**
54
* Returns the JSXAttribute itself or false, indicating the attribute
65
* is not present on the JSXOpeningElement. This skips over spread attributes

src/util/isHiddenFromScreenReader.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import hasAttribute from './hasAttribute';
44
import getAttributeValue from './getAttributeValue';
55

6+
7+
68
/**
79
* Returns boolean indicating that the aria-hidden prop
810
* is present or the value is true.
@@ -11,7 +13,7 @@ import getAttributeValue from './getAttributeValue';
1113
*/
1214
const isHiddenFromScreenReader = attributes => {
1315
const ariaHidden = getAttributeValue(hasAttribute(attributes, 'aria-hidden'));
14-
return ariaHidden === true || ariaHidden === null;
16+
return ariaHidden === true;
1517
};
1618

1719
export default isHiddenFromScreenReader;

src/util/isInteractiveElement.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import hasAttribute from './hasAttribute';
44
import getAttributeValue from './getAttributeValue';
55
import DOMElements from './attributes/DOM';
66

7+
8+
9+
// Map of tagNames to functions that return whether that element is interactive or not.
710
const interactiveMap = {
811
a: attributes => {
912
const hasHref = hasAttribute(attributes, 'href');

src/util/values/JSXElement.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
const extract = () => undefined;
1+
'use strict';
22

3-
export default extract;
3+
/**
4+
* Extractor function for a JSXElement type value node.
5+
*
6+
* Currently just returns undefined as we haven't found
7+
* a use case for this yet.
8+
*/
9+
export default function extractValueFromJSXElement() {
10+
return undefined;
11+
}

src/util/values/Literal.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
const extract = value => {
1+
'use strict';
2+
3+
/**
4+
* Extractor function for a Literal type value node.
5+
*
6+
* @param - value - AST Value object with type `Literal`
7+
* @returns { String|Boolean } - The extracted value converted to correct type.
8+
*/
9+
export default function extractValueFromLiteral(value) {
210
const { value: extractedValue } = value;
311

412
if (extractedValue === 'true') {
@@ -8,6 +16,4 @@ const extract = value => {
816
}
917

1018
return extractedValue;
11-
};
12-
13-
export default extract;
19+
}
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
'use strict';
2+
13
import getValue from './index';
24

3-
const extract = value => getValue(value.callee);
45

5-
export default extract;
6+
7+
/**
8+
* Extractor function for a CallExpression type value node.
9+
* A call expression looks like `bar()`
10+
* This will return `bar` as the value to indicate its existence,
11+
* since we can not execute the function bar in a static environment.
12+
*
13+
* @param - value - AST Value object with type `CallExpression`
14+
* @returns - The extracted value converted to correct type.
15+
*/
16+
export default function extractValueFromCallExpression(value) {
17+
return getValue(value.callee);
18+
}

0 commit comments

Comments
 (0)