From 7a9ac146a62087b43f0040af327a526c2ec81f9c Mon Sep 17 00:00:00 2001 From: "gwenael.larmet" Date: Tue, 13 Mar 2018 16:09:22 +0100 Subject: [PATCH 1/2] handle ternary operation for jsx-no-bind --- lib/rules/jsx-no-bind.js | 6 +++++- tests/lib/rules/jsx-no-bind.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/rules/jsx-no-bind.js b/lib/rules/jsx-no-bind.js index f16bb5ecde..ccf7f36d8f 100644 --- a/lib/rules/jsx-no-bind.js +++ b/lib/rules/jsx-no-bind.js @@ -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' @@ -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; diff --git a/tests/lib/rules/jsx-no-bind.js b/tests/lib/rules/jsx-no-bind.js index 244acaed56..13fe3fe970 100644 --- a/tests/lib/rules/jsx-no-bind.js +++ b/tests/lib/rules/jsx-no-bind.js @@ -397,6 +397,36 @@ ruleTester.run('jsx-no-bind', rule, { ], parser: 'babel-eslint' }, + { + code: ` + const foo = { + render: ({onClick}) => ( +
Hello
+ ) + }; + `, + errors: [{message: 'JSX props should not use .bind()'}] + }, + { + code: ` + const foo = { + render: ({onClick}) => ( +
Hello
+ ) + }; + `, + errors: [{message: 'JSX props should not use .bind()'}] + }, + { + code: ` + const foo = { + render: ({onClick}) => ( +
Hello
+ ) + }; + `, + errors: [{message: 'JSX props should not use .bind()'}] + }, // Arrow functions { From e2fd714ddc5d714e4a8b8ccd71a05959ba3f48e8 Mon Sep 17 00:00:00 2001 From: gwenaellarmet Date: Wed, 14 Mar 2018 07:58:02 +0100 Subject: [PATCH 2/2] Add test on condition + fix broken unit-test --- lib/rules/jsx-no-bind.js | 4 +++- tests/lib/rules/jsx-no-bind.js | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/rules/jsx-no-bind.js b/lib/rules/jsx-no-bind.js index ccf7f36d8f..def107f5f9 100644 --- a/lib/rules/jsx-no-bind.js +++ b/lib/rules/jsx-no-bind.js @@ -84,7 +84,9 @@ module.exports = { } else if ( nodeType === 'ConditionalExpression' ) { - return getNodeViolationType(node.consequent) | getNodeViolationType(node.alternate); + return getNodeViolationType(node.test) || + getNodeViolationType(node.consequent) || + getNodeViolationType(node.alternate); } else if ( !configuration.allowArrowFunctions && nodeType === 'ArrowFunctionExpression' diff --git a/tests/lib/rules/jsx-no-bind.js b/tests/lib/rules/jsx-no-bind.js index 13fe3fe970..05f8b264d7 100644 --- a/tests/lib/rules/jsx-no-bind.js +++ b/tests/lib/rules/jsx-no-bind.js @@ -401,7 +401,7 @@ ruleTester.run('jsx-no-bind', rule, { code: ` const foo = { render: ({onClick}) => ( -
Hello
+
Hello
) }; `, @@ -411,7 +411,7 @@ ruleTester.run('jsx-no-bind', rule, { code: ` const foo = { render: ({onClick}) => ( -
Hello
+
Hello
) }; `, @@ -421,12 +421,23 @@ ruleTester.run('jsx-no-bind', rule, { code: ` const foo = { render: ({onClick}) => ( -
Hello
+
Hello
) }; `, errors: [{message: 'JSX props should not use .bind()'}] }, + { + code: ` + const foo = { + render: ({onClick}) => ( +
Hello
+ ) + }; + `, + errors: [{message: 'JSX props should not use .bind()'}] + }, + // Arrow functions {