Skip to content

Commit

Permalink
fix(css_formatter): don't ident css selector when has leading comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fireairforce committed Oct 30, 2024
1 parent d2b3b7f commit 4bf2149
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

### Formatter

### Bug fixes

- Fix [#4121](https://github.com/biomejs/biome/issues/4326), don't ident css selector when has leading comments. Contributed by @fireairforce

### JavaScript APIs

### Linter
Expand Down
37 changes: 26 additions & 11 deletions crates/biome_css_formatter/src/css/lists/relative_selector_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,34 @@ impl FormatRule<CssRelativeSelectorList> for FormatCssRelativeSelectorList {
let mut joiner = f.join_with(&separator);

for formatted in node.format_separated(",") {
// Each selector gets `indent` added in case it breaks over multiple
// lines. The break is added here rather than in each selector both
// for simplicity and to avoid recursively adding indents when
// selectors are nested within other rules. The group is then added
// around the indent to ensure that it tries using a flat layout
// first and only expands when the single selector can't fit the line.
//
// For example, a selector like `div span a` is structured like
// `[div, [span, [a]]]`, so `a` would end up double-indented if it
// was handled by the selector rather than here.
joiner.entry(&group(&indent(&formatted)));
let has_comments_before = formatted.node()?
.as_css_relative_selector()
.and_then(|relative_selector| relative_selector.selector()?.as_css_compound_selector())
.and_then(|computed_selector| computed_selector.simple_selector())
.and_then(|simple_selector| simple_selector.as_css_type_selector())
.and_then(|type_selector| type_selector.ident()?.value_token()?.has_leading_comments())
.unwrap_or_default();

if has_comments_before {
// Computed Selector which contains a leading comments should be formatted without indent.
joiner.entry(&group(&formatted));
} else {
// Each selector gets `indent` added in case it breaks over multiple
// lines. The break is added here rather than in each selector both
// for simplicity and to avoid recursively adding indents when
// selectors are nested within other rules. The group is then added
// around the indent to ensure that it tries using a flat layout
// first and only expands when the single selector can't fit the line.
//
// For example, a selector like `div span a` is structured like
// `[div, [span, [a]]]`, so `a` would end up double-indented if it
// was handled by the selector rather than here.
joiner.entry(&group(&indent(&formatted)));
}
}

joiner.finish()
}
}


2 changes: 1 addition & 1 deletion crates/biome_css_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn quick_test() {
@charset "UTF-8";
"#;
let parse = parse_css(src, CssParserOptions::default());
println!("{parse:#?}");
// println!("{parse:#?}");

let options = CssFormatOptions::default()
.with_line_width(LineWidth::try_from(80).unwrap())
Expand Down
16 changes: 16 additions & 0 deletions crates/biome_css_formatter/tests/specs/css/declaration_list.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,20 @@ a {

a {
color: red;;;;
}

.with-comments {
/* hello */
a,
/* world */
button {
color: blue;
}
}

.without-comments {
a,
button {
color: blue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ a {
a {
color: red;;;;
}

.with-comments {
/* hello */
a,
/* world */
button {
color: blue;
}
}

.without-comments {
a,
button {
color: blue;
}
}
```


Expand Down Expand Up @@ -103,4 +119,20 @@ a {
a {
color: red;
}

.with-comments {
/* hello */
a,
/* world */
button {
color: blue;
}
}

.without-comments {
a,
button {
color: blue;
}
}
```

0 comments on commit 4bf2149

Please sign in to comment.