Skip to content

Commit a5f259f

Browse files
authored
Merge pull request jsx-eslint#1723 from gwenaellarmet/handle-ternary
[fix] handle ternary operation for jsx-no-bind
2 parents 06ed294 + e2fd714 commit a5f259f

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/rules/jsx-no-bind.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ module.exports = {
8181
node.callee.property.name === 'bind'
8282
) {
8383
return 'bindCall';
84+
} else if (
85+
nodeType === 'ConditionalExpression'
86+
) {
87+
return getNodeViolationType(node.test) ||
88+
getNodeViolationType(node.consequent) ||
89+
getNodeViolationType(node.alternate);
8490
} else if (
8591
!configuration.allowArrowFunctions &&
8692
nodeType === 'ArrowFunctionExpression'
@@ -154,7 +160,7 @@ module.exports = {
154160
}
155161
},
156162

157-
JSXAttribute: function(node) {
163+
JSXAttribute: function (node) {
158164
const isRef = configuration.ignoreRefs && propName(node) === 'ref';
159165
if (isRef || !node.value || !node.value.expression) {
160166
return;

tests/lib/rules/jsx-no-bind.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,47 @@ ruleTester.run('jsx-no-bind', rule, {
397397
],
398398
parser: 'babel-eslint'
399399
},
400+
{
401+
code: `
402+
const foo = {
403+
render: ({onClick}) => (
404+
<div onClick={(returningBoolean()) ? onClick.bind(this) : onClick.bind(this)}>Hello</div>
405+
)
406+
};
407+
`,
408+
errors: [{message: 'JSX props should not use .bind()'}]
409+
},
410+
{
411+
code: `
412+
const foo = {
413+
render: ({onClick}) => (
414+
<div onClick={(returningBoolean()) ? onClick.bind(this) : handleClick()}>Hello</div>
415+
)
416+
};
417+
`,
418+
errors: [{message: 'JSX props should not use .bind()'}]
419+
},
420+
{
421+
code: `
422+
const foo = {
423+
render: ({onClick}) => (
424+
<div onClick={(returningBoolean()) ? handleClick() : this.onClick.bind(this)}>Hello</div>
425+
)
426+
};
427+
`,
428+
errors: [{message: 'JSX props should not use .bind()'}]
429+
},
430+
{
431+
code: `
432+
const foo = {
433+
render: ({onClick}) => (
434+
<div onClick={returningBoolean.bind(this) ? handleClick() : onClick()}>Hello</div>
435+
)
436+
};
437+
`,
438+
errors: [{message: 'JSX props should not use .bind()'}]
439+
},
440+
400441

401442
// Arrow functions
402443
{

0 commit comments

Comments
 (0)