From 12f256f22534c4a4e1ca0ba54c37c6db81441461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Fri, 5 Apr 2019 04:33:54 +0800 Subject: [PATCH] Breaking: no-confusing-arrow enable allowParens: true (fixes #11503) (#11520) --- docs/rules/no-confusing-arrow.md | 2 +- lib/rules/no-confusing-arrow.js | 8 ++++--- tests/lib/rules/no-confusing-arrow.js | 30 +++++++++++++++++---------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/docs/rules/no-confusing-arrow.md b/docs/rules/no-confusing-arrow.md index dca249f798b..6947af0fb86 100644 --- a/docs/rules/no-confusing-arrow.md +++ b/docs/rules/no-confusing-arrow.md @@ -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 diff --git a/lib/rules/no-confusing-arrow.js b/lib/rules/no-confusing-arrow.js index 79df9a51387..09d6e27d216 100644 --- a/lib/rules/no-confusing-arrow.js +++ b/lib/rules/no-confusing-arrow.js @@ -41,7 +41,7 @@ module.exports = { schema: [{ type: "object", properties: { - allowParens: { type: "boolean", default: false } + allowParens: { type: "boolean", default: true } }, additionalProperties: false }], @@ -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. @@ -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)})`); } }); } diff --git a/tests/lib/rules/no-confusing-arrow.js b/tests/lib/rules/no-confusing-arrow.js index 7df2538a2d0..8c7efe6e287 100644 --- a/tests/lib/rules/no-confusing-arrow.js +++ b/tests/lib/rules/no-confusing-arrow.js @@ -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" }] } ]