Skip to content

Commit 6e7c18d

Browse files
mdjermanovicplatinumazure
authored andcommitted
Update: enforceForNewInMemberExpressions no-extra-parens (fixes #12428) (#12436)
1 parent 51fbbd7 commit 6e7c18d

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

docs/rules/no-extra-parens.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This rule has an object option for exceptions to the `"all"` option:
2525
* `"ignoreJSX": "none|all|multi-line|single-line"` allows extra parentheses around no/all/multi-line/single-line JSX components. Defaults to `none`.
2626
* `"enforceForArrowConditionals": false` allows extra parentheses around ternary expressions which are the body of an arrow function
2727
* `"enforceForSequenceExpressions": false` allows extra parentheses around sequence expressions
28+
* `"enforceForNewInMemberExpressions": false` allows extra parentheses around `new` expressions in member expressions
2829

2930
### all
3031

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

211+
### enforceForNewInMemberExpressions
212+
213+
Examples of **correct** code for this rule with the `"all"` and `{ "enforceForNewInMemberExpressions": false }` options:
214+
215+
```js
216+
/* eslint no-extra-parens: ["error", "all", { "enforceForNewInMemberExpressions": false }] */
217+
218+
const foo = (new Bar()).baz;
219+
220+
const quux = (new Bar())[baz];
221+
222+
(new Bar()).doSomething();
223+
```
224+
210225
### functions
211226

212227
Examples of **incorrect** code for this rule with the `"functions"` option:

lib/rules/no-extra-parens.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ module.exports = {
5050
returnAssign: { type: "boolean" },
5151
ignoreJSX: { enum: ["none", "all", "single-line", "multi-line"] },
5252
enforceForArrowConditionals: { type: "boolean" },
53-
enforceForSequenceExpressions: { type: "boolean" }
53+
enforceForSequenceExpressions: { type: "boolean" },
54+
enforceForNewInMemberExpressions: { type: "boolean" }
5455
},
5556
additionalProperties: false
5657
}
@@ -80,6 +81,8 @@ module.exports = {
8081
context.options[1].enforceForArrowConditionals === false;
8182
const IGNORE_SEQUENCE_EXPRESSIONS = ALL_NODES && context.options[1] &&
8283
context.options[1].enforceForSequenceExpressions === false;
84+
const IGNORE_NEW_IN_MEMBER_EXPR = ALL_NODES && context.options[1] &&
85+
context.options[1].enforceForNewInMemberExpressions === false;
8386

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

895898
if (nodeObjHasExcessParens &&
899+
!IGNORE_NEW_IN_MEMBER_EXPR &&
896900
node.object.type === "NewExpression" &&
897901
isNewExpressionWithParens(node.object)) {
898902
report(node.object);

tests/lib/rules/no-extra-parens.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,14 @@ ruleTester.run("no-extra-parens", rule, {
456456
{ code: "if((a, b)){}", options: ["all", { enforceForSequenceExpressions: false }] },
457457
{ code: "while ((val = foo(), val < 10));", options: ["all", { enforceForSequenceExpressions: false }] },
458458

459+
// ["all", { enforceForNewInMemberExpressions: false }]
460+
{ code: "(new foo()).bar", options: ["all", { enforceForNewInMemberExpressions: false }] },
461+
{ code: "(new foo())[bar]", options: ["all", { enforceForNewInMemberExpressions: false }] },
462+
{ code: "(new foo()).bar()", options: ["all", { enforceForNewInMemberExpressions: false }] },
463+
{ code: "(new foo(bar)).baz", options: ["all", { enforceForNewInMemberExpressions: false }] },
464+
{ code: "(new foo.bar()).baz", options: ["all", { enforceForNewInMemberExpressions: false }] },
465+
{ code: "(new foo.bar()).baz()", options: ["all", { enforceForNewInMemberExpressions: false }] },
466+
459467
"let a = [ ...b ]",
460468
"let a = { ...b }",
461469
{
@@ -659,6 +667,7 @@ ruleTester.run("no-extra-parens", rule, {
659667
invalid("(foo.bar()).baz", "foo.bar().baz", "CallExpression"),
660668
invalid("(foo\n.bar())\n.baz", "foo\n.bar()\n.baz", "CallExpression"),
661669
invalid("(new foo()).bar", "new foo().bar", "NewExpression"),
670+
invalid("(new foo())[bar]", "new foo()[bar]", "NewExpression"),
662671
invalid("(new foo()).bar()", "new foo().bar()", "NewExpression"),
663672
invalid("(new foo(bar)).baz", "new foo(bar).baz", "NewExpression"),
664673
invalid("(new foo.bar()).baz", "new foo.bar().baz", "NewExpression"),
@@ -1165,6 +1174,63 @@ ruleTester.run("no-extra-parens", rule, {
11651174
]
11661175
},
11671176

1177+
// ["all", { enforceForNewInMemberExpressions: true }]
1178+
{
1179+
code: "(new foo()).bar",
1180+
output: "new foo().bar",
1181+
options: ["all"],
1182+
errors: [
1183+
{
1184+
messageId: "unexpected",
1185+
type: "NewExpression"
1186+
}
1187+
]
1188+
},
1189+
{
1190+
code: "(new foo()).bar",
1191+
output: "new foo().bar",
1192+
options: ["all", {}],
1193+
errors: [
1194+
{
1195+
messageId: "unexpected",
1196+
type: "NewExpression"
1197+
}
1198+
]
1199+
},
1200+
{
1201+
code: "(new foo()).bar",
1202+
output: "new foo().bar",
1203+
options: ["all", { enforceForNewInMemberExpressions: true }],
1204+
errors: [
1205+
{
1206+
messageId: "unexpected",
1207+
type: "NewExpression"
1208+
}
1209+
]
1210+
},
1211+
{
1212+
code: "(new foo())[bar]",
1213+
output: "new foo()[bar]",
1214+
options: ["all", { enforceForNewInMemberExpressions: true }],
1215+
errors: [
1216+
{
1217+
messageId: "unexpected",
1218+
type: "NewExpression"
1219+
}
1220+
]
1221+
},
1222+
{
1223+
code: "(new foo.bar()).baz",
1224+
output: "new foo.bar().baz",
1225+
options: ["all", { enforceForNewInMemberExpressions: true }],
1226+
errors: [
1227+
{
1228+
messageId: "unexpected",
1229+
type: "NewExpression"
1230+
}
1231+
]
1232+
},
1233+
11681234
// https://github.com/eslint/eslint/issues/8175
11691235
invalid(
11701236
"let a = [...(b)]",

0 commit comments

Comments
 (0)