Skip to content

Commit 6d74a25

Browse files
committed
Refactor value extraction into more readable components.
1 parent 2af9ef8 commit 6d74a25

12 files changed

+98
-44
lines changed

src/rules/img-uses-alt.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ module.exports = context => ({
4343

4444
// Check if alt prop is undefined.
4545
const altValue = getAttributeValue(hasAltProp);
46+
const isNullValued = hasAltProp.value === null; // <img alt />
4647

47-
if (altValue || altValue === '') {
48+
if ((altValue && !isNullValued) || altValue === '') {
4849
return;
4950
}
5051

src/util/getAttributeValue.js

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

3-
import buildTemplateLiteral from './buildTemplateLiteral';
4-
5-
const getValue = value => {
6-
if (value.type === 'Literal') {
7-
return value.value;
8-
} else if (value.type === 'Identifier') {
9-
return value.name === "" ? undefined : value.name;
10-
} else if (value.type === 'JSXElement') {
11-
return undefined; // For now, just so things don't break.
12-
}
13-
14-
const { expression } = value;
15-
const type = expression ? expression.type : value.type;
16-
const obj = expression || value;
17-
18-
switch (type) {
19-
case 'Literal':
20-
return obj.value;
21-
case 'TemplateLiteral':
22-
return buildTemplateLiteral(obj);
23-
case 'Identifier':
24-
return obj.name == 'undefined' ? undefined : obj.name;
25-
case 'ArrowFunctionExpression':
26-
case 'FunctionExpression':
27-
return () => void 0;
28-
case 'LogicalExpression':
29-
const { operator, left, right } = obj;
30-
const leftVal = getValue(left);
31-
const rightVal = getValue(right);
32-
33-
return operator == '&&' ? leftVal && rightVal : leftVal || rightVal;
34-
case 'MemberExpression':
35-
return `${getValue(obj.object)}.${getValue(obj.property)}`;
36-
case 'CallExpression':
37-
return getValue(obj.callee);
38-
default:
39-
return undefined;
40-
}
41-
42-
};
3+
import getValue from './values';
434

445
/**
456
* Returns the value of a given attribute.
@@ -48,11 +9,18 @@ const getValue = value => {
489
*
4910
* This function should return the most *closely* associated
5011
* value with the intention of the JSX.
12+
*
13+
* @param attribute - The JSXAttribute collected by AST parser.
5114
*/
5215
const getAttributeValue = attribute => {
53-
if (attribute.value === null) {
54-
return null;
55-
} else if (attribute.type === 'JSXAttribute') {
16+
if (attribute.type === 'JSXAttribute') {
17+
if (attribute.value === null) {
18+
// Null valued attributes imply truthiness.
19+
// For example: <div aria-hidden />
20+
// See: https://facebook.github.io/react/docs/jsx-in-depth.html#boolean-attributes
21+
return true;
22+
}
23+
5624
return getValue(attribute.value);
5725
}
5826

src/util/values/Identifier.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const extract = value => {
2+
const { name } = value;
3+
4+
return name == 'undefined' ? undefined : name;
5+
};
6+
7+
export default extract;

src/util/values/JSXElement.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const extract = () => undefined;
2+
3+
export default extract;

src/util/values/Literal.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const extract = value => value.value;
2+
3+
export default extract;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import getValue from './index';
2+
3+
const extract = value => getValue(value.callee);
4+
5+
export default extract;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const extract = () => () => void 0;
2+
3+
export default extract;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import getValue from './index';
2+
3+
const extract = value => {
4+
const { operator, left, right } = value;
5+
const leftVal = getValue(left);
6+
const rightVal = getValue(right);
7+
8+
return operator == '&&' ? leftVal && rightVal : leftVal || rightVal;
9+
};
10+
11+
export default extract;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import getValue from './index';
2+
3+
const extract = value => `${getValue(value.object)}.${getValue(value.property)}`;
4+
5+
export default extract;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import buildTemplateLiteral from '../../buildTemplateLiteral';
2+
3+
const extract = value => buildTemplateLiteral(value);
4+
5+
export default extract;

0 commit comments

Comments
 (0)