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

Support Multi-Case Switch Statements with the "Convert to switch expression" VScode code action #54567

Closed
verygoodstefan opened this issue Jan 10, 2024 · 1 comment
Assignees
Labels
analyzer-assist Issues with analysis server assists area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@verygoodstefan
Copy link

Note: I originally raised this issue in dart-code, and was directed to raise it here.

There's a neat code action to convert switch statements into expressions that take advantage of Dart 3's pattern matching:

Screenshot 2023-12-20 at 10 46 01 AM

Unfortunately, it does not work with switches with multiple cases giving the same return value:

Screenshot 2023-12-20 at 10 46 15 AM

It would be nice to be able to convert a multi-case switch like the following:

String myMultipleReturnEnumStatement(MyEnum enumVal) {
  switch (enumVal) {
    case MyEnum.first:
    case MyEnum.second:
    case MyEnum.third:
      return 'first, second, or third';
    case MyEnum.fourth:
    case MyEnum.fifth:
      return 'fourth or fifth';
    default:
      return 'default';
  }
}

into:

String myMultipleReturnEnumStatement(MyEnum enumVal) {
  return switch (enumVal) {
    MyEnum.first || MyEnum.second || MyEnum.third => 'first, second, or third',
    MyEnum.fourth || MyEnum.fifth => 'fourth or fifth',
    _ => 'default',
  };
}

I published a custom extension that supports this behavior, however, if it would be useful to enough people, it might be nice to have this behavior supported in the basic Dart VS code functionality.

  • Dart version: 3.0.6
  • Flutter SDK: 3.10.6
@mit-mit mit-mit added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Jan 10, 2024
@bwilkerson
Copy link
Member

That's very cool! And this is definitely the right issue tracker.

I'd love to see our support extended to cover this case, and I expect that it wouldn't be too difficult to do.

@pq pq added P2 A bug or feature request we're likely to work on analyzer-assist Issues with analysis server assists labels Jan 11, 2024
@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Mar 15, 2024
@scheglov scheglov self-assigned this Jun 12, 2024
copybara-service bot pushed a commit that referenced this issue Jun 12, 2024
…ression'.

Bug: #54567
Change-Id: Idb6cc7ecd88e1e9e3b14c6a54176c958bac36afa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/371201
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-assist Issues with analysis server assists area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants