Skip to content

Commit 0059cd9

Browse files
authored
refactor(css_parser): flatten AnyCssDeclarationWithSemicolon (#6912) (#6879)
1 parent 5ff50f8 commit 0059cd9

File tree

16 files changed

+319
-253
lines changed

16 files changed

+319
-253
lines changed

.changeset/chilly-lions-rule.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Refactor: remove one level of indirection for CSS declarations with semicolon
6+
Previously, accessing a declaration from a list required an extra step:
7+
8+
```rust
9+
item
10+
.as_any_css_declaration_with_semicolon()
11+
.as_css_declaration_with_semicolon()
12+
```
13+
14+
Now, it can be done directly with:
15+
16+
```rust
17+
item.as_css_declaration_with_semicolon()
18+
```

crates/biome_css_analyze/src/assist/source/use_sorted_properties.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,8 @@ impl RecessOrderMember {
248248
AnyCssRule::CssNestedQualifiedRule(_) => NodeKindOrder::NestedRuleOrAtRule,
249249
AnyCssRule::CssQualifiedRule(_) => NodeKindOrder::UnknownKind,
250250
},
251-
AnyCssDeclarationOrRule::AnyCssDeclarationWithSemicolon(any_decl_with_semicolon) => {
252-
let Some(decl_with_semicolon) =
253-
any_decl_with_semicolon.as_css_declaration_with_semicolon()
254-
else {
255-
return NodeKindOrder::UnknownKind;
256-
};
251+
AnyCssDeclarationOrRule::CssEmptyDeclaration(_) => NodeKindOrder::UnknownKind,
252+
AnyCssDeclarationOrRule::CssDeclarationWithSemicolon(decl_with_semicolon) => {
257253
let Some(decl) = decl_with_semicolon.declaration().ok() else {
258254
return NodeKindOrder::UnknownKind;
259255
};
@@ -284,8 +280,7 @@ impl RecessOrderMember {
284280
pub fn property_index(&self) -> usize {
285281
let Some(prop_text) = &self
286282
.0
287-
.as_any_css_declaration_with_semicolon()
288-
.and_then(|decl| decl.as_css_declaration_with_semicolon())
283+
.as_css_declaration_with_semicolon()
289284
.and_then(css_declaration_to_prop_text)
290285
else {
291286
return usize::MAX;
@@ -300,8 +295,7 @@ impl RecessOrderMember {
300295
pub fn vendor_prefix_index(&self) -> usize {
301296
let Some(prop_text) = &self
302297
.0
303-
.as_any_css_declaration_with_semicolon()
304-
.and_then(|decl| decl.as_css_declaration_with_semicolon())
298+
.as_css_declaration_with_semicolon()
305299
.and_then(css_declaration_to_prop_text)
306300
else {
307301
return usize::MAX;
@@ -408,8 +402,7 @@ fn contains_shorthand_after_longhand(nodes: &[AnyCssDeclarationOrRule]) -> bool
408402
// Starting from the bottom, when we see a shorthand property, record the set of longhand properties that are no longer allowed to appear above it.
409403
for node in nodes.iter().rev() {
410404
let Some(prop_text) = &node
411-
.as_any_css_declaration_with_semicolon()
412-
.and_then(|decl| decl.as_css_declaration_with_semicolon())
405+
.as_css_declaration_with_semicolon()
413406
.and_then(css_declaration_to_prop_text)
414407
else {
415408
continue;
@@ -442,8 +435,7 @@ fn contains_shorthand_after_longhand(nodes: &[AnyCssDeclarationOrRule]) -> bool
442435
fn contains_unknown_property(nodes: &[AnyCssDeclarationOrRule]) -> bool {
443436
for node in nodes.iter() {
444437
let Some(prop_text) = &node
445-
.as_any_css_declaration_with_semicolon()
446-
.and_then(|decl| decl.as_css_declaration_with_semicolon())
438+
.as_css_declaration_with_semicolon()
447439
.and_then(css_declaration_to_prop_text)
448440
else {
449441
continue;

crates/biome_css_analyze/src/lint/correctness/no_invalid_grid_areas.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ impl Rule for NoInvalidGridAreas {
9191
.into_iter()
9292
.filter_map(|item| {
9393
let binding = item
94-
.as_any_css_declaration_with_semicolon()?
9594
.as_css_declaration_with_semicolon()?
9695
.declaration()
9796
.ok()?

crates/biome_css_factory/src/generated/node_factory.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/biome_css_factory/src/generated/syntax_factory.rs

Lines changed: 3 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! This is a generated file. Don't modify it by hand! Run 'cargo codegen formatter' to re-generate the file.
2+
3+
use crate::prelude::*;
4+
use biome_css_syntax::AnyCssDeclaration;
5+
#[derive(Debug, Clone, Default)]
6+
pub(crate) struct FormatAnyCssDeclaration;
7+
impl FormatRule<AnyCssDeclaration> for FormatAnyCssDeclaration {
8+
type Context = CssFormatContext;
9+
fn fmt(&self, node: &AnyCssDeclaration, f: &mut CssFormatter) -> FormatResult<()> {
10+
match node {
11+
AnyCssDeclaration::CssDeclarationWithSemicolon(node) => node.format().fmt(f),
12+
AnyCssDeclaration::CssEmptyDeclaration(node) => node.format().fmt(f),
13+
}
14+
}
15+
}

crates/biome_css_formatter/src/css/any/declaration_or_at_rule.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ impl FormatRule<AnyCssDeclarationOrAtRule> for FormatAnyCssDeclarationOrAtRule {
88
type Context = CssFormatContext;
99
fn fmt(&self, node: &AnyCssDeclarationOrAtRule, f: &mut CssFormatter) -> FormatResult<()> {
1010
match node {
11-
AnyCssDeclarationOrAtRule::AnyCssDeclarationWithSemicolon(node) => node.format().fmt(f),
1211
AnyCssDeclarationOrAtRule::CssAtRule(node) => node.format().fmt(f),
12+
AnyCssDeclarationOrAtRule::CssDeclarationWithSemicolon(node) => node.format().fmt(f),
13+
AnyCssDeclarationOrAtRule::CssEmptyDeclaration(node) => node.format().fmt(f),
1314
}
1415
}
1516
}

crates/biome_css_formatter/src/css/any/declaration_or_rule.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ impl FormatRule<AnyCssDeclarationOrRule> for FormatAnyCssDeclarationOrRule {
88
type Context = CssFormatContext;
99
fn fmt(&self, node: &AnyCssDeclarationOrRule, f: &mut CssFormatter) -> FormatResult<()> {
1010
match node {
11-
AnyCssDeclarationOrRule::AnyCssDeclarationWithSemicolon(node) => node.format().fmt(f),
1211
AnyCssDeclarationOrRule::AnyCssRule(node) => node.format().fmt(f),
1312
AnyCssDeclarationOrRule::CssBogus(node) => node.format().fmt(f),
13+
AnyCssDeclarationOrRule::CssDeclarationWithSemicolon(node) => node.format().fmt(f),
14+
AnyCssDeclarationOrRule::CssEmptyDeclaration(node) => node.format().fmt(f),
1415
AnyCssDeclarationOrRule::CssMetavariable(node) => node.format().fmt(f),
1516
}
1617
}

crates/biome_css_formatter/src/css/any/declaration_with_semicolon.rs

Lines changed: 0 additions & 17 deletions
This file was deleted.

crates/biome_css_formatter/src/css/any/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ pub(crate) mod container_style_in_parens;
1414
pub(crate) mod container_style_or_combinable_query;
1515
pub(crate) mod container_style_query;
1616
pub(crate) mod custom_identifier;
17+
pub(crate) mod declaration;
1718
pub(crate) mod declaration_block;
1819
pub(crate) mod declaration_name;
1920
pub(crate) mod declaration_or_at_rule;
2021
pub(crate) mod declaration_or_at_rule_block;
2122
pub(crate) mod declaration_or_rule;
2223
pub(crate) mod declaration_or_rule_block;
23-
pub(crate) mod declaration_with_semicolon;
2424
pub(crate) mod dimension;
2525
pub(crate) mod document_matcher;
2626
pub(crate) mod expression;

0 commit comments

Comments
 (0)