Skip to content

Commit 27f0de6

Browse files
authored
Fix: account for linebreaks before postfix ++/-- in no-extra-parens (#13731)
1 parent da78fa1 commit 27f0de6

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/rules/no-extra-parens.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,22 @@ module.exports = {
11091109
},
11101110

11111111
UnaryExpression: checkArgumentWithPrecedence,
1112-
UpdateExpression: checkArgumentWithPrecedence,
1112+
UpdateExpression(node) {
1113+
if (node.prefix) {
1114+
checkArgumentWithPrecedence(node);
1115+
} else {
1116+
const { argument } = node;
1117+
const operatorToken = sourceCode.getLastToken(node);
1118+
1119+
if (argument.loc.end.line === operatorToken.loc.start.line) {
1120+
checkArgumentWithPrecedence(node);
1121+
} else {
1122+
if (hasDoubleExcessParens(argument)) {
1123+
report(argument);
1124+
}
1125+
}
1126+
}
1127+
},
11131128
AwaitExpression: checkArgumentWithPrecedence,
11141129

11151130
VariableDeclarator(node) {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,15 @@ ruleTester.run("no-extra-parens", rule, {
363363
"}"
364364
].join("\n"),
365365

366+
// linebreaks before postfix update operators are not allowed
367+
"(a\n)++",
368+
"(a\n)--",
369+
"(a\n\n)++",
370+
"(a.b\n)--",
371+
"(a\n.b\n)++",
372+
"(a[\nb\n]\n)--",
373+
"(a[b]\n\n)++",
374+
366375
// async/await
367376
"async function a() { await (a + b) }",
368377
"async function a() { await (a + await b) }",
@@ -755,6 +764,18 @@ ruleTester.run("no-extra-parens", rule, {
755764
invalid("+((bar-foo))", "+(bar-foo)", "BinaryExpression"),
756765
invalid("++(foo)", "++foo", "Identifier"),
757766
invalid("--(foo)", "--foo", "Identifier"),
767+
invalid("++\n(foo)", "++\nfoo", "Identifier"),
768+
invalid("--\n(foo)", "--\nfoo", "Identifier"),
769+
invalid("++(\nfoo)", "++\nfoo", "Identifier"),
770+
invalid("--(\nfoo)", "--\nfoo", "Identifier"),
771+
invalid("(foo)++", "foo++", "Identifier"),
772+
invalid("(foo)--", "foo--", "Identifier"),
773+
invalid("((foo)\n)++", "(foo\n)++", "Identifier"),
774+
invalid("((foo\n))--", "(foo\n)--", "Identifier"),
775+
invalid("((foo\n)\n)++", "(foo\n\n)++", "Identifier"),
776+
invalid("(a\n.b)--", "a\n.b--", "MemberExpression"),
777+
invalid("(a.\nb)++", "a.\nb++", "MemberExpression"),
778+
invalid("(a\n[\nb\n])--", "a\n[\nb\n]--", "MemberExpression"),
758779
invalid("(a || b) ? c : d", "a || b ? c : d", "LogicalExpression"),
759780
invalid("a ? (b = c) : d", "a ? b = c : d", "AssignmentExpression"),
760781
invalid("a ? b : (c = d)", "a ? b : c = d", "AssignmentExpression"),

0 commit comments

Comments
 (0)