You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Ensure comment formatting is idempotent.
In some cases, a line comment can appear between two tokens that otherwise never split, like after "if (" and before the condition. That leads to some tricky edge case behavior. If you formatted:
```dart
if (
// Comment
condition) {
;
}
```
It would see the newline before the comment and decide the comment was a "leading comment" which means it gets attached to the condition expression. Then the formatter would output it like:
```dart
if (// Comment
condition) {
;
}
```
That's because leading comments don't write a newline before themselves. Then if you format that again, there's no newline before the `//`, so now it's a hanging comment. Hanging comments get a space before them, so you get:
```dart
if ( // Comment
condition) {
;
}
```
Really, leading comments (as the name implies) are intended to always begin a line. So this PR makes sure they do that.
While I was at it, I modified the test runner to run the formatter twice on *every* test to ensure that everything is idempotent. That doesn't *prove* that the formatter will always produce idempotent output, but it at least gives us pretty good test coverage that it *does* behave idempotent-ly.
In practice, most normal looking code would never hit this edge case. You have to put a comment in an unusual spot where a split doesn't occur.
This still feels like a fairly brittle part of the formatter to me. Comments appearing between tokens that never split otherwise is handled on a pretty ad hoc basis (which is why some of the tests in this PR have weird indentation). I'd like a cleaner more systematic solution, but I'm not sure what that would look like.
Fix#1606.
* Fix version number in CHANGELOG.
* Update lib/src/back_end/code_writer.dart
Co-authored-by: Nate Bosch <[email protected]>
* Apply review feedback.
---------
Co-authored-by: Nate Bosch <[email protected]>
0 commit comments