Skip to content

Commit 7a9ac14

Browse files
author
gwenael.larmet
committed
handle ternary operation for jsx-no-bind
1 parent 06ed294 commit 7a9ac14

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/rules/jsx-no-bind.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ module.exports = {
8181
node.callee.property.name === 'bind'
8282
) {
8383
return 'bindCall';
84+
} else if (
85+
nodeType === 'ConditionalExpression'
86+
) {
87+
return getNodeViolationType(node.consequent) | getNodeViolationType(node.alternate);
8488
} else if (
8589
!configuration.allowArrowFunctions &&
8690
nodeType === 'ArrowFunctionExpression'
@@ -154,7 +158,7 @@ module.exports = {
154158
}
155159
},
156160

157-
JSXAttribute: function(node) {
161+
JSXAttribute: function (node) {
158162
const isRef = configuration.ignoreRefs && propName(node) === 'ref';
159163
if (isRef || !node.value || !node.value.expression) {
160164
return;

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,36 @@ ruleTester.run('jsx-no-bind', rule, {
397397
],
398398
parser: 'babel-eslint'
399399
},
400+
{
401+
code: `
402+
const foo = {
403+
render: ({onClick}) => (
404+
<div onClick={(true) ? 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={(true) ? 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={(true) ? handleClick() : onClick.bind(this)}>Hello</div>
425+
)
426+
};
427+
`,
428+
errors: [{message: 'JSX props should not use .bind()'}]
429+
},
400430

401431
// Arrow functions
402432
{

0 commit comments

Comments
 (0)