Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit11ae6fc

Browse files
mdjermanovickaicataldo
authored andcommitted
Update: Fix call, new and member expressions in no-extra-parens (#12302)
1 parenta7894eb commit11ae6fc

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,14 @@ module.exports = {
205205
constlastToken=sourceCode.getLastToken(newExpression);
206206
constpenultimateToken=sourceCode.getTokenBefore(lastToken);
207207

208-
returnnewExpression.arguments.length>0||astUtils.isOpeningParenToken(penultimateToken)&&astUtils.isClosingParenToken(lastToken);
208+
returnnewExpression.arguments.length>0||
209+
(
210+
211+
// The expression should end with its own parens, e.g., new new foo() is not a new expression with parens
212+
astUtils.isOpeningParenToken(penultimateToken)&&
213+
astUtils.isClosingParenToken(lastToken)&&
214+
newExpression.callee.range[1]<newExpression.range[1]
215+
);
209216
}
210217

211218
/**
@@ -338,7 +345,7 @@ module.exports = {
338345
functionfinishReport(){
339346
context.report({
340347
node,
341-
loc:leftParenToken.loc.start,
348+
loc:leftParenToken.loc,
342349
messageId:"unexpected",
343350
fix(fixer){
344351
constparenthesizedSource=sourceCode.text.slice(leftParenToken.range[1],rightParenToken.range[0]);
@@ -887,6 +894,12 @@ module.exports = {
887894
report(node.object);
888895
}
889896

897+
if(nodeObjHasExcessParens&&
898+
node.object.type==="NewExpression"&&
899+
isNewExpressionWithParens(node.object)){
900+
report(node.object);
901+
}
902+
890903
if(node.computed&&hasExcessParens(node.property)){
891904
report(node.property);
892905
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ ruleTester.run("no-extra-parens", rule, {
9595
"new A()()",
9696
"(new A)()",
9797
"(new (Foo || Bar))()",
98+
"(new new foo())()",
99+
"new (new new foo())(bar)",
100+
"(new foo).bar",
101+
"(new foo)[bar]",
102+
"(new foo).bar.baz",
103+
"(new foo.bar).baz",
104+
"(new foo).bar()",
105+
"(new foo.bar).baz()",
106+
"new (new foo).bar",
107+
"new (new foo.bar).baz",
108+
"(new new foo()).baz",
98109
"(2 + 3) ** 4",
99110
"2 ** (2 + 3)",
100111
"new (import(source))",
@@ -647,6 +658,11 @@ ruleTester.run("no-extra-parens", rule, {
647658
invalid("(foo()).bar","foo().bar","CallExpression"),
648659
invalid("(foo.bar()).baz","foo.bar().baz","CallExpression"),
649660
invalid("(foo\n.bar())\n.baz","foo\n.bar()\n.baz","CallExpression"),
661+
invalid("(new foo()).bar","new foo().bar","NewExpression"),
662+
invalid("(new foo()).bar()","new foo().bar()","NewExpression"),
663+
invalid("(new foo(bar)).baz","new foo(bar).baz","NewExpression"),
664+
invalid("(new foo.bar()).baz","new foo.bar().baz","NewExpression"),
665+
invalid("(new foo.bar()).baz()","new foo.bar().baz()","NewExpression"),
650666

651667
invalid("new (A)","new A","Identifier"),
652668
invalid("(new A())()","new A()()","NewExpression"),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp