diff --git a/changelog_unreleased/scss/9184.md b/changelog_unreleased/scss/9184.md new file mode 100644 index 000000000000..de3cace7df77 --- /dev/null +++ b/changelog_unreleased/scss/9184.md @@ -0,0 +1,35 @@ +#### Fix SCSS map in arguments (#9184 by @agamkrbit) + + +```scss +// Input +$display-breakpoints: map-deep-merge( + ( + "print-only": "only print", + "screen-only": "only screen", + "xs-only": "only screen and (max-width: #{map-get($grid-breakpoints, "sm")-1})", + ), + $display-breakpoints +); + +// Prettier stable +$display-breakpoints: map-deep-merge( + ( + "print-only": "only print", + "screen-only": "only screen", + "xs-only": "only screen and (max-width: #{map-get($grid-breakpoints, " sm + ")-1})", + ), + $display-breakpoints +); + +// Prettier main +$display-breakpoints: map-deep-merge( + ( + "print-only": "only print", + "screen-only": "only screen", + "xs-only": "only screen and (max-width: #{map-get($grid-breakpoints, "sm")-1})", + ), + $display-breakpoints +); +``` diff --git a/src/language-css/parser-postcss.js b/src/language-css/parser-postcss.js index 46c696fae13e..a0014a2b6779 100644 --- a/src/language-css/parser-postcss.js +++ b/src/language-css/parser-postcss.js @@ -122,6 +122,23 @@ function parseValueNode(valueNode, options) { parenGroupStack.pop(); parenGroup = getLast(parenGroupStack); } else if (node.type === "comma") { + if (commaGroup.groups.length > 1) { + for (const group of commaGroup.groups) { + // if css interpolation + if ( + group.value && + typeof group.value === "string" && + group.value.includes("#{") + ) { + commaGroup.groups = [ + stringifyNode({ + groups: commaGroup.groups, + }).trim(), + ]; + break; + } + } + } parenGroup.groups.push(commaGroup); commaGroup = { groups: [], diff --git a/tests/format/scss/map/function-argument/__snapshots__/jsfmt.spec.js.snap b/tests/format/scss/map/function-argument/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..0ea75e6bb2b6 --- /dev/null +++ b/tests/format/scss/map/function-argument/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,146 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`functional-argument.scss format 1`] = ` +====================================options===================================== +parsers: ["scss"] +printWidth: 80 + | printWidth +=====================================input====================================== +//simple test +$display-breakpoints: map-deep-merge( + ( + "print-only": "only print", + "screen-only": "only screen", + "xs-only": "only screen and (max-width: #{map-get($grid-breakpoints, "sm") - 1})", + "sm-only": "only screen and (min-width: #{map-get($grid-breakpoints, "sm")}) and (max-width: #{map-get($grid-breakpoints, "md") - 1})", + ), + $display-breakpoints +); + + +//list test +@each +$size +in +$sizes { +.icon +#{$size} +{ +border +: +"#{$size + "px"}" +solid +red; +} +} + +@each +$size +in +$sizes { +.icon-#{$size} +{ +border +: +"#{$size + "px"}" +solid +red; +} +} + +//map test +$font-weights +: +("regular": 400 +, +"medium": 500, +"bold": +700); +@each +$name, +$boldness +in +$icons { +.text +#{ +$name +} { +color: red; +font-weight: +"#{$boldness}" +} +} + +$font-weights +: +("regular": 400 +, +"medium": 500, +"bold": +700); +@each +$name, +$boldness +in +$icons { +.text-#{ +$name +} { +color: red; +font-weight: +"#{$boldness}" +} +} + +=====================================output===================================== +//simple test +$display-breakpoints: map-deep-merge( + ( + "print-only": "only print", + "screen-only": "only screen", + "xs-only": "only screen and (max-width: #{map-get($grid-breakpoints, "sm") - 1})", + "sm-only": "only screen and (min-width: #{map-get($grid-breakpoints, "sm")}) and (max-width: #{map-get($grid-breakpoints, "md") - 1})", + ), + $display-breakpoints +); + +//list test +@each $size in $sizes { + .icon #{$size} { + border: "#{$size + "px"}" solid red; + } +} + +@each $size in $sizes { + .icon-#{$size} { + border: "#{$size + "px"}" solid red; + } +} + +//map test +$font-weights: ( + "regular": 400, + "medium": 500, + "bold": 700, +); +@each $name, $boldness in $icons { + .text #{ $name } { + color: red; + font-weight: "#{$boldness}"; + } +} + +$font-weights: ( + "regular": 400, + "medium": 500, + "bold": 700, +); +@each $name, $boldness in $icons { + .text-#{ $name } { + color: red; + font-weight: "#{$boldness}"; + } +} + +================================================================================ +`; diff --git a/tests/format/scss/map/function-argument/functional-argument.scss b/tests/format/scss/map/function-argument/functional-argument.scss new file mode 100644 index 000000000000..150afc22eb8b --- /dev/null +++ b/tests/format/scss/map/function-argument/functional-argument.scss @@ -0,0 +1,85 @@ +//simple test +$display-breakpoints: map-deep-merge( + ( + "print-only": "only print", + "screen-only": "only screen", + "xs-only": "only screen and (max-width: #{map-get($grid-breakpoints, "sm") - 1})", + "sm-only": "only screen and (min-width: #{map-get($grid-breakpoints, "sm")}) and (max-width: #{map-get($grid-breakpoints, "md") - 1})", + ), + $display-breakpoints +); + + +//list test +@each +$size +in +$sizes { +.icon +#{$size} +{ +border +: +"#{$size + "px"}" +solid +red; +} +} + +@each +$size +in +$sizes { +.icon-#{$size} +{ +border +: +"#{$size + "px"}" +solid +red; +} +} + +//map test +$font-weights +: +("regular": 400 +, +"medium": 500, +"bold": +700); +@each +$name, +$boldness +in +$icons { +.text +#{ +$name +} { +color: red; +font-weight: +"#{$boldness}" +} +} + +$font-weights +: +("regular": 400 +, +"medium": 500, +"bold": +700); +@each +$name, +$boldness +in +$icons { +.text-#{ +$name +} { +color: red; +font-weight: +"#{$boldness}" +} +} diff --git a/tests/format/scss/map/function-argument/jsfmt.spec.js b/tests/format/scss/map/function-argument/jsfmt.spec.js new file mode 100644 index 000000000000..539bde0869da --- /dev/null +++ b/tests/format/scss/map/function-argument/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["scss"]);