提交 d1a18140 编写于 作者: C CyrusNajmabadi

VB side of do-not-simplify-nameof if it changes semantics.

上级 5db2d642
......@@ -120,6 +120,94 @@ Module Program
End Module")
End Function
<WorkItem(21449, "https://github.com/dotnet/roslyn/issues/21449")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsSimplifyTypeNames)>
Public Async Function DoNotChangeToAliasInNameOfIfItChangesNameOfName() As Task
Await TestInRegularAndScript1Async(
"Imports System
Imports Foo = SimplifyInsideNameof.Program
namespace SimplifyInsideNameof
class Program
shared sub Main()
Console.WriteLine(nameof([|SimplifyInsideNameof.Program|]))
end sub
end class
end namespace",
"Imports System
Imports Foo = SimplifyInsideNameof.Program
namespace SimplifyInsideNameof
class Program
shared sub Main()
Console.WriteLine(nameof(Program))
end sub
end class
end namespace")
End Function
<WorkItem(21449, "https://github.com/dotnet/roslyn/issues/21449")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsSimplifyTypeNames)>
Public Async Function DoChangeToAliasInNameOfIfItDoesNotAffectName1() As Task
Await TestInRegularAndScriptAsync(
"Imports System
Imports Goo = SimplifyInsideNameof.Program
namespace SimplifyInsideNameof
class Program
shared sub Main()
Console.WriteLine(nameof([|SimplifyInsideNameof.Program|].Main))
end sub
end class
end namespace",
"Imports System
Imports Goo = SimplifyInsideNameof.Program
namespace SimplifyInsideNameof
class Program
shared sub Main()
Console.WriteLine(nameof(Goo.Main))
end sub
end class
end namespace")
End Function
<WorkItem(21449, "https://github.com/dotnet/roslyn/issues/21449")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsSimplifyTypeNames)>
Public Async Function DoChangeToAliasInNameOfIfItDoesNotAffectName2() As Task
Await TestInRegularAndScriptAsync(
"Imports System
Imports Goo = N.Goo
namespace N
class Goo
end class
end namespace
namespace SimplifyInsideNameof
class Program
shared sub Main()
Console.WriteLine(nameof([|N.Goo|]))
end sub
end class
end namespace",
"Imports System
Imports Goo = N.Goo
namespace N
class Goo
end class
end namespace
namespace SimplifyInsideNameof
class Program
shared sub Main()
Console.WriteLine(nameof(Goo))
end sub
end class
end namespace")
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsSimplifyTypeNames)>
Public Async Function TestWithCursorAtBeginning() As Task
Await TestInRegularAndScriptAsync(
......
......@@ -1761,6 +1761,21 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
' It's possible there is another symbol with the same name as the alias that binds first
Private Function ValidateAliasForTarget(aliasReplacement As IAliasSymbol, semanticModel As SemanticModel, node As ExpressionSyntax, symbol As ISymbol) As Boolean
Dim 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() Then
Dim nameofValueOpt = semanticModel.GetConstantValue(node.Parent.Parent.Parent)
If Not nameofValueOpt.HasValue Then
Return False
End If
Dim existingValue = TryCast(nameofValueOpt.Value, String)
If existingValue Is Nothing OrElse existingValue <> aliasName Then
Return False
End If
End If
Dim boundSymbols = semanticModel.LookupNamespacesAndTypes(node.SpanStart, name:=aliasName)
If boundSymbols.Length = 1 Then
Dim boundAlias = TryCast(boundSymbols(0), IAliasSymbol)
......@@ -1776,6 +1791,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
Return False
End Function
<Extension>
Public Function IsNameOfArgumentExpression(expression As ExpressionSyntax) As Boolean
Return expression.IsParentKind(SyntaxKind.NameOfExpression)
End Function
<Extension()>
Private Function CanReplaceWithReducedName(
name As NameSyntax,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册