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(formatter): prevent line break when comment follows end of attributes, irrespective of bracketSameLine value #3534

Merged
merged 6 commits into from
Jul 29, 2024

Conversation

satojin219
Copy link
Contributor

Summary

// before format
<div
  sx={{
    position: 'absolute'
  }}
  // comment
>
  test
</div>

<div
  sx={{
    position: 'absolute'
  }} // comment
>
  test
</div>
// after format
```ts
<div
  sx={{
    position: 'absolute'
  }} 
>
  // comment
  test
</div>

There was a bug where when the bracket same line option was true and there was a comment after an attribute, that comment would be formatted as the value of the element.
When the comment is //, putting the bracket on the same line would also comment out the closing >, so I thought that even if bracket same line is true, we shouldn't break the line when there's a comment after an attribute.
I created a function called has_last_attribute_comments in crates/biome_js_formatter/src/jsx/tag/opening_element.rs that returns a boolean indicating whether there's a comment after an attribute.
I added a last_attribute_comments argument to OpeningElementLayout::IndentAttributes and passed has_last_attribute_comments to it.
When both force_bracket_same_line and last_attribute_has_comments are true, we make it break the line.

Test Plan

added test cases for when attributes have comments in crates/biome_js_formatter/tests/specs/jsx/bracket_same_line/bracket_same_line.jsx.
I confirmed that all tests in the jsx_module in crates/biome_js_formatter/tests/spec_tests.rs pass.

close #3354

@github-actions github-actions bot added A-Formatter Area: formatter L-JavaScript Language: JavaScript and super languages labels Jul 28, 2024
Copy link
Member

@Conaclos Conaclos left a comment

Choose a reason for hiding this comment

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

Great fix! Thanks!

@ematipico ematipico changed the title fix: (biome-js-formatter) prevent line break when comment follows end of attributes, irrespective of bracketSameLine value fix(formatter): prevent line break when comment follows end of attributes, irrespective of bracketSameLine value Jul 28, 2024
@Conaclos
Copy link
Member

Conaclos commented Jul 28, 2024

Some tests are failing.

Copy link

codspeed-hq bot commented Jul 28, 2024

CodSpeed Performance Report

Merging #3534 will improve performances by 6.92%

Comparing satojin219:fix/bracket-smae-line-comment (d47cc05) with main (9aea758)

Summary

⚡ 1 improvements
✅ 103 untouched benchmarks

Benchmarks breakdown

Benchmark main satojin219:fix/bracket-smae-line-comment Change
react.production.min_3378072959512366797.js[cached] 2 ms 1.9 ms +6.92%

@satojin219 satojin219 requested a review from Conaclos July 29, 2024 00:11
@satojin219
Copy link
Contributor Author

@Conaclos
d47cc05
Thank you for your review.
When is_self_closing is true, line breaks are maintained, so I made changes accordingly.
Please confirm.

@Conaclos Conaclos merged commit 5d8a31e into biomejs:main Jul 29, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Formatter Area: formatter L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📝 "Bracket Same Line" and comment mis format
2 participants