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

dart format produce invalid code #1404

Closed
nielsenko opened this issue Feb 22, 2024 · 2 comments · Fixed by #1405
Closed

dart format produce invalid code #1404

nielsenko opened this issue Feb 22, 2024 · 2 comments · Fixed by #1405

Comments

@nielsenko
Copy link

Try formatting:

int weird(Object o) {
  return switch (o) {
    int i => i, // evil comment
    _ => 42
  };
}

produces:

int weird(Object o) {
  return switch (o) { int i => i, // evil comment _ => 42 };
}

which is obviously not valid Dart.

Reproduced with both Dart 3.3.0 and 3.4.0-157.0.dev, and 3.0.0 for that matter.

@srawlins
Copy link
Member

Ouch. Can confirm on DartPad.

@munificent
Copy link
Member

Oof, nasty! I'll take a look.

munificent added a commit that referenced this issue Feb 23, 2024
The fix here is pretty hacky, but this is in the old formatter which
will be going away hopefully relatively soon. I honestly don't know how
to fix it more elegantly and I'm a little worried that any more systemic
fix will cause knock-on bugs since I don't have the old code loaded in
my head as well as I used to.

Fixes #1404.
munificent added a commit that referenced this issue Feb 23, 2024
)

The fix here is pretty hacky, but this is in the old formatter which
will be going away hopefully relatively soon. I honestly don't know how
to fix it more elegantly and I'm a little worried that any more systemic
fix will cause knock-on bugs since I don't have the old code loaded in
my head as well as I used to.

Fixes #1404.
copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue Feb 26, 2024
This fixes a serious (but somewhat rare) bug where a line comment in a
switch expression might eat subsequent cases. See:

dart-lang/dart_style#1404

There are no other formatting changes.

Change-Id: I7a1cb751c76abcd0507113635aab3f866a798364
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/354063
Commit-Queue: Alexander Thomas <athom@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants