Skip to content

Commit 524dbca

Browse files
authored
feat(css_formatter): expand compound SCSS variable lists (#10577)
1 parent 80449d6 commit 524dbca

7 files changed

Lines changed: 129 additions & 555 deletions

File tree

crates/biome_css_formatter/src/utils/scss_list_layout.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use crate::utils::scss_closing_comments::{
44
ClosingCommentSpacing, owns_include_closing_comments, write_include_closing_comments,
55
};
66
use biome_css_syntax::{
7-
ScssExpression, ScssListExpression, ScssListExpressionElementList, ScssListExpressionFields,
8-
ScssModuleConfiguration, ScssParenthesizedExpression, is_in_scss_include_arguments,
7+
ScssExpression, ScssExpressionItemList, ScssListExpression, ScssListExpressionElement,
8+
ScssListExpressionElementList, ScssListExpressionFields, ScssModuleConfiguration,
9+
ScssParenthesizedExpression, ScssVariableDeclaration, is_in_scss_include_arguments,
910
is_scss_map_key, is_scss_map_outer_parenthesized_value_list,
1011
scss_include_keyword_argument_owner, single_expression_item, unwrap_single_expression_item,
1112
};
@@ -71,6 +72,19 @@ impl<'a> ScssListLayout<'a> {
7172
return write!(f, [group(&format_args![elements.format()])]);
7273
}
7374

75+
if is_compound_variable_list(self.node, &elements) {
76+
// Print `$buttonConfig:
77+
// "save" 50px,
78+
// "cancel" 50px;`.
79+
return write!(
80+
f,
81+
[
82+
group(&indent(&format_args![soft_line_break(), elements.format()]))
83+
.should_expand(true)
84+
]
85+
);
86+
}
87+
7488
write!(
7589
f,
7690
[group(&indent(&format_args![
@@ -233,6 +247,38 @@ fn is_include_keyword_list_value_expanded_by_comments(
233247
has_trailing_comment || has_keyword_closing_comments
234248
}
235249

250+
/// Detects `$buttonConfig: "save" 50px, "cancel" 50px;`.
251+
fn is_compound_variable_list(
252+
node: &ScssListExpression,
253+
elements: &ScssListExpressionElementList,
254+
) -> bool {
255+
is_scss_variable_value_list(node) && elements.len() > 1 && has_compound_list_element(elements)
256+
}
257+
258+
/// Checks that the list is the value in `$buttonConfig: "save" 50px, "cancel" 50px;`.
259+
fn is_scss_variable_value_list(node: &ScssListExpression) -> bool {
260+
node.parent::<ScssExpressionItemList>()
261+
.and_then(|items| items.parent::<ScssExpression>())
262+
.and_then(|expression| expression.parent::<ScssVariableDeclaration>())
263+
.is_some()
264+
}
265+
266+
/// Checks for list items like `"save" 50px`.
267+
fn has_compound_list_element(elements: &ScssListExpressionElementList) -> bool {
268+
elements
269+
.iter()
270+
.any(|element| element.as_ref().is_ok_and(is_compound_list_element))
271+
}
272+
273+
/// Checks whether `"save" 50px` has multiple values.
274+
fn is_compound_list_element(element: &ScssListExpressionElement) -> bool {
275+
element.value().ok().is_some_and(|value| {
276+
value
277+
.as_scss_expression()
278+
.is_some_and(|expression| expression.items().len() > 1)
279+
})
280+
}
281+
236282
/// Detects the list in `@use "x" with ($family: (a, b))`.
237283
fn is_module_configuration_parenthesized_list_value(node: &ScssListExpression) -> bool {
238284
let Some(parenthesized) = node

crates/biome_css_formatter/tests/specs/prettier/scss/interpolation/3943.scss.snap

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: crates/biome_formatter_test/src/snapshot_builder.rs
3+
assertion_line: 249
34
info: scss/interpolation/3943.scss
45
---
56

@@ -149,18 +150,6 @@ $icons: wifi "\600", wifi-hotspot "\601", weather "\602";
149150
content: "I have #{8 + 2} books on SASS!";
150151
border: #{$var} #{$var} #{$var};
151152
filter: #{$var}#{$var}#{$var};
152-
@@ -66,10 +67,7 @@
153-
}
154-
}
155-
156-
-$icons:
157-
- wifi "\600",
158-
- wifi-hotspot "\601",
159-
- weather "\602";
160-
+$icons: wifi "\600", wifi-hotspot "\601", weather "\602";
161-
162-
@each $icon in $icons {
163-
.icon-#{nth($icon, 1)},
164153
```
165154
166155
# Output
@@ -235,7 +224,10 @@ $foundation-dir: "foundation";
235224
}
236225
}
237226
238-
$icons: wifi "\600", wifi-hotspot "\601", weather "\602";
227+
$icons:
228+
wifi "\600",
229+
wifi-hotspot "\601",
230+
weather "\602";
239231
240232
@each $icon in $icons {
241233
.icon-#{nth($icon, 1)},

0 commit comments

Comments
 (0)