提交 17d363eb 编写于 作者: J Jonathon Marolf

Generate types with the correct accessibility if they are part of public fields

fixes internal issue #1107929
上级 d61d8fda
......@@ -1951,5 +1951,36 @@ public void TestWithUsingStatic2()
{
TestMissing(@"using [|Sample|] ; ");
}
[WorkItem(1107929)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)]
public void TestAccesiblityForPublicFields()
{
Test(
@"class A { public B b = new [|B|](); }",
@"public class B { public B() { } }",
index: 0,
isAddedDocument:true);
}
[WorkItem(1107929)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)]
public void TestAccesiblityForPublicFields2()
{
Test(
@"class A { public B b = new [|B|](); }",
@"class A { public B b = new B(); } public class B { public B() {}}",
index: 1);
}
[WorkItem(1107929)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)]
public void TestAccesiblityForPublicFields3()
{
Test(
@"class A { public B b = new [|B|](); }",
@"class A { public B b = new B(); public class B { public B() {}}}",
index: 2);
}
}
}
......@@ -747,6 +747,34 @@ NewLines("Public Interface I \n Sub Foo(a As X.Y.Z) \n End Interface \n Public
index:=0)
End Sub
<WorkItem(1107929)>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)>
Public Sub TestAccesiblityForPublicFields()
Test(
NewLines("Public Class A \n Public B As New [|B|]() \n End Class"),
NewLines("Public Class B \n Public Sub New() \n End Sub \n End Class"),
index:=0,
isAddedDocument:=True)
End Sub
<WorkItem(1107929)>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)>
Public Sub TestAccesiblityForPublicFields2()
Test(
NewLines("Public Class A \n Public B As New [|B|]() \n End Class"),
NewLines("Public Class A \n Public B As New B() \n End Class \n\n Public Class B \n Public Sub New() \n End Sub \n End Class"),
index:=1)
End Sub
<WorkItem(1107929)>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)>
Public Sub TestAccesiblityForPublicFields3()
Test(
NewLines("Public Class A \n Public B As New [|B|]() \n End Class"),
NewLines("Public Class A \n Public B As New B() \n Public Class B \n Public Sub New() \n End Sub \n End Class \n End Class"),
index:=2)
End Sub
Public Class AddImportTestsWithAddImportDiagnosticProvider
Inherits AbstractVisualBasicDiagnosticProviderBasedUserDiagnosticTest
......
......@@ -366,6 +366,18 @@ public static ISet<INamespaceSymbol> GetUsingNamespacesInScope(this SemanticMode
variableDeclaration.Variables[0], cancellationToken)).DeclaredAccessibility;
}
// Also do the same check if we are in an object creation expression
if (type.IsParentKind(SyntaxKind.ObjectCreationExpression) &&
type.Parent.IsParentKind(SyntaxKind.EqualsValueClause) &&
type.Parent.Parent.IsParentKind(SyntaxKind.VariableDeclarator) &&
type.Parent.Parent.Parent.IsParentKind(SyntaxKind.VariableDeclaration) &&
type.Parent.Parent.Parent.Parent.IsParentKind(SyntaxKind.FieldDeclaration))
{
var variableDeclaration = (VariableDeclarationSyntax)type.Parent.Parent.Parent.Parent;
return ((ISymbol)semanticModel.GetDeclaredSymbol(
variableDeclaration.Variables[0], cancellationToken)).DeclaredAccessibility;
}
// 3) The return type of a delegate type must be at least as accessible as the
// delegate type itself.
// 6) The return type of a method must be at least as accessible as the method
......
......@@ -233,6 +233,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
Return containingType.DeclaredAccessibility
End If
' Determine accessibility of field or event
' Public B as B
If type.IsParentKind(SyntaxKind.SimpleAsClause) AndAlso
type.Parent.IsParentKind(SyntaxKind.VariableDeclarator) Then
If type.Parent.Parent.IsParentKind(SyntaxKind.FieldDeclaration) OrElse
......@@ -245,6 +247,21 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
End If
End If
' Determine accessibility of field or event
' Public B as New B()
If type.IsParentKind(SyntaxKind.ObjectCreationExpression) AndAlso
type.Parent.IsParentKind(SyntaxKind.AsNewClause) AndAlso
type.Parent.Parent.IsParentKind(SyntaxKind.VariableDeclarator) Then
If type.Parent.Parent.Parent.IsParentKind(SyntaxKind.FieldDeclaration) OrElse
type.Parent.Parent.Parent.IsParentKind(SyntaxKind.EventStatement) Then
Dim variableDeclarator = DirectCast(type.Parent.Parent.Parent, VariableDeclaratorSyntax)
If variableDeclarator.Names.Count > 0 Then
Dim variableDeclaration = semanticModel.GetDeclaredSymbol(variableDeclarator.Names(0), cancellationToken)
Return variableDeclaration.DeclaredAccessibility
End If
End If
End If
If type.IsParentKind(SyntaxKind.SimpleAsClause) Then
If type.Parent.IsParentKind(SyntaxKind.DelegateFunctionStatement) OrElse
type.Parent.IsParentKind(SyntaxKind.FunctionStatement) OrElse
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册