Skip to content

Commit 6f554c9

Browse files
author
Ethan Cohen
committed
Add call expression handler and recursively derive certain attr values.
Fixes #10 Fixes last mentioned part of #6
1 parent 9f788ca commit 6f554c9

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

src/util/getAttributeValue.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,38 @@ const getValue = value => {
77
return value.value === "" ? undefined : value.value;
88
} else if (value.type === 'Identifier') {
99
return value.name === "" ? undefined : value.name;
10-
} else if (value.type === 'JSXExpressionContainer') {
11-
const { expression } = value;
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-
const { operator, left, right } = expression;
25-
const leftVal = getValue(left);
26-
const rightVal = getValue(right);
27-
28-
return operator == '&&' ? leftVal && rightVal : leftVal || rightVal;
29-
case 'MemberExpression':
30-
return `${getValue(expression.object)}.${expression.property}`;
31-
default:
32-
return undefined;
33-
}
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 === "" ? undefined : 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;
3440
}
3541

36-
return undefined;
3742
};
3843

3944
/**

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ ruleTester.run('img-uses-alt', rule, {
5757
{ code: '<UX.Layout>test</UX.Layout>', parserOptions },
5858
{ code: '<img alt={alt || "Alt text" } />', parserOptions },
5959
{ code: '<img alt={photo.caption} />;', parserOptions },
60+
{ code: '<img alt={bar()} />;', parserOptions },
61+
{ code: '<img alt={foo.bar || ""} />', parserOptions },
62+
{ code: '<img alt={bar() || ""} />', parserOptions },
63+
{ code: '<img alt={foo.bar() || ""} />', parserOptions },
6064
{ code: '<img alt=" " />', parserOptions }, // For decorative images.
6165

6266
// CUSTOM ELEMENT TESTS FOR STRING OPTION

0 commit comments

Comments
 (0)