提交 d523c633 编写于 作者: C Cyrus Najmabadi

VB can simplify static member access off of 'me'.

上级 56d5b11b
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. ' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Threading.Tasks
Imports Microsoft.CodeAnalysis.CodeStyle Imports Microsoft.CodeAnalysis.CodeStyle
Imports Microsoft.CodeAnalysis.CSharp.CodeStyle Imports Microsoft.CodeAnalysis.CSharp.CodeStyle
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
...@@ -5908,6 +5907,46 @@ End Class ...@@ -5908,6 +5907,46 @@ End Class
Await TestAsync(input, expected, DontPreferIntrinsicPredefinedTypeKeywordInDeclaration) Await TestAsync(input, expected, DontPreferIntrinsicPredefinedTypeKeywordInDeclaration)
End Function End Function
<Fact(), Trait(Traits.Feature, Traits.Features.Simplification)>
Public Async Function TestSharedMemberOffOfMe() As Task
' even if the user prefers qualified member access, we will strip off 'Me' when calling
' a static method through it.
Dim input =
<Workspace>
<Project Language="Visual Basic" CommonReferences="true">
<Document>
<![CDATA[
Imports System
Class Class1
Sub Main()
{|SimplifyParent:Me.Goo|}()
End Sub
Shared Sub Goo()
End Sub
End Class
]]>
</Document>
</Project>
</Workspace>
Dim expected =
<text>
<![CDATA[
Imports System
Class Class1
Sub Main()
Goo()
End Sub
Shared Sub Goo()
End Sub
End Class
]]></text>
Await TestAsync(input, expected, QualifyMethodAccessOption(LanguageNames.VisualBasic))
End Function
#End Region #End Region
#Region "Helpers" #Region "Helpers"
......
...@@ -820,7 +820,7 @@ private static bool CanReplace(ISymbol symbol) ...@@ -820,7 +820,7 @@ private static bool CanReplace(ISymbol symbol)
return false; return false;
if (memberAccess.Expression.IsKind(SyntaxKind.ThisExpression) && if (memberAccess.Expression.IsKind(SyntaxKind.ThisExpression) &&
!SimplificationHelpers.ShouldSimplifyMemberAccessExpression(semanticModel, optionSet, symbol)) !SimplificationHelpers.ShouldSimplifyThisOrMeMemberAccessExpression(semanticModel, optionSet, symbol))
{ {
return false; return false;
} }
......
...@@ -101,11 +101,14 @@ internal static bool IsValidSymbolInfo(ISymbol symbol) ...@@ -101,11 +101,14 @@ internal static bool IsValidSymbolInfo(ISymbol symbol)
return symbol != null && !symbol.IsErrorType(); return symbol != null && !symbol.IsErrorType();
} }
internal static bool ShouldSimplifyMemberAccessExpression( internal static bool ShouldSimplifyThisOrMeMemberAccessExpression(
SemanticModel semanticModel, OptionSet optionSet, ISymbol symbol) SemanticModel semanticModel, OptionSet optionSet, ISymbol symbol)
{ {
// If we're accessing a static member off of this/me then we should always consider this
// simplifiable. Note: in C# this isn't even legal to access a static off of `this`,
// but in VB it is legal to access a static off of `me`.
if (symbol.IsStatic) if (symbol.IsStatic)
return false; return true;
if ((symbol.IsKind(SymbolKind.Field) && optionSet.GetOption(CodeStyleOptions.QualifyFieldAccess, semanticModel.Language).Value || if ((symbol.IsKind(SymbolKind.Field) && optionSet.GetOption(CodeStyleOptions.QualifyFieldAccess, semanticModel.Language).Value ||
(symbol.IsKind(SymbolKind.Property) && optionSet.GetOption(CodeStyleOptions.QualifyPropertyAccess, semanticModel.Language).Value) || (symbol.IsKind(SymbolKind.Property) && optionSet.GetOption(CodeStyleOptions.QualifyPropertyAccess, semanticModel.Language).Value) ||
......
...@@ -985,7 +985,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions ...@@ -985,7 +985,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
End If End If
If memberAccess.Expression.IsKind(SyntaxKind.MeExpression) AndAlso If memberAccess.Expression.IsKind(SyntaxKind.MeExpression) AndAlso
Not SimplificationHelpers.ShouldSimplifyMemberAccessExpression(semanticModel, optionSet, symbol) Then Not SimplificationHelpers.ShouldSimplifyThisOrMeMemberAccessExpression(semanticModel, optionSet, symbol) Then
Return False Return False
End If End If
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册