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

fix(biome_css_formatter): update css format style #4476

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

eryue0220
Copy link
Contributor

@eryue0220 eryue0220 commented Nov 6, 2024

Summary

Close #4328
Close #4298

Test Plan

@github-actions github-actions bot added A-Formatter Area: formatter L-CSS Language: CSS labels Nov 6, 2024
Copy link

codspeed-hq bot commented Nov 6, 2024

CodSpeed Performance Report

Merging #4476 will degrade performances by 9.61%

Comparing eryue0220:fix/css-formater-update (87c39be) with main (c4cb4d0)

Summary

❌ 6 regressions
✅ 91 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main eryue0220:fix/css-formater-update Change
css_formatter[bootstrap_18416142857265205439.css] 121.9 ms 133.8 ms -8.89%
css_formatter[bulma_5641719244145477318.css] 388.3 ms 429.6 ms -9.61%
css_formatter[foundation_11602414662825430680.css] 80.2 ms 87.7 ms -8.56%
css_formatter[full_5814491140539129161.css] 1.3 s 1.5 s -9.58%
css_formatter[materialize_5526761731747548557.css] 88.1 ms 95.2 ms -7.42%
css_formatter[pure_9395922602181450299.css] 9.8 ms 10.4 ms -6.04%

@eryue0220 eryue0220 marked this pull request as ready for review November 6, 2024 10:42
@ematipico
Copy link
Member

Thank you @eryue0220 for the contribution. Did you take inspiration from Prettier to apply the fix? If so, can you point us to the original code?

Also, there's an evident regression in performance, which isn't good, usually. We will help you with that, however it would be great if you could at least explain to us the logic of your algorithm.

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should look after the regressions. Once we have a clear idea of what this PR does, we can help with that. For now, I left some preliminary feedback

@@ -1,5 +1,6 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
assertion_line: 212
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you see this line,it means you're using an old version of cargo-insta. Run just upgrade-tools to update the tools we use internally.

@@ -12,6 +12,45 @@ impl FormatNodeRule<CssGenericProperty> for FormatCssGenericProperty {
value,
} = node.as_fields();

let is_dash_identity = name.clone().unwrap().as_css_dashed_identifier().is_some();

let has_comma = value.iter().any(|v| v.text().eq(","));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for a comma (,) like this isn't correct. You should rely on the CST/AST, so you don't need to transform a node into a test.

You can use the playground to surgically understand how to query it: https://biomejs.dev/playground/?lintRules=all&files.main.css=aAB0AG0AbAAgAHsACgAJAGYAbwBuAHQALQBmAGEAbQBpAGwAeQA6ACAAcwB5AHMAdABlAG0ALQB1AGkALAAgAC0AYQBwAHAAbABlAC0AcwB5AHMAdABlAG0AOwAKAH0A

(Click on the comma on the left side, and it should point to the relative node on the right, in the syntax tab)

@@ -12,6 +12,45 @@ impl FormatNodeRule<CssGenericProperty> for FormatCssGenericProperty {
value,
} = node.as_fields();

let is_dash_identity = name.clone().unwrap().as_css_dashed_identifier().is_some();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use unwrap(); you rarely need to use this method because it panics at runtime. In your case you can use CssDashedIdentifer::can_cast(name.syntax().kind());

name.format(),
colon_token.format(),
indent(&format_with(|f| {
let last = value.last().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No unwrap

Comment on lines +28 to +44
for (idx, v) in value.iter().enumerate() {
if idx == 0 {
write!(f, [soft_line_break_or_space()])?;
}

if v.text().eq(",") {
write!(f, [v.format(), hard_line_break()])?;
} else if let Some(next) = value.iter().nth(idx + 1) {
if next.text().eq(",") || v == last {
write!(f, [v.format()])?;
} else {
write!(f, [v.format(), space()])?;
}
} else {
write!(f, [v.format()])?;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not really sure I understand what's happening here, so I am waiting for your to update the PR description and explain how the algorithm works.

@@ -1,5 +1,6 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
assertion_line: 212
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update cargo-insta and revert these changes

@eryue0220
Copy link
Contributor Author

Thank you @eryue0220 for the contribution. Did you take inspiration from Prettier to apply the fix? If so, can you point us to the original code?

Also, there's an evident regression in performance, which isn't good, usually. We will help you with that, however it would be great if you could at least explain to us the logic of your algorithm.

Currently, I just implemented this by my own. The main idea is just to check if every css property value includes comma or not, if true then just inserts a line_break. And the exception is the dash property key, then it should break according to the max-width, which is following the prettier result.

I agreed, I'll check the prettier implementation and try to fix the performance problem.

@eryue0220 eryue0220 marked this pull request as draft November 7, 2024 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Formatter Area: formatter L-CSS Language: CSS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📝 CSS legibility for comma-separated features CSS line wrapping
2 participants