-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure comment formatting is idempotent. (#1610)
* 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 <nbosch@google.com> * Apply review feedback. --------- Co-authored-by: Nate Bosch <nbosch@google.com>
- Loading branch information
1 parent
c57d49c
commit 1208f9e
Showing
11 changed files
with
149 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,8 @@ if (obj case | |
constant as Type) {;} | ||
<<< | ||
if (obj | ||
case // comment | ||
case | ||
// comment | ||
constant as Type) { | ||
; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
>>> | ||
class AaaaAaaaAaaaAaaaaaaaAaaaaaaa { | ||
@aaaaaaaa | ||
AaaaaAaaaAaaaAaaaaaaa get aaaaAaaaAaaaaaaa => aaaaaaaaAaaaaaaaaAaaaaa | ||
? (AaaaaaaaaAaaAaaaaaaa() | ||
// AAA/Aaaaa aaaaaaaaa aaaaaaaa aaaa AAAAAAAA aaaaaaaaa aaaaaa aaaaaaaa. | ||
..aaaaAaaaaaaaaAaaaaaAaagetaaa = | ||
AaaaAaaaaaaaaAaaaaaAaagetaaa.aaaaAaaaaa(aaaaaaaa: [ | ||
AaaaaaaaAaaaaaa()..aaaaaaaa = AaaaaaaaAA_Aaaaaaaa.AAAAA, | ||
], aaaaaaAaaaaa: [ | ||
AaaaaaAaaaaaAaaaaaa() | ||
..aaaaaaAaaaaa = AaaaaaAaaaaa.AAAAAA_AAAAAAAAAA_AAAAAA, | ||
AaaaaaAaaaaaAaaaaaa()..aaaaaaAaaaaa = AaaaaaAaaaaa.AAAAAA_AAAA_AAAAAAA | ||
])) | ||
: AaaaAaaaaaaAaaaaaaAaaaaaaa(); | ||
} | ||
<<< | ||
class AaaaAaaaAaaaAaaaaaaaAaaaaaaa { | ||
@aaaaaaaa | ||
AaaaaAaaaAaaaAaaaaaaa get aaaaAaaaAaaaaaaa => | ||
aaaaaaaaAaaaaaaaaAaaaaa | ||
? (AaaaaaaaaAaaAaaaaaaa() | ||
// AAA/Aaaaa aaaaaaaaa aaaaaaaa aaaa AAAAAAAA aaaaaaaaa aaaaaa aaaaaaaa. | ||
..aaaaAaaaaaaaaAaaaaaAaagetaaa = | ||
AaaaAaaaaaaaaAaaaaaAaagetaaa.aaaaAaaaaa( | ||
aaaaaaaa: [ | ||
AaaaaaaaAaaaaaa()..aaaaaaaa = AaaaaaaaAA_Aaaaaaaa.AAAAA, | ||
], | ||
aaaaaaAaaaaa: [ | ||
AaaaaaAaaaaaAaaaaaa() | ||
..aaaaaaAaaaaa = AaaaaaAaaaaa.AAAAAA_AAAAAAAAAA_AAAAAA, | ||
AaaaaaAaaaaaAaaaaaa() | ||
..aaaaaaAaaaaa = AaaaaaAaaaaa.AAAAAA_AAAA_AAAAAAA, | ||
], | ||
)) | ||
: AaaaAaaaaaaAaaaaaaAaaaaaaa(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters