From d7cf639b765d4e85c43ec3738759578572a03f61 Mon Sep 17 00:00:00 2001 From: Rayan Salhab Date: Wed, 27 May 2026 00:29:26 +0300 Subject: [PATCH] Avoid trailing commas for parenthesized SCSS scalars (#19091) Co-authored-by: fisker Cheung --- changelog_unreleased/scss/19091.md | 22 +++++++++++++ src/language-css/utilities/index.js | 11 +++++++ .../__snapshots__/format.test.js.snap | 32 +++++++++++++++++++ tests/format/scss/function/issue-19073.scss | 1 + .../map/__snapshots__/format.test.js.snap | 24 ++++---------- 5 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 changelog_unreleased/scss/19091.md create mode 100644 tests/format/scss/function/issue-19073.scss diff --git a/changelog_unreleased/scss/19091.md b/changelog_unreleased/scss/19091.md new file mode 100644 index 000000000..a85715bfd --- /dev/null +++ b/changelog_unreleased/scss/19091.md @@ -0,0 +1,22 @@ +#### Avoid adding trailing comma to parenthesized scalars (#19091 by @cyphercodes) + + +```scss +// Input +.a { background-color: rgba($color: (#808080), $alpha: 0.9); } + +// Prettier stable +.a { + background-color: rgba( + $color: ( + #808080, + ), + $alpha: 0.9 + ); +} + +// Prettier main +.a { + background-color: rgba($color: (#808080), $alpha: 0.9); +} +``` diff --git a/src/language-css/utilities/index.js b/src/language-css/utilities/index.js index b8595300f..6692d9287 100644 --- a/src/language-css/utilities/index.js +++ b/src/language-css/utilities/index.js @@ -261,6 +261,17 @@ function isSCSSMapItemNode(path, options) { return false; } + // A parenthesized scalar (i.e. `$key: (value)`) isn't a single-item list. + if ( + node.type === "value-paren_group" && + node.open && + node.close && + node.groups.length === 1 && + node.groups[0].type !== "value-comma_group" + ) { + return false; + } + const parentNode = path.parent; // Don't treat SCSS if function arguments as maps (`if(sass(condition): value; else: value)`) diff --git a/tests/format/scss/function/__snapshots__/format.test.js.snap b/tests/format/scss/function/__snapshots__/format.test.js.snap index 2c54ae0c6..e75d11c37 100644 --- a/tests/format/scss/function/__snapshots__/format.test.js.snap +++ b/tests/format/scss/function/__snapshots__/format.test.js.snap @@ -539,3 +539,35 @@ a { ================================================================================ `; + +exports[`issue-19073.scss - {"trailingComma":"es5"} format 1`] = ` +====================================options===================================== +parsers: ["scss"] +trailingComma: "es5" + printWidth: 80 (default) | +=====================================input====================================== +.a { background-color: rgba($color: (#808080), $alpha: 0.9); } + +=====================================output===================================== +.a { + background-color: rgba($color: (#808080), $alpha: 0.9); +} + +================================================================================ +`; + +exports[`issue-19073.scss - {"trailingComma":"none"} format 1`] = ` +====================================options===================================== +parsers: ["scss"] +trailingComma: "none" + printWidth: 80 (default) | +=====================================input====================================== +.a { background-color: rgba($color: (#808080), $alpha: 0.9); } + +=====================================output===================================== +.a { + background-color: rgba($color: (#808080), $alpha: 0.9); +} + +================================================================================ +`; diff --git a/tests/format/scss/function/issue-19073.scss b/tests/format/scss/function/issue-19073.scss new file mode 100644 index 000000000..b9e094480 --- /dev/null +++ b/tests/format/scss/function/issue-19073.scss @@ -0,0 +1 @@ +.a { background-color: rgba($color: (#808080), $alpha: 0.9); } diff --git a/tests/format/scss/map/__snapshots__/format.test.js.snap b/tests/format/scss/map/__snapshots__/format.test.js.snap index 840882e27..4ea8b1c6b 100644 --- a/tests/format/scss/map/__snapshots__/format.test.js.snap +++ b/tests/format/scss/map/__snapshots__/format.test.js.snap @@ -776,9 +776,7 @@ $o-grid-default-config: ( ); $map: ( - key: ( - value, - ), + key: (value), other-key: ( key: other-other-value, ), @@ -927,9 +925,7 @@ $o-grid-default-config: ( ); $map: ( - key: ( - value - ), + key: (value), other-key: ( key: other-other-value ) @@ -1154,9 +1150,7 @@ $map: ( "key": "value", "key": "value", "key": "value", - ): ( - "list", - ), + ): ("list"), ( "list", "list", @@ -1168,9 +1162,7 @@ $map: ( "list", "list", "list", - ): ( - "list", - ), + ): ("list"), ( "list", "list", @@ -1233,9 +1225,7 @@ $map: ( "key": "value", "key": "value", "key": "value" - ): ( - "list" - ), + ): ("list"), ( "list", "list", @@ -1247,9 +1237,7 @@ $map: ( "list", "list", "list" - ): ( - "list" - ), + ): ("list"), ( "list", "list",