未验证 提交 4abc5dd5 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #23835 from MaStr11/UseAutoPropertySuppressFixForExplicitInterface

Suppress analyzer for properties with explicit interface implementation
......@@ -22,12 +22,15 @@ protected override bool SupportsReadOnlyProperties(Compilation compilation)
protected override bool SupportsPropertyInitializer(Compilation compilation)
=> ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp6;
protected override bool CanExplicitInterfaceImplementationsBeFixed()
=> false;
protected override void AnalyzeCompilationUnit(
SemanticModelAnalysisContext context, SyntaxNode root, List<AnalysisResult> analysisResults)
=> AnalyzeMembers(context, ((CompilationUnitSyntax)root).Members, analysisResults);
private void AnalyzeMembers(
SemanticModelAnalysisContext context,
SemanticModelAnalysisContext context,
SyntaxList<MemberDeclarationSyntax> members,
List<AnalysisResult> analysisResults)
{
......
......@@ -1000,5 +1000,64 @@ int Q
int Q { get; }
}", fixAllActionEquivalenceKey: FeaturesResources.Use_auto_property);
}
[WorkItem(23735, "https://github.com/dotnet/roslyn/issues/23735")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)]
public async Task ExplicitInterfaceImplementationGetterOnly()
{
await TestMissingInRegularAndScriptAsync(@"
namespace RoslynSandbox
{
public interface IFoo
{
object Bar { get; }
}
class Foo : IFoo
{
public Foo(object bar)
{
this.bar = bar;
}
readonly object [|bar|];
object IFoo.Bar
{
get { return bar; }
}
}
}");
}
[WorkItem(23735, "https://github.com/dotnet/roslyn/issues/23735")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)]
public async Task ExplicitInterfaceImplementationGetterAndSetter()
{
await TestMissingInRegularAndScriptAsync(@"
namespace RoslynSandbox
{
public interface IFoo
{
object Bar { get; set; }
}
class Foo : IFoo
{
public Foo(object bar)
{
this.bar = bar;
}
object [|bar|];
object IFoo.Bar
{
get { return bar; }
set { bar = value; }
}
}
}");
}
}
}
......@@ -21,6 +21,10 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UseAutoProperty
Return DirectCast(compilation, VisualBasicCompilation).LanguageVersion >= LanguageVersion.VisualBasic10
End Function
Protected Overrides Function CanExplicitInterfaceImplementationsBeFixed() As Boolean
Return True
End Function
Protected Overrides Sub AnalyzeCompilationUnit(context As SemanticModelAnalysisContext, root As SyntaxNode, analysisResults As List(Of AnalysisResult))
AnalyzeMembers(context, DirectCast(root, CompilationUnitSyntax).Members, analysisResults)
End Sub
......
......@@ -553,5 +553,50 @@ end class")
Public Property [|P|] As Integer
End Class")
End Function
<WorkItem(23735, "https://github.com/dotnet/roslyn/issues/23735")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)>
Public Async Function ExplicitInterfaceImplementation() As Task
Await TestInRegularAndScriptAsync("
Namespace RoslynSandbox
Public Interface IFoo
ReadOnly Property Bar() As Object
End Interface
Friend Class Foo
Implements IFoo
Private [|_bar|] As Object
Private ReadOnly Property Bar() As Object Implements IFoo.Bar
Get
Return _bar
End Get
End Property
Public Sub New(bar As Object)
_bar = bar
End Sub
End Class
End Namespace
",
"
Namespace RoslynSandbox
Public Interface IFoo
ReadOnly Property Bar() As Object
End Interface
Friend Class Foo
Implements IFoo
Private ReadOnly Property Bar() As Object Implements IFoo.Bar
Public Sub New(bar As Object)
Me.Bar = bar
End Sub
End Class
End Namespace
")
End Function
End Class
End Namespace
......@@ -31,6 +31,7 @@ protected AbstractUseAutoPropertyAnalyzer()
protected abstract void AnalyzeCompilationUnit(SemanticModelAnalysisContext context, SyntaxNode root, List<AnalysisResult> analysisResults);
protected abstract bool SupportsReadOnlyProperties(Compilation compilation);
protected abstract bool SupportsPropertyInitializer(Compilation compilation);
protected abstract bool CanExplicitInterfaceImplementationsBeFixed();
protected abstract TExpression GetFieldInitializer(TVariableDeclarator variable, CancellationToken cancellationToken);
protected abstract TExpression GetGetterExpression(IMethodSymbol getMethod, CancellationToken cancellationToken);
protected abstract TExpression GetSetterExpression(IMethodSymbol setMethod, SemanticModel semanticModel, CancellationToken cancellationToken);
......@@ -113,6 +114,11 @@ protected void AnalyzeProperty(SemanticModelAnalysisContext context, TPropertyDe
return;
}
if (!CanExplicitInterfaceImplementationsBeFixed() && property.ExplicitInterfaceImplementations.Length != 0)
{
return;
}
var containingType = property.ContainingType;
if (containingType == null)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册