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: enforceForNewInMemberExpressions no-extra-parens (fixes #12428) #12436

Merged
merged 1 commit into from Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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