Skip to content

Commit

Permalink
fix(formatter): don't move comments in grid-template (#4518)
Browse files Browse the repository at this point in the history
  • Loading branch information
fireairforce authored Nov 15, 2024
1 parent 42da14f commit 63b213b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
- Fix [#4121](https://github.com/biomejs/biome/issues/4326), don't ident a CSS selector when has leading comments. Contributed by @fireairforce
- Fix [#4334](https://github.com/biomejs/biome/issues/4334), don't insert trailing comma on type import statement. Contributed by @fireairforce
- Fix [#3229](https://github.com/biomejs/biome/issues/3229), where Biome wasn't idempotent when block comments were placed inside compound selectors. Contributed by @ematipico
- Fix [#4026](https://github.com/biomejs/biome/issues/4026), don't move comments in `grid-template`. Contributed by @fireairforce

### JavaScript APIs

### Linter
Expand Down
11 changes: 9 additions & 2 deletions crates/biome_css_formatter/src/comments.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::prelude::*;
use biome_css_syntax::{
AnyCssDeclarationName, CssComplexSelector, CssFunction, CssIdentifier, CssLanguage, TextLen,
AnyCssDeclarationName, CssComplexSelector, CssFunction, CssIdentifier, CssLanguage,
CssSyntaxKind, TextLen,
};
use biome_diagnostics::category;
use biome_formatter::comments::{
Expand Down Expand Up @@ -109,7 +110,13 @@ fn handle_declaration_name_comment(
) -> CommentPlacement<CssLanguage> {
match comment.preceding_node() {
Some(following_node) if AnyCssDeclarationName::can_cast(following_node.kind()) => {
CommentPlacement::leading(following_node.clone(), comment)
if following_node.parent().map_or(false, |p| {
p.kind() == CssSyntaxKind::CSS_GENERIC_COMPONENT_VALUE_LIST
}) {
CommentPlacement::Default(comment)
} else {
CommentPlacement::leading(following_node.clone(), comment)
}
}
_ => CommentPlacement::Default(comment),
}
Expand Down
14 changes: 8 additions & 6 deletions crates/biome_css_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ mod language {
#[test]
// use this test check if your snippet prints as you wish, without using a snapshot
fn quick_test() {
let src = r#"foo
/* a comment */
.aRule {
color: red;
}
let src = r#"
#grid {
grid-template-columns:
1fr /* first comment */
auto /* second comment */
60px /* fourth comment */
2fr /* third comment */
}
"#;
let parse = parse_css(src, CssParserOptions::default());
println!("{parse:#?}");
Expand Down
15 changes: 15 additions & 0 deletions crates/biome_css_formatter/tests/specs/css/properties/grid.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,18 @@
grid-template-columns: [first nav-start] 150px [main-start] 1fr [last];
grid-template-rows: [first header-start] 50px [main-start] 1fr [footer-start] 50px [last];
}

#grid {
grid-template-columns:
1fr /* first comment */
auto /* second comment */
2fr /* third comment */
}

div {
grid-template-columns:
1fr /* first comment */
auto /* second comment */
40px /* third comment */
2fr /* fourth comment */
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ info: css/properties/grid.css
grid-template-rows: [first header-start] 50px [main-start] 1fr [footer-start] 50px [last];
}

#grid {
grid-template-columns:
1fr /* first comment */
auto /* second comment */
2fr /* third comment */
}

div {
grid-template-columns:
1fr /* first comment */
auto /* second comment */
40px /* third comment */
2fr /* fourth comment */
}
```


Expand Down Expand Up @@ -45,6 +59,21 @@ Quote style: Double Quotes
grid-template-columns: [first nav-start] 150px [main-start] 1fr [last];
grid-template-rows: [first header-start] 50px [main-start] 1fr [footer-start] 50px [last];
}

#grid {
grid-template-columns:
1fr /* first comment */
auto /* second comment */
2fr; /* third comment */
}

div {
grid-template-columns:
1fr /* first comment */
auto /* second comment */
40px /* third comment */
2fr; /* fourth comment */
}
```

# Lines exceeding max width of 80 characters
Expand Down

0 comments on commit 63b213b

Please sign in to comment.