Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
45d50125
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,发现更多精彩内容 >>
未验证
提交
45d50125
编写于
11月 01, 2017
作者:
S
Sam Harwell
提交者:
GitHub
11月 01, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #21503 from zaytsev-victor/Fixed21501
Fixed 'Add null check' refactoring
上级
3b04e9ff
790ba501
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
219 addition
and
15 deletion
+219
-15
src/EditorFeatures/CSharpTest/InitializeParameter/AddParameterCheckTests.cs
.../CSharpTest/InitializeParameter/AddParameterCheckTests.cs
+152
-0
src/Features/CSharp/Portable/InitializeParameter/CSharpAddParameterCheckCodeRefactoringProvider.cs
...rameter/CSharpAddParameterCheckCodeRefactoringProvider.cs
+14
-0
src/Features/CSharp/Portable/InitializeParameter/InitializeParameterHelpers.cs
...ortable/InitializeParameter/InitializeParameterHelpers.cs
+24
-12
src/Features/Core/Portable/InitializeParameter/AbstractAddParameterCheckCodeRefactoringProvider.cs
...meter/AbstractAddParameterCheckCodeRefactoringProvider.cs
+7
-0
src/Features/VisualBasic/Portable/InitializeParameter/VisualBasicAddParameterCheckCodeRefactoringProvider.vb
...er/VisualBasicAddParameterCheckCodeRefactoringProvider.vb
+4
-0
src/Workspaces/CSharp/Portable/Extensions/ArrowExpressionClauseSyntaxExtensions.cs
...table/Extensions/ArrowExpressionClauseSyntaxExtensions.cs
+18
-3
未找到文件。
src/EditorFeatures/CSharpTest/InitializeParameter/AddParameterCheckTests.cs
浏览文件 @
45d50125
...
...
@@ -886,6 +886,158 @@ public C(string s)
throw new ArgumentNullException(nameof(s));
}
}
}"
);
}
[
WorkItem
(
21501
,
"https://github.com/dotnet/roslyn/issues/21501"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsInitializeParameter
)]
public
async
Task
TestInArrowExpression1
()
{
await
TestInRegularAndScript1Async
(
@"
using System;
using System.Linq;
class C
{
public int Foo(int[] array[||]) =>
array.Where(x => x > 3)
.OrderBy(x => x)
.Count();
}"
,
@"
using System;
using System.Linq;
class C
{
public int Foo(int[] array)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
return array.Where(x => x > 3)
.OrderBy(x => x)
.Count();
}
}"
);
}
[
WorkItem
(
21501
,
"https://github.com/dotnet/roslyn/issues/21501"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsInitializeParameter
)]
public
async
Task
TestInArrowExpression2
()
{
await
TestInRegularAndScript1Async
(
@"
using System;
using System.Linq;
class C
{
public int Foo(int[] array[||]) /* Bar */ => /* Bar */
array.Where(x => x > 3)
.OrderBy(x => x)
.Count(); /* Bar */
}"
,
@"
using System;
using System.Linq;
class C
{
public int Foo(int[] array) /* Bar */
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
/* Bar */
return array.Where(x => x > 3)
.OrderBy(x => x)
.Count(); /* Bar */
}
}"
);
}
[
WorkItem
(
21501
,
"https://github.com/dotnet/roslyn/issues/21501"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsInitializeParameter
)]
public
async
Task
TestMissingInArrowExpression1
()
{
await
TestMissingInRegularAndScriptAsync
(
@"
using System;
using System.Linq;
class C
{
public void Foo(string bar[||]) =>
#if DEBUG
Console.WriteLine(""debug"" + bar);
#else
Console.WriteLine(""release"" + bar);
#endif
}"
);
}
[
WorkItem
(
21501
,
"https://github.com/dotnet/roslyn/issues/21501"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsInitializeParameter
)]
public
async
Task
TestMissingInArrowExpression2
()
{
await
TestMissingInRegularAndScriptAsync
(
@"
using System;
using System.Linq;
class C
{
public int Foo(int[] array[||]) =>
#if DEBUG
array.Where(x => x > 3)
.OrderBy(x => x)
.Count();
#else
array.Where(x => x > 3)
.OrderBy(x => x)
.Count();
#endif
}"
);
}
[
WorkItem
(
21501
,
"https://github.com/dotnet/roslyn/issues/21501"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsInitializeParameter
)]
public
async
Task
TestInArrowExpression3
()
{
await
TestInRegularAndScript1Async
(
@"
using System;
using System.Linq;
class C
{
public void Foo(int[] array[||]) =>
array.Where(x => x > 3)
.OrderBy(x => x)
.Count();
}"
,
@"
using System;
using System.Linq;
class C
{
public void Foo(int[] array)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
array.Where(x => x > 3)
.OrderBy(x => x)
.Count();
}
}"
);
}
}
...
...
src/Features/CSharp/Portable/InitializeParameter/CSharpAddParameterCheckCodeRefactoringProvider.cs
浏览文件 @
45d50125
...
...
@@ -2,6 +2,7 @@
using
System.Composition
;
using
Microsoft.CodeAnalysis.CodeRefactorings
;
using
Microsoft.CodeAnalysis.CSharp.Extensions
;
using
Microsoft.CodeAnalysis.CSharp.Syntax
;
using
Microsoft.CodeAnalysis.Editing
;
using
Microsoft.CodeAnalysis.InitializeParameter
;
...
...
@@ -30,5 +31,18 @@ protected override void InsertStatement(SyntaxEditor editor, BaseMethodDeclarati
protected
override
bool
IsImplicitConversion
(
Compilation
compilation
,
ITypeSymbol
source
,
ITypeSymbol
destination
)
=>
InitializeParameterHelpers
.
IsImplicitConversion
(
compilation
,
source
,
destination
);
protected
override
bool
CanOffer
(
SyntaxNode
body
)
{
if
(
body
is
ArrowExpressionClauseSyntax
arrowExpressionClauseSyntax
)
{
return
arrowExpressionClauseSyntax
.
TryConvertToStatement
(
semicolonToken
:
SyntaxFactory
.
Token
(
SyntaxKind
.
SemicolonToken
),
createReturnStatementForExpression
:
false
,
statement
:
out
var
_
);
}
return
true
;
}
}
}
src/Features/CSharp/Portable/InitializeParameter/InitializeParameterHelpers.cs
浏览文件 @
45d50125
...
...
@@ -28,28 +28,23 @@ public static SyntaxNode TryGetLastStatement(IBlockOperation blockStatementOpt)
SyntaxNode
statementToAddAfterOpt
,
StatementSyntax
statement
)
{
var
generator
=
editor
.
Generator
;
if
(
methodDeclaration
.
ExpressionBody
!=
null
)
{
// If this is a => method, then we'll have to convert the method to have a block
// body. Add the new statement as the first/last statement of the new block
// depending if we were asked to go after something or not.
if
(!
methodDeclaration
.
ExpressionBody
.
TryConvertToStatement
(
methodDeclaration
.
SemicolonToken
,
CreateReturnStatement
(
methodDeclaration
),
out
var
declaration
))
{
return
;
}
if
(
statementToAddAfterOpt
==
null
)
{
editor
.
SetStatements
(
methodDeclaration
,
ImmutableArray
.
Create
(
statement
,
generator
.
ExpressionStatement
(
methodDeclaration
.
ExpressionBody
.
Expression
)));
editor
.
SetStatements
(
methodDeclaration
,
ImmutableArray
.
Create
(
statement
,
declaration
));
}
else
{
editor
.
SetStatements
(
methodDeclaration
,
ImmutableArray
.
Create
(
generator
.
ExpressionStatement
(
methodDeclaration
.
ExpressionBody
.
Expression
),
statement
));
editor
.
SetStatements
(
methodDeclaration
,
ImmutableArray
.
Create
(
declaration
,
statement
));
}
}
else
if
(
methodDeclaration
.
Body
!=
null
)
...
...
@@ -84,5 +79,22 @@ public static SyntaxNode TryGetLastStatement(IBlockOperation blockStatementOpt)
.
WithBody
(
SyntaxFactory
.
Block
(
statement
)));
}
}
private
static
bool
CreateReturnStatement
(
BaseMethodDeclarationSyntax
declarationSyntax
)
{
switch
(
declarationSyntax
)
{
case
MethodDeclarationSyntax
method
:
return
!
method
.
ReturnType
.
IsVoid
();
case
ConversionOperatorDeclarationSyntax
_
:
case
OperatorDeclarationSyntax
_
:
return
true
;
case
DestructorDeclarationSyntax
_
:
case
ConstructorDeclarationSyntax
_
:
return
false
;
}
return
false
;
}
}
}
src/Features/Core/Portable/InitializeParameter/AbstractAddParameterCheckCodeRefactoringProvider.cs
浏览文件 @
45d50125
...
...
@@ -55,6 +55,11 @@ internal abstract partial class AbstractAddParameterCheckCodeRefactoringProvider
// people do strange things in their constructors.
if
(
blockStatementOpt
!=
null
)
{
if
(!
CanOffer
(
blockStatementOpt
.
Syntax
))
{
return
ImmutableArray
<
CodeAction
>.
Empty
;
}
foreach
(
var
statement
in
blockStatementOpt
.
Operations
)
{
if
(
IsIfNullCheck
(
statement
,
parameter
))
...
...
@@ -93,6 +98,8 @@ internal abstract partial class AbstractAddParameterCheckCodeRefactoringProvider
return
result
.
ToImmutableAndFree
();
}
protected
abstract
bool
CanOffer
(
SyntaxNode
body
);
private
bool
ContainsNullCoalesceCheck
(
ISyntaxFactsService
syntaxFacts
,
SemanticModel
semanticModel
,
IOperation
statement
,
IParameterSymbol
parameter
,
...
...
src/Features/VisualBasic/Portable/InitializeParameter/VisualBasicAddParameterCheckCodeRefactoringProvider.vb
浏览文件 @
45d50125
...
...
@@ -33,5 +33,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.InitializeParameter
Protected
Overrides
Sub
InsertStatement
(
editor
As
SyntaxEditor
,
methodDeclaration
As
MethodBlockBaseSyntax
,
statementToAddAfterOpt
As
SyntaxNode
,
statement
As
StatementSyntax
)
InitializeParameterHelpers
.
InsertStatement
(
editor
,
methodDeclaration
,
statementToAddAfterOpt
,
statement
)
End
Sub
Protected
Overrides
Function
CanOffer
(
body
As
SyntaxNode
)
As
Boolean
Return
True
End
Function
End
Class
End
Namespace
src/Workspaces/CSharp/Portable/Extensions/ArrowExpressionClauseSyntaxExtensions.cs
浏览文件 @
45d50125
...
...
@@ -13,23 +13,38 @@ internal static class ArrowExpressionClauseSyntaxExtensions
SyntaxToken
semicolonToken
,
bool
createReturnStatementForExpression
,
out
BlockSyntax
block
)
{
if
(!
arrowExpression
.
TryConvertToStatement
(
semicolonToken
,
createReturnStatementForExpression
,
out
var
statement
))
{
block
=
null
;
return
false
;
}
block
=
SyntaxFactory
.
Block
(
statement
);
return
true
;
}
public
static
bool
TryConvertToStatement
(
this
ArrowExpressionClauseSyntax
arrowExpression
,
SyntaxToken
semicolonToken
,
bool
createReturnStatementForExpression
,
out
StatementSyntax
statement
)
{
// It's tricky to convert an arrow expression with directives over to a block.
// We'd need to find and remove the directives *after* the arrow expression and
// move them accordingly. So, for now, we just disallow this.
if
(
arrowExpression
.
Expression
.
GetLeadingTrivia
().
Any
(
t
=>
t
.
IsDirective
))
{
block
=
null
;
statement
=
null
;
return
false
;
}
var
statement
=
ConvertToStatement
(
arrowExpression
.
Expression
,
semicolonToken
,
createReturnStatementForExpression
);
statement
=
ConvertToStatement
(
arrowExpression
.
Expression
,
semicolonToken
,
createReturnStatementForExpression
);
if
(
arrowExpression
.
ArrowToken
.
TrailingTrivia
.
Any
(
t
=>
t
.
IsSingleOrMultiLineComment
()))
{
statement
=
statement
.
WithPrependedLeadingTrivia
(
arrowExpression
.
ArrowToken
.
TrailingTrivia
);
}
block
=
SyntaxFactory
.
Block
(
statement
);
return
true
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录