Skip to content

Commit

Permalink
Update: enforceForNewInMemberExpressions no-extra-parens (fixes #12428)…
Browse files Browse the repository at this point in the history
… (#12436)
  • Loading branch information
mdjermanovic authored and platinumazure committed Oct 20, 2019
1 parent 51fbbd7 commit 6e7c18d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
15 changes: 15 additions & 0 deletions docs/rules/no-extra-parens.md
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion lib/rules/no-extra-parens.js
Expand Up @@ -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
}
Expand Down Expand Up @@ -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" });
Expand Down Expand Up @@ -893,6 +896,7 @@ module.exports = {
}

if (nodeObjHasExcessParens &&
!IGNORE_NEW_IN_MEMBER_EXPR &&
node.object.type === "NewExpression" &&
isNewExpressionWithParens(node.object)) {
report(node.object);
Expand Down
66 changes: 66 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -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 }",
{
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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)]",
Expand Down

0 comments on commit 6e7c18d

Please sign in to comment.