mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-08-15 18:44:57 +00:00
Raise diagnostics when not exposing expression bodies members
This commit is contained in:
@@ -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 (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.ReturnTypeName = methodDeclarationSyntax.ReturnType.ToString();
|
||||||
descriptor.Body = expressionSyntaxRewriter.Visit(methodDeclarationSyntax.ExpressionBody.Expression);
|
descriptor.Body = expressionSyntaxRewriter.Visit(methodDeclarationSyntax.ExpressionBody.Expression);
|
||||||
foreach (var additionalParameter in ((ParameterListSyntax)parameterSyntaxRewriter.Visit(methodDeclarationSyntax.ParameterList)).Parameters)
|
foreach (var additionalParameter in ((ParameterListSyntax)parameterSyntaxRewriter.Visit(methodDeclarationSyntax.ParameterList)).Parameters)
|
||||||
@@ -93,6 +100,13 @@ namespace EntityFrameworkCore.Projections.Generator
|
|||||||
}
|
}
|
||||||
else if (memberDeclarationSyntax is PropertyDeclarationSyntax propertyDeclarationSyntax)
|
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.ReturnTypeName = propertyDeclarationSyntax.Type.ToString();
|
||||||
descriptor.Body = expressionSyntaxRewriter.Visit(propertyDeclarationSyntax.ExpressionBody.Expression);
|
descriptor.Body = expressionSyntaxRewriter.Visit(propertyDeclarationSyntax.ExpressionBody.Expression);
|
||||||
}
|
}
|
||||||
|
|||||||
+43
@@ -340,6 +340,49 @@ namespace Foo {
|
|||||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
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
|
#region Helpers
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user