Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
0106c3ab
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
0106c3ab
编写于
10月 29, 2019
作者:
J
Jason Malinowski
提交者:
GitHub
10月 29, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #39465 from alrz/fix-if-to-switch
Fix minor issue in IfToSwitch refactoring
上级
74ada1df
66ced6b9
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
124 addition
and
12 deletion
+124
-12
src/EditorFeatures/CSharpTest/CodeActions/ConvertIfToSwitch/ConvertIfToSwitchTests.cs
...t/CodeActions/ConvertIfToSwitch/ConvertIfToSwitchTests.cs
+114
-6
src/Features/Core/Portable/ConvertIfToSwitch/AbstractConvertIfToSwitchCodeRefactoringProvider.Analyzer.cs
...tractConvertIfToSwitchCodeRefactoringProvider.Analyzer.cs
+10
-6
未找到文件。
src/EditorFeatures/CSharpTest/CodeActions/ConvertIfToSwitch/ConvertIfToSwitchTests.cs
浏览文件 @
0106c3ab
...
...
@@ -70,6 +70,87 @@ void M(int i)
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsConvertIfToSwitch
)]
public
async
Task
TestMissingOnSubsequentBlock
()
{
await
TestMissingInRegularAndScriptAsync
(
@"class C
{
int M(int i)
{
[||]if (i == 3) return 0;
{ if (i == 6) return 1; }
return 2;
}
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsConvertIfToSwitch
)]
public
async
Task
TestElseBlock_01
()
{
await
TestInRegularAndScriptAsync
(
@"class C
{
int M(int i)
{
[||]if (i == 3) return 0;
else { if (i == 6) return 1; }
}
}"
,
@"class C
{
int M(int i)
{
switch (i)
{
case 3:
return 0;
case 6:
return 1;
}
}
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsConvertIfToSwitch
)]
public
async
Task
TestElseBlock_02
()
{
await
TestInRegularAndScriptAsync
(
@"class C
{
int M(int i)
{
[||]if (i == 3)
{
return 0;
}
else
{
if (i == 6) return 1;
if (i == 7) return 1;
return 0;
}
}
}"
,
@"class C
{
int M(int i)
{
switch (i)
{
case 3:
return 0;
case 6:
return 1;
case 7:
return 1;
default:
return 0;
}
}
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsConvertIfToSwitch
)]
public
async
Task
TestMultipleCases_01
()
{
...
...
@@ -595,6 +676,33 @@ int M(int? i)
}"
,
index
:
1
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsConvertIfToSwitch
)]
public
async
Task
TestSwitchExpression_02
()
{
await
TestInRegularAndScriptAsync
(
@"class C
{
int M(int? i)
{
[||]if (i == null) { return 5; }
if (i == 0) { return 6; }
else { return 7; }
}
}"
,
@"class C
{
int M(int? i)
{
return i switch
{
null => 5,
0 => 6,
_ => 7
};
}
}"
,
index
:
1
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsConvertIfToSwitch
)]
public
async
Task
TestSubsequentIfStatements_02
()
{
...
...
@@ -706,7 +814,7 @@ int M(int i)
[||]if (i == 10) return 5;
if (i == 20) return 6;
if (i == i) return 0;
re
u
turn 7;
return 7;
}
}"
,
@"class C
...
...
@@ -721,7 +829,7 @@ int M(int i)
return 6;
}
if (i == i) return 0;
re
u
turn 7;
return 7;
}
}"
);
}
...
...
@@ -746,7 +854,7 @@ int M(int i)
{
return 0;
}
re
u
turn 7;
return 7;
}
}"
,
@"class C
...
...
@@ -764,7 +872,7 @@ int M(int i)
{
return 0;
}
re
u
turn 7;
return 7;
}
}"
);
}
...
...
@@ -798,7 +906,7 @@ int M(int i)
{
return 0;
}
re
u
turn 7;
return 7;
}
}"
,
@"class C
...
...
@@ -825,7 +933,7 @@ int M(int i)
{
return 0;
}
re
u
turn 7;
return 7;
}
}"
);
}
...
...
src/Features/Core/Portable/ConvertIfToSwitch/AbstractConvertIfToSwitchCodeRefactoringProvider.Analyzer.cs
浏览文件 @
0106c3ab
...
...
@@ -26,9 +26,9 @@ internal abstract partial class AbstractConvertIfToSwitchCodeRefactoringProvider
//
// <if-statement>
// : if (<section-expr>) { _ } else <if-statement>
// | if (<section-expr>) { _ } else { <if-statement-sequence> }
// | if (<section-expr>) { _ } else { _ }
// | if (<section-expr>) { _ }
// | { <if-statement-sequence> }
//
// <section-expr>
// : <section-expr> || <pattern-expr>
...
...
@@ -131,17 +131,14 @@ private bool ParseIfStatementSequence(ReadOnlySpan<IOperation> operations, Array
//
// <if-statement>
// : if (<section-expr>) { _ } else <if-statement>
// | if (<section-expr>) { _ } else { <if-statement-sequence> }
// | if (<section-expr>) { _ } else { _ }
// | if (<section-expr>) { _ }
// | { <if-statement-sequence> }
//
private
bool
ParseIfStatement
(
IOperation
operation
,
ArrayBuilder
<
AnalyzedSwitchSection
>
sections
,
out
IOperation
?
defaultBodyOpt
)
{
switch
(
operation
)
{
case
IBlockOperation
op
:
return
ParseIfStatementSequence
(
op
.
Operations
.
AsSpan
(),
sections
,
out
defaultBodyOpt
);
case
IConditionalOperation
op
when
CanConvert
(
op
):
var
section
=
ParseSwitchSection
(
op
);
if
(
section
is
null
)
...
...
@@ -155,7 +152,7 @@ private bool ParseIfStatement(IOperation operation, ArrayBuilder<AnalyzedSwitchS
{
defaultBodyOpt
=
null
;
}
else
if
(!
ParseIfStatement
(
op
.
WhenFalse
,
sections
,
out
defaultBodyOpt
))
else
if
(!
ParseIfStatement
OrBlock
(
op
.
WhenFalse
,
sections
,
out
defaultBodyOpt
))
{
defaultBodyOpt
=
op
.
WhenFalse
;
}
...
...
@@ -167,6 +164,13 @@ private bool ParseIfStatement(IOperation operation, ArrayBuilder<AnalyzedSwitchS
return
false
;
}
private
bool
ParseIfStatementOrBlock
(
IOperation
op
,
ArrayBuilder
<
AnalyzedSwitchSection
>
sections
,
out
IOperation
?
defaultBodyOpt
)
{
return
op
is
IBlockOperation
block
?
ParseIfStatementSequence
(
block
.
Operations
.
AsSpan
(),
sections
,
out
defaultBodyOpt
)
:
ParseIfStatement
(
op
,
sections
,
out
defaultBodyOpt
);
}
private
AnalyzedSwitchSection
?
ParseSwitchSection
(
IConditionalOperation
operation
)
{
var
labels
=
ArrayBuilder
<
AnalyzedSwitchLabel
>.
GetInstance
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录