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

Utility method for converting expressions into case patterns #59595

Open
FMorschel opened this issue Nov 22, 2024 · 2 comments
Open

Utility method for converting expressions into case patterns #59595

FMorschel opened this issue Nov 22, 2024 · 2 comments
Labels
analyzer-api Issues that impact the public API of the analyzer package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request

Comments

@FMorschel
Copy link
Contributor

Inside analyais_server_plugin\lib\edit\correction_utils.dart we have CorrectionUtils.invertCondition for doing what the name suggests. It takes an expression and returns another with the exact opposite result.

For #45831, I was thinking maybe we could have something similar for converting expressions with one constant side into case patterns.

Also one for converting back, from a case pattern into an expression. These could also be exposed as assists (all three of them) if we choose to.

@FMorschel FMorschel added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Nov 22, 2024
@bwilkerson
Copy link
Member

I was thinking maybe we could have something similar for converting expressions with one constant side into case patterns.

Based on the linked issue, I'm guessing that you want a utility that would take count == 1 and return '1'.

I suspect that we'd get more value from a utility that takes an expression and returns a pattern if the whole expression can be converted to one. In other words, rather that take count == 1 and converting the 1, have a utility to just convert the 1 (and make the assist decide which side of the original condition to convert).

The assist will also need to ask the question of whether all of the expressions to be converted into patterns. It can probably do that by allowing the utility to return null if the expression can't be converted.

Also one for converting back, from a case pattern into an expression.

Constant patterns can be converted (though converting an expression to a pattern and then back to an expression might not always return the same expression), but other patterns are probably harder than we want to try to tackle (or just impossible in some cases).

@FMorschel
Copy link
Contributor Author

but other patterns are probably harder than we want to try to tackle (or just impossible in some cases).

This is fine I think. I was simply thinking of a way for the user to swap between cases like:

if (count == 1) {...}  <-->  if (count case 1) {...}
if (count > 10 && count < 20) {...}  <-->  if (count case > 10 && < 20) {...}

It can probably do that by allowing the utility to return null if the expression can't be converted.

Agreed in both cases to and from. If it is impossible or too hard to handle we can just return null.

@srawlins srawlins added the analyzer-api Issues that impact the public API of the analyzer package label Nov 22, 2024
@pq pq added the P3 A lower priority bug or feature request label Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-api Issues that impact the public API of the analyzer package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request
Projects
None yet
Development

No branches or pull requests

4 participants