Add feature to convert switch statement to switch expression#31812
Add feature to convert switch statement to switch expression#31812mavasani merged 45 commits intodotnet:masterfrom
Conversation
|
/cc @gafter, @CyrusNajmabadi |
...res/CSharpTest/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionTests.cs
Show resolved
Hide resolved
...res/CSharpTest/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionTests.cs
Outdated
Show resolved
Hide resolved
...able/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
...able/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
...able/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
...e/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
...e/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
...SwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.Analyzer.cs
Outdated
Show resolved
Hide resolved
|
Definitely intrigued! but needs more explanation as to what it's doing :D |
...SwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.Analyzer.cs
Outdated
Show resolved
Hide resolved
...StatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.AnalysisResult.cs
Outdated
Show resolved
Hide resolved
...StatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.AnalysisResult.cs
Outdated
Show resolved
Hide resolved
...SwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.Analyzer.cs
Outdated
Show resolved
Hide resolved
...SwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.Analyzer.cs
Outdated
Show resolved
Hide resolved
...res/CSharpTest/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionTests.cs
Outdated
Show resolved
Hide resolved
| } | ||
|
|
||
| [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertSwitchStatementToExpression)] | ||
| public async Task TestAssignmnet_Array() |
| } | ||
|
|
||
| [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertSwitchStatementToExpression)] | ||
| public async Task TestMissingOnDefalutBreak_01() |
There was a problem hiding this comment.
Same typo in tests below.
|
|
||
| // Only on simple assignments we attempt to remove variable declarators. | ||
| var isSimpleAssignment = nodeToGenerate == SyntaxKind.SimpleAssignmentExpression; | ||
| var generateDeclaration = isSimpleAssignment && rewriter.TryRemoveVariableDeclarators(switchStatement, semanticModel, editor); |
There was a problem hiding this comment.
Did you attempt to switch to using this service? I think that might simplify the code quite a bit.
| => false; | ||
|
|
||
| public override DiagnosticAnalyzerCategory GetAnalyzerCategory() | ||
| => DiagnosticAnalyzerCategory.SyntaxTreeWithoutSemanticsAnalysis; |
There was a problem hiding this comment.
This seems wrong, as explained in detail by this comment added recently by @CyrusNajmabadi. You want SemanticSpanAnalysis here.
| EditorConfigStorageLocation.ForBoolCodeStyleOption("csharp_style_conditional_delegate_call"), | ||
| new RoamingProfileStorageLocation("TextEditor.CSharp.Specific.PreferConditionalDelegateCall")}); | ||
|
|
||
| public static readonly Option<CodeStyleOption<bool>> PreferSwitchExpression = CreateOption( |
There was a problem hiding this comment.
CI is failing as this change needs a corresponding baseline update in tests within http://source.roslyn.io/#Microsoft.VisualStudio.LanguageServices.UnitTests/Options/CSharpEditorConfigGeneratorTests.vb,12.
|
@alrz Would you be able to address the last set of feedback comments? PR seems very close to completion. |
| var generateDeclaration = isSimpleAssignment && rewriter.TryRemoveVariableDeclarators(switchStatement, semanticModel, editor); | ||
|
|
||
| // Generate the final statement to wrap the switch expression, e.g. a "return" or an assignment. | ||
| return rewriter.GetFinalStatement(switchExpression, |
There was a problem hiding this comment.
We should add a cast if none of arms have a neutral type.
I did not go that path for now because at the moment we rewrite the document in one pass. We should eventually use recursive approach so that we can handle nested switches as well. |
|
I'll resolve conflicts as the last step. ping me if this looks good so far. |
# Conflicts: # src/Features/CSharp/Portable/CSharpFeaturesResources.resx # src/Features/Core/Portable/Diagnostics/Analyzers/IDEDiagnosticIds.cs # src/VisualStudio/CSharp/Impl/CSharpVSResources.resx # src/VisualStudio/CSharp/Impl/Options/Formatting/StyleViewModel.cs # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.cs.xlf # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.de.xlf # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.es.xlf # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.fr.xlf # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.it.xlf # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.ja.xlf # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.ko.xlf # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.pl.xlf # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.pt-BR.xlf # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.ru.xlf # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.tr.xlf # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.zh-Hans.xlf # src/VisualStudio/CSharp/Impl/xlf/CSharpVSResources.zh-Hant.xlf
|
tests are not running for some reason? |
|
@dotnet-bot retest this please |
|
I cannot wait for this :) |
Totally, agree. Thanks @alrz |
No description provided.