-
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
Merged
Merged
Changes from 27 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
1d98f68
Add feature to convert switch statement to switch expression
alrz a250d78
Update resources.
alrz 508bfa3
PR feedback
alrz c596326
Add comments.
alrz b6fe664
Rewrite the code fix
alrz c0225ab
Move out usings.
alrz 901f1c6
Simplify code.
alrz dd01202
Remove redundant code
alrz b9ff8aa
Rewrite analyzer to operate on syntax nodes.
alrz 30a6f65
Adjust the rewriter to the new analysis.
alrz e0af547
Skip spans
alrz b934b18
Try to remove declarators.
alrz 1debd30
Add tests
alrz 50cdae1
Compare expressions rather than symbols
alrz 1574025
Do not remove declarator if used
alrz fa4b5e1
Typo
alrz 195d7fe
Address PR feedback.
alrz f837d22
Add tests
alrz 2238e0c
Add tests
alrz 82a11c1
Add suggested test.
alrz 46321c5
Revert code.
alrz 3b779f7
Return failure on unexpected node after assignment.
alrz 00b6a8d
Add test for interdependent assignments
alrz 0f75ded
Remove multi-assignment analysis.
alrz c11903e
No need to create immutable array.
alrz 8417416
Fix typo.
alrz 4f34b5b
Preserve trivia.
alrz 651ed39
Merge remote-tracking branch 'origin/master' into switch
alrz 8f169f2
Use invariant culture
alrz f87ddbf
Fixup merge
alrz dcb6400
Add .editorconfig option
alrz a5deae6
Check user preference
alrz 2b38621
Reuse variable.
alrz 03b8e90
Fixup tests.
alrz c223eb6
Reorder statements.
alrz 13cc48e
Add suggested test.
alrz fd2d148
Update resources.
alrz 9ddb384
Fix typo.
alrz d914014
PR feedback.
alrz 8619eac
Adjust baseline.
alrz 35c5cf7
Merge remote-tracking branch 'origin/master' into switch-merge
alrz 4565b19
Update resources.
alrz 04a5297
Fix formatting.
alrz b63c5f1
Fix typo.
alrz 833c7cd
Fix test.
alrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
203 changes: 203 additions & 0 deletions
203
...pTest/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionFixAllTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Test.Utilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ConvertSwitchStatementToExpression | ||
{ | ||
public partial class ConvertSwitchStatementToExpressionTests | ||
{ | ||
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertSwitchStatementToExpression)] | ||
public async Task TestNested_01() | ||
{ | ||
await TestInRegularAndScriptAsync( | ||
@"class Program | ||
{ | ||
int M(int i, int j) | ||
{ | ||
int r; | ||
{|FixAllInDocument:switch|} (i) | ||
{ | ||
case 1: | ||
r = 1; | ||
break; | ||
case 2: | ||
r = 2; | ||
break; | ||
case 3: | ||
r = 3; | ||
break; | ||
default: | ||
r = 4; | ||
break; | ||
} | ||
int x, y; | ||
switch (i) | ||
{ | ||
case 1: | ||
x = 1; | ||
y = 1; | ||
break; | ||
case 2: | ||
x = 1; | ||
y = 1; | ||
break; | ||
case 3: | ||
x = 1; | ||
y = 1; | ||
break; | ||
default: | ||
x = 1; | ||
y = 1; | ||
break; | ||
} | ||
switch (i) | ||
{ | ||
default: | ||
throw null; | ||
case 1: | ||
switch (j) | ||
{ | ||
case 10: | ||
return 10; | ||
case 20: | ||
return 20; | ||
case 30: | ||
return 30; | ||
} | ||
return 0; | ||
case 2: | ||
switch (j) | ||
{ | ||
case 10: | ||
return 10; | ||
case 20: | ||
return 20; | ||
case 30: | ||
return 30; | ||
case var _: | ||
return 0; | ||
} | ||
case 3: | ||
switch (j) | ||
{ | ||
case 10: | ||
return 10; | ||
case 20: | ||
return 20; | ||
case 30: | ||
return 30; | ||
case var v: | ||
return 0; | ||
} | ||
} | ||
} | ||
}", | ||
@"class Program | ||
{ | ||
int M(int i, int j) | ||
{ | ||
var r = i switch | ||
{ | ||
1 => 1, | ||
2 => 2, | ||
3 => 3, | ||
_ => 4, | ||
}; | ||
int x, y; | ||
switch (i) | ||
{ | ||
case 1: | ||
x = 1; | ||
y = 1; | ||
break; | ||
case 2: | ||
x = 1; | ||
y = 1; | ||
break; | ||
case 3: | ||
x = 1; | ||
y = 1; | ||
break; | ||
default: | ||
x = 1; | ||
y = 1; | ||
break; | ||
} | ||
switch (i) | ||
{ | ||
default: | ||
throw null; | ||
case 1: | ||
return j switch | ||
{ | ||
10 => 10, | ||
20 => 20, | ||
30 => 30, | ||
_ => 0, | ||
}; | ||
case 2: | ||
return j switch | ||
{ | ||
10 => 10, | ||
20 => 20, | ||
30 => 30, | ||
var _ => 0, | ||
}; | ||
case 3: | ||
return j switch | ||
{ | ||
10 => 10, | ||
20 => 20, | ||
30 => 30, | ||
var v => 0, | ||
}; | ||
} | ||
} | ||
}"); | ||
} | ||
|
||
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertSwitchStatementToExpression)] | ||
public async Task TestNested_02() | ||
{ | ||
await TestInRegularAndScriptAsync( | ||
@"class Program | ||
{ | ||
System.Action<int> M(int i, int j) | ||
{ | ||
{|FixAllInDocument:switch|} (i) | ||
{ | ||
default: | ||
return () => | ||
{ | ||
switch (j) | ||
{ | ||
default: | ||
return 3; | ||
} | ||
}; | ||
} | ||
} | ||
}", | ||
@"class Program | ||
{ | ||
System.Action<int> M(int i, int j) | ||
{ | ||
return i switch | ||
{ | ||
_ => () => | ||
{ | ||
switch (j) | ||
{ | ||
default: | ||
return 3; | ||
} | ||
} | ||
, | ||
}; | ||
} | ||
}"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 have no idea where this trivia is coming from.
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.
Fortunately, there are debuggers :D