Skip to content

Commit

Permalink
Update: add fix for no-confusing-arrow
Browse files Browse the repository at this point in the history
  • Loading branch information
tikotzky committed Mar 29, 2017
1 parent 0541eaf commit d221dce
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/rules/no-confusing-arrow.js
Expand Up @@ -33,6 +33,8 @@ module.exports = {
recommended: false
},

fixable: "code",

schema: [{
type: "object",
properties: {
Expand All @@ -55,7 +57,15 @@ module.exports = {
const body = node.body;

if (isConditional(body) && !(config.allowParens && astUtils.isParenthesised(sourceCode, body))) {
context.report({ node, message: "Arrow function used ambiguously with a conditional expression." });
context.report({
node,
message: "Arrow function used ambiguously with a conditional expression.",
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)})`);
}
});
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/no-confusing-arrow.js
Expand Up @@ -28,19 +28,41 @@ ruleTester.run("no-confusing-arrow", rule, {
invalid: [
{
code: "a => 1 ? 2 : 3",
output: null,
errors: [{ message: "Arrow function used ambiguously with a conditional expression." }]
},
{
code: "var x = a => 1 ? 2 : 3",
output: null,
errors: [{ message: "Arrow function used ambiguously with a conditional expression." }]
},
{
code: "var x = (a) => 1 ? 2 : 3",
output: null,
errors: [{ message: "Arrow function used ambiguously with a conditional expression." }]
},
{
code: "var x = a => (1 ? 2 : 3)",
output: null,
errors: [{ message: "Arrow function used ambiguously with a conditional expression." }]
},
{
code: "a => 1 ? 2 : 3",
output: "a => (1 ? 2 : 3)",
errors: [{ message: "Arrow function used ambiguously with a conditional expression." }],
options: [{ allowParens: true }]
},
{
code: "var x = a => 1 ? 2 : 3",
output: "var x = a => (1 ? 2 : 3)",
errors: [{ message: "Arrow function used ambiguously with a conditional expression." }],
options: [{ allowParens: true }]
},
{
code: "var x = (a) => 1 ? 2 : 3",
output: "var x = (a) => (1 ? 2 : 3)",
errors: [{ message: "Arrow function used ambiguously with a conditional expression." }],
options: [{ allowParens: true }]
}
]
});

0 comments on commit d221dce

Please sign in to comment.