Skip to content

Commit

Permalink
Improve Code Analyzer (ardalis#30). Ignore static methods and constru…
Browse files Browse the repository at this point in the history
…ctors. (ardalis#33)
  • Loading branch information
ppittle authored Aug 17, 2020
1 parent 610d335 commit 9255320
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ private void AnalyzeMethodDeclaration(SymbolAnalysisContext context)
// not a method declaration
if (null == methodSymbol) return;

// ignore constructors
if (methodSymbol.MethodKind == MethodKind.Constructor) return;

// ignore everything except 'ordinary' methods (delegates, operators, etc)
if (methodSymbol.MethodKind != MethodKind.Ordinary) return;

// ignore statics
if (methodSymbol.IsStatic) return;

// isn't public
if (methodSymbol.DeclaredAccessibility != Accessibility.Public) return;

Expand All @@ -70,7 +73,10 @@ private void AnalyzeMethodDeclaration(SymbolAnalysisContext context)
.ContainingType
.GetMembers()
.OfType<IMethodSymbol>()
.Where(m => m.DeclaredAccessibility == Accessibility.Public)
.Where(m =>
!m.IsStatic &&
m.MethodKind == MethodKind.Ordinary &&
m.DeclaredAccessibility == Accessibility.Public)
.ToList();

// and sort the methods so the "most correct" is first
Expand Down Expand Up @@ -126,8 +132,8 @@ public int Compare(IMethodSymbol x, IMethodSymbol y)
return 1;
}

// give precedence to whoever has the shorter method name
return x.Name.Length.CompareTo(y.Name.Length);
// give precedence to which ever came first - so always x
return -1;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ public override async Task<ActionResult<object>> HandleAsync([FromBody]object re
}
}";

private const string ValidEndpointWithExtraStaticMethod = @"
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Ardalis.ApiEndpoints;
namespace ApiEndpointsAnalyzersTest
{
public class TestEndpoint : BaseAsyncEndpoint<object, object>
{
public override async Task<ActionResult<object>> HandleAsync([FromBody]object request)
{
throw new Exception();
}
public static void ExtraMethod(){}
}
}";

private const string ValidEndpointWithPublicConstructor = @"
using System;
using System.Threading.Tasks;
Expand Down Expand Up @@ -57,6 +76,48 @@ public override async Task<ActionResult<object>> HandleAsync([FromBody]object re
namespace ApiEndpointsAnalyzersTest
{
public class TestEndpoint : BaseAsyncEndpoint
{
public TestEndpoint() { }
public override async Task<ActionResult<object>> HandleAsync([FromBody]object request)
{
return base.FooBar();
}
}
}";

private const string ValidEndpointUsingCustomBaseClass = @"
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Ardalis.ApiEndpoints;
namespace ApiEndpointsAnalyzersTest
{
public abstract class CustomBase : BaseAsyncEndpoint
{
public abstract Task<ActionResult<object>> HandleAsync([FromBody]object request);
}
public class TestEndpoint : CustomBase
{
public override async Task<ActionResult<object>> HandleAsync([FromBody]object request)
{
return base.FooBar();
}
}
}";

private const string ValidEndpointWithCustomBaseAsyncEndpointDefined = @"
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace ApiEndpointsAnalyzersTest
{
public abstract class BaseAsyncEndpoint { }
public class TestEndpoint : BaseAsyncEndpoint
{
public async Task<ActionResult<object>> HandleAsync([FromBody]object request)
Expand Down Expand Up @@ -501,9 +562,12 @@ public override ActionResult<object> HandleAsync([FromBody]object request)
[DataTestMethod]
[DataRow(""),
DataRow(ValidEndpoint),
DataRow(ValidEndpointWithExtraStaticMethod),
DataRow(ValidEndpointWithExtraNonPublicMethods),
DataRow(ValidEndpointWithPublicConstructor),
DataRow(ValidEndpointUsingNonGenericBaseClass)]
DataRow(ValidEndpointUsingNonGenericBaseClass),
DataRow(ValidEndpointUsingCustomBaseClass),
DataRow(ValidEndpointWithCustomBaseAsyncEndpointDefined)]
public void WhenTestCodeIsValidNoDiagnosticIsTriggered(string testCode)
{
VerifyCSharpDiagnostic(testCode);
Expand Down

0 comments on commit 9255320

Please sign in to comment.