Skip to content

Commit be3d26a

Browse files
committed
Enhance isReturningJSX to handle more node types
1 parent 8c61d14 commit be3d26a

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

lib/rules/jsx-no-bind.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@ var propName = require('jsx-ast-utils/propName');
1212
// Rule Definition
1313
// -----------------------------------------------------------------------------
1414

15-
function renderReturnsJSX(node, utils) {
16-
var i = node.value.body.body.length - 1;
17-
for (; i >= 0; i--) {
18-
var testNode = node.value.body.body[i];
19-
if (testNode.type === 'ReturnStatement' && utils.isReturningJSX(testNode)) {
20-
return true;
21-
}
22-
}
23-
return false;
24-
}
25-
2615
module.exports = Components.detect(function(context, components, utils) {
2716
var configuration = context.options[0] || {};
2817

@@ -42,7 +31,7 @@ module.exports = Components.detect(function(context, components, utils) {
4231
(ancestors[i].type === 'MethodDefinition' && ancestors[i].key.name === 'render') ||
4332
(ancestors[i].type === 'Property' && ancestors[i].key.name === 'render')
4433
) {
45-
if (renderReturnsJSX(ancestors[i], utils)) {
34+
if (utils.isReturningJSX(ancestors[i])) {
4635
context.report({
4736
node: callee,
4837
message: 'JSX props should not use .bind()'

lib/util/Components.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,13 @@ function componentRule(rule, context) {
199199
/**
200200
* Check if the node is returning JSX
201201
*
202-
* @param {ASTNode} node The AST node being checked (can be a ReturnStatement or an ArrowFunctionExpression).
202+
* @param {ASTNode} ASTnode The AST node being checked
203203
* @param {Boolean} strict If true, in a ternary condition the node must return JSX in both cases
204204
* @returns {Boolean} True if the node is returning JSX, false if not
205205
*/
206-
isReturningJSX: function(node, strict) {
206+
isReturningJSX: function(ASTnode, strict) {
207207
var property;
208+
var node = ASTnode;
208209
switch (node.type) {
209210
case 'ReturnStatement':
210211
property = 'argument';
@@ -213,7 +214,11 @@ function componentRule(rule, context) {
213214
property = 'body';
214215
break;
215216
default:
216-
return false;
217+
node = utils.findReturnStatement(node);
218+
if (!node) {
219+
return false;
220+
}
221+
property = 'argument';
217222
}
218223

219224
var returnsConditionalJSXConsequent =
@@ -248,6 +253,24 @@ function componentRule(rule, context) {
248253
);
249254
},
250255

256+
/**
257+
* Find a return statment in the current node
258+
*
259+
* @param {ASTNode} ASTnode The AST node being checked
260+
*/
261+
findReturnStatement: function(node) {
262+
if (!node.value || !node.value.body) {
263+
return false;
264+
}
265+
var i = node.value.body.body.length - 1;
266+
for (; i >= 0; i--) {
267+
if (node.value.body.body[i].type === 'ReturnStatement') {
268+
return node.value.body.body[i];
269+
}
270+
}
271+
return false;
272+
},
273+
251274
/**
252275
* Get the parent component node from the current scope
253276
*

0 commit comments

Comments
 (0)