Skip to content

Commit

Permalink
Fix no space after unary minus when followed by opening parenthesis i…
Browse files Browse the repository at this point in the history
…n LESS (#14008)
  • Loading branch information
mvorisek committed Dec 29, 2022
1 parent 948b3af commit 4551fe6
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 5 deletions.
43 changes: 43 additions & 0 deletions changelog_unreleased/less/14008.md
@@ -0,0 +1,43 @@
#### Fix no space after unary minus when followed by opening parenthesis in LESS (#14008 by @mvorisek)

<!-- prettier-ignore -->
```less
// Input
.unary_minus_single {
margin: -(@a);
}

.unary_minus_multi {
margin: 0 -(@a);
}

.binary_minus {
margin: 0 - (@a);
}

// Prettier stable
.unary_minus_single {
margin: - (@a);
}

.unary_minus_multi {
margin: 0 - (@a);
}

.binary_minus {
margin: 0 - (@a);
}

// Prettier main
.unary_minus_single {
margin: -(@a);
}

.unary_minus_multi {
margin: 0 -(@a);
}

.binary_minus {
margin: 0 - (@a);
}
```
12 changes: 12 additions & 0 deletions src/language-css/printer-postcss.js
Expand Up @@ -744,6 +744,18 @@ function genericPrint(path, options, print) {
continue;
}

// No space before unary minus followed by an opening parenthesis `-(`
if (
(options.parser === "scss" || options.parser === "less") &&
isMathOperator &&
iNode.value === "-" &&
isParenGroupNode(iNextNode) &&
locEnd(iNode) === locStart(iNextNode.open) &&
iNextNode.open.value === "("
) {
continue;
}

// Add `hardline` after inline comment (i.e. `// comment\n foo: bar;`)
if (isInlineValueCommentNode(iNode)) {
if (parentNode.type === "value-paren_group") {
Expand Down
38 changes: 36 additions & 2 deletions tests/format/less/parens/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -302,6 +302,23 @@ a {
unicode-range: U+0025-00FF, U+4??; /* multiple values */
}
// no space after unary minus when followed by opening parenthesis, #13998
.unary_minus_single {
margin: -(@a);
}
.unary_minus_multi_1 {
margin: 0 -(@a);
}
.unary_minus_multi_2 {
margin: 0 -( @a + @b );
}
.binary_minus {
margin: 0 - (@a);
}
=====================================output=====================================
a {
prop1: func(1px, 1px, 1px, func(1px, 1px, 1px, func(1px, 1px, 1px)));
Expand Down Expand Up @@ -365,7 +382,7 @@ a {
background: element(#css-source);
padding-top: var(--paddingC);
margin: 1 * 1 (1) * 1 1 * (1) (1) * (1);
prop: -1 * -1 - (-1) * -1 -1 * -(-1) - (-1) * -(-1);
prop: -1 * -1 -(-1) * -1 -1 * -(-1) -(-1) * -(-1);
prop4: +1;
prop5: -1;
prop6: word + 1; /* word1 */
Expand Down Expand Up @@ -396,7 +413,7 @@ a {
prop41: --(1);
prop42: 1px+1px+1px+1px;
prop43: 1px + 1px + 1px + 1px;
prop44: -1+-1 - (-1)+-1 -1+-(-1) - (-1)+-(-1);
prop44: -1+-1 -(-1)+-1 -1+-(-1) -(-1)+-(-1);
prop45: round(1.5) * 2 round(1.5) * 2 round(1.5) * 2 round(1.5) * 2;
prop46: 2 * round(1.5) 2 * round(1.5) 2 * round(1.5) 2 * round(1.5);
prop47: (round(1.5) * 2) (round(1.5) * 2) (round(1.5) * 2) (round(1.5) * 2);
Expand Down Expand Up @@ -472,5 +489,22 @@ a {
unicode-range: U+0025-00FF, U+4??; /* multiple values */
}
// no space after unary minus when followed by opening parenthesis, #13998
.unary_minus_single {
margin: -(@a);
}
.unary_minus_multi_1 {
margin: 0 -(@a);
}
.unary_minus_multi_2 {
margin: 0 -(@a + @b);
}
.binary_minus {
margin: 0 - (@a);
}
================================================================================
`;
17 changes: 17 additions & 0 deletions tests/format/less/parens/parens.less
Expand Up @@ -259,3 +259,20 @@ a {
unicode-range: U+4??; /* wildcard range */
unicode-range: U+0025-00FF, U+4??; /* multiple values */
}

// no space after unary minus when followed by opening parenthesis, #13998
.unary_minus_single {
margin: -(@a);
}

.unary_minus_multi_1 {
margin: 0 -(@a);
}

.unary_minus_multi_2 {
margin: 0 -( @a + @b );
}

.binary_minus {
margin: 0 - (@a);
}
6 changes: 3 additions & 3 deletions tests/format/scss/parens/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -418,10 +418,10 @@ a {
);
padding-top: var(--paddingC);
margin: 1 * 1 (1) * 1 1 * (1) (1) * (1);
prop: -1 * -1 - (-1) * -1 -1 * -(-1) - (-1) * -(-1);
prop: -1 * -1 -(-1) * -1 -1 * -(-1) -(-1) * -(-1);
prop1: #{($m) * (10)};
prop2: #{$m * 10};
prop3: #{- (-$m) * -(-10)};
prop3: #{-(-$m) * -(-10)};
prop4: +1;
prop5: -1;
prop6: word + 1; /* word1 */
Expand Down Expand Up @@ -469,7 +469,7 @@ a {
prop41: --(1);
prop42: 1px+1px+1px+1px;
prop43: 1px + 1px + 1px + 1px;
prop44: -1+-1 - (-1)+-1 -1+-(-1) - (-1)+-(-1);
prop44: -1+-1 -(-1)+-1 -1+-(-1) -(-1)+-(-1);
prop45: round(1.5) * 2 round(1.5) * 2 round(1.5) * 2 round(1.5) * 2;
prop46: 2 * round(1.5) 2 * round(1.5) 2 * round(1.5) 2 * round(1.5);
prop47: (round(1.5) * 2) (round(1.5) * 2) (round(1.5) * 2) (round(1.5) * 2);
Expand Down

0 comments on commit 4551fe6

Please sign in to comment.