Skip to content

Commit

Permalink
Update: Fix no-extra-parens false negative (fixes #7229) (#7231)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark authored and kaicataldo committed Sep 27, 2016
1 parent 2909c19 commit e3f95de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/rules/no-extra-parens.js
Expand Up @@ -367,15 +367,15 @@ module.exports = {
* @private
*/
function dryBinaryLogical(node) {
if (!NESTED_BINARY) {
const prec = precedence(node);
const prec = precedence(node);
const shouldSkipLeft = NESTED_BINARY && (node.left.type === "BinaryExpression" || node.left.type === "LogicalExpression");
const shouldSkipRight = NESTED_BINARY && (node.right.type === "BinaryExpression" || node.right.type === "LogicalExpression");

if (hasExcessParens(node.left) && precedence(node.left) >= prec) {
report(node.left);
}
if (hasExcessParens(node.right) && precedence(node.right) > prec) {
report(node.right);
}
if (!shouldSkipLeft && hasExcessParens(node.left) && precedence(node.left) >= prec) {
report(node.left);
}
if (!shouldSkipRight && hasExcessParens(node.right) && precedence(node.right) > prec) {
report(node.right);
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -284,6 +284,14 @@ ruleTester.run("no-extra-parens", rule, {
{ code: "async function a() { await (a + await b) }", parserOptions: { ecmaVersion: 8 } },
{ code: "async function a() { (await a)() }", parserOptions: { ecmaVersion: 8 } },
{ code: "async function a() { new (await a) }", parserOptions: { ecmaVersion: 8 } },
{ code: "(foo instanceof bar) instanceof baz", options: ["all", { nestedBinaryExpressions: false }] },
{ code: "(foo in bar) in baz", options: ["all", { nestedBinaryExpressions: false }] },
{ code: "(foo + bar) + baz", options: ["all", { nestedBinaryExpressions: false }] },
{ code: "(foo && bar) && baz", options: ["all", { nestedBinaryExpressions: false }] },
{ code: "foo instanceof (bar instanceof baz)", options: ["all", { nestedBinaryExpressions: false }] },
{ code: "foo in (bar in baz)", options: ["all", { nestedBinaryExpressions: false }] },
{ code: "foo + (bar + baz)", options: ["all", { nestedBinaryExpressions: false }] },
{ code: "foo && (bar && baz)", options: ["all", { nestedBinaryExpressions: false }] },
],

invalid: [
Expand Down Expand Up @@ -690,5 +698,13 @@ ruleTester.run("no-extra-parens", rule, {
invalid("async function a() { await (a()); }", "async function a() { await a(); }", "CallExpression", null, {parserOptions: { ecmaVersion: 8 }}),
invalid("async function a() { await (+a); }", "async function a() { await +a; }", "UnaryExpression", null, {parserOptions: { ecmaVersion: 8 }}),
invalid("async function a() { +(await a); }", "async function a() { +await a; }", "AwaitExpression", null, {parserOptions: { ecmaVersion: 8 }}),
invalid("(foo) instanceof bar", "foo instanceof bar", "Identifier", 1, {options: ["all", {nestedBinaryExpressions: false}]}),
invalid("(foo) in bar", "foo in bar", "Identifier", 1, {options: ["all", {nestedBinaryExpressions: false}]}),
invalid("(foo) + bar", "foo + bar", "Identifier", 1, {options: ["all", {nestedBinaryExpressions: false}]}),
invalid("(foo) && bar", "foo && bar", "Identifier", 1, {options: ["all", {nestedBinaryExpressions: false}]}),
invalid("foo instanceof (bar)", "foo instanceof bar", "Identifier", 1, {options: ["all", {nestedBinaryExpressions: false}]}),
invalid("foo in (bar)", "foo in bar", "Identifier", 1, {options: ["all", {nestedBinaryExpressions: false}]}),
invalid("foo + (bar)", "foo + bar", "Identifier", 1, {options: ["all", {nestedBinaryExpressions: false}]}),
invalid("foo && (bar)", "foo && bar", "Identifier", 1, {options: ["all", {nestedBinaryExpressions: false}]})
]
});

0 comments on commit e3f95de

Please sign in to comment.