Skip to content

Commit c0c334f

Browse files
committed
[fix] - Throw error when trying to extract value from unexpected expression type.
1 parent 7679c3f commit c0c334f

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/util/values/expressions/index.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import CallExpression from './CallExpression';
1212
import UnaryExpression from './UnaryExpression';
1313
import ThisExpression from './ThisExpression';
1414
import ConditionalExpression from './ConditionalExpression';
15+
import BinaryExpression from './BinaryExpression';
16+
import ObjectExpression from './ObjectExpression';
17+
import NewExpression from './NewExpression';
1518

1619

1720

@@ -27,7 +30,10 @@ const TYPES = {
2730
CallExpression,
2831
UnaryExpression,
2932
ThisExpression,
30-
ConditionalExpression
33+
ConditionalExpression,
34+
BinaryExpression,
35+
ObjectExpression,
36+
NewExpression
3137
};
3238

3339
const noop = () => null;
@@ -55,9 +61,16 @@ const LITERAL_TYPES = assign({}, TYPES, {
5561
return extractedVal === undefined ? null : extractedVal;
5662
},
5763
ThisExpression: noop,
58-
ConditionalExpression: noop
64+
ConditionalExpression: noop,
65+
BinaryExpression: noop,
66+
ObjectExpression: noop,
67+
NewExpression: noop
5968
});
6069

70+
const ERROR_MESSAGE = expression =>
71+
`The prop value with an expression type of ${expression} could not be resolved.
72+
Please file issue to get this fixed immediately.`;
73+
6174
/**
6275
* This function maps an AST value node
6376
* to its correct extractor function for its
@@ -71,8 +84,13 @@ const LITERAL_TYPES = assign({}, TYPES, {
7184
export default function extract(value) {
7285
// Value will not have the expression property when we recurse.
7386
const expression = value.expression || value;
87+
const { type } = expression;
88+
89+
if (TYPES[type] === undefined) {
90+
throw new Error(ERROR_MESSAGE(type));
91+
}
7492

75-
return TYPES[expression.type](expression);
93+
return TYPES[type](expression);
7694
}
7795

7896
/**
@@ -86,7 +104,13 @@ export default function extract(value) {
86104
* @returns The extracted value.
87105
*/
88106
export function extractLiteral(value) {
107+
// Value will not have the expression property when we recurse.
89108
const expression = value.expression || value;
109+
const { type } = expression;
110+
111+
if (LITERAL_TYPES[type] === undefined) {
112+
throw new Error(ERROR_MESSAGE(type));
113+
}
90114

91-
return LITERAL_TYPES[expression.type](expression);
115+
return LITERAL_TYPES[type](expression);
92116
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ ruleTester.run('img-has-alt', rule, {
7474
{ code: '<img alt="this is lit..." role="presentation" />', parserOptions },
7575
{ code: '<img alt={error ? "not working": "working"} />', parserOptions },
7676
{ code: '<img alt={undefined ? "working": "not working"} />', parserOptions },
77+
{ code: '<img alt={plugin.name + " Logo"} />', parserOptions },
7778

7879
// CUSTOM ELEMENT TESTS FOR STRING OPTION
7980
{ code: '<Avatar alt="foo" />;', options: string, parserOptions },

tests/src/rules/onclick-has-focus.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ ruleTester.run('onclick-has-focus', rule, {
3636
valid: [
3737
{ code: '<div />', parserOptions },
3838
{ code: '<div aria-hidden onClick={() => void 0} />', parserOptions },
39+
{ code: '<div aria-hidden={true == true} onClick={() => void 0} />', parserOptions },
40+
{ code: '<div aria-hidden={true === true} onClick={() => void 0} />', parserOptions },
41+
{ code: '<div aria-hidden={hidden !== false} onClick={() => void 0} />', parserOptions },
42+
{ code: '<div aria-hidden={hidden != false} onClick={() => void 0} />', parserOptions },
43+
{ code: '<div aria-hidden={1 < 2} onClick={() => void 0} />', parserOptions },
44+
{ code: '<div aria-hidden={1 <= 2} onClick={() => void 0} />', parserOptions },
45+
{ code: '<div aria-hidden={2 > 1} onClick={() => void 0} />', parserOptions },
46+
{ code: '<div aria-hidden={2 >= 1} onClick={() => void 0} />', parserOptions },
3947
{ code: '<input type="text" onClick={() => void 0} />', parserOptions },
4048
{ code: '<input type="hidden" onClick={() => void 0} tabIndex="-1" />', parserOptions },
4149
{ code: '<input onClick={() => void 0} />', parserOptions },

0 commit comments

Comments
 (0)