Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
8ce70292
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,体验更适合开发者的 AI 搜索 >>
提交
8ce70292
编写于
1月 19, 2020
作者:
S
Sam Harwell
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Calculate aliases at the start of analysis
上级
32881a23
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
75 addition
and
15 deletion
+75
-15
src/Features/CSharp/Portable/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.cs
...table/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.cs
+45
-8
src/Features/VisualBasic/Portable/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.vb
...table/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.vb
+30
-7
未找到文件。
src/Features/CSharp/Portable/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.cs
浏览文件 @
8ce70292
...
...
@@ -15,6 +15,8 @@ namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.SimplifyTypeNames
{
internal
class
TypeSyntaxSimplifierWalker
:
CSharpSyntaxWalker
{
private
static
readonly
ImmutableHashSet
<
string
>
s_emptyAliasedNames
=
ImmutableHashSet
.
Create
<
string
>(
StringComparer
.
Ordinal
);
/// <summary>
/// This set contains the full names of types that have equivalent predefined names in the language.
/// </summary>
...
...
@@ -48,7 +50,7 @@ internal class TypeSyntaxSimplifierWalker : CSharpSyntaxWalker
/// This is used so we can easily tell if we should try to simplify some identifier to an
/// alias when we encounter it.
/// </summary>
private
ImmutableHashSet
<
string
>
_aliasedNames
=
ImmutableHashSet
.
Create
<
string
>(
StringComparer
.
Ordinal
)
;
private
readonly
ImmutableHashSet
<
string
>
_aliasedNames
;
public
List
<
Diagnostic
>
Diagnostics
{
get
;
}
=
new
List
<
Diagnostic
>();
...
...
@@ -59,23 +61,58 @@ public TypeSyntaxSimplifierWalker(CSharpSimplifyTypeNamesDiagnosticAnalyzer anal
_semanticModel
=
semanticModel
;
_optionSet
=
optionSet
;
_cancellationToken
=
cancellationToken
;
var
root
=
semanticModel
.
SyntaxTree
.
GetRoot
(
cancellationToken
);
_aliasedNames
=
GetAliasedNames
(
root
as
CompilationUnitSyntax
);
}
p
ublic
override
void
VisitUsingDirective
(
UsingDirectiveSyntax
node
)
p
rivate
static
ImmutableHashSet
<
string
>
GetAliasedNames
(
CompilationUnitSyntax
?
compilationUnit
)
{
if
(
node
.
Alias
is
object
)
var
aliasedNames
=
s_emptyAliasedNames
;
if
(
compilationUnit
is
null
)
return
aliasedNames
;
foreach
(
var
usingDirective
in
compilationUnit
.
Usings
)
{
if
(
node
.
Name
.
GetRightmostName
()
is
IdentifierNameSyntax
identifierName
)
AddAliasedName
(
usingDirective
);
}
foreach
(
var
member
in
compilationUnit
.
Members
)
{
if
(
member
is
NamespaceDeclarationSyntax
namespaceDeclaration
)
AddAliasedNames
(
namespaceDeclaration
);
}
return
aliasedNames
;
void
AddAliasedName
(
UsingDirectiveSyntax
usingDirective
)
{
if
(
usingDirective
.
Alias
is
object
)
{
var
identifierAlias
=
identifierName
.
Identifier
.
ValueText
;
if
(!
RoslynString
.
IsNullOrEmpty
(
identifierAlias
))
if
(
usingDirective
.
Name
.
GetRightmostName
()
is
IdentifierNameSyntax
identifierName
)
{
ImmutableInterlocked
.
Update
(
ref
_aliasedNames
,
(
set
,
alias
)
=>
set
.
Add
(
alias
),
identifierAlias
);
var
identifierAlias
=
identifierName
.
Identifier
.
ValueText
;
if
(!
RoslynString
.
IsNullOrEmpty
(
identifierAlias
))
{
aliasedNames
=
aliasedNames
.
Add
(
identifierAlias
);
}
}
}
}
base
.
VisitUsingDirective
(
node
);
void
AddAliasedNames
(
NamespaceDeclarationSyntax
namespaceDeclaration
)
{
foreach
(
var
usingDirective
in
namespaceDeclaration
.
Usings
)
{
AddAliasedName
(
usingDirective
);
}
foreach
(
var
member
in
namespaceDeclaration
.
Members
)
{
if
(
member
is
NamespaceDeclarationSyntax
memberNamespace
)
AddAliasedNames
(
memberNamespace
);
}
}
}
public
override
void
VisitQualifiedName
(
QualifiedNameSyntax
node
)
...
...
src/Features/VisualBasic/Portable/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.vb
浏览文件 @
8ce70292
...
...
@@ -9,6 +9,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.SimplifyTypeNames
Friend
Class
TypeSyntaxSimplifierWalker
Inherits
VisualBasicSyntaxWalker
Private
Shared
ReadOnly
s_emptyAliasedNames
As
ImmutableHashSet
(
Of
String
)
=
ImmutableHashSet
.
Create
(
Of
String
)(
CaseInsensitiveComparison
.
Comparer
)
Private
Shared
ReadOnly
s_predefinedTypeMetadataNames
As
ImmutableHashSet
(
Of
String
)
=
ImmutableHashSet
.
Create
(
CaseInsensitiveComparison
.
Comparer
,
NameOf
(
[
Boolean
]
),
...
...
@@ -39,7 +41,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.SimplifyTypeNames
''' This is used so we can easily tell if we should try to simplify some identifier to an
''' alias when we encounter it.
''' </summary>
Private
_aliasedNames
As
ImmutableHashSet
(
Of
String
)
=
ImmutableHashSet
.
Create
(
Of
String
)(
CaseInsensitiveComparison
.
Comparer
)
Private
ReadOnly
_aliasedNames
As
ImmutableHashSet
(
Of
String
)
Public
ReadOnly
Property
Diagnostics
As
List
(
Of
Diagnostic
)
=
New
List
(
Of
Diagnostic
)()
...
...
@@ -51,22 +53,43 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.SimplifyTypeNames
_optionSet
=
optionSet
_cancellationToken
=
cancellationToken
Dim
root
=
semanticModel
.
SyntaxTree
.
GetRoot
(
cancellationToken
)
_aliasedNames
=
GetAliasedNames
(
TryCast
(
root
,
CompilationUnitSyntax
))
For
Each
aliasSymbol
In
semanticModel
.
Compilation
.
AliasImports
()
_aliasedNames
=
_aliasedNames
.
Add
(
aliasSymbol
.
Target
.
Name
)
Next
End
Sub
Public
Overrides
Sub
VisitSimpleImportsClause
(
node
As
SimpleImportsClauseSyntax
)
If
node
.
Alias
IsNot
Nothing
Then
Dim
identifierName
=
TryCast
(
node
.
Name
.
GetRightmostName
(),
IdentifierNameSyntax
)
Private
Shared
Function
GetAliasedNames
(
compilationUnit
As
CompilationUnitSyntax
)
As
ImmutableHashSet
(
Of
String
)
Dim
aliasedNames
=
s_emptyAliasedNames
If
compilationUnit
Is
Nothing
Then
Return
aliasedNames
End
If
For
Each
importsStatement
In
compilationUnit
.
Imports
For
Each
importsClause
In
importsStatement
.
ImportsClauses
Dim
simpleImportsClause
=
TryCast
(
importsClause
,
SimpleImportsClauseSyntax
)
If
simpleImportsClause
Is
Nothing
Then
Continue
For
End
If
AddAliasedName
(
aliasedNames
,
simpleImportsClause
)
Next
Next
Return
aliasedNames
End
Function
Private
Shared
Sub
AddAliasedName
(
ByRef
aliasedNames
As
ImmutableHashSet
(
Of
String
),
simpleImportsClause
As
SimpleImportsClauseSyntax
)
If
simpleImportsClause
.
Alias
IsNot
Nothing
Then
Dim
identifierName
=
TryCast
(
simpleImportsClause
.
Name
.
GetRightmostName
(),
IdentifierNameSyntax
)
If
identifierName
IsNot
Nothing
Then
If
Not
String
.
IsNullOrEmpty
(
identifierName
.
Identifier
.
ValueText
)
Then
ImmutableInterlocked
.
Update
(
_aliasedNames
,
Function
(
names
,
identifier
)
names
.
Add
(
identifier
),
identifierName
.
Identifier
.
ValueText
)
aliasedNames
=
aliasedNames
.
Add
(
identifierName
.
Identifier
.
ValueText
)
End
If
End
If
End
If
MyBase
.
VisitSimpleImportsClause
(
node
)
End
Sub
Public
Overrides
Sub
VisitQualifiedName
(
node
As
QualifiedNameSyntax
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录