-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Analyzer code should start passing a language version to dart_style #56685
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
Comments
See: #56685 Change-Id: Ifa7fbbc7aff7c5fd96b02cafe0da76b22a33b1e9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/386824 Commit-Queue: Phil Quitslund <pquitslund@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
FYI @DanTup, @bwilkerson: as per a conversation with @munificent, formatting is not yet being gated by version so testing is complicated. As suggested above, I propose we leave tests as a TODO for now and fill them in once the formatter work is complete. |
(Testing to follow.) See: #56685 Change-Id: I9da0e3868cd3c873c41e8a19a77fad58b9b8dea0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/386901 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Phil Quitslund <pquitslund@google.com>
Chatting with Bob, it turns out `languageVersion` when required will be non-nullable. This change anticipates that. Bug: #56685 Change-Id: I53ec5b4ed408197b6b99871bf176e6c6cc9654dd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/387142 Commit-Queue: Phil Quitslund <pquitslund@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
In terms of separation of concerns, it's not really analyzer's job to validate which formatting style you get. That's the formatter's job. All this issue is about is ensuring that you're passing the correct language version to the formatter. You can test that by trying to format syntax that is only valid at certain language versions. So:
Thank you for working on this! |
Is this complete? |
Good question. I noted some opportunities to improve testing but Bob's points are good and maybe those aren't the right places to test. @bwilkerson: I'm curious what you think? |
I think I understand Bob's point. If the goal is to ensure that server passes the correct language version to the formatter (which then presumably passes that to the parser?), then those tests should work. The test code can't use language override comments, though, because those will cause the parser to ignore the default language version and we won't know whether the expected version was passed through correctly. |
I think https://dart-review.googlesource.com/c/sdk/+/397540 adds the tests we want? The LSP code already has equivalent tests added in 93fda3e. |
That's correct, it does.
Those look right to me too. |
Greetings analyzer team friends!
The formatter is moving to being language version aware. This means that when it's parsing some code, it needs to be told what language version to parse it as. The
DartFormatter
constructor now takes an optional parameter where you can pass in the language version. In a future version of dart_style, that parameter will become mandatory.If you own code that uses dart_style as a library, you should update your code to pass in a language version. To migrate:
If this code is inside a package (as opposed to, say, the Dart core libraries), then update your constraint on dart_style to
^2.3.7
. That's the version where the new parameter was added.If you know the precise language version the code should be parsed as, then pass in that version (as an instance of the pub_semver package's
Version
class). This is what "real" tools should do.For simple one-off scripts and other utilities where precise behavior doesn't matter much, you can pass in
DartFormatter.latestLanguageVersion
to unconditionally parse the code as the latest language version that the formatter itself supports.When the
--tall-style
experiment flag ships, the passed in language version will also be used to determine whether you get the current formatting style or the new "tall" style.I did a search through the SDK, and the constructor calls that I think are relevant are:
Thank you!
Implementation:
g3/utilities.dart
legacy/edit_format.dart
legacy/edit_format_if_enabled.dart
lsp/source_edits.dart
lsp_spec/codegen_dart.dart
change_builder/change_builder_dart.dart
Testing:
Testing is currently tricky as the formatter is not yet gating tall-style on language version (requiring an experiment flag in the meantime). We'll want to loop back add tests once that work is complete.
edit_format_test.dart
to test tall/short stylesformat_test.dart
to test tall/short stylessource_edits_test.dart
to test tall/short stylesThe text was updated successfully, but these errors were encountered: