Skip to content

Commit 4551fe6

Browse files
authored
Fix no space after unary minus when followed by opening parenthesis in LESS (#14008)
1 parent 948b3af commit 4551fe6

5 files changed

Lines changed: 111 additions & 5 deletions

File tree

changelog_unreleased/less/14008.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#### Fix no space after unary minus when followed by opening parenthesis in LESS (#14008 by @mvorisek)
2+
3+
<!-- prettier-ignore -->
4+
```less
5+
// Input
6+
.unary_minus_single {
7+
margin: -(@a);
8+
}
9+
10+
.unary_minus_multi {
11+
margin: 0 -(@a);
12+
}
13+
14+
.binary_minus {
15+
margin: 0 - (@a);
16+
}
17+
18+
// Prettier stable
19+
.unary_minus_single {
20+
margin: - (@a);
21+
}
22+
23+
.unary_minus_multi {
24+
margin: 0 - (@a);
25+
}
26+
27+
.binary_minus {
28+
margin: 0 - (@a);
29+
}
30+
31+
// Prettier main
32+
.unary_minus_single {
33+
margin: -(@a);
34+
}
35+
36+
.unary_minus_multi {
37+
margin: 0 -(@a);
38+
}
39+
40+
.binary_minus {
41+
margin: 0 - (@a);
42+
}
43+
```

src/language-css/printer-postcss.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,18 @@ function genericPrint(path, options, print) {
744744
continue;
745745
}
746746

747+
// No space before unary minus followed by an opening parenthesis `-(`
748+
if (
749+
(options.parser === "scss" || options.parser === "less") &&
750+
isMathOperator &&
751+
iNode.value === "-" &&
752+
isParenGroupNode(iNextNode) &&
753+
locEnd(iNode) === locStart(iNextNode.open) &&
754+
iNextNode.open.value === "("
755+
) {
756+
continue;
757+
}
758+
747759
// Add `hardline` after inline comment (i.e. `// comment\n foo: bar;`)
748760
if (isInlineValueCommentNode(iNode)) {
749761
if (parentNode.type === "value-paren_group") {

tests/format/less/parens/__snapshots__/jsfmt.spec.js.snap

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,23 @@ a {
302302
unicode-range: U+0025-00FF, U+4??; /* multiple values */
303303
}
304304
305+
// no space after unary minus when followed by opening parenthesis, #13998
306+
.unary_minus_single {
307+
margin: -(@a);
308+
}
309+
310+
.unary_minus_multi_1 {
311+
margin: 0 -(@a);
312+
}
313+
314+
.unary_minus_multi_2 {
315+
margin: 0 -( @a + @b );
316+
}
317+
318+
.binary_minus {
319+
margin: 0 - (@a);
320+
}
321+
305322
=====================================output=====================================
306323
a {
307324
prop1: func(1px, 1px, 1px, func(1px, 1px, 1px, func(1px, 1px, 1px)));
@@ -365,7 +382,7 @@ a {
365382
background: element(#css-source);
366383
padding-top: var(--paddingC);
367384
margin: 1 * 1 (1) * 1 1 * (1) (1) * (1);
368-
prop: -1 * -1 - (-1) * -1 -1 * -(-1) - (-1) * -(-1);
385+
prop: -1 * -1 -(-1) * -1 -1 * -(-1) -(-1) * -(-1);
369386
prop4: +1;
370387
prop5: -1;
371388
prop6: word + 1; /* word1 */
@@ -396,7 +413,7 @@ a {
396413
prop41: --(1);
397414
prop42: 1px+1px+1px+1px;
398415
prop43: 1px + 1px + 1px + 1px;
399-
prop44: -1+-1 - (-1)+-1 -1+-(-1) - (-1)+-(-1);
416+
prop44: -1+-1 -(-1)+-1 -1+-(-1) -(-1)+-(-1);
400417
prop45: round(1.5) * 2 round(1.5) * 2 round(1.5) * 2 round(1.5) * 2;
401418
prop46: 2 * round(1.5) 2 * round(1.5) 2 * round(1.5) 2 * round(1.5);
402419
prop47: (round(1.5) * 2) (round(1.5) * 2) (round(1.5) * 2) (round(1.5) * 2);
@@ -472,5 +489,22 @@ a {
472489
unicode-range: U+0025-00FF, U+4??; /* multiple values */
473490
}
474491
492+
// no space after unary minus when followed by opening parenthesis, #13998
493+
.unary_minus_single {
494+
margin: -(@a);
495+
}
496+
497+
.unary_minus_multi_1 {
498+
margin: 0 -(@a);
499+
}
500+
501+
.unary_minus_multi_2 {
502+
margin: 0 -(@a + @b);
503+
}
504+
505+
.binary_minus {
506+
margin: 0 - (@a);
507+
}
508+
475509
================================================================================
476510
`;

tests/format/less/parens/parens.less

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,20 @@ a {
259259
unicode-range: U+4??; /* wildcard range */
260260
unicode-range: U+0025-00FF, U+4??; /* multiple values */
261261
}
262+
263+
// no space after unary minus when followed by opening parenthesis, #13998
264+
.unary_minus_single {
265+
margin: -(@a);
266+
}
267+
268+
.unary_minus_multi_1 {
269+
margin: 0 -(@a);
270+
}
271+
272+
.unary_minus_multi_2 {
273+
margin: 0 -( @a + @b );
274+
}
275+
276+
.binary_minus {
277+
margin: 0 - (@a);
278+
}

tests/format/scss/parens/__snapshots__/jsfmt.spec.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,10 @@ a {
418418
);
419419
padding-top: var(--paddingC);
420420
margin: 1 * 1 (1) * 1 1 * (1) (1) * (1);
421-
prop: -1 * -1 - (-1) * -1 -1 * -(-1) - (-1) * -(-1);
421+
prop: -1 * -1 -(-1) * -1 -1 * -(-1) -(-1) * -(-1);
422422
prop1: #{($m) * (10)};
423423
prop2: #{$m * 10};
424-
prop3: #{- (-$m) * -(-10)};
424+
prop3: #{-(-$m) * -(-10)};
425425
prop4: +1;
426426
prop5: -1;
427427
prop6: word + 1; /* word1 */
@@ -469,7 +469,7 @@ a {
469469
prop41: --(1);
470470
prop42: 1px+1px+1px+1px;
471471
prop43: 1px + 1px + 1px + 1px;
472-
prop44: -1+-1 - (-1)+-1 -1+-(-1) - (-1)+-(-1);
472+
prop44: -1+-1 -(-1)+-1 -1+-(-1) -(-1)+-(-1);
473473
prop45: round(1.5) * 2 round(1.5) * 2 round(1.5) * 2 round(1.5) * 2;
474474
prop46: 2 * round(1.5) 2 * round(1.5) 2 * round(1.5) 2 * round(1.5);
475475
prop47: (round(1.5) * 2) (round(1.5) * 2) (round(1.5) * 2) (round(1.5) * 2);

0 commit comments

Comments
 (0)