Skip to content

Commit

Permalink
StyleCop.Analyzers
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed Nov 6, 2023
1 parent a218d68 commit bf33a12
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 138 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,21 @@

# 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

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

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

# Default severity for analyzer diagnostics with category 'MicrosoftCodeAnalysisReleaseTracking'
dotnet_analyzer_diagnostic.category-MicrosoftCodeAnalysisReleaseTracking.severity = none

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

# Default severity for analyzer diagnostics with category 'StyleCop.CSharp.SpacingRules'
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.SpacingRules.severity = none
4 changes: 2 additions & 2 deletions Biwen.AutoClassGen.Attributes/AutoGenAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public sealed class AutoGenAttribute : Attribute
/// </summary>
public string ClassName { get; private set; }
/// <summary>
///full interface name
/// full interface name
/// </summary>
public string InterfaceName { get; private set; }

/// <summary>
///
/// AutoGen
/// </summary>
/// <param name="className">类名</param>
/// <param name="interfaceName">命名空间全名,如:Biwen.QuickApi</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>Roslyn,SourceGenerator,QuickApi</PackageTags>
<PackageReleaseNotes>SG</PackageReleaseNotes>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1573,1591,1712</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand All @@ -24,6 +26,10 @@

<ItemGroup>
<PackageReference Include="Biwen.AutoClassGen" Version="1.1.0.2" PrivateAssets="contentfiles;analyzers" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.507">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>


Expand Down
9 changes: 9 additions & 0 deletions Biwen.AutoClassGen.Gen/Biwen.AutoClassGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<AnalysisLevel>6.0-all</AnalysisLevel>
<Authors>万雅虎</Authors>
<PackageVersion>1.1.0.2</PackageVersion>
Expand All @@ -25,6 +26,8 @@
<PackageTags>Roslyn,SourceGenerator,QuickApi</PackageTags>
<PackageReleaseNotes>提供对DTO的Gen支持,提供简单的Mapper</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/vipwan/Biwen.AutoClassGen</PackageProjectUrl>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1573,1591,1712</NoWarn>
</PropertyGroup>

<!--开发调试阶段-->
Expand All @@ -40,6 +43,12 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0-3.final" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0-3.final" PrivateAssets="all" />

<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.507">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

</ItemGroup>

</Project>
49 changes: 19 additions & 30 deletions Biwen.AutoClassGen.Gen/SourceGenAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,78 +1,68 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;
using System.Linq;

namespace Biwen.AutoClassGen
namespace Biwen.AutoClassGen
{
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
#pragma warning disable RS1036 // 指定分析器禁止的 API 强制设置
public class SourceGenAnalyzer : DiagnosticAnalyzer
#pragma warning restore RS1036 // 指定分析器禁止的 API 强制设置
{
#region DiagnosticDescriptors

const string helplink = "https://github.com/vipwan/Biwen.AutoClassGen#gen-error-code";
private const string Helplink = "https://github.com/vipwan/Biwen.AutoClassGen#gen-error-code";

public const string GEN001 = "GEN001";
public const string GEN011 = "GEN011";
public const string GEN021 = "GEN021";
public const string GEN031 = "GEN031";//推荐生成
public const string GEN031 = "GEN031"; // 推荐生成

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


/// <summary>
/// 重名错误
/// </summary>
#pragma warning disable RS2008 // 启用分析器发布跟踪
private static readonly DiagnosticDescriptor InvalidDeclareNameError = new(id: GEN011,
#pragma warning restore RS2008 // 启用分析器发布跟踪
title: "生成类的类名称不可和接口名重名",
messageFormat: "生成类的类名称不可和接口名重名",
category: typeof(SourceGenerator).Assembly.GetName().Name,
DiagnosticSeverity.Error,
helpLinkUri: helplink,
helpLinkUri: Helplink,
isEnabledByDefault: true);

/// <summary>
/// 命名空间规范警告
/// </summary>
#pragma warning disable RS2008 // 启用分析器发布跟踪
private static readonly DiagnosticDescriptor SuggestDeclareNameWarning = new(id: GEN021,
#pragma warning restore RS2008 // 启用分析器发布跟踪
title: "推荐使用相同的命名空间",
messageFormat: "推荐使用相同的命名空间",
category: typeof(SourceGenerator).Assembly.GetName().Name,
DiagnosticSeverity.Warning,
helpLinkUri: helplink,
helpLinkUri: Helplink,
isEnabledByDefault: true);


/// <summary>
/// 推荐使用自动生成
/// </summary>
#pragma warning disable RS2008 // 启用分析器发布跟踪
private static readonly DiagnosticDescriptor SuggestAutoGen = new(id: GEN031,
#pragma warning restore RS2008 // 启用分析器发布跟踪
title: "使用[AutoGen]自动生成",
messageFormat: "使用[AutoGen]自动生成",
category: typeof(SourceGenerator).Assembly.GetName().Name,
DiagnosticSeverity.Info,
helpLinkUri: helplink,
helpLinkUri: Helplink,
isEnabledByDefault: true);


Expand All @@ -82,10 +72,9 @@ public class SourceGenAnalyzer : DiagnosticAnalyzer
InvalidDeclareError,
InvalidDeclareNameError,
SuggestDeclareNameWarning,
SuggestAutoGen
);
SuggestAutoGen);

const string AttributeValueMetadataName = "AutoGen";
private const string AttributeValueMetadataName = "AutoGen";

public override void Initialize(AnalysisContext context)
{
Expand Down Expand Up @@ -123,15 +112,15 @@ public override void Initialize(AnalysisContext context)
ctx.ReportDiagnostic(Diagnostic.Create(InvalidDeclareNameError, location));
}

//NamespaceDeclarationSyntax
// NamespaceDeclarationSyntax
if (declaration.Parent is NamespaceDeclarationSyntax @namespace &&
@namespace?.Name.ToString() != arg1?.GetText().ToString().Replace("\"", ""))
{
var location = arg1?.GetLocation();
// issue warning
ctx.ReportDiagnostic(Diagnostic.Create(SuggestDeclareNameWarning, location));
}
//FileScopedNamespaceDeclaration
// FileScopedNamespaceDeclaration
if (declaration.Parent is FileScopedNamespaceDeclarationSyntax @namespace2 &&
@namespace2?.Name.ToString() != arg1?.GetText().ToString().Replace("\"", ""))
{
Expand All @@ -143,7 +132,7 @@ public override void Initialize(AnalysisContext context)
}
}

//suggest
// suggest
if (declaration.BaseList != null && declaration.BaseList.Types.Any(x => x.IsKind(SyntaxKind.SimpleBaseType)))
{
var haveAttr = declaration.AttributeLists.Any(x => x.Attributes.Any(x => x.Name.ToString() == AttributeValueMetadataName));
Expand Down
Loading

0 comments on commit bf33a12

Please sign in to comment.