Skip to content
Permalink
Browse files

Update: enforceForNewInMemberExpressions no-extra-parens (fixes #12428)…

… (#12436)
  • Loading branch information
mdjermanovic authored and platinumazure committed Oct 20, 2019
1 parent 51fbbd7 commit 6e7c18ddb30b32ee5b2e842cc8258aa7aebb7445
Showing with 86 additions and 1 deletion.
  1. +15 −0 docs/rules/no-extra-parens.md
  2. +5 −1 lib/rules/no-extra-parens.js
  3. +66 −0 tests/lib/rules/no-extra-parens.js
@@ -25,6 +25,7 @@ This rule has an object option for exceptions to the `"all"` option:
* `"ignoreJSX": "none|all|multi-line|single-line"` allows extra parentheses around no/all/multi-line/single-line JSX components. Defaults to `none`.
* `"enforceForArrowConditionals": false` allows extra parentheses around ternary expressions which are the body of an arrow function
* `"enforceForSequenceExpressions": false` allows extra parentheses around sequence expressions
* `"enforceForNewInMemberExpressions": false` allows extra parentheses around `new` expressions in member expressions

### all

@@ -207,6 +208,20 @@ if ((val = foo(), val < 10)) {}
while ((val = foo(), val < 10));
```

### enforceForNewInMemberExpressions

Examples of **correct** code for this rule with the `"all"` and `{ "enforceForNewInMemberExpressions": false }` options:

```js
/* eslint no-extra-parens: ["error", "all", { "enforceForNewInMemberExpressions": false }] */
const foo = (new Bar()).baz;
const quux = (new Bar())[baz];
(new Bar()).doSomething();
```

### functions

Examples of **incorrect** code for this rule with the `"functions"` option:
@@ -50,7 +50,8 @@ module.exports = {
returnAssign: { type: "boolean" },
ignoreJSX: { enum: ["none", "all", "single-line", "multi-line"] },
enforceForArrowConditionals: { type: "boolean" },
enforceForSequenceExpressions: { type: "boolean" }
enforceForSequenceExpressions: { type: "boolean" },
enforceForNewInMemberExpressions: { type: "boolean" }
},
additionalProperties: false
}
@@ -80,6 +81,8 @@ module.exports = {
context.options[1].enforceForArrowConditionals === false;
const IGNORE_SEQUENCE_EXPRESSIONS = ALL_NODES && context.options[1] &&
context.options[1].enforceForSequenceExpressions === false;
const IGNORE_NEW_IN_MEMBER_EXPR = ALL_NODES && context.options[1] &&
context.options[1].enforceForNewInMemberExpressions === false;

const PRECEDENCE_OF_ASSIGNMENT_EXPR = precedence({ type: "AssignmentExpression" });
const PRECEDENCE_OF_UPDATE_EXPR = precedence({ type: "UpdateExpression" });
@@ -893,6 +896,7 @@ module.exports = {
}

if (nodeObjHasExcessParens &&
!IGNORE_NEW_IN_MEMBER_EXPR &&
node.object.type === "NewExpression" &&
isNewExpressionWithParens(node.object)) {
report(node.object);
@@ -456,6 +456,14 @@ ruleTester.run("no-extra-parens", rule, {
{ code: "if((a, b)){}", options: ["all", { enforceForSequenceExpressions: false }] },
{ code: "while ((val = foo(), val < 10));", options: ["all", { enforceForSequenceExpressions: false }] },

// ["all", { enforceForNewInMemberExpressions: false }]
{ code: "(new foo()).bar", options: ["all", { enforceForNewInMemberExpressions: false }] },
{ code: "(new foo())[bar]", options: ["all", { enforceForNewInMemberExpressions: false }] },
{ code: "(new foo()).bar()", options: ["all", { enforceForNewInMemberExpressions: false }] },
{ code: "(new foo(bar)).baz", options: ["all", { enforceForNewInMemberExpressions: false }] },
{ code: "(new foo.bar()).baz", options: ["all", { enforceForNewInMemberExpressions: false }] },
{ code: "(new foo.bar()).baz()", options: ["all", { enforceForNewInMemberExpressions: false }] },

"let a = [ ...b ]",
"let a = { ...b }",
{
@@ -659,6 +667,7 @@ ruleTester.run("no-extra-parens", rule, {
invalid("(foo.bar()).baz", "foo.bar().baz", "CallExpression"),
invalid("(foo\n.bar())\n.baz", "foo\n.bar()\n.baz", "CallExpression"),
invalid("(new foo()).bar", "new foo().bar", "NewExpression"),
invalid("(new foo())[bar]", "new foo()[bar]", "NewExpression"),
invalid("(new foo()).bar()", "new foo().bar()", "NewExpression"),
invalid("(new foo(bar)).baz", "new foo(bar).baz", "NewExpression"),
invalid("(new foo.bar()).baz", "new foo.bar().baz", "NewExpression"),
@@ -1165,6 +1174,63 @@ ruleTester.run("no-extra-parens", rule, {
]
},

// ["all", { enforceForNewInMemberExpressions: true }]
{
code: "(new foo()).bar",
output: "new foo().bar",
options: ["all"],
errors: [
{
messageId: "unexpected",
type: "NewExpression"
}
]
},
{
code: "(new foo()).bar",
output: "new foo().bar",
options: ["all", {}],
errors: [
{
messageId: "unexpected",
type: "NewExpression"
}
]
},
{
code: "(new foo()).bar",
output: "new foo().bar",
options: ["all", { enforceForNewInMemberExpressions: true }],
errors: [
{
messageId: "unexpected",
type: "NewExpression"
}
]
},
{
code: "(new foo())[bar]",
output: "new foo()[bar]",
options: ["all", { enforceForNewInMemberExpressions: true }],
errors: [
{
messageId: "unexpected",
type: "NewExpression"
}
]
},
{
code: "(new foo.bar()).baz",
output: "new foo.bar().baz",
options: ["all", { enforceForNewInMemberExpressions: true }],
errors: [
{
messageId: "unexpected",
type: "NewExpression"
}
]
},

// https://github.com/eslint/eslint/issues/8175
invalid(
"let a = [...(b)]",

0 comments on commit 6e7c18d

Please sign in to comment.
You can’t perform that action at this time.