Skip to content

Commit

Permalink
Fix: fix inconsistently works option in no-extra-parens (fixes #12717) (
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan committed Feb 14, 2020
1 parent b5adcaa commit 1f1424c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
3 changes: 1 addition & 2 deletions lib/rules/no-extra-parens.js
Expand Up @@ -702,8 +702,7 @@ module.exports = {
}

if (node.body.type === "ConditionalExpression" &&
IGNORE_ARROW_CONDITIONALS &&
!isParenthesisedTwice(node.body)
IGNORE_ARROW_CONDITIONALS
) {
return;
}
Expand Down
35 changes: 32 additions & 3 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -244,19 +244,28 @@ ruleTester.run("no-extra-parens", rule, {

// "functions" enables reports for function nodes only
{ code: "(0)", options: ["functions"] },
{ code: "((0))", options: ["functions"] },
{ code: "a + (b * c)", options: ["functions"] },
{ code: "a + ((b * c))", options: ["functions"] },
{ code: "(a)(b)", options: ["functions"] },
{ code: "((a))(b)", options: ["functions"] },
{ code: "a, (b = c)", options: ["functions"] },
{ code: "a, ((b = c))", options: ["functions"] },
{ code: "for(a in (0));", options: ["functions"] },
{ code: "for(a in ((0)));", options: ["functions"] },
{ code: "var a = (b = c)", options: ["functions"] },
{ code: "var a = ((b = c))", options: ["functions"] },
{ code: "_ => (a = 0)", options: ["functions"] },
{ code: "_ => ((a = 0))", options: ["functions"] },

// ["all", { conditionalAssign: false }] enables extra parens around conditional assignments
{ code: "while ((foo = bar())) {}", options: ["all", { conditionalAssign: false }] },
{ code: "if ((foo = bar())) {}", options: ["all", { conditionalAssign: false }] },
{ code: "do; while ((foo = bar()))", options: ["all", { conditionalAssign: false }] },
{ code: "for (;(a = b););", options: ["all", { conditionalAssign: false }] },
{ code: "var a = ((b = c)) ? foo : bar;", options: ["all", { conditionalAssign: false }] },
{ code: "while (((foo = bar()))) {}", options: ["all", { conditionalAssign: false }] },
{ code: "var a = (((b = c))) ? foo : bar;", options: ["all", { conditionalAssign: false }] },

// ["all", { nestedBinaryExpressions: false }] enables extra parens around conditional assignments
{ code: "a + (b * c)", options: ["all", { nestedBinaryExpressions: false }] },
Expand Down Expand Up @@ -369,6 +378,7 @@ ruleTester.run("no-extra-parens", rule, {

// ["all", { ignoreJSX: "all" }]
{ code: "const Component = (<div />)", options: ["all", { ignoreJSX: "all" }] },
{ code: "const Component = ((<div />))", options: ["all", { ignoreJSX: "all" }] },
{
code: [
"const Component = (<>",
Expand All @@ -377,6 +387,14 @@ ruleTester.run("no-extra-parens", rule, {
].join("\n"),
options: ["all", { ignoreJSX: "all" }]
},
{
code: [
"const Component = ((<>",
" <p />",
"</>));"
].join("\n"),
options: ["all", { ignoreJSX: "all" }]
},
{
code: [
"const Component = (<div>",
Expand All @@ -403,6 +421,7 @@ ruleTester.run("no-extra-parens", rule, {

// ["all", { ignoreJSX: "single-line" }]
{ code: "const Component = (<div />);", options: ["all", { ignoreJSX: "single-line" }] },
{ code: "const Component = ((<div />));", options: ["all", { ignoreJSX: "single-line" }] },
{
code: [
"const Component = (",
Expand Down Expand Up @@ -430,6 +449,16 @@ ruleTester.run("no-extra-parens", rule, {
].join("\n"),
options: ["all", { ignoreJSX: "multi-line" }]
},
{
code: [
"const Component = ((",
"<div>",
" <p />",
"</div>",
"));"
].join("\n"),
options: ["all", { ignoreJSX: "multi-line" }]
},
{
code: [
"const Component = (<div>",
Expand Down Expand Up @@ -459,13 +488,15 @@ ruleTester.run("no-extra-parens", rule, {
// ["all", { enforceForArrowConditionals: false }]
{ code: "var a = b => 1 ? 2 : 3", options: ["all", { enforceForArrowConditionals: false }] },
{ code: "var a = (b) => (1 ? 2 : 3)", options: ["all", { enforceForArrowConditionals: false }] },
{ code: "var a = (b) => ((1 ? 2 : 3))", options: ["all", { enforceForArrowConditionals: false }] },

// ["all", { enforceForSequenceExpressions: false }]
{ code: "(a, b)", options: ["all", { enforceForSequenceExpressions: false }] },
{ code: "((a, b))", options: ["all", { enforceForSequenceExpressions: false }] },
{ code: "(foo(), bar());", options: ["all", { enforceForSequenceExpressions: false }] },
{ code: "((foo(), bar()));", options: ["all", { enforceForSequenceExpressions: false }] },
{ code: "if((a, b)){}", options: ["all", { enforceForSequenceExpressions: false }] },
{ code: "if(((a, b))){}", options: ["all", { enforceForSequenceExpressions: false }] },
{ code: "while ((val = foo(), val < 10));", options: ["all", { enforceForSequenceExpressions: false }] },

// ["all", { enforceForNewInMemberExpressions: false }]
Expand Down Expand Up @@ -1122,12 +1153,10 @@ ruleTester.run("no-extra-parens", rule, {
}
]
},

// ["all", { enforceForArrowConditionals: false }]
{
code: "var a = (b) => ((1 ? 2 : 3))",
output: "var a = (b) => (1 ? 2 : 3)",
options: ["all", { enforceForArrowConditionals: false }],
options: ["all", { enforceForArrowConditionals: true }],
errors: [
{
messageId: "unexpected"
Expand Down

0 comments on commit 1f1424c

Please sign in to comment.