Skip to content

Commit

Permalink
Merge pull request #374 from lukekarrys/373-jsx-key-crash
Browse files Browse the repository at this point in the history
Fix crash in jsx-key (fixes #373)
  • Loading branch information
yannickcr committed Dec 24, 2015
2 parents 98541c5 + 50f4762 commit 095c91b
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 095c91b

Please sign in to comment.