Skip to content

Commit

Permalink
Revert "feat: Add onlyOneSimpleParam option to no-confusing-arrow r…
Browse files Browse the repository at this point in the history
…ule (eslint#15566)"

This reverts commit 94f9b89.
  • Loading branch information
srijan-deepsource committed May 30, 2022
1 parent 39daf42 commit 68603c2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 86 deletions.
34 changes: 5 additions & 29 deletions docs/rules/no-confusing-arrow.md
Expand Up @@ -10,9 +10,7 @@ Here's an example where the usage of `=>` could be confusing:
// The intent is not clear
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;
```
Expand All @@ -34,25 +32,21 @@ 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) => {
return 1 ? 2 : 3;
};
var x = a => { return 1 ? 2 : 3; };
var x = (a) => { return 1 ? 2 : 3; };
```

## Options

This rule accepts two options argument with the following defaults:
This rule accepts a single options argument with the following defaults:

```json
{
"rules": {
"no-confusing-arrow": [
"error",
{ "allowParens": true, "onlyOneSimpleParam": false }
]
"no-confusing-arrow": ["error", {"allowParens": true}]
}
}
```
Expand All @@ -71,24 +65,6 @@ 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 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;
(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)
Expand Down
8 changes: 2 additions & 6 deletions lib/rules/no-confusing-arrow.js
Expand Up @@ -41,8 +41,7 @@ module.exports = {
schema: [{
type: "object",
properties: {
allowParens: { type: "boolean", default: true },
onlyOneSimpleParam: { type: "boolean", default: false }
allowParens: { type: "boolean", default: true }
},
additionalProperties: false
}],
Expand All @@ -55,7 +54,6 @@ module.exports = {
create(context) {
const config = context.options[0] || {};
const allowParens = config.allowParens || (config.allowParens === void 0);
const onlyOneSimpleParam = config.onlyOneSimpleParam;
const sourceCode = context.getSourceCode();


Expand All @@ -67,9 +65,7 @@ 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 === "Identifier"))) {
if (isConditional(body) && !(allowParens && astUtils.isParenthesised(sourceCode, body))) {
context.report({
node,
messageId: "confusing",
Expand Down
52 changes: 1 addition & 51 deletions tests/lib/rules/no-confusing-arrow.js
Expand Up @@ -30,15 +30,7 @@ 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 }] },

"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 }] }
{ code: "var x = a => (1 ? 2 : 3)", options: [{ allowParens: true }] }
],
invalid: [
{
Expand Down Expand Up @@ -79,48 +71,6 @@ 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" }]
}
]
});

0 comments on commit 68603c2

Please sign in to comment.