Skip to content

Commit

Permalink
fix crashes in jsx-key; fixes #373
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Dec 24, 2015
1 parent 98541c5 commit 50f4762
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,24 @@ module.exports = function(context) {

// Array.prototype.map
CallExpression: function (node) {
if (node.callee.property.name !== 'map') {
if (node.callee && node.callee.property && node.callee.property.name !== 'map') {
return;
}

var fn = node.arguments[0];
var isFn = fn.type === 'FunctionExpression';
var isArrFn = fn.type === 'ArrowFunctionExpression';
var isFn = fn && fn.type === 'FunctionExpression';
var isArrFn = fn && fn.type === 'ArrowFunctionExpression';

if (isArrFn && fn.body.type === 'JSXElement') {
checkIteratorElement(fn.body);
}

if (isFn || isArrFn) {
if (fn.body.type === 'BlockStatement') {
checkIteratorElement(
getReturnStatement(fn.body.body).argument
);
var returnStatement = getReturnStatement(fn.body.body);
if (returnStatement) {
checkIteratorElement(returnStatement.argument);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var parserOptions = {
var ruleTester = new RuleTester();
ruleTester.run('jsx-key', rule, {
valid: [
{code: 'fn()', parserOptions: parserOptions},
{code: '[1, 2, 3].map(function () {})', parserOptions: parserOptions},
{code: '<App />;', parserOptions: parserOptions},
{code: '[<App key={0} />, <App key={1} />];', parserOptions: parserOptions},
{code: '[1, 2, 3].map(function(x) { return <App key={x} /> });', parserOptions: parserOptions},
Expand Down

0 comments on commit 50f4762

Please sign in to comment.