diff --git a/src/EditorFeatures/VisualBasicTest/Diagnostics/SimplifyTypeNames/SimplifyTypeNamesTests.vb b/src/EditorFeatures/VisualBasicTest/Diagnostics/SimplifyTypeNames/SimplifyTypeNamesTests.vb index 07675464ece8629eeb0842bed86041fc5deffea6..96d4ac5a27a2c634470706ec059ddcab2995587b 100644 --- a/src/EditorFeatures/VisualBasicTest/Diagnostics/SimplifyTypeNames/SimplifyTypeNamesTests.vb +++ b/src/EditorFeatures/VisualBasicTest/Diagnostics/SimplifyTypeNames/SimplifyTypeNamesTests.vb @@ -120,6 +120,94 @@ Module Program End Module") End Function + + + 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 + + + + 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 + + + + 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 + Public Async Function TestWithCursorAtBeginning() As Task Await TestInRegularAndScriptAsync( diff --git a/src/Workspaces/VisualBasic/Portable/Extensions/ExpressionSyntaxExtensions.vb b/src/Workspaces/VisualBasic/Portable/Extensions/ExpressionSyntaxExtensions.vb index 095fed6c2c48bd31e0db29ffa618912f304416f2..ee8e8a0176b1bd9e204494bb17e8f2d65f9cb8e6 100644 --- a/src/Workspaces/VisualBasic/Portable/Extensions/ExpressionSyntaxExtensions.vb +++ b/src/Workspaces/VisualBasic/Portable/Extensions/ExpressionSyntaxExtensions.vb @@ -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 + + Public Function IsNameOfArgumentExpression(expression As ExpressionSyntax) As Boolean + Return expression.IsParentKind(SyntaxKind.NameOfExpression) + End Function + Private Function CanReplaceWithReducedName( name As NameSyntax,