Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly format Sass module configuration values #11516

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));