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

Update: add fix for "no-confusing-arrow" rule #8347

Merged
merged 1 commit into from Apr 20, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 }]
}
]
});