From c1e49514440764be65af955092bc8b314bc111fe Mon Sep 17 00:00:00 2001 From: Gautam Arora Date: Wed, 2 Feb 2022 18:34:42 +0530 Subject: [PATCH 1/7] feat: Add option to no-confusing-arrow rule --- docs/rules/no-confusing-arrow.md | 17 +++++++++++++++-- lib/rules/no-confusing-arrow.js | 6 ++++-- tests/lib/rules/no-confusing-arrow.js | 23 ++++++++++++++++++++++- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/docs/rules/no-confusing-arrow.md b/docs/rules/no-confusing-arrow.md index 3671163eadf..0e17b09128c 100644 --- a/docs/rules/no-confusing-arrow.md +++ b/docs/rules/no-confusing-arrow.md @@ -41,12 +41,12 @@ var x = (a) => { return 1 ? 2 : 3; }; ## Options -This rule accepts a single options argument with the following defaults: +This rule accepts two options argument with the following defaults: ```json { "rules": { - "no-confusing-arrow": ["error", {"allowParens": true}] + "no-confusing-arrow": ["error", {"allowParens": true,"onlyOneSimpleParam": false}] } } ``` @@ -65,6 +65,19 @@ var x = a => (1 ? 2 : 3); var x = (a) => (1 ? 2 : 3); ``` +`onlyOneSimpleParam` is a boolean setting that can be `true` or `false`(false): + +1. `true` warns if arrow functions with a single argument that is either an identifier or a primitive literal is used. +2. `false` relaxes the rule. + +Examples of **incorrect** code for this rule with the `{"onlyOneSimpleParam": true}` option: + +```js +/*eslint no-confusing-arrow: ["error", {"onlyOneSimpleParam": true}]*/ +/*eslint-env es6*/ +var x = (a) => (1 ? 2 : 3); +``` + ## Related Rules * [no-constant-condition](no-constant-condition.md) diff --git a/lib/rules/no-confusing-arrow.js b/lib/rules/no-confusing-arrow.js index 7b736c19728..69c3bd2a8bf 100644 --- a/lib/rules/no-confusing-arrow.js +++ b/lib/rules/no-confusing-arrow.js @@ -41,7 +41,8 @@ module.exports = { schema: [{ type: "object", properties: { - allowParens: { type: "boolean", default: true } + allowParens: { type: "boolean", default: true }, + onlyOneSimpleParam: { type: "boolean", default: false } }, additionalProperties: false }], @@ -54,6 +55,7 @@ module.exports = { create(context) { const config = context.options[0] || {}; const allowParens = config.allowParens || (config.allowParens === void 0); + const onlyOneSimpleParam = context.options[1]; const sourceCode = context.getSourceCode(); @@ -65,7 +67,7 @@ module.exports = { function checkArrowFunc(node) { const body = node.body; - if (isConditional(body) && !(allowParens && astUtils.isParenthesised(sourceCode, body))) { + if (isConditional(body) && (!(allowParens && astUtils.isParenthesised(sourceCode, body)) || (onlyOneSimpleParam && node.params.length === 1 && node.params[0].type !== "ArrayPattern" && node.params[0].type !== "ObjectPattern"))) { context.report({ node, messageId: "confusing", diff --git a/tests/lib/rules/no-confusing-arrow.js b/tests/lib/rules/no-confusing-arrow.js index 58724d9ace2..6ccd9517a23 100644 --- a/tests/lib/rules/no-confusing-arrow.js +++ b/tests/lib/rules/no-confusing-arrow.js @@ -30,7 +30,10 @@ ruleTester.run("no-confusing-arrow", rule, { { code: "var x = (a) => { return 1 ? 2 : 3; }", options: [{ allowParens: false }] }, "var x = a => (1 ? 2 : 3)", - { code: "var x = a => (1 ? 2 : 3)", options: [{ allowParens: true }] } + { code: "var x = a => (1 ? 2 : 3)", options: [{ allowParens: true }] }, + + "var x = (a,b) => (1 ? 2 : 3)", + { code: "var x = (a,b) => (1 ? 2 : 3)", options: [{ onlyOneSimpleParam: false }] } ], invalid: [ { @@ -71,6 +74,24 @@ ruleTester.run("no-confusing-arrow", rule, { code: "var x = (a) => 1 ? 2 : 3", output: "var x = (a) => (1 ? 2 : 3)", errors: [{ messageId: "confusing" }] + }, + { + code: "var x = (a) => 1 ? 2 : 3", + output: null, + options: [{ onlyOneSimpleParam: true, allowParens: false }], + errors: [{ messageId: "confusing" }] + }, + { + code: "var x = (a) => 1 ? 2 : 3", + output: "var x = (a) => (1 ? 2 : 3)", + options: [{ onlyOneSimpleParam: true, allowParens: true }], + errors: [{ messageId: "confusing" }] + }, + { + code: "var x = ({}) => 1 ? 2 : 3", + output: null, + options: [{ onlyOneSimpleParam: true, allowParens: false }], + errors: [{ messageId: "confusing" }] } ] }); From 5d8458cfe0e89640296f7b0243382d8cee21ee04 Mon Sep 17 00:00:00 2001 From: Gautam Arora Date: Wed, 2 Feb 2022 20:33:21 +0530 Subject: [PATCH 2/7] Follow up commit --- lib/rules/no-confusing-arrow.js | 6 ++++-- tests/lib/rules/no-confusing-arrow.js | 14 +++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/rules/no-confusing-arrow.js b/lib/rules/no-confusing-arrow.js index 69c3bd2a8bf..08ba1ca94f9 100644 --- a/lib/rules/no-confusing-arrow.js +++ b/lib/rules/no-confusing-arrow.js @@ -55,7 +55,7 @@ module.exports = { create(context) { const config = context.options[0] || {}; const allowParens = config.allowParens || (config.allowParens === void 0); - const onlyOneSimpleParam = context.options[1]; + const onlyOneSimpleParam = !!config.onlyOneSimpleParam; const sourceCode = context.getSourceCode(); @@ -67,7 +67,9 @@ module.exports = { function checkArrowFunc(node) { const body = node.body; - if (isConditional(body) && (!(allowParens && astUtils.isParenthesised(sourceCode, body)) || (onlyOneSimpleParam && node.params.length === 1 && node.params[0].type !== "ArrayPattern" && node.params[0].type !== "ObjectPattern"))) { + if (isConditional(body) && + !(allowParens && astUtils.isParenthesised(sourceCode, body)) && + !(onlyOneSimpleParam && !(node.params.length === 1 && node.params[0].type === "Identifier"))) { context.report({ node, messageId: "confusing", diff --git a/tests/lib/rules/no-confusing-arrow.js b/tests/lib/rules/no-confusing-arrow.js index 6ccd9517a23..ae552099bc5 100644 --- a/tests/lib/rules/no-confusing-arrow.js +++ b/tests/lib/rules/no-confusing-arrow.js @@ -33,7 +33,13 @@ ruleTester.run("no-confusing-arrow", rule, { { code: "var x = a => (1 ? 2 : 3)", options: [{ allowParens: true }] }, "var x = (a,b) => (1 ? 2 : 3)", - { code: "var x = (a,b) => (1 ? 2 : 3)", options: [{ onlyOneSimpleParam: false }] } + { code: "var x = (a,b) => (1 ? 2 : 3)", options: [{ onlyOneSimpleParam: false }] }, + { code: "() => 1 ? 2 : 3", options: [{ onlyOneSimpleParam: true }] }, + { code: "(a, b) => 1 ? 2 : 3", options: [{ onlyOneSimpleParam: true }] }, + { code: "(a = b) => 1 ? 2 : 3", options: [{ onlyOneSimpleParam: true }] }, + { code: "({ a }) => 1 ? 2 : 3", options: [{ onlyOneSimpleParam: true }] }, + { code: "([a]) => 1 ? 2 : 3", options: [{ onlyOneSimpleParam: true }] }, + { code: "(...a) => 1 ? 2 : 3", options: [{ onlyOneSimpleParam: true }] } ], invalid: [ { @@ -86,12 +92,6 @@ ruleTester.run("no-confusing-arrow", rule, { output: "var x = (a) => (1 ? 2 : 3)", options: [{ onlyOneSimpleParam: true, allowParens: true }], errors: [{ messageId: "confusing" }] - }, - { - code: "var x = ({}) => 1 ? 2 : 3", - output: null, - options: [{ onlyOneSimpleParam: true, allowParens: false }], - errors: [{ messageId: "confusing" }] } ] }); From 2d15c914912ad41fbbc158cbe9331c606b71c77f Mon Sep 17 00:00:00 2001 From: Gautam Arora Date: Thu, 10 Feb 2022 12:34:21 +0530 Subject: [PATCH 3/7] Another follow up commit --- docs/rules/no-confusing-arrow.md | 41 +++++++++++++++---------- tests/lib/rules/no-confusing-arrow.js | 43 ++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 23 deletions(-) diff --git a/docs/rules/no-confusing-arrow.md b/docs/rules/no-confusing-arrow.md index 0e17b09128c..799b7ec7d4a 100644 --- a/docs/rules/no-confusing-arrow.md +++ b/docs/rules/no-confusing-arrow.md @@ -8,9 +8,11 @@ Here's an example where the usage of `=>` could be confusing: ```js // The intent is not clear -var x = a => 1 ? 2 : 3; +var x = (a) => (1 ? 2 : 3); // Did the author mean this -var x = function (a) { return 1 ? 2 : 3 }; +var x = function (a) { + return 1 ? 2 : 3; +}; // Or this var x = a <= 1 ? 2 : 3; ``` @@ -23,8 +25,8 @@ Examples of **incorrect** code for this rule: /*eslint no-confusing-arrow: "error"*/ /*eslint-env es6*/ -var x = a => 1 ? 2 : 3; -var x = (a) => 1 ? 2 : 3; +var x = (a) => (1 ? 2 : 3); +var x = (a) => (1 ? 2 : 3); ``` Examples of **correct** code for this rule: @@ -33,10 +35,14 @@ Examples of **correct** code for this rule: /*eslint no-confusing-arrow: "error"*/ /*eslint-env es6*/ -var x = a => (1 ? 2 : 3); var x = (a) => (1 ? 2 : 3); -var x = a => { return 1 ? 2 : 3; }; -var x = (a) => { return 1 ? 2 : 3; }; +var x = (a) => (1 ? 2 : 3); +var x = (a) => { + return 1 ? 2 : 3; +}; +var x = (a) => { + return 1 ? 2 : 3; +}; ``` ## Options @@ -46,7 +52,10 @@ This rule accepts two options argument with the following defaults: ```json { "rules": { - "no-confusing-arrow": ["error", {"allowParens": true,"onlyOneSimpleParam": false}] + "no-confusing-arrow": [ + "error", + { "allowParens": true, "onlyOneSimpleParam": false } + ] } } ``` @@ -61,24 +70,24 @@ Examples of **incorrect** code for this rule with the `{"allowParens": false}` o ```js /*eslint no-confusing-arrow: ["error", {"allowParens": false}]*/ /*eslint-env es6*/ -var x = a => (1 ? 2 : 3); +var x = (a) => (1 ? 2 : 3); var x = (a) => (1 ? 2 : 3); ``` -`onlyOneSimpleParam` is a boolean setting that can be `true` or `false`(false): +`onlyOneSimpleParam` is a boolean setting that can be `true` or `false`(default): -1. `true` warns if arrow functions with a single argument that is either an identifier or a primitive literal is used. -2. `false` relaxes the rule. +1. `true` relaxes the rule and doesn't report errors if 0 or more than 1 arguments is used or the argument is not an identifier. -Examples of **incorrect** code for this rule with the `{"onlyOneSimpleParam": true}` option: +Examples of **correct** code for this rule with the `{"onlyOneSimpleParam": true}` option: ```js /*eslint no-confusing-arrow: ["error", {"onlyOneSimpleParam": true}]*/ /*eslint-env es6*/ -var x = (a) => (1 ? 2 : 3); +() => (1 ? 2 : 3); +(a, b) => (1 ? 2 : 3); ``` ## Related Rules -* [no-constant-condition](no-constant-condition.md) -* [arrow-parens](arrow-parens.md) +* [no-constant-condition](no-constant-condition.md) +* [arrow-parens](arrow-parens.md) diff --git a/tests/lib/rules/no-confusing-arrow.js b/tests/lib/rules/no-confusing-arrow.js index ae552099bc5..372f2057bbf 100644 --- a/tests/lib/rules/no-confusing-arrow.js +++ b/tests/lib/rules/no-confusing-arrow.js @@ -33,7 +33,6 @@ ruleTester.run("no-confusing-arrow", rule, { { code: "var x = a => (1 ? 2 : 3)", options: [{ allowParens: true }] }, "var x = (a,b) => (1 ? 2 : 3)", - { code: "var x = (a,b) => (1 ? 2 : 3)", options: [{ onlyOneSimpleParam: false }] }, { code: "() => 1 ? 2 : 3", options: [{ onlyOneSimpleParam: true }] }, { code: "(a, b) => 1 ? 2 : 3", options: [{ onlyOneSimpleParam: true }] }, { code: "(a = b) => 1 ? 2 : 3", options: [{ onlyOneSimpleParam: true }] }, @@ -82,15 +81,45 @@ ruleTester.run("no-confusing-arrow", rule, { errors: [{ messageId: "confusing" }] }, { - code: "var x = (a) => 1 ? 2 : 3", - output: null, - options: [{ onlyOneSimpleParam: true, allowParens: false }], + code: "var x = () => 1 ? 2 : 3", + output: "var x = () => (1 ? 2 : 3)", errors: [{ messageId: "confusing" }] }, { - code: "var x = (a) => 1 ? 2 : 3", - output: "var x = (a) => (1 ? 2 : 3)", - options: [{ onlyOneSimpleParam: true, allowParens: true }], + code: "var x = () => 1 ? 2 : 3", + output: "var x = () => (1 ? 2 : 3)", + options: [{}], + errors: [{ messageId: "confusing" }] + }, + { + code: "var x = () => 1 ? 2 : 3", + output: "var x = () => (1 ? 2 : 3)", + options: [{ onlyOneSimpleParam: false }], + errors: [{ messageId: "confusing" }] + }, + { + code: "var x = (a, b) => 1 ? 2 : 3", + output: "var x = (a, b) => (1 ? 2 : 3)", + errors: [{ messageId: "confusing" }] + }, + { + code: "var x = (a = b) => 1 ? 2 : 3", + output: "var x = (a = b) => (1 ? 2 : 3)", + errors: [{ messageId: "confusing" }] + }, + { + code: "var x = ({ a }) => 1 ? 2 : 3", + output: "var x = ({ a }) => (1 ? 2 : 3)", + errors: [{ messageId: "confusing" }] + }, + { + code: "var x = ([a]) => 1 ? 2 : 3", + output: "var x = ([a]) => (1 ? 2 : 3)", + errors: [{ messageId: "confusing" }] + }, + { + code: "var x = (...a) => 1 ? 2 : 3", + output: "var x = (...a) => (1 ? 2 : 3)", errors: [{ messageId: "confusing" }] } ] From f9c43dc677650840f69884d0e341317f86a98d45 Mon Sep 17 00:00:00 2001 From: Gautam Arora Date: Sun, 13 Feb 2022 23:21:38 +0530 Subject: [PATCH 4/7] Another follow up commit --- docs/rules/no-confusing-arrow.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/rules/no-confusing-arrow.md b/docs/rules/no-confusing-arrow.md index 799b7ec7d4a..35d629fa543 100644 --- a/docs/rules/no-confusing-arrow.md +++ b/docs/rules/no-confusing-arrow.md @@ -8,7 +8,7 @@ Here's an example where the usage of `=>` could be confusing: ```js // The intent is not clear -var x = (a) => (1 ? 2 : 3); +var x = a => 1 ? 2 : 3; // Did the author mean this var x = function (a) { return 1 ? 2 : 3; @@ -25,8 +25,8 @@ Examples of **incorrect** code for this rule: /*eslint no-confusing-arrow: "error"*/ /*eslint-env es6*/ -var x = (a) => (1 ? 2 : 3); -var x = (a) => (1 ? 2 : 3); +var x = a => 1 ? 2 : 3; +var x = (a) => 1 ? 2 : 3; ``` Examples of **correct** code for this rule: @@ -34,15 +34,11 @@ Examples of **correct** code for this rule: ```js /*eslint no-confusing-arrow: "error"*/ /*eslint-env es6*/ - -var x = (a) => (1 ? 2 : 3); +var x = a => (1 ? 2 : 3); var x = (a) => (1 ? 2 : 3); var x = (a) => { return 1 ? 2 : 3; }; -var x = (a) => { - return 1 ? 2 : 3; -}; ``` ## Options @@ -71,23 +67,27 @@ Examples of **incorrect** code for this rule with the `{"allowParens": false}` o /*eslint no-confusing-arrow: ["error", {"allowParens": false}]*/ /*eslint-env es6*/ var x = (a) => (1 ? 2 : 3); -var x = (a) => (1 ? 2 : 3); ``` `onlyOneSimpleParam` is a boolean setting that can be `true` or `false`(default): -1. `true` relaxes the rule and doesn't report errors if 0 or more than 1 arguments is used or the argument is not an identifier. +1. `true` relaxes the rule and doesn't report errors if the arrow function has 0 or more than 1 parameters, or the parameter is not an identifier. +2. `false` warns regardless of parameters. Examples of **correct** code for this rule with the `{"onlyOneSimpleParam": true}` option: ```js /*eslint no-confusing-arrow: ["error", {"onlyOneSimpleParam": true}]*/ /*eslint-env es6*/ -() => (1 ? 2 : 3); -(a, b) => (1 ? 2 : 3); +() => 1 ? 2 : 3; +(a, b) => 1 ? 2 : 3; +(a = b) => 1 ? 2 : 3; +({ a }) => 1 ? 2 : 3; +([a]) => 1 ? 2 : 3; +(...a) => 1 ? 2 : 3; ``` ## Related Rules -* [no-constant-condition](no-constant-condition.md) -* [arrow-parens](arrow-parens.md) +* [no-constant-condition](no-constant-condition.md) +* [arrow-parens](arrow-parens.md) From d7867be043fb65d8050bd689384c7a7b355be2f6 Mon Sep 17 00:00:00 2001 From: Gautam Arora Date: Thu, 17 Feb 2022 00:02:15 +0530 Subject: [PATCH 5/7] Another commit --- docs/rules/no-confusing-arrow.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/rules/no-confusing-arrow.md b/docs/rules/no-confusing-arrow.md index 35d629fa543..35eb92b1ad8 100644 --- a/docs/rules/no-confusing-arrow.md +++ b/docs/rules/no-confusing-arrow.md @@ -27,6 +27,7 @@ Examples of **incorrect** code for this rule: var x = a => 1 ? 2 : 3; var x = (a) => 1 ? 2 : 3; +var x = a => { return 1 ? 2 : 3; }; ``` Examples of **correct** code for this rule: @@ -66,6 +67,7 @@ Examples of **incorrect** code for this rule with the `{"allowParens": false}` o ```js /*eslint no-confusing-arrow: ["error", {"allowParens": false}]*/ /*eslint-env es6*/ +var x = a => (1 ? 2 : 3); var x = (a) => (1 ? 2 : 3); ``` From 0bbca1a923235cfee0947a6a13a211a5ae94ad3e Mon Sep 17 00:00:00 2001 From: Gautam Arora Date: Thu, 17 Feb 2022 09:46:45 +0530 Subject: [PATCH 6/7] Another followup commit --- lib/rules/no-confusing-arrow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/no-confusing-arrow.js b/lib/rules/no-confusing-arrow.js index 08ba1ca94f9..9cdd0a85dbb 100644 --- a/lib/rules/no-confusing-arrow.js +++ b/lib/rules/no-confusing-arrow.js @@ -55,7 +55,7 @@ module.exports = { create(context) { const config = context.options[0] || {}; const allowParens = config.allowParens || (config.allowParens === void 0); - const onlyOneSimpleParam = !!config.onlyOneSimpleParam; + const onlyOneSimpleParam = config.onlyOneSimpleParam; const sourceCode = context.getSourceCode(); From 6ec85511b27cc49652f7abfb05af57fc883a807a Mon Sep 17 00:00:00 2001 From: Gautam Arora Date: Thu, 17 Feb 2022 23:59:19 +0530 Subject: [PATCH 7/7] Final Commit --- docs/rules/no-confusing-arrow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-confusing-arrow.md b/docs/rules/no-confusing-arrow.md index 35eb92b1ad8..f88af8e5d9a 100644 --- a/docs/rules/no-confusing-arrow.md +++ b/docs/rules/no-confusing-arrow.md @@ -27,7 +27,6 @@ Examples of **incorrect** code for this rule: var x = a => 1 ? 2 : 3; var x = (a) => 1 ? 2 : 3; -var x = a => { return 1 ? 2 : 3; }; ``` Examples of **correct** code for this rule: @@ -40,6 +39,7 @@ var x = (a) => (1 ? 2 : 3); var x = (a) => { return 1 ? 2 : 3; }; +var x = a => { return 1 ? 2 : 3; }; ``` ## Options