Skip to content

Commit

Permalink
Update: check class fields in no-extra-parens (refs #14857) (#14906)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Aug 9, 2021
1 parent 5c3a470 commit 9052eee
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
16 changes: 16 additions & 0 deletions docs/rules/no-extra-parens.md
Expand Up @@ -48,6 +48,14 @@ for (a of (b));
typeof (a);

(function(){} ? a() : b());

class A {
[(x)] = 1;
}

class B {
x = (y + z);
}
```

Examples of **correct** code for this rule with the default `"all"` option:
Expand All @@ -72,6 +80,14 @@ for (a of b);
for (a in b, c);

for (a in b);

class A {
[x] = 1;
}

class B {
x = y + z;
}
```

### conditionalAssign
Expand Down
23 changes: 16 additions & 7 deletions lib/rules/no-extra-parens.js
Expand Up @@ -808,13 +808,6 @@ module.exports = {

CallExpression: checkCallNew,

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

ConditionalExpression(node) {
if (isReturnAssignException(node)) {
return;
Expand Down Expand Up @@ -1063,6 +1056,12 @@ module.exports = {
}
},

"MethodDefinition[computed=true]"(node) {
if (hasExcessParensWithPrecedence(node.key, PRECEDENCE_OF_ASSIGNMENT_EXPR)) {
report(node.key);
}
},

NewExpression: checkCallNew,

ObjectExpression(node) {
Expand Down Expand Up @@ -1090,6 +1089,16 @@ module.exports = {
}
},

PropertyDefinition(node) {
if (node.computed && hasExcessParensWithPrecedence(node.key, PRECEDENCE_OF_ASSIGNMENT_EXPR)) {
report(node.key);
}

if (node.value && hasExcessParensWithPrecedence(node.value, PRECEDENCE_OF_ASSIGNMENT_EXPR)) {
report(node.value);
}
},

RestElement(node) {
const argument = node.argument;

Expand Down
41 changes: 40 additions & 1 deletion tests/lib/rules/no-extra-parens.js
Expand Up @@ -44,7 +44,7 @@ function invalid(code, output, type, line, config) {

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2021,
ecmaVersion: 2022,
ecmaFeatures: {
jsx: true
}
Expand Down Expand Up @@ -182,6 +182,25 @@ ruleTester.run("no-extra-parens", rule, {
"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(){} }",
"class foo { x; }",
"class foo { static x; }",
"class foo { x = 1; }",
"class foo { static x = 1; }",
"class foo { #x; }",
"class foo { static #x; }",
"class foo { static #x = 1; }",
"class foo { #x(){} get #y() {} set #y(value) {} static #z(){} static get #q() {} static set #q(value) {} }",
"const foo = class { #x(){} get #y() {} set #y(value) {} static #z(){} static get #q() {} static set #q(value) {} }",
"class foo { [(x, y)]; }",
"class foo { static [(x, y)]; }",
"class foo { [(x, y)] = 1; }",
"class foo { static [(x, y)] = 1; }",
"class foo { x = (y, z); }",
"class foo { static x = (y, z); }",
"class foo { #x = (y, z); }",
"class foo { static #x = (y, z); }",
"class foo { [(1, 2)] = (3, 4) }",
"const foo = class { [(1, 2)] = (3, 4) }",

// ExpressionStatement restricted productions
"({});",
Expand Down Expand Up @@ -771,6 +790,26 @@ ruleTester.run("no-extra-parens", rule, {
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("class foo { [(x)]; }", "class foo { [x]; }", "Identifier"),
invalid("class foo { static [(x)]; }", "class foo { static [x]; }", "Identifier"),
invalid("class foo { [(x)] = 1; }", "class foo { [x] = 1; }", "Identifier"),
invalid("class foo { static [(x)] = 1; }", "class foo { static [x] = 1; }", "Identifier"),
invalid("const foo = class { [(x)]; }", "const foo = class { [x]; }", "Identifier"),
invalid("class foo { [(x = y)]; }", "class foo { [x = y]; }", "AssignmentExpression"),
invalid("class foo { [(x + y)]; }", "class foo { [x + y]; }", "BinaryExpression"),
invalid("class foo { [(x ? y : z)]; }", "class foo { [x ? y : z]; }", "ConditionalExpression"),
invalid("class foo { [((x, y))]; }", "class foo { [(x, y)]; }", "SequenceExpression"),
invalid("class foo { x = (y); }", "class foo { x = y; }", "Identifier"),
invalid("class foo { static x = (y); }", "class foo { static x = y; }", "Identifier"),
invalid("class foo { #x = (y); }", "class foo { #x = y; }", "Identifier"),
invalid("class foo { static #x = (y); }", "class foo { static #x = y; }", "Identifier"),
invalid("const foo = class { x = (y); }", "const foo = class { x = y; }", "Identifier"),
invalid("class foo { x = (() => {}); }", "class foo { x = () => {}; }", "ArrowFunctionExpression"),
invalid("class foo { x = (y + z); }", "class foo { x = y + z; }", "BinaryExpression"),
invalid("class foo { x = (y ? z : q); }", "class foo { x = y ? z : q; }", "ConditionalExpression"),
invalid("class foo { x = ((y, z)); }", "class foo { x = (y, z); }", "SequenceExpression"),

//
invalid(
"var foo = (function*() { if ((yield foo())) { return; } }())",
"var foo = (function*() { if (yield foo()) { return; } }())",
Expand Down

0 comments on commit 9052eee

Please sign in to comment.