Skip to content
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

feat: Add onlyOneSimpleParam option to no-confusing-arrow rule #15566

Merged
merged 7 commits into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 34 additions & 12 deletions docs/rules/no-confusing-arrow.md
Expand Up @@ -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);
Gautam-Arora24 marked this conversation as resolved.
Show resolved Hide resolved
// 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;
```
Expand All @@ -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);
Gautam-Arora24 marked this conversation as resolved.
Show resolved Hide resolved
```

Examples of **correct** code for this rule:
Expand All @@ -33,20 +35,27 @@ 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; };
Gautam-Arora24 marked this conversation as resolved.
Show resolved Hide resolved
var x = (a) => { return 1 ? 2 : 3; };
var x = (a) => (1 ? 2 : 3);
Gautam-Arora24 marked this conversation as resolved.
Show resolved Hide resolved
var x = (a) => {
return 1 ? 2 : 3;
};
var x = (a) => {
return 1 ? 2 : 3;
};
Gautam-Arora24 marked this conversation as resolved.
Show resolved Hide resolved
```

## 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 }
]
}
}
```
Expand All @@ -61,11 +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);
Gautam-Arora24 marked this conversation as resolved.
Show resolved Hide resolved
var x = (a) => (1 ? 2 : 3);
var x = (a) => (1 ? 2 : 3);
Gautam-Arora24 marked this conversation as resolved.
Show resolved Hide resolved
```

`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.
Gautam-Arora24 marked this conversation as resolved.
Show resolved Hide resolved

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);
Gautam-Arora24 marked this conversation as resolved.
Show resolved Hide resolved
```

## 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)
Gautam-Arora24 marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 6 additions & 2 deletions lib/rules/no-confusing-arrow.js
Expand Up @@ -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
}],
Expand All @@ -54,6 +55,7 @@ module.exports = {
create(context) {
const config = context.options[0] || {};
const allowParens = config.allowParens || (config.allowParens === void 0);
const onlyOneSimpleParam = !!config.onlyOneSimpleParam;
Gautam-Arora24 marked this conversation as resolved.
Show resolved Hide resolved
const sourceCode = context.getSourceCode();


Expand All @@ -65,7 +67,9 @@ 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 === "Identifier"))) {
context.report({
node,
messageId: "confusing",
Expand Down
52 changes: 51 additions & 1 deletion tests/lib/rules/no-confusing-arrow.js
Expand Up @@ -30,7 +30,15 @@ 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: "() => 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: [
{
Expand Down Expand Up @@ -71,6 +79,48 @@ 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 = () => 1 ? 2 : 3",
output: "var x = () => (1 ? 2 : 3)",
errors: [{ messageId: "confusing" }]
},
{
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" }]
Gautam-Arora24 marked this conversation as resolved.
Show resolved Hide resolved
}
]
});