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

Add feature to convert switch statement to switch expression #31812

Merged
merged 45 commits into from
May 23, 2019
Merged
Show file tree
Hide file tree
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 Dec 15, 2018
a250d78
Update resources.
alrz Dec 15, 2018
508bfa3
PR feedback
alrz Dec 15, 2018
c596326
Add comments.
alrz Dec 15, 2018
b6fe664
Rewrite the code fix
alrz Dec 15, 2018
c0225ab
Move out usings.
alrz Dec 15, 2018
901f1c6
Simplify code.
alrz Dec 15, 2018
dd01202
Remove redundant code
alrz Dec 15, 2018
b9ff8aa
Rewrite analyzer to operate on syntax nodes.
alrz Dec 17, 2018
30a6f65
Adjust the rewriter to the new analysis.
alrz Dec 17, 2018
e0af547
Skip spans
alrz Dec 17, 2018
b934b18
Try to remove declarators.
alrz Dec 17, 2018
1debd30
Add tests
alrz Dec 17, 2018
50cdae1
Compare expressions rather than symbols
alrz Dec 18, 2018
1574025
Do not remove declarator if used
alrz Dec 18, 2018
fa4b5e1
Typo
alrz Dec 18, 2018
195d7fe
Address PR feedback.
alrz Dec 20, 2018
f837d22
Add tests
alrz Dec 20, 2018
2238e0c
Add tests
alrz Dec 20, 2018
82a11c1
Add suggested test.
alrz Dec 20, 2018
46321c5
Revert code.
alrz Dec 20, 2018
3b779f7
Return failure on unexpected node after assignment.
alrz Dec 20, 2018
00b6a8d
Add test for interdependent assignments
alrz Jan 14, 2019
0f75ded
Remove multi-assignment analysis.
alrz Jan 17, 2019
c11903e
No need to create immutable array.
alrz Jan 17, 2019
8417416
Fix typo.
alrz Jan 17, 2019
4f34b5b
Preserve trivia.
alrz Jan 17, 2019
651ed39
Merge remote-tracking branch 'origin/master' into switch
alrz Jan 23, 2019
8f169f2
Use invariant culture
alrz Jan 23, 2019
f87ddbf
Fixup merge
alrz Jan 23, 2019
dcb6400
Add .editorconfig option
alrz Jan 23, 2019
a5deae6
Check user preference
alrz Jan 23, 2019
2b38621
Reuse variable.
alrz Jan 23, 2019
03b8e90
Fixup tests.
alrz Jan 23, 2019
c223eb6
Reorder statements.
alrz Jan 23, 2019
13cc48e
Add suggested test.
alrz Jan 23, 2019
fd2d148
Update resources.
alrz Jan 28, 2019
9ddb384
Fix typo.
alrz May 17, 2019
d914014
PR feedback.
alrz May 17, 2019
8619eac
Adjust baseline.
alrz May 17, 2019
35c5cf7
Merge remote-tracking branch 'origin/master' into switch-merge
alrz May 22, 2019
4565b19
Update resources.
alrz May 22, 2019
04a5297
Fix formatting.
alrz May 22, 2019
b63c5f1
Fix typo.
alrz May 22, 2019
833c7cd
Fix test.
alrz May 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
,
Copy link
Member Author

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.

Copy link
Member

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

};
}
}");
}
}
}
Loading