Skip to content

Commit

Permalink
[fix] - Throw error when trying to extract value from unexpected expr…
Browse files Browse the repository at this point in the history
…ession type.
  • Loading branch information
beefancohen committed May 6, 2016
1 parent 7679c3f commit c0c334f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/util/values/expressions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import CallExpression from './CallExpression';
import UnaryExpression from './UnaryExpression';
import ThisExpression from './ThisExpression';
import ConditionalExpression from './ConditionalExpression';
import BinaryExpression from './BinaryExpression';
import ObjectExpression from './ObjectExpression';
import NewExpression from './NewExpression';



Expand All @@ -27,7 +30,10 @@ const TYPES = {
CallExpression,
UnaryExpression,
ThisExpression,
ConditionalExpression
ConditionalExpression,
BinaryExpression,
ObjectExpression,
NewExpression
};

const noop = () => null;
Expand Down Expand Up @@ -55,9 +61,16 @@ const LITERAL_TYPES = assign({}, TYPES, {
return extractedVal === undefined ? null : extractedVal;
},
ThisExpression: noop,
ConditionalExpression: noop
ConditionalExpression: noop,
BinaryExpression: noop,
ObjectExpression: noop,
NewExpression: noop
});

const ERROR_MESSAGE = expression =>
`The prop value with an expression type of ${expression} could not be resolved.
Please file issue to get this fixed immediately.`;

/**
* This function maps an AST value node
* to its correct extractor function for its
Expand All @@ -71,8 +84,13 @@ const LITERAL_TYPES = assign({}, TYPES, {
export default function extract(value) {
// Value will not have the expression property when we recurse.
const expression = value.expression || value;
const { type } = expression;

if (TYPES[type] === undefined) {
throw new Error(ERROR_MESSAGE(type));
}

return TYPES[expression.type](expression);
return TYPES[type](expression);
}

/**
Expand All @@ -86,7 +104,13 @@ export default function extract(value) {
* @returns The extracted value.
*/
export function extractLiteral(value) {
// Value will not have the expression property when we recurse.
const expression = value.expression || value;
const { type } = expression;

if (LITERAL_TYPES[type] === undefined) {
throw new Error(ERROR_MESSAGE(type));
}

return LITERAL_TYPES[expression.type](expression);
return LITERAL_TYPES[type](expression);
}
1 change: 1 addition & 0 deletions tests/src/rules/img-has-alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ ruleTester.run('img-has-alt', rule, {
{ code: '<img alt="this is lit..." role="presentation" />', parserOptions },
{ code: '<img alt={error ? "not working": "working"} />', parserOptions },
{ code: '<img alt={undefined ? "working": "not working"} />', parserOptions },
{ code: '<img alt={plugin.name + " Logo"} />', parserOptions },

// CUSTOM ELEMENT TESTS FOR STRING OPTION
{ code: '<Avatar alt="foo" />;', options: string, parserOptions },
Expand Down
8 changes: 8 additions & 0 deletions tests/src/rules/onclick-has-focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ ruleTester.run('onclick-has-focus', rule, {
valid: [
{ code: '<div />', parserOptions },
{ code: '<div aria-hidden onClick={() => void 0} />', parserOptions },
{ code: '<div aria-hidden={true == true} onClick={() => void 0} />', parserOptions },
{ code: '<div aria-hidden={true === true} onClick={() => void 0} />', parserOptions },
{ code: '<div aria-hidden={hidden !== false} onClick={() => void 0} />', parserOptions },
{ code: '<div aria-hidden={hidden != false} onClick={() => void 0} />', parserOptions },
{ code: '<div aria-hidden={1 < 2} onClick={() => void 0} />', parserOptions },
{ code: '<div aria-hidden={1 <= 2} onClick={() => void 0} />', parserOptions },
{ code: '<div aria-hidden={2 > 1} onClick={() => void 0} />', parserOptions },
{ code: '<div aria-hidden={2 >= 1} onClick={() => void 0} />', parserOptions },
{ code: '<input type="text" onClick={() => void 0} />', parserOptions },
{ code: '<input type="hidden" onClick={() => void 0} tabIndex="-1" />', parserOptions },
{ code: '<input onClick={() => void 0} />', parserOptions },
Expand Down

0 comments on commit c0c334f

Please # to comment.