Add parenthesis for no-confusing-arrow rule (#1182)

I'm unclear whether anyone was ever confused by this but the eslint page is kind of compelling

```js
// The intent is not clear
var x = a => 1 ? 2 : 3;
// Did the author mean this
var x = function (a) { return 1 ? 2 : 3 };
// Or this
var x = a <= 1 ? 2 : 3;
```

Adding a parenthesis makes it valid with `{"allowParens": true}` rule. Note that if this option is not enabled, the code would not pass lint in the first place.
master
Christopher Chedeau 2017-04-11 09:20:39 -07:00 committed by GitHub
parent e0eb438e7b
commit 2e63684ff5
6 changed files with 18 additions and 3 deletions

View File

@ -449,6 +449,7 @@ FPp.needsParens = function(assumeExpressionContext) {
case "ExportDefaultDeclaration":
case "AwaitExpression":
case "JSXSpreadAttribute":
case "ArrowFunctionExpression":
return true;
case "NewExpression":

View File

@ -70,7 +70,7 @@ function someFunction(url) {
}
const mapChargeItems = fp.flow(
l => l < 10 ? l : 1,
l => (l < 10 ? l : 1),
l => Immutable.Range(l).toMap()
);

View File

@ -89,3 +89,14 @@ const testConsole = new TestConsole(
);
"
`;
exports[`no-confusing-arrow.js 1`] = `
"// no-confusing-arrow
var x = a => 1 ? 2 : 3;
var x = a <= 1 ? 2 : 3;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// no-confusing-arrow
var x = a => (1 ? 2 : 3);
var x = a <= 1 ? 2 : 3;
"
`;

View File

@ -0,0 +1,3 @@
// no-confusing-arrow
var x = a => 1 ? 2 : 3;
var x = a <= 1 ? 2 : 3;

View File

@ -49,7 +49,7 @@ function unannotated(x) {
}
// test deduping of inferred types
const nullToUndefined = val => val === null ? undefined : val;
const nullToUndefined = val => (val === null ? undefined : val);
function f0(x: ?Object) {
return nullToUndefined(x);

View File

@ -73,7 +73,7 @@ true
}),
require(\\"postcss-url\\")({
url: url =>
url.startsWith(\\"/\\") || /^[a-z]+:/.test(url) ? url : \`/static/\${url}\`
(url.startsWith(\\"/\\") || /^[a-z]+:/.test(url) ? url : \`/static/\${url}\`)
})
]
});