From c82ebfaaa023d4daf8f35b3f96b71cbe592bbdfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=87=E9=9B=85=E8=99=8E?= Date: Sun, 5 Nov 2023 23:03:16 +0800 Subject: [PATCH] 1.1.0.1 released #4 --- .../Biwen.AutoClassGen.csproj | 4 +-- Biwen.AutoClassGen.Gen/SourceGenAnalyzer.cs | 11 ++++++- .../SourceGenCodeFixProvider.cs | 29 +++++++++++++------ Biwen.AutoClassGen.TestConsole/Classes.cs | 28 +++++++++--------- Biwen.AutoClassGen.TestConsole/Program.cs | 2 ++ 5 files changed, 47 insertions(+), 27 deletions(-) diff --git a/Biwen.AutoClassGen.Gen/Biwen.AutoClassGen.csproj b/Biwen.AutoClassGen.Gen/Biwen.AutoClassGen.csproj index 1d0a7ce..e56f31a 100644 --- a/Biwen.AutoClassGen.Gen/Biwen.AutoClassGen.csproj +++ b/Biwen.AutoClassGen.Gen/Biwen.AutoClassGen.csproj @@ -7,7 +7,7 @@ true 6.0-all 万雅虎 - 1.1.0.0 + 1.1.0.1 @@ -23,7 +23,7 @@ MIT README.md Roslyn,SourceGenerator,QuickApi - 提供对DTO的Gen支持 + 提供对DTO的Gen支持,提供简单的Mapper https://github.com/vipwan/Biwen.AutoClassGen diff --git a/Biwen.AutoClassGen.Gen/SourceGenAnalyzer.cs b/Biwen.AutoClassGen.Gen/SourceGenAnalyzer.cs index eac8110..85efcf9 100644 --- a/Biwen.AutoClassGen.Gen/SourceGenAnalyzer.cs +++ b/Biwen.AutoClassGen.Gen/SourceGenAnalyzer.cs @@ -123,8 +123,17 @@ public override void Initialize(AnalysisContext context) ctx.ReportDiagnostic(Diagnostic.Create(InvalidDeclareNameError, location)); } + //NamespaceDeclarationSyntax if (declaration.Parent is NamespaceDeclarationSyntax @namespace && - @namespace?.Name.ToString() != arg1.GetText().ToString().Replace("\"", "")) + @namespace?.Name.ToString() != arg1?.GetText().ToString().Replace("\"", "")) + { + var location = arg1?.GetLocation(); + // issue warning + ctx.ReportDiagnostic(Diagnostic.Create(SuggestDeclareNameWarning, location)); + } + //FileScopedNamespaceDeclaration + if (declaration.Parent is FileScopedNamespaceDeclarationSyntax @namespace2 && + @namespace2?.Name.ToString() != arg1?.GetText().ToString().Replace("\"", "")) { var location = arg1?.GetLocation(); // issue warning diff --git a/Biwen.AutoClassGen.Gen/SourceGenCodeFixProvider.cs b/Biwen.AutoClassGen.Gen/SourceGenCodeFixProvider.cs index 74dc448..a8210e6 100644 --- a/Biwen.AutoClassGen.Gen/SourceGenCodeFixProvider.cs +++ b/Biwen.AutoClassGen.Gen/SourceGenCodeFixProvider.cs @@ -53,14 +53,20 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) //var @namespace = root.Parent?.AncestorsAndSelf().OfType().First().Name.ToString(); var rootCompUnit = (CompilationUnitSyntax)root; - var @namespace = ((rootCompUnit.Members.Where(m => m.IsKind(SyntaxKind.NamespaceDeclaration)).Single()) as NamespaceDeclarationSyntax)?.Name.ToString(); - - // Register a code action that will invoke the fix. - CodeAction action = CodeAction.Create( - "GEN:使用推荐的命名空间", - c => ReplaceWithNameOfAsync(context.Document, nodeToReplace, @namespace!, c), - equivalenceKey: nameof(SourceGenCodeFixProvider)); - context.RegisterCodeFix(action, diagnostic); + var @namespace = (rootCompUnit.Members.Where(m => m.IsKind(SyntaxKind.NamespaceDeclaration)).FirstOrDefault() as NamespaceDeclarationSyntax)?.Name.ToString(); + if (string.IsNullOrEmpty(@namespace)) + { + @namespace = (rootCompUnit.Members.Where(m => m.IsKind(SyntaxKind.FileScopedNamespaceDeclaration)).FirstOrDefault() as FileScopedNamespaceDeclarationSyntax)?.Name.ToString(); + } + if (!string.IsNullOrEmpty(@namespace)) + { + // Register a code action that will invoke the fix. + CodeAction action = CodeAction.Create( + "GEN:使用推荐的命名空间", + c => ReplaceWithNameOfAsync(context.Document, nodeToReplace, @namespace!, c), + equivalenceKey: nameof(SourceGenCodeFixProvider)); + context.RegisterCodeFix(action, diagnostic); + } } else if (diagnostic.Id == SourceGenAnalyzer.GEN011) { @@ -159,7 +165,12 @@ private static async Task AddAttributeAsync(Document document, SyntaxN var rootCompUnit = (CompilationUnitSyntax)root!; //命名空间 var @namespace = ((rootCompUnit.Members.Where( - m => m.IsKind(SyntaxKind.NamespaceDeclaration)).Single()) as NamespaceDeclarationSyntax)?.Name.ToString(); + m => m.IsKind(SyntaxKind.NamespaceDeclaration)).FirstOrDefault()) as NamespaceDeclarationSyntax)?.Name.ToString(); + if (string.IsNullOrEmpty(@namespace)) + { + @namespace = ((rootCompUnit.Members.Where( + m => m.IsKind(SyntaxKind.FileScopedNamespaceDeclaration)).FirstOrDefault()) as FileScopedNamespaceDeclarationSyntax)?.Name.ToString(); + } //类名 var @class = "YourClassName"; diff --git a/Biwen.AutoClassGen.TestConsole/Classes.cs b/Biwen.AutoClassGen.TestConsole/Classes.cs index e85b667..a5528e8 100644 --- a/Biwen.AutoClassGen.TestConsole/Classes.cs +++ b/Biwen.AutoClassGen.TestConsole/Classes.cs @@ -1,21 +1,19 @@ using Biwen.AutoClassGen.TestConsole.Interfaces; -namespace Biwen.AutoClassGen.Models -{ +namespace Biwen.AutoClassGen.Models; - /// - /// 分页请求 - /// - [AutoGen("QueryRequest", "Biwen.AutoClassGen.Models")] - [AutoGen("Query2Request", "Biwen.AutoClassGen.Models")] - public interface IQueryRequest : IPager, IQuery - { - } - [AutoGen("TenantRealRequest", "Biwen.AutoClassGen.Models")] - public interface ITenantRealRequest : ITenantRequest - { +/// +/// 分页请求 +/// +[AutoGen("QueryRequest", "Biwen.AutoClassGen.Models")] +[AutoGen("Query2Request", "Biwen.AutoClassGen.Models")] +public interface IQueryRequest : IPager, IQuery +{ +} - } +[AutoGen("TenantRealRequest", "Biwen.AutoClassGen.Models")] +public interface ITenantRealRequest : ITenantRequest +{ -} \ No newline at end of file +} diff --git a/Biwen.AutoClassGen.TestConsole/Program.cs b/Biwen.AutoClassGen.TestConsole/Program.cs index a29aa05..fa43c4c 100644 --- a/Biwen.AutoClassGen.TestConsole/Program.cs +++ b/Biwen.AutoClassGen.TestConsole/Program.cs @@ -12,6 +12,8 @@ KeyWord = "biwen" }; + + Biwen.AutoClassGen.TestConsole.Dtos.UserDto userDto2 = new() { FirstName = "biwen",