Skip to content

Quick assist for converting a switch statement in which each case just assigns to a variable #51836

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

Closed
srawlins opened this issue Mar 23, 2023 · 4 comments
Labels
devexp-quick-fix Issues with analysis server (quick) fixes legacy-area-analyzer Use area-devexp instead. type-enhancement A request for a change that isn't a bug

Comments

@srawlins
Copy link
Member

It'd be great to offer to convert this to a Dart 3.0 switch expression.

For example:

String f(Color color) {
  String result;
  switch (color) {
    case Color.blue:
      result = 'blue';
      break;
    case Color.red:
      throw 'Unsupported';
  }
  return result;
}

could be converted to:

String f(Color color) {
  String result;
  result = switch (color) {
    case Color.blue => 'blue',
    case Color.red => throw 'Unsupported'
  }
  return result;
}

CC @bwilkerson @pq @mit-mit @stereotype441

@srawlins srawlins added legacy-area-analyzer Use area-devexp instead. devexp-quick-fix Issues with analysis server (quick) fixes type-enhancement A request for a change that isn't a bug labels Mar 23, 2023
@pq
Copy link
Member

pq commented Mar 23, 2023

I think this is complete as part of #50417?

@pq
Copy link
Member

pq commented Mar 23, 2023

(And improved w/ #51826.)

@srawlins
Copy link
Member Author

When writing these multi-token assists, there is an opportunity to eat comments. 🙁 For example:

var name = '';
/* one */
switch (color) {
  /* two */ case /* three */ Color.blue /* four */: /* five */
    name /* six */ = /* seven */ 'blue' / *eight */; / * nine */
    break; /* ten */
}

It can be excruciating to try to account for all of those, and not lose them. Many of those will be exceedingly rare, but I think /* one */, /* two */, /* five */, /* nine */, and /* ten */ should be considered. Can either work to preserve them, or not offer the assist if they are detected. If there is a tracking issue or a PR, I can comment there.

For example, in our code search I see examples like:

case foo: // Comment here.
  result = 'foo';
case bar:
  // Comment here.
  result = 'bar'; // Comment here.

(here and here and here)

@srawlins
Copy link
Member Author

Perfect, thanks. I see comments in the tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devexp-quick-fix Issues with analysis server (quick) fixes legacy-area-analyzer Use area-devexp instead. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants