From 4f1c4d9eb85fe6afb925cc0d57de7f28e786cabe Mon Sep 17 00:00:00 2001 From: Konv Suu <2583695112@qq.com> Date: Fri, 26 Dec 2025 16:24:35 +0800 Subject: [PATCH] Prevent SCSS math expressions in group treated as map item (#18530) --- changelog_unreleased/scss/18530.md | 18 ++++ src/language-css/utilities/index.js | 5 + tests/format/scss/math/7419.scss | 15 +++ .../math/__snapshots__/format.test.js.snap | 94 +++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 changelog_unreleased/scss/18530.md create mode 100644 tests/format/scss/math/7419.scss diff --git a/changelog_unreleased/scss/18530.md b/changelog_unreleased/scss/18530.md new file mode 100644 index 000000000..55bd402d1 --- /dev/null +++ b/changelog_unreleased/scss/18530.md @@ -0,0 +1,18 @@ +#### Prevent adding trailing comma in math expression within function arguments (#18530 by @kovsu) + + +```scss +// Input +@include container($foo: 2 * ($bar + $baz)); + +// Prettier stable +@include container( + $foo: 2 * + ( + $bar + $baz, + ) +); + +// Prettier main +@include container($foo: 2 * ($bar + $baz)); +``` diff --git a/src/language-css/utilities/index.js b/src/language-css/utilities/index.js index 1d71d087d..b8595300f 100644 --- a/src/language-css/utilities/index.js +++ b/src/language-css/utilities/index.js @@ -292,6 +292,11 @@ function isSCSSMapItemNode(path, options) { // List as value of key inside SCSS map (i.e. `$map: (key: (value other-value other-other-value))`) if (isKeyValuePairInParenGroupNode(parentParentNode)) { + // If there is any operator in the value, it is not a map item + if (parentNode.groups.some((node) => isMathOperatorNode(node))) { + return false; + } + return true; } diff --git a/tests/format/scss/math/7419.scss b/tests/format/scss/math/7419.scss new file mode 100644 index 000000000..308e98725 --- /dev/null +++ b/tests/format/scss/math/7419.scss @@ -0,0 +1,15 @@ +@include container($foo: 2); +@include container($foo: 2 * ($bar)); +@include container($foo: 2 * ($bar + $baz)); + +@include container($foo: 1, $bar: 2); + +@include container($foo: ( + $bar, + $baz +)); + +@include container($foo: ( + $bar + $baz, + $qux * 2 +)); diff --git a/tests/format/scss/math/__snapshots__/format.test.js.snap b/tests/format/scss/math/__snapshots__/format.test.js.snap index 44f1f6726..36601328b 100644 --- a/tests/format/scss/math/__snapshots__/format.test.js.snap +++ b/tests/format/scss/math/__snapshots__/format.test.js.snap @@ -523,3 +523,97 @@ $colors: ( ================================================================================ `; + +exports[`7419.scss - {"trailingComma":"es5"} format 1`] = ` +====================================options===================================== +parsers: ["scss"] +printWidth: 80 +trailingComma: "es5" + | printWidth +=====================================input====================================== +@include container($foo: 2); +@include container($foo: 2 * ($bar)); +@include container($foo: 2 * ($bar + $baz)); + +@include container($foo: 1, $bar: 2); + +@include container($foo: ( + $bar, + $baz +)); + +@include container($foo: ( + $bar + $baz, + $qux * 2 +)); + +=====================================output===================================== +@include container($foo: 2); +@include container($foo: 2 * ($bar)); +@include container($foo: 2 * ($bar + $baz)); + +@include container($foo: 1, $bar: 2); + +@include container( + $foo: ( + $bar, + $baz, + ) +); + +@include container( + $foo: ( + $bar + $baz, + $qux * 2, + ) +); + +================================================================================ +`; + +exports[`7419.scss - {"trailingComma":"none"} format 1`] = ` +====================================options===================================== +parsers: ["scss"] +printWidth: 80 +trailingComma: "none" + | printWidth +=====================================input====================================== +@include container($foo: 2); +@include container($foo: 2 * ($bar)); +@include container($foo: 2 * ($bar + $baz)); + +@include container($foo: 1, $bar: 2); + +@include container($foo: ( + $bar, + $baz +)); + +@include container($foo: ( + $bar + $baz, + $qux * 2 +)); + +=====================================output===================================== +@include container($foo: 2); +@include container($foo: 2 * ($bar)); +@include container($foo: 2 * ($bar + $baz)); + +@include container($foo: 1, $bar: 2); + +@include container( + $foo: ( + $bar, + $baz + ) +); + +@include container( + $foo: ( + $bar + $baz, + $qux * 2 + ) +); + +================================================================================ +`;