Skip to content

Commit

Permalink
ReportDiagnostic , GEN001
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed Nov 1, 2023
1 parent 09f68c2 commit 81dce8c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions Biwen.AutoClassGen.Gen/Biwen.AutoClassGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<!--开发调试阶段-->
<ItemGroup>
<!-- Package the generator in the analyzer directory of the nuget package -->
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="..\README.md">
<Pack>True</Pack>
Expand Down
43 changes: 30 additions & 13 deletions Biwen.AutoClassGen.Gen/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,41 @@ public class SourceGenerator : IIncrementalGenerator
const string AttributeValueMetadataName = "AutoGen";



public void Initialize(IncrementalGeneratorInitializationContext context)
{

var nodes = context.SyntaxProvider.ForAttributeWithMetadataName(
AttributeMetadataName,
(context, attributeSyntax) => true,
(syntaxContext, _) => syntaxContext.TargetNode).Collect();


IncrementalValueProvider<(Compilation, ImmutableArray<SyntaxNode>)> compilationAndTypes =
context.CompilationProvider.Combine(nodes);

context.RegisterSourceOutput(compilationAndTypes, static (spc, source) => HandleAnnotatedNodes(source.Item1, source.Item2, spc));

}

/// <summary>
/// 无法生成类的错误
/// </summary>
#pragma warning disable RS2008 // 启用分析器发布跟踪
private static readonly DiagnosticDescriptor InvalidDeclareError = new(id: "GEN001",
#pragma warning restore RS2008 // 启用分析器发布跟踪
title: "来自Biwen.AutoClassGen的报错信息",
#pragma warning disable RS1032 // 正确定义诊断消息
messageFormat: "没有实现基础接口因此不能生成类,请删除标注的特性[AutoGen] or 继承相应的接口.",
#pragma warning restore RS1032 // 正确定义诊断消息
category: typeof(SourceGenerator).Assembly.GetName().Name,
DiagnosticSeverity.Error,
isEnabledByDefault: true);

private static void HandleAnnotatedNodes(Compilation compilation, ImmutableArray<SyntaxNode> nodes, SourceProductionContext context)
{
var sb = new StringBuilder();
sb.AppendLine("// <auto-generated />");
sb.AppendLine("// Path: Biwen.AutoClassGen.Gen/SourceGenerator.cs");
sb.AppendLine("// author :[email protected] 万雅虎");
sb.AppendLine("// 如果你在使用中遇到问题,请第一时间issue,谢谢!");
sb.AppendLine("// author:[email protected] 万雅虎");
sb.AppendLine("// issue:https://github.com/vipwan/Biwen.AutoClassGen/issues 如果你在使用中遇到问题,请第一时间issue,谢谢!");
sb.AppendLine("// This file is generated by Biwen.AutoClassGen.SourceGenerator");
sb.AppendLine();
sb.AppendLine("using System;");
Expand All @@ -51,27 +63,32 @@ private static void HandleAnnotatedNodes(Compilation compilation, ImmutableArray
sb.AppendLine("using System.ComponentModel.DataAnnotations;");
sb.AppendLine("using System.Text;");
sb.AppendLine("using System.Threading.Tasks;");
sb.AppendLine("#pragma warning disable");

sb.AppendLine();
sb.AppendLine("$namespace");

sb.AppendLine();

sb.AppendLine("#pragma warning disable");
sb.AppendLine("namespace $ni");
sb.AppendLine("{");
sb.AppendLine("\t$classes");
sb.AppendLine("$classes");
sb.AppendLine("}");
sb.AppendLine("#pragma warning restore");

string classTemp = $"public partial class $className : $interfaceName {{ $body }}";


foreach (InterfaceDeclarationSyntax node in nodes.AsEnumerable().Cast<InterfaceDeclarationSyntax>())
{
if (node.BaseList == null)
{
continue;
}
if (!node.BaseList.Types.Any())

if (node.BaseList == null || !node.BaseList.Types.Any())
{
var type = compilation.GetSymbolsWithName(node.Identifier.ValueText).AsEnumerable().FirstOrDefault();
var path = type.Locations[0].ToString();

// issue error
context.ReportDiagnostic(Diagnostic.Create(InvalidDeclareError, type.Locations[0]));

continue;
}

Expand Down

0 comments on commit 81dce8c

Please sign in to comment.