Skip to content

Commit

Permalink
Fix: account for linebreaks before postfix ++/-- in no-extra-pare…
Browse files Browse the repository at this point in the history
…ns (#13731)
  • Loading branch information
mdjermanovic committed Oct 6, 2020
1 parent da78fa1 commit 27f0de6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/rules/no-extra-parens.js
Expand Up @@ -1109,7 +1109,22 @@ module.exports = {
},

UnaryExpression: checkArgumentWithPrecedence,
UpdateExpression: checkArgumentWithPrecedence,
UpdateExpression(node) {
if (node.prefix) {
checkArgumentWithPrecedence(node);
} else {
const { argument } = node;
const operatorToken = sourceCode.getLastToken(node);

if (argument.loc.end.line === operatorToken.loc.start.line) {
checkArgumentWithPrecedence(node);
} else {
if (hasDoubleExcessParens(argument)) {
report(argument);
}
}
}
},
AwaitExpression: checkArgumentWithPrecedence,

VariableDeclarator(node) {
Expand Down
21 changes: 21 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -363,6 +363,15 @@ ruleTester.run("no-extra-parens", rule, {
"}"
].join("\n"),

// linebreaks before postfix update operators are not allowed
"(a\n)++",
"(a\n)--",
"(a\n\n)++",
"(a.b\n)--",
"(a\n.b\n)++",
"(a[\nb\n]\n)--",
"(a[b]\n\n)++",

// async/await
"async function a() { await (a + b) }",
"async function a() { await (a + await b) }",
Expand Down Expand Up @@ -755,6 +764,18 @@ ruleTester.run("no-extra-parens", rule, {
invalid("+((bar-foo))", "+(bar-foo)", "BinaryExpression"),
invalid("++(foo)", "++foo", "Identifier"),
invalid("--(foo)", "--foo", "Identifier"),
invalid("++\n(foo)", "++\nfoo", "Identifier"),
invalid("--\n(foo)", "--\nfoo", "Identifier"),
invalid("++(\nfoo)", "++\nfoo", "Identifier"),
invalid("--(\nfoo)", "--\nfoo", "Identifier"),
invalid("(foo)++", "foo++", "Identifier"),
invalid("(foo)--", "foo--", "Identifier"),
invalid("((foo)\n)++", "(foo\n)++", "Identifier"),
invalid("((foo\n))--", "(foo\n)--", "Identifier"),
invalid("((foo\n)\n)++", "(foo\n\n)++", "Identifier"),
invalid("(a\n.b)--", "a\n.b--", "MemberExpression"),
invalid("(a.\nb)++", "a.\nb++", "MemberExpression"),
invalid("(a\n[\nb\n])--", "a\n[\nb\n]--", "MemberExpression"),
invalid("(a || b) ? c : d", "a || b ? c : d", "LogicalExpression"),
invalid("a ? (b = c) : d", "a ? b = c : d", "AssignmentExpression"),
invalid("a ? b : (c = d)", "a ? b : c = d", "AssignmentExpression"),
Expand Down

0 comments on commit 27f0de6

Please sign in to comment.