Raise diagnostics when not exposing expression bodies members

This commit is contained in:
Koen Bekkenutte
2021-05-31 02:05:57 +08:00
parent 999aca522c
commit c87509e07f
5 changed files with 86 additions and 0 deletions
@@ -0,0 +1,3 @@
; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
@@ -0,0 +1,5 @@
### New Rules
Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
EFP0001 | Design | Error | ST0001_AnalyzerName
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
namespace EntityFrameworkCore.Projections.Generator
{
public static class Diagnostics
{
public static readonly DiagnosticDescriptor RequiresExpressionBodyDefinition = new(
id: "EFP0001",
title: "Method or property should expose an expression body definition",
messageFormat: "Method or property '{0}' should expose an expression body definition.",
category: "Design",
DiagnosticSeverity.Error,
isEnabledByDefault: true);
}
}
@@ -84,6 +84,13 @@ namespace EntityFrameworkCore.Projections.Generator
if (memberDeclarationSyntax is MethodDeclarationSyntax methodDeclarationSyntax)
{
if (methodDeclarationSyntax.ExpressionBody is null)
{
var diagnostic = Diagnostic.Create(Diagnostics.RequiresExpressionBodyDefinition, methodDeclarationSyntax.GetLocation(), memberSymbol.Name);
context.ReportDiagnostic(diagnostic);
return null;
}
descriptor.ReturnTypeName = methodDeclarationSyntax.ReturnType.ToString();
descriptor.Body = expressionSyntaxRewriter.Visit(methodDeclarationSyntax.ExpressionBody.Expression);
foreach (var additionalParameter in ((ParameterListSyntax)parameterSyntaxRewriter.Visit(methodDeclarationSyntax.ParameterList)).Parameters)
@@ -93,6 +100,13 @@ namespace EntityFrameworkCore.Projections.Generator
}
else if (memberDeclarationSyntax is PropertyDeclarationSyntax propertyDeclarationSyntax)
{
if (propertyDeclarationSyntax.ExpressionBody is null)
{
var diagnostic = Diagnostic.Create(Diagnostics.RequiresExpressionBodyDefinition, propertyDeclarationSyntax.GetLocation(), memberSymbol.Name);
context.ReportDiagnostic(diagnostic);
return null;
}
descriptor.ReturnTypeName = propertyDeclarationSyntax.Type.ToString();
descriptor.Body = expressionSyntaxRewriter.Visit(propertyDeclarationSyntax.ExpressionBody.Expression);
}
@@ -340,6 +340,49 @@ namespace Foo {
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public void BlockBodiedMember_RaisesDiagnostics()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projections;
namespace Foo {
class C {
[Projectable]
public int Foo
{
get => 1;
}
}
}
");
var result = RunGenerator(compilation);
Assert.Single(result.Diagnostics);
}
[Fact]
public void BlockBodiedMethod_RaisesDiagnostics()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projections;
namespace Foo {
class C {
[Projectable]
public int Foo()
{
return 1;
}
}
}
");
var result = RunGenerator(compilation);
Assert.Single(result.Diagnostics);
}
#region Helpers