Skip to content

Commit

Permalink
禁止[AutoDto]重复标注&禁止标注abstract类的分析器 #4
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed Nov 8, 2023
1 parent cf728e6 commit 3d8a171
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
3 changes: 0 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
[*.cs]

# GEN001: 没有实现基础接口因此不能生成类
dotnet_diagnostic.GEN001.severity = error

# Default severity for analyzer diagnostics with category 'StyleCop.CSharp.DocumentationRules'
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.DocumentationRules.severity = none

Expand Down
43 changes: 33 additions & 10 deletions Biwen.AutoClassGen.Gen/SourceGenAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public class SourceGenAnalyzer : DiagnosticAnalyzer
public const string GEN021 = "GEN021";
public const string GEN031 = "GEN031"; // 推荐生成
public const string GEN041 = "GEN041"; // 重复标注
public const string GEN042 = "GEN042"; // 不可用于abstract基类

/// <summary>
/// 无法生成类的错误
/// </summary>
private static readonly DiagnosticDescriptor InvalidDeclareError = new(id: GEN001,
public static readonly DiagnosticDescriptor InvalidDeclareError = new(id: GEN001,
title: "标注接口没有继承基础接口因此不能生成类",
messageFormat: "没有实现基础接口因此不能生成类,请删除标注的特性[AutoGen] or 继承相应的接口",
category: typeof(SourceGenerator).Assembly.GetName().Name,
Expand All @@ -36,7 +37,7 @@ public class SourceGenAnalyzer : DiagnosticAnalyzer
/// <summary>
/// 重名错误
/// </summary>
private static readonly DiagnosticDescriptor InvalidDeclareNameError = new(id: GEN011,
public static readonly DiagnosticDescriptor InvalidDeclareNameError = new(id: GEN011,
title: "生成类的类名称不可和接口名重名",
messageFormat: "生成类的类名称不可和接口名重名",
category: typeof(SourceGenerator).Assembly.GetName().Name,
Expand All @@ -47,7 +48,7 @@ public class SourceGenAnalyzer : DiagnosticAnalyzer
/// <summary>
/// 命名空间规范警告
/// </summary>
private static readonly DiagnosticDescriptor SuggestDeclareNameWarning = new(id: GEN021,
public static readonly DiagnosticDescriptor SuggestDeclareNameWarning = new(id: GEN021,
title: "推荐使用相同的命名空间",
messageFormat: "推荐使用相同的命名空间",
category: typeof(SourceGenerator).Assembly.GetName().Name,
Expand All @@ -58,7 +59,7 @@ public class SourceGenAnalyzer : DiagnosticAnalyzer
/// <summary>
/// 推荐使用自动生成
/// </summary>
private static readonly DiagnosticDescriptor SuggestAutoGen = new(id: GEN031,
public static readonly DiagnosticDescriptor SuggestAutoGen = new(id: GEN031,
title: "使用[AutoGen]自动生成",
messageFormat: "使用[AutoGen]自动生成",
category: typeof(SourceGenerator).Assembly.GetName().Name,
Expand All @@ -70,7 +71,7 @@ public class SourceGenAnalyzer : DiagnosticAnalyzer
/// <summary>
/// Dto特性重复标注
/// </summary>
private static readonly DiagnosticDescriptor MutiMarkedAutoDtoError = new(id: GEN041,
public static readonly DiagnosticDescriptor MutiMarkedAutoDtoError = new(id: GEN041,
title: "重复标注[AutoDto]",
messageFormat: "重复标注了[AutoDto],请删除多余的标注",
category: typeof(SourceGenerator).Assembly.GetName().Name,
Expand All @@ -79,7 +80,16 @@ public class SourceGenAnalyzer : DiagnosticAnalyzer
isEnabledByDefault: true);



/// <summary>
/// Dto错误标注
/// </summary>
public static readonly DiagnosticDescriptor MarkedAbstractAutoDtoError = new(id: GEN042,
title: "不可在abstract类上标注[AutoDto]",
messageFormat: "不可在abstract类上标注[AutoDto]",
category: typeof(SourceGenerator).Assembly.GetName().Name,
DiagnosticSeverity.Error,
helpLinkUri: Helplink,
isEnabledByDefault: true);

#endregion

Expand All @@ -88,7 +98,8 @@ public class SourceGenAnalyzer : DiagnosticAnalyzer
InvalidDeclareNameError,
SuggestDeclareNameWarning,
SuggestAutoGen,
MutiMarkedAutoDtoError);
MutiMarkedAutoDtoError,
MarkedAbstractAutoDtoError);

private const string AttributeValueMetadataName = "AutoGen";
/// <summary>
Expand All @@ -105,6 +116,7 @@ public override void Initialize(AnalysisContext context)
context.RegisterSyntaxNodeAction(ctx =>
{
var kind = ctx.Node.Kind();

// InterfaceDeclarationSyntax
if (kind == SyntaxKind.InterfaceDeclaration)
{
Expand Down Expand Up @@ -166,6 +178,7 @@ public override void Initialize(AnalysisContext context)
}
}
}

// ClassDeclarationSyntax
if (kind == SyntaxKind.ClassDeclaration)
{
Expand All @@ -177,15 +190,25 @@ public override void Initialize(AnalysisContext context)
if (attr.Attributes.Where(x => x.Name.ToString().IndexOf(
AttributeValueMetadataNameDto, StringComparison.Ordinal) == 0).Count() > 1)
{
var location = declaration.GetLocation();

var location = attr.GetLocation();
// issue error
ctx.ReportDiagnostic(Diagnostic.Create(MutiMarkedAutoDtoError, location));
}

if (attr.Attributes.Where(x => x.Name.ToString().IndexOf(
AttributeValueMetadataNameDto, StringComparison.Ordinal) == 0).Any())
{
if (declaration.Modifiers.Any(x => x.ValueText == "abstract"))
{
var location = attr.Attributes.Where(
x => x.Name.ToString().IndexOf(AttributeValueMetadataNameDto, StringComparison.Ordinal) == 0).First().GetLocation();
// issue error
ctx.ReportDiagnostic(Diagnostic.Create(MarkedAbstractAutoDtoError, location));
}
}
}
}
}

},
SyntaxKind.InterfaceDeclaration,
SyntaxKind.ClassDeclaration);
Expand Down
13 changes: 11 additions & 2 deletions Biwen.AutoClassGen.Gen/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Biwen.AutoClassGen
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -63,7 +65,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

#endregion


#region AutoDtoAttributeG

var nodesDtoG = context.SyntaxProvider.ForAttributeWithMetadataName(
Expand Down Expand Up @@ -274,7 +275,6 @@ private static void HandleAnnotatedNodesDto(Compilation compilation, ImmutableAr
continue;
}


//转译的Entity类名
var entityName = string.Empty;
var eType = (attributeSyntax.ArgumentList!.Arguments[0].Expression as TypeOfExpressionSyntax)!.Type;
Expand All @@ -295,6 +295,15 @@ private static void HandleAnnotatedNodesDto(Compilation compilation, ImmutableAr
continue;
}

if (node.AttributeLists.AsEnumerable().Where(
x => x.Attributes[0].Name.ToString().IndexOf(AttributeValueMetadataNameDto, StringComparison.Ordinal) == 0).Count() > 1)
{
var location = node.GetLocation();
// issue error
context.ReportDiagnostic(Diagnostic.Create(SourceGenAnalyzer.MutiMarkedAutoDtoError, location));
continue;
}

var sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine($"//generate {entityName}-{node.Identifier.ValueText}");
Expand Down
15 changes: 4 additions & 11 deletions Biwen.AutoClassGen.TestConsole/Dtos/UserDto.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Biwen.AutoClassGen.TestConsole.Dtos;
using Biwen.AutoClassGen.TestConsole.Entitys;


#pragma warning disable
namespace Biwen.AutoClassGen.TestConsole.Dtos
namespace Biwen.AutoClassGen.TestConsole.Dtos
{
using Biwen.AutoClassGen.TestConsole.Entitys;

/// <summary>
/// to be generated
Expand All @@ -27,10 +23,10 @@ public partial class User2Dto
/// another way to be generated
/// </summary>
[AutoDto<User>(nameof(User.Email), "TestCol")]
//[AutoDto(typeof(User), nameof(User.Email))]
public partial class User3Dto
{
}

}

namespace Biwen.AutoClassGen.TestConsole.Entitys
Expand All @@ -51,7 +47,4 @@ public static partial class UserToUserDtoExtentions
// };
//}
}
}


#pragma warning restore
}

0 comments on commit 3d8a171

Please sign in to comment.