Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
5db2d642
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,发现更多精彩内容 >>
提交
5db2d642
编写于
8月 12, 2017
作者:
C
CyrusNajmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Do not simplify to an alias in a nameof if it changes the value of hte nameof.
上级
0a304b01
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
143 addition
and
14 deletion
+143
-14
src/EditorFeatures/CSharpTest/Diagnostics/SimplifyTypeNames/SimplifyTypeNamesTests.cs
...t/Diagnostics/SimplifyTypeNames/SimplifyTypeNamesTests.cs
+108
-3
src/Workspaces/CSharp/Portable/Extensions/ContextQuery/SyntaxTreeExtensions.cs
.../Portable/Extensions/ContextQuery/SyntaxTreeExtensions.cs
+3
-8
src/Workspaces/CSharp/Portable/Extensions/ExpressionSyntaxExtensions.cs
.../CSharp/Portable/Extensions/ExpressionSyntaxExtensions.cs
+32
-3
未找到文件。
src/EditorFeatures/CSharpTest/Diagnostics/SimplifyTypeNames/SimplifyTypeNamesTests.cs
浏览文件 @
5db2d642
...
...
@@ -2,7 +2,6 @@
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.CodeFixes
;
using
Microsoft.CodeAnalysis.CodeStyle
;
...
...
@@ -10,9 +9,7 @@
using
Microsoft.CodeAnalysis.CSharp.CodeFixes.SimplifyTypeNames
;
using
Microsoft.CodeAnalysis.CSharp.Diagnostics.SimplifyTypeNames
;
using
Microsoft.CodeAnalysis.Diagnostics
;
using
Microsoft.CodeAnalysis.Editor.UnitTests.Extensions
;
using
Microsoft.CodeAnalysis.Options
;
using
Microsoft.CodeAnalysis.Text
;
using
Roslyn.Test.Utilities
;
using
Xunit
;
...
...
@@ -346,6 +343,114 @@ class A
}"
);
}
[
WorkItem
(
21449
,
"https://github.com/dotnet/roslyn/issues/21449"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsSimplifyTypeNames
)]
public
async
Task
DoNotChangeToAliasInNameOfIfItChangesNameOfName
()
{
await
TestInRegularAndScript1Async
(
@"using System;
using Foo = SimplifyInsideNameof.Program;
namespace SimplifyInsideNameof
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(nameof([|SimplifyInsideNameof.Program|]));
}
}
}"
,
@"using System;
using Foo = SimplifyInsideNameof.Program;
namespace SimplifyInsideNameof
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(nameof(Program));
}
}
}"
);
}
[
WorkItem
(
21449
,
"https://github.com/dotnet/roslyn/issues/21449"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsSimplifyTypeNames
)]
public
async
Task
DoChangeToAliasInNameOfIfItDoesNotAffectName1
()
{
await
TestInRegularAndScriptAsync
(
@"using System;
using Goo = SimplifyInsideNameof.Program;
namespace SimplifyInsideNameof
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(nameof([|SimplifyInsideNameof.Program|].Main));
}
}
}"
,
@"using System;
using Goo = SimplifyInsideNameof.Program;
namespace SimplifyInsideNameof
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(nameof(Goo.Main));
}
}
}"
);
}
[
WorkItem
(
21449
,
"https://github.com/dotnet/roslyn/issues/21449"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsSimplifyTypeNames
)]
public
async
Task
DoChangeToAliasInNameOfIfItDoesNotAffectName2
()
{
await
TestInRegularAndScriptAsync
(
@"using System;
using Goo = N.Goo;
namespace N {
class Goo { }
}
namespace SimplifyInsideNameof
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(nameof([|N.Goo|]));
}
}
}"
,
@"using System;
using Goo = N.Goo;
namespace N {
class Goo { }
}
namespace SimplifyInsideNameof
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(nameof(Goo));
}
}
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsSimplifyTypeNames
)]
public
async
Task
TwoAliases
()
{
...
...
src/Workspaces/CSharp/Portable/Extensions/ContextQuery/SyntaxTreeExtensions.cs
浏览文件 @
5db2d642
...
...
@@ -2497,15 +2497,10 @@ public static bool IsNameOfContext(this SyntaxTree syntaxTree, int position, Sem
// if the nameof expression has a missing close paren, it is parsed as an invocation expression.
if
(
token
.
Parent
.
IsKind
(
SyntaxKind
.
ArgumentList
)
&&
token
.
Parent
.
IsParentKind
(
SyntaxKind
.
InvocationExpression
))
token
.
Parent
.
Parent
is
InvocationExpressionSyntax
invocationExpression
&&
invocationExpression
.
IsNameOfInvocation
())
{
var
invocationExpression
=
(
InvocationExpressionSyntax
)
token
.
Parent
.
Parent
;
if
(!
invocationExpression
.
IsRightSideOfDotOrArrowOrColonColon
()
&&
invocationExpression
.
Expression
.
IsKind
(
SyntaxKind
.
IdentifierName
)
&&
((
IdentifierNameSyntax
)
invocationExpression
.
Expression
).
Identifier
.
IsKindOrHasMatchingText
(
SyntaxKind
.
NameOfKeyword
))
{
parentExpression
=
invocationExpression
;
}
parentExpression
=
invocationExpression
;
}
if
(
parentExpression
!=
null
)
...
...
src/Workspaces/CSharp/Portable/Extensions/ExpressionSyntaxExtensions.cs
浏览文件 @
5db2d642
...
...
@@ -412,9 +412,7 @@ public static bool IsNamedArgumentIdentifier(this ExpressionSyntax expression)
}
public
static
bool
IsInsideNameOf
(
this
ExpressionSyntax
expression
)
{
return
expression
.
SyntaxTree
.
IsNameOfContext
(
expression
.
SpanStart
);
}
=>
expression
.
SyntaxTree
.
IsNameOfContext
(
expression
.
SpanStart
);
private
static
bool
CanReplace
(
ISymbol
symbol
)
{
...
...
@@ -1114,6 +1112,23 @@ private static bool ValidateAliasForTarget(IAliasSymbol aliasReplacement, Semant
{
var
aliasName
=
aliasReplacement
.
Name
;
// If we're the argument of a nameof(X.Y) call, then we can't simplify to an
// alias unless the alias has the same name as us (i.e. 'Y').
if
(
node
.
IsNameOfArgumentExpression
())
{
var
nameofValueOpt
=
semanticModel
.
GetConstantValue
(
node
.
Parent
.
Parent
.
Parent
);
if
(!
nameofValueOpt
.
HasValue
)
{
return
false
;
}
if
(
nameofValueOpt
.
Value
is
string
existingVal
&&
existingVal
!=
aliasName
)
{
return
false
;
}
}
var
boundSymbols
=
semanticModel
.
LookupNamespacesAndTypes
(
node
.
SpanStart
,
name
:
aliasName
);
if
(
boundSymbols
.
Length
==
1
)
...
...
@@ -1128,6 +1143,20 @@ private static bool ValidateAliasForTarget(IAliasSymbol aliasReplacement, Semant
return
false
;
}
public
static
bool
IsNameOfArgumentExpression
(
this
ExpressionSyntax
expression
)
{
return
expression
.
IsParentKind
(
SyntaxKind
.
Argument
)
&&
expression
.
Parent
.
IsParentKind
(
SyntaxKind
.
ArgumentList
)
&&
expression
.
Parent
.
Parent
.
Parent
is
InvocationExpressionSyntax
invocation
&&
invocation
.
IsNameOfInvocation
();
}
public
static
bool
IsNameOfInvocation
(
this
InvocationExpressionSyntax
invocation
)
{
return
invocation
.
Expression
is
IdentifierNameSyntax
identifierName
&&
identifierName
.
Identifier
.
IsKindOrHasMatchingText
(
SyntaxKind
.
NameOfKeyword
);
}
public
static
IAliasSymbol
GetAliasForSymbol
(
INamespaceOrTypeSymbol
symbol
,
SyntaxToken
token
,
SemanticModel
semanticModel
,
CancellationToken
cancellationToken
)
{
var
originalSemanticModel
=
semanticModel
.
GetOriginalSemanticModel
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录