-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add feature to simplify interpolation expressions when possible #40070
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
Add feature to simplify interpolation expressions when possible #40070
Conversation
|
Tagging @jinujoseph @dotnet/roslyn-ide |
|
@jnm2 Would you be willing to help with tests? |
|
@CyrusNajmabadi Sure, soon as I can. |
...Features/Core/Portable/SimplifyInterpolation/AbstractSimplifyInterpolationCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
| if (invocation.Arguments.Length == 1 && | ||
| invocation.Arguments[0].Value.ConstantValue is { HasValue: true, Value: string format }) | ||
| { | ||
| unwrapped = invocation.Instance; | ||
| formatString = format; | ||
| return; | ||
| } | ||
|
|
||
| if (invocation.Arguments.Length == 0) | ||
| { | ||
| unwrapped = invocation.Instance; | ||
| formatString = ""; | ||
| return; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Option:
if (invocation.Arguments.Length <= 1)
{
unwrapped = invocation.Instance;
formatString = invocation.Arguments.FirstOrDefault()?.Value.ConstantValue.GetValueOrDefault()
?? string.Empty;
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm ok in the current form. seems clearer to me.
...isualBasic/Portable/SimplifyInterpolation/VisualBasicSimplifyInterpolationCodeFixProvider.vb
Outdated
Show resolved
Hide resolved
|
@CyrusNajmabadi What's the best way to help with tests? PR to this branch in your fork? |
yes please! |
src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs
Outdated
Show resolved
Hide resolved
Oh, ok. I'll take a look :) |
|
Did you want me to do make the feature changes too? A couple of tests are suggestions (treat them all that way) and you mentioned wanting to be the one to merge the consecutive Unnecessary spans before comparing them to the test markup. |
|
Ah 👍 |
|
@ryzngard LMK what you need here. This is ready for review. |
|
Hey @ryzngard could you ptal at this PR? It's been more than two weeks without any movement... :-/ |
| [Fact] | ||
| public async Task ToStringWithNoParameterWhenBothComponentsAreSpecified() | ||
| { | ||
| await TestMissingInRegularAndScriptAsync( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should support simplifying this scenario as well. Will file a separate task
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryzngard I wrote the test this way because I thought of it as a correctness issue. The goo format string is being applied to the return value of ToString() and is a no-op. Removing ToString() would cause it to stop being a no-op. Wouldn't it be better to put a warning squiggle on :goo than to mark ToString() as redundant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be a separate feature though, a warning squiggle whenever it is known that the format component cannot be applied to the expression type.
src/EditorFeatures/CSharpTest/SimplifyInterpolation/SimplifyInterpolationTests.cs
Show resolved
Hide resolved
src/EditorFeatures/TestUtilities/CodeActions/AbstractCodeActionOrUserDiagnosticTest.cs
Outdated
Show resolved
Hide resolved
src/EditorFeatures/TestUtilities/CodeActions/AbstractCodeActionOrUserDiagnosticTest.cs
Outdated
Show resolved
Hide resolved
...tures/Core/Portable/SimplifyInterpolation/AbstractSimplifyInterpolationDiagnosticAnalyzer.cs
Show resolved
Hide resolved
...Features/Core/Portable/SimplifyInterpolation/AbstractSimplifyInterpolationCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
...tures/Core/Portable/SimplifyInterpolation/AbstractSimplifyInterpolationDiagnosticAnalyzer.cs
Show resolved
Hide resolved
|
Thanks! |
|
|
||
| unwrapped = invocation.Instance; | ||
| alignment = alignmentSyntax as TExpressionSyntax; | ||
| negate = targetName == nameof(string.PadLeft); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😱 We did a bad thing. Fix in #41845.
Fixes #40066
Looks like this:
Todo: