Skip to content

Commit 954d71e

Browse files
committed
Handle LogicalExpression attribute types to get values.
1 parent 645bf16 commit 954d71e

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

src/util/getAttributeValue.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@
22

33
import buildTemplateLiteral from './buildTemplateLiteral';
44

5+
const getValue = value => {
6+
if (value.type === 'Literal') {
7+
return value.value === "" ? undefined : value.value;
8+
} else if (value.type === 'Identifier') {
9+
return value.name === "" ? undefined : value.name;
10+
} else if (value.type === 'JSXExpressionContainer') {
11+
const expression = value.expression;
12+
13+
switch (expression.type) {
14+
case 'Literal':
15+
return expression.value === "" ? undefined : expression.value;
16+
case 'TemplateLiteral':
17+
return buildTemplateLiteral(expression);
18+
case 'Identifier':
19+
return expression.name == 'undefined' ? undefined : expression.name;
20+
case 'ArrowFunctionExpression':
21+
case 'FunctionExpression':
22+
return () => void 0;
23+
case 'LogicalExpression':
24+
return getValue(expression.left) && getValue(expression.right);
25+
default:
26+
return undefined;
27+
}
28+
}
29+
30+
return undefined;
31+
};
32+
533
/**
634
* Returns the value of a given attribute.
735
* Different types of attributes have their associated
@@ -14,26 +42,7 @@ const getAttributeValue = attribute => {
1442
if (attribute.value === null) {
1543
return null;
1644
} else if (attribute.type === 'JSXAttribute') {
17-
18-
if (attribute.value.type === 'Literal') {
19-
return attribute.value.value === "" ? undefined : attribute.value.value;
20-
} else if (attribute.value.type === 'JSXExpressionContainer') {
21-
const expression = attribute.value.expression;
22-
23-
switch (expression.type) {
24-
case 'Literal':
25-
return expression.value === "" ? undefined : expression.value;
26-
case 'TemplateLiteral':
27-
return buildTemplateLiteral(expression);
28-
case 'Identifier':
29-
return expression.name == 'undefined' ? undefined : expression.name;
30-
case 'ArrowFunctionExpression':
31-
case 'FunctionExpression':
32-
return () => void 0;
33-
default:
34-
return undefined;
35-
}
36-
}
45+
return getValue(attribute.value);
3746
}
3847

3948
return undefined;

tests/src/rules/img-uses-alt.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ ruleTester.run('img-uses-alt', rule, {
5555
{ code: '<img alt={() => void 0} />', parserOptions },
5656
{ code: '<IMG />', parserOptions },
5757
{ code: '<UX.Layout>test</UX.Layout>', parserOptions },
58+
{ code: '<img alt={alt || "Alt text" } />', parserOptions },
5859

5960
// CUSTOM ELEMENT TESTS FOR STRING OPTION
6061
{ code: '<Avatar alt="foo" />;', options: string, parserOptions },
@@ -70,6 +71,7 @@ ruleTester.run('img-uses-alt', rule, {
7071
{ code: '<div alt={function(e) {} } />', options: string, parserOptions },
7172
{ code: '<Avatar alt={() => void 0} />', options: string, parserOptions },
7273
{ code: '<AVATAR />', options: string, parserOptions },
74+
{ code: '<Avatar alt={alt || "foo" } />', options: string, parserOptions },
7375

7476
// CUSTOM ELEMENT TESTS FOR ARRAY OPTION TESTS
7577
{ code: '<Thumbnail alt="foo" />;', options: array, parserOptions },
@@ -85,6 +87,7 @@ ruleTester.run('img-uses-alt', rule, {
8587
{ code: '<div alt={function(e) {} } />', options: array, parserOptions },
8688
{ code: '<Thumbnail alt={() => void 0} />', options: array, parserOptions },
8789
{ code: '<THUMBNAIL />', options: array, parserOptions },
90+
{ code: '<Thumbnail alt={alt || "foo" } />', options: array, parserOptions },
8891
{ code: '<Image alt="foo" />;', options: array, parserOptions },
8992
{ code: '<Image alt={"foo"} />;', options: array, parserOptions },
9093
{ code: '<Image alt={alt} />;', options: array, parserOptions },
@@ -97,7 +100,8 @@ ruleTester.run('img-uses-alt', rule, {
97100
{ code: '<Image alt={function(e) {} } />', options: array, parserOptions },
98101
{ code: '<div alt={function(e) {} } />', options: array, parserOptions },
99102
{ code: '<Image alt={() => void 0} />', options: array, parserOptions },
100-
{ code: '<IMAGE />', options: array, parserOptions }
103+
{ code: '<IMAGE />', options: array, parserOptions },
104+
{ code: '<Image alt={alt || "foo" } />', options: array, parserOptions }
101105
],
102106
invalid: [
103107
// DEFAULT ELEMENT 'img' TESTS
@@ -108,6 +112,7 @@ ruleTester.run('img-uses-alt', rule, {
108112
{ code: '<img alt="" />;', errors: [ expectedError ], parserOptions },
109113
{ code: '<img src="xyz" />', errors: [ expectedError ], parserOptions },
110114
{ code: '<img {...this.props} />', errors: [ expectedError ], parserOptions },
115+
{ code: '<img alt={false || false} />', errors: [ expectedError ], parserOptions },
111116

112117
// CUSTOM ELEMENT TESTS FOR STRING OPTION
113118
{ code: '<Avatar />;', errors: [ customError('Avatar') ], options: string, parserOptions },

0 commit comments

Comments
 (0)