Skip to content

Commit

Permalink
handle ternary operation for jsx-no-bind
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenael.larmet committed Mar 13, 2018
1 parent 06ed294 commit 7a9ac14
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rules/jsx-no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ module.exports = {
node.callee.property.name === 'bind'
) {
return 'bindCall';
} else if (
nodeType === 'ConditionalExpression'
) {
return getNodeViolationType(node.consequent) | getNodeViolationType(node.alternate);
} else if (
!configuration.allowArrowFunctions &&
nodeType === 'ArrowFunctionExpression'
Expand Down Expand Up @@ -154,7 +158,7 @@ module.exports = {
}
},

JSXAttribute: function(node) {
JSXAttribute: function (node) {
const isRef = configuration.ignoreRefs && propName(node) === 'ref';
if (isRef || !node.value || !node.value.expression) {
return;
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/jsx-no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,36 @@ ruleTester.run('jsx-no-bind', rule, {
],
parser: 'babel-eslint'
},
{
code: `
const foo = {
render: ({onClick}) => (
<div onClick={(true) ? onClick.bind(this) : onClick.bind(this)}>Hello</div>
)
};
`,
errors: [{message: 'JSX props should not use .bind()'}]
},
{
code: `
const foo = {
render: ({onClick}) => (
<div onClick={(true) ? onClick.bind(this) : handleClick()}>Hello</div>
)
};
`,
errors: [{message: 'JSX props should not use .bind()'}]
},
{
code: `
const foo = {
render: ({onClick}) => (
<div onClick={(true) ? handleClick() : onClick.bind(this)}>Hello</div>
)
};
`,
errors: [{message: 'JSX props should not use .bind()'}]
},

// Arrow functions
{
Expand Down

0 comments on commit 7a9ac14

Please sign in to comment.