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

Update: Check computed property keys in no-extra-parens #11952

Merged
merged 2 commits into from Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions lib/rules/no-extra-parens.js
Expand Up @@ -664,6 +664,16 @@ module.exports = {
}).forEach(property => report(property.value));
},

Property(node) {
if (node.computed) {
const { key } = node;

if (key && hasExcessParens(key) && precedence(key) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {
report(key);
}
}
},

ReturnStatement(node) {
const returnToken = sourceCode.getFirstToken(node);

Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -148,7 +148,18 @@ ruleTester.run("no-extra-parens", rule, {
"var a = (b, c);",
"[]",
"[a, b]",
"!{a}",
"!{a: 0, b: 1}",
"!{[a]:0}",
"!{[(a, b)]:0}",
"!{a, ...b}",
"const {a} = {}",
"const {a:b} = {}",
"const {a:b=1} = {}",
"const {[a]:b} = {}",
"const {[a]:b=1} = {}",
"const {[(a, b)]:c} = {}",
"const {a, ...b} = {}",

// ExpressionStatement restricted productions
"({});",
Expand Down Expand Up @@ -485,6 +496,12 @@ ruleTester.run("no-extra-parens", rule, {
invalid("do; while((0))", "do; while(0)", "Literal"),
invalid("for(a in (0));", "for(a in 0);", "Literal"),
invalid("for(a of (0));", "for(a of 0);", "Literal", 1),
invalid("const foo = {[(a)]:1}", "const foo = {[a]:1}", "Identifier", 1),
invalid("const foo = {[(a=b)]:1}", "const foo = {[a=b]:1}", "AssignmentExpression", 1),
invalid("const foo = {*[(Symbol.iterator)]() {}}", "const foo = {*[Symbol.iterator]() {}}", "MemberExpression", 1),
invalid("const foo = { get [(a)]() {}}", "const foo = { get [a]() {}}", "Identifier", 1),
invalid("const {[(a)]:b} = {}", "const {[a]:b} = {}", "Identifier", 1),
invalid("const {[(a=b)]:c=1} = {}", "const {[a=b]:c=1} = {}", "AssignmentExpression", 1),
invalid(
"var foo = (function*() { if ((yield foo())) { return; } }())",
"var foo = (function*() { if (yield foo()) { return; } }())",
Expand Down