Skip to content

Commit

Permalink
Fix removing commas from function arguments in maps (#14089)
Browse files Browse the repository at this point in the history
Co-authored-by: fisker Cheung <lionkay@gmail.com>
  • Loading branch information
sosukesuzuki and fisker committed Jan 6, 2023
1 parent b77d912 commit 0c5d4f3
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 31 deletions.
22 changes: 22 additions & 0 deletions changelog_unreleased/scss/14089.md
@@ -0,0 +1,22 @@
#### Fix removing commas from function arguments in maps (#14089 by @sosukesuzuki)

<!-- prettier-ignore -->
```scss
/* Input */
$foo: map-fn(
(
"#{prop}": inner-fn($first, $second),
)
);

/* Prettier stable */
$foo: map-fn(("#{prop}": inner-fn($first $second)));

/* Prettier main */
$foo: map-fn(
(
"#{prop}": inner-fn($first, $second),
)
);

```
17 changes: 0 additions & 17 deletions src/language-css/parser-postcss.js
Expand Up @@ -122,23 +122,6 @@ 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: [],
Expand Down
29 changes: 15 additions & 14 deletions src/language-css/printer-postcss.js
Expand Up @@ -547,6 +547,7 @@ function genericPrint(path, options, print) {

let insideSCSSInterpolationInString = false;
let didBreak = false;

for (let i = 0; i < node.groups.length; ++i) {
parts.push(printed[i]);

Expand Down Expand Up @@ -594,20 +595,20 @@ function genericPrint(path, options, print) {
}

// Ignore spaces before/after string interpolation (i.e. `"#{my-fn("_")}"`)
const isStartSCSSInterpolationInString =
iNode.type === "value-string" && iNode.value.startsWith("#{");
const isEndingSCSSInterpolationInString =
insideSCSSInterpolationInString &&
iNextNode.type === "value-string" &&
iNextNode.value.endsWith("}");

if (
isStartSCSSInterpolationInString ||
isEndingSCSSInterpolationInString
) {
insideSCSSInterpolationInString = !insideSCSSInterpolationInString;

continue;
if (iNode.type === "value-string" && iNode.quoted) {
const positionOfOpeningInterpolation = iNode.value.lastIndexOf("#{");
const positionOfClosingInterpolation = iNode.value.lastIndexOf("}");
if (
positionOfOpeningInterpolation !== -1 &&
positionOfClosingInterpolation !== -1
) {
insideSCSSInterpolationInString =
positionOfOpeningInterpolation > positionOfClosingInterpolation;
} else if (positionOfOpeningInterpolation !== -1) {
insideSCSSInterpolationInString = true;
} else if (positionOfClosingInterpolation !== -1) {
insideSCSSInterpolationInString = false;
}
}

if (insideSCSSInterpolationInString) {
Expand Down
@@ -1,5 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`function-argument-2.scss format 1`] = `
====================================options=====================================
parsers: ["scss"]
printWidth: 80
| printWidth
=====================================input======================================
$display-breakpoints: map-deep-merge(
(
"sm-only": "only screen and (min-width: #{$map-get + $grid-breakpoints + "hogehoge"}) and (max-width: #{$a})",
"sm-only": "inside a long long long long long long long long long long long long long long string #{call("")}",
"sm-only": "inside a long long long long long long long long long long long long long long string #{$foo} and #{call("")}",
"sm-only": "inside a long long long long long long long long long long long long long long string #{call($a)}",
),
$display-breakpoints
);
@each $name, $hue in $hues {
$map: map.merge(
$map,
(
'#{$prefix}-#{$name}': blend.set($base, $hue: $hue),
)
);
}
=====================================output=====================================
$display-breakpoints: map-deep-merge(
(
"sm-only": "only screen and (min-width: #{$map-get + $grid-breakpoints + "hogehoge"}) and (max-width: #{$a})",
"sm-only":
"inside a long long long long long long long long long long long long long long string #{call("")}",
"sm-only":
"inside a long long long long long long long long long long long long long long string #{$foo} and #{call("")}",
"sm-only":
"inside a long long long long long long long long long long long long long long string #{call($a)}",
),
$display-breakpoints
);
@each $name, $hue in $hues {
$map: map.merge(
$map,
(
"#{$prefix}-#{$name}": blend.set($base, $hue: $hue),
)
);
}
================================================================================
`;

exports[`functional-argument.scss format 1`] = `
====================================options=====================================
parsers: ["scss"]
Expand Down
18 changes: 18 additions & 0 deletions tests/format/scss/map/function-argument/function-argument-2.scss
@@ -0,0 +1,18 @@
$display-breakpoints: map-deep-merge(
(
"sm-only": "only screen and (min-width: #{$map-get + $grid-breakpoints + "hogehoge"}) and (max-width: #{$a})",
"sm-only": "inside a long long long long long long long long long long long long long long string #{call("")}",
"sm-only": "inside a long long long long long long long long long long long long long long string #{$foo} and #{call("")}",
"sm-only": "inside a long long long long long long long long long long long long long long string #{call($a)}",
),
$display-breakpoints
);

@each $name, $hue in $hues {
$map: map.merge(
$map,
(
'#{$prefix}-#{$name}': blend.set($base, $hue: $hue),
)
);
}

0 comments on commit 0c5d4f3

Please sign in to comment.