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

Breaking: no-confusing-arrow enable allowParens: true (fixes #11503) #11520

Merged
merged 3 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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