Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: Check computed method keys in no-extra-parens (#11973)
* Update: Check computed method keys in no-extra-parens

* Remove member null check
  • Loading branch information
mdjermanovic authored and platinumazure committed Aug 3, 2019
1 parent d961438 commit f5e0cc4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/rules/no-extra-parens.js
Expand Up @@ -686,6 +686,13 @@ module.exports = {

CallExpression: checkCallNew,

ClassBody(node) {
node.body
.filter(member => member.type === "MethodDefinition" && member.computed &&
member.key && hasExcessParens(member.key) && precedence(member.key) >= PRECEDENCE_OF_ASSIGNMENT_EXPR)
.forEach(member => report(member.key));
},

ConditionalExpression(node) {
if (isReturnAssignException(node)) {
return;
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -160,6 +160,13 @@ ruleTester.run("no-extra-parens", rule, {
"const {[a]:b=1} = {}",
"const {[(a, b)]:c} = {}",
"const {a, ...b} = {}",
"class foo {}",
"class foo { constructor(){} a(){} get b(){} set b(bar){} get c(){} set d(baz){} static e(){} }",
"class foo { [a](){} get [b](){} set [b](bar){} get [c](){} set [d](baz){} static [e](){} }",
"class foo { [(a,b)](){} }",
"class foo { a(){} [b](){} c(){} [(d,e)](){} }",
"class foo { [(a,b)](){} c(){} [d](){} e(){} }",
"const foo = class { constructor(){} a(){} get b(){} set b(bar){} get c(){} set d(baz){} static e(){} }",

// ExpressionStatement restricted productions
"({});",
Expand Down Expand Up @@ -553,6 +560,22 @@ ruleTester.run("no-extra-parens", rule, {
invalid("const {a, b:c, [(d+e)]:f, [(g,h)]:i, [j]:k} = {}", "const {a, b:c, [d+e]:f, [(g,h)]:i, [j]:k} = {}", "BinaryExpression", 1),
invalid("const {[a+(b*c)]:d} = {}", "const {[a+b*c]:d} = {}", "BinaryExpression", 1),
invalid("const {[(a, (b+c))]:d} = {}", "const {[(a, b+c)]:d} = {}", "BinaryExpression", 1),
invalid("class foo { [(a)](){} }", "class foo { [a](){} }", "Identifier"),
invalid("class foo {*[(Symbol.iterator)]() {}}", "class foo {*[Symbol.iterator]() {}}", "MemberExpression"),
invalid("class foo { get [(a)](){} }", "class foo { get [a](){} }", "Identifier"),
invalid("class foo { set [(a)](bar){} }", "class foo { set [a](bar){} }", "Identifier"),
invalid("class foo { static [(a)](bar){} }", "class foo { static [a](bar){} }", "Identifier"),
invalid("class foo { [(a=b)](){} }", "class foo { [a=b](){} }", "AssignmentExpression"),
invalid("class foo { constructor (){} [(a+b)](){} }", "class foo { constructor (){} [a+b](){} }", "BinaryExpression"),
invalid("class foo { [(a+b)](){} constructor (){} }", "class foo { [a+b](){} constructor (){} }", "BinaryExpression"),
invalid("class foo { [(a+b)](){} c(){} }", "class foo { [a+b](){} c(){} }", "BinaryExpression"),
invalid("class foo { a(){} [(b+c)](){} d(){} }", "class foo { a(){} [b+c](){} d(){} }", "BinaryExpression"),
invalid("class foo { [(a+b)](){} [c](){} }", "class foo { [a+b](){} [c](){} }", "BinaryExpression"),
invalid("class foo { [a](){} [(b+c)](){} [d](){} }", "class foo { [a](){} [b+c](){} [d](){} }", "BinaryExpression"),
invalid("class foo { [(a+b)](){} [(c,d)](){} }", "class foo { [a+b](){} [(c,d)](){} }", "BinaryExpression"),
invalid("class foo { [(a,b)](){} [(c+d)](){} }", "class foo { [(a,b)](){} [c+d](){} }", "BinaryExpression"),
invalid("class foo { [a+(b*c)](){} }", "class foo { [a+b*c](){} }", "BinaryExpression"),
invalid("const foo = class { [(a)](){} }", "const foo = class { [a](){} }", "Identifier"),
invalid(
"var foo = (function*() { if ((yield foo())) { return; } }())",
"var foo = (function*() { if (yield foo()) { return; } }())",
Expand Down

0 comments on commit f5e0cc4

Please sign in to comment.