Skip to content

handle ternary operation for jsx-no-bind #1723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/rules/jsx-no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ module.exports = {
node.callee.property.name === 'bind'
) {
return 'bindCall';
} else if (
nodeType === 'ConditionalExpression'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this if condition can fit on one line :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to match the code style of the function, I can change it if needed

) {
return getNodeViolationType(node.test) ||
getNodeViolationType(node.consequent) ||
getNodeViolationType(node.alternate);
} else if (
!configuration.allowArrowFunctions &&
nodeType === 'ArrowFunctionExpression'
Expand Down Expand Up @@ -154,7 +160,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
41 changes: 41 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,47 @@ ruleTester.run('jsx-no-bind', rule, {
],
parser: 'babel-eslint'
},
{
code: `
const foo = {
render: ({onClick}) => (
<div onClick={(returningBoolean()) ? onClick.bind(this) : onClick.bind(this)}>Hello</div>
)
};
`,
errors: [{message: 'JSX props should not use .bind()'}]
},
{
code: `
const foo = {
render: ({onClick}) => (
<div onClick={(returningBoolean()) ? onClick.bind(this) : handleClick()}>Hello</div>
)
};
`,
errors: [{message: 'JSX props should not use .bind()'}]
},
{
code: `
const foo = {
render: ({onClick}) => (
<div onClick={(returningBoolean()) ? handleClick() : this.onClick.bind(this)}>Hello</div>
)
};
`,
errors: [{message: 'JSX props should not use .bind()'}]
},
{
code: `
const foo = {
render: ({onClick}) => (
<div onClick={returningBoolean.bind(this) ? handleClick() : onClick()}>Hello</div>
)
};
`,
errors: [{message: 'JSX props should not use .bind()'}]
},


// Arrow functions
{
Expand Down