Skip to content

Commit 177f908

Browse files
kovsufisker
andcommitted
Prevent trailing comma in SCSS if() function (#18471)
Co-authored-by: fisker Cheung <[email protected]>
1 parent 1cd4066 commit 177f908

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

changelog_unreleased/scss/18471.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#### Prevent trailing comma in `if()` function (#18471 by @kovsu)
2+
3+
<!-- prettier-ignore -->
4+
```scss
5+
// Input
6+
$value: if(sass(false): 1; else: -1);
7+
8+
// Prettier stable
9+
$value: if(
10+
sass(false): 1; else: -1,
11+
);
12+
13+
// Prettier main
14+
$value: if(sass(false): 1; else: -1);
15+
```

src/language-css/utilities/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,18 @@ function isSCSSMapItemNode(path, options) {
261261
return false;
262262
}
263263

264+
const parentNode = path.parent;
265+
266+
// Don't treat SCSS if function arguments as maps (`if(sass(condition): value; else: value)`)
267+
// https://sass-lang.com/documentation/breaking-changes/if-function/
268+
if (
269+
parentNode &&
270+
parentNode.type === "value-func" &&
271+
parentNode.value === "if"
272+
) {
273+
return false;
274+
}
275+
264276
const parentParentNode = path.grandparent;
265277

266278
// Check open parens contain key/value pair (i.e. `(key: value)` and `(key: (value, other-value)`)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$value: if(sass(false): 1; else: -1);

tests/format/scss/function/__snapshots__/format.test.js.snap

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,36 @@ div {
210210
================================================================================
211211
`;
212212

213+
exports[`18465.scss - {"trailingComma":"es5"} format 1`] = `
214+
====================================options=====================================
215+
parsers: ["scss"]
216+
printWidth: 80
217+
trailingComma: "es5"
218+
| printWidth
219+
=====================================input======================================
220+
$value: if(sass(false): 1; else: -1);
221+
222+
=====================================output=====================================
223+
$value: if(sass(false): 1; else: -1);
224+
225+
================================================================================
226+
`;
227+
228+
exports[`18465.scss - {"trailingComma":"none"} format 1`] = `
229+
====================================options=====================================
230+
parsers: ["scss"]
231+
printWidth: 80
232+
trailingComma: "none"
233+
| printWidth
234+
=====================================input======================================
235+
$value: if(sass(false): 1; else: -1);
236+
237+
=====================================output=====================================
238+
$value: if(sass(false): 1; else: -1);
239+
240+
================================================================================
241+
`;
242+
213243
exports[`arbitrary-arguments.scss - {"trailingComma":"es5"} format 1`] = `
214244
====================================options=====================================
215245
parsers: ["scss"]

0 commit comments

Comments
 (0)