-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 convert switch statement to switch expression #31812
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() |
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.
Nit: Typo Assignmnet
} | ||
|
||
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertSwitchStatementToExpression)] | ||
public async Task TestMissingOnDefalutBreak_01() |
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.
Nit: Typo Defalut
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems wrong, as explained in detail by this comment added recently by @CyrusNajmabadi. You want SemanticSpanAnalysis
here.
@@ -45,14 +45,23 @@ private static Option<T> CreateOption<T>(OptionGroup group, string name, T defau | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
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.
Thanks @alrz.
# 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.