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

Fix: fix inconsistently works option in no-extra-parens (fixes #12717) #12843

Merged
merged 1 commit into from Feb 14, 2020
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
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 }] },
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved

// ["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 }],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was definitely intentional (#8439 comment). Unfortunately, other options don't work like this, so it's better to align.

options: ["all", { enforceForArrowConditionals: true }],
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved
errors: [
{
messageId: "unexpected"
Expand Down