Skip to content

Commit

Permalink
Properly format Sass module configuration values
Browse files Browse the repository at this point in the history
  • Loading branch information
niksy committed Aug 2, 2022
1 parent cd9955f commit 6e7530c
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
22 changes: 22 additions & 0 deletions changelog_unreleased/scss/11516.md
@@ -0,0 +1,22 @@
#### Properly format Sass module configuration values (#11516 by @niksy)

<!-- prettier-ignore -->
```scss
// Input
@use "library" with ($black: #222, $border-radius: 0.1rem, $font-family: "Helvetica, sans-serif", $counters: (1, 2, 3));

// Prettier stable
@use "library" with ($black: #222, $border-radius: 0.1rem, $font-family: "Helvetica, sans-serif", $counters: (1, 2, 3));

// Prettier main
@use "library" with (
$black: #222,
$border-radius: 0.1rem,
$font-family: "Helvetica, sans-serif",
$list: (
1,
2,
3,
)
);
```
5 changes: 5 additions & 0 deletions src/language-css/printer-postcss.js
Expand Up @@ -72,6 +72,7 @@ const {
isColorAdjusterFuncNode,
lastLineHasInlineComment,
isAtWordPlaceholderNode,
isSCSSModuleConfigurationProvided,
isConfigurationNode,
isParenGroupNode,
} = require("./utils/index.js");
Expand Down Expand Up @@ -849,6 +850,10 @@ function genericPrint(path, options, print) {
return group(fill(parts));
}

if (isSCSSModuleConfigurationProvided(path)) {
return group(indent(fill(parts)));
}

return group(indent(fill(parts)));
}
case "value-paren_group": {
Expand Down
16 changes: 16 additions & 0 deletions src/language-css/utils/index.js
Expand Up @@ -118,6 +118,21 @@ function insideURLFunctionInImportAtRuleNode(path) {
);
}

function isSCSSModuleConfigurationProvided(path) {
const node = path.getValue();
const atRuleAncestorNode = getAncestorNode(path, "css-atrule");
const { length } = node.groups;

return (
atRuleAncestorNode &&
(atRuleAncestorNode.name === "use" ||
atRuleAncestorNode.name === "forward") &&
node.groups[length - 1].type === "value-paren_group" &&
node.groups[length - 2].type === "value-word" &&
node.groups[length - 2].value === "with"
);
}

function isURLFunctionNode(node) {
return node.type === "value-func" && node.value.toLowerCase() === "url";
}
Expand Down Expand Up @@ -441,4 +456,5 @@ module.exports = {
isAtWordPlaceholderNode,
isConfigurationNode,
isParenGroupNode,
isSCSSModuleConfigurationProvided,
};
73 changes: 73 additions & 0 deletions tests/format/scss/print-width/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,73 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`print-width.scss - {"printWidth":40} format 1`] = `
====================================options=====================================
parsers: ["scss"]
printWidth: 40
| printWidth
=====================================input======================================
@use "library" with ($black: #222, $border-radius: 0.1rem, $font-family: "Helvetica, sans-serif", $list: (1, 2, 3));
@forward "library" with ($black: #222, $border-radius: 0.1rem, $font-family: "Helvetica, sans-serif", $list: (1, 2, 3));
=====================================output=====================================
@use "library" with (
$black: #222,
$border-radius: 0.1rem,
$font-family: "Helvetica, sans-serif",
$list: (
1,
2,
3,
)
);
@forward "library" with (
$black: #222,
$border-radius: 0.1rem,
$font-family: "Helvetica, sans-serif",
$list: (
1,
2,
3,
)
);
================================================================================
`;

exports[`print-width.scss format 1`] = `
====================================options=====================================
parsers: ["scss"]
printWidth: 80
| printWidth
=====================================input======================================
@use "library" with ($black: #222, $border-radius: 0.1rem, $font-family: "Helvetica, sans-serif", $list: (1, 2, 3));
@forward "library" with ($black: #222, $border-radius: 0.1rem, $font-family: "Helvetica, sans-serif", $list: (1, 2, 3));
=====================================output=====================================
@use "library" with (
$black: #222,
$border-radius: 0.1rem,
$font-family: "Helvetica, sans-serif",
$list: (
1,
2,
3,
)
);
@forward "library" with (
$black: #222,
$border-radius: 0.1rem,
$font-family: "Helvetica, sans-serif",
$list: (
1,
2,
3,
)
);
================================================================================
`;
2 changes: 2 additions & 0 deletions tests/format/scss/print-width/jsfmt.spec.js
@@ -0,0 +1,2 @@
run_spec(__dirname, ["scss"]);
run_spec(__dirname, ["scss"], { printWidth: 40 });
3 changes: 3 additions & 0 deletions tests/format/scss/print-width/print-width.scss
@@ -0,0 +1,3 @@
@use "library" with ($black: #222, $border-radius: 0.1rem, $font-family: "Helvetica, sans-serif", $list: (1, 2, 3));

@forward "library" with ($black: #222, $border-radius: 0.1rem, $font-family: "Helvetica, sans-serif", $list: (1, 2, 3));

0 comments on commit 6e7530c

Please sign in to comment.