Skip to content

Commit

Permalink
Breaking: no-confusing-arrow enable allowParens: true (fixes #11503) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add authored and not-an-aardvark committed Apr 4, 2019
1 parent 25cc63d commit 12f256f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/rules/no-confusing-arrow.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ This rule accepts a single options argument with the following defaults:
}
```

`allowParens` is a boolean setting that can be `true` or `false`:
`allowParens` is a boolean setting that can be `true`(default) or `false`:

1. `true` relaxes the rule and accepts parenthesis as a valid "confusion-preventing" syntax.
2. `false` warns even if the expression is wrapped in parenthesis
Expand Down
8 changes: 5 additions & 3 deletions lib/rules/no-confusing-arrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
schema: [{
type: "object",
properties: {
allowParens: { type: "boolean", default: false }
allowParens: { type: "boolean", default: true }
},
additionalProperties: false
}],
Expand All @@ -53,8 +53,10 @@ module.exports = {

create(context) {
const config = context.options[0] || {};
const allowParens = config.allowParens || (config.allowParens === void 0);
const sourceCode = context.getSourceCode();


/**
* Reports if an arrow function contains an ambiguous conditional.
* @param {ASTNode} node - A node to check and report.
Expand All @@ -63,14 +65,14 @@ module.exports = {
function checkArrowFunc(node) {
const body = node.body;

if (isConditional(body) && !(config.allowParens && astUtils.isParenthesised(sourceCode, body))) {
if (isConditional(body) && !(allowParens && astUtils.isParenthesised(sourceCode, body))) {
context.report({
node,
messageId: "confusing",
fix(fixer) {

// if `allowParens` is not set to true dont bother wrapping in parens
return config.allowParens && fixer.replaceText(node.body, `(${sourceCode.getText(node.body)})`);
return allowParens && fixer.replaceText(node.body, `(${sourceCode.getText(node.body)})`);
}
});
}
Expand Down
30 changes: 19 additions & 11 deletions tests/lib/rules/no-confusing-arrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,55 @@ const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
ruleTester.run("no-confusing-arrow", rule, {
valid: [
"a => { return 1 ? 2 : 3; }",
{ code: "a => { return 1 ? 2 : 3; }", options: [{ allowParens: false }] },

"var x = a => { return 1 ? 2 : 3; }",
{ code: "var x = a => { return 1 ? 2 : 3; }", options: [{ allowParens: false }] },

"var x = (a) => { return 1 ? 2 : 3; }",
{ 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 }] }
],
invalid: [
{
code: "a => 1 ? 2 : 3",
output: null,
output: "a => (1 ? 2 : 3)",
errors: [{ messageId: "confusing" }]
},
{
code: "var x = a => 1 ? 2 : 3",
output: null,
code: "a => 1 ? 2 : 3",
output: "a => (1 ? 2 : 3)",
options: [{ allowParens: true }],
errors: [{ messageId: "confusing" }]
},
{
code: "var x = (a) => 1 ? 2 : 3",
code: "a => 1 ? 2 : 3",
output: null,
options: [{ allowParens: false }],
errors: [{ messageId: "confusing" }]
},
{
code: "var x = a => (1 ? 2 : 3)",
output: null,
code: "var x = a => 1 ? 2 : 3",
output: "var x = a => (1 ? 2 : 3)",
errors: [{ messageId: "confusing" }]
},
{
code: "a => 1 ? 2 : 3",
output: "a => (1 ? 2 : 3)",
code: "var x = a => 1 ? 2 : 3",
output: "var x = a => (1 ? 2 : 3)",
options: [{ allowParens: true }],
errors: [{ messageId: "confusing" }]
},
{
code: "var x = a => 1 ? 2 : 3",
output: "var x = a => (1 ? 2 : 3)",
options: [{ allowParens: true }],
output: null,
options: [{ allowParens: false }],
errors: [{ messageId: "confusing" }]
},
{
code: "var x = (a) => 1 ? 2 : 3",
output: "var x = (a) => (1 ? 2 : 3)",
options: [{ allowParens: true }],
errors: [{ messageId: "confusing" }]
}
]
Expand Down

0 comments on commit 12f256f

Please sign in to comment.