Răsfoiți Sursa

添加分析器限制普通类在model程序集声明 可以对类标记EnableClassAttribute解除限制 (#564)

* 限制普通类在model程序集声明
susices 1 an în urmă
părinte
comite
1a9f826ffe

+ 61 - 0
Share/Analyzer/Analyzer/DisableNormalClassDeclaratonInModelAssemblyAnalyzer.cs

@@ -0,0 +1,61 @@
+using System.Collections.Immutable;
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+using Microsoft.CodeAnalysis.Diagnostics;
+
+namespace ET.Analyzer
+{
+    [DiagnosticAnalyzer(LanguageNames.CSharp)]
+    public class DisableNormalClassDeclaratonInModelAssemblyAnalyzer : DiagnosticAnalyzer
+    {
+        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(EntityClassDeclarationAnalyzerrRule.Rule);
+
+        public override void Initialize(AnalysisContext context)
+        {
+            if (!AnalyzerGlobalSetting.EnableAnalyzer)
+            {
+                return;
+            }
+            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
+            context.EnableConcurrentExecution();
+            context.RegisterCompilationStartAction(analysisContext =>
+            {
+                if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.Compilation.AssemblyName, AnalyzeAssembly.AllModel))
+                {
+                    analysisContext.RegisterSyntaxNodeAction(Analyze,SyntaxKind.ClassDeclaration);
+                }
+            });
+            
+        }
+        
+        private void Analyze(SyntaxNodeAnalysisContext context)
+        {
+            if (context.Node is not ClassDeclarationSyntax classDeclarationSyntax)
+            {
+                return;
+            }
+
+            var namedTypeSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax);
+
+            if (namedTypeSymbol==null)
+            {
+                return;
+            }
+
+            if (namedTypeSymbol.IsETEntity())
+            {
+                return;
+            }
+
+            if (namedTypeSymbol.HasAttribute(Definition.EnableClassAttribute))
+            {
+                return;
+            }
+            
+            Diagnostic diagnostic = Diagnostic.Create(EntityClassDeclarationAnalyzerrRule.Rule, classDeclarationSyntax.Identifier.GetLocation(), namedTypeSymbol);
+            context.ReportDiagnostic(diagnostic);
+        }
+    }
+}
+

+ 2 - 0
Share/Analyzer/Config/Definition.cs

@@ -94,6 +94,8 @@
         public const string EntityWeakRefType = "EntityWeakRef";
 
         public const string DisableNewAttribute = "ET.DisableNewAttribute";
+
+        public const string EnableClassAttribute = "ET.EnableClassAttribute";
     }
 }
 

+ 2 - 0
Share/Analyzer/Config/DiagnosticIds.cs

@@ -64,5 +64,7 @@
         public const string NetMessageAnalyzerRuleId = "ET0030";
 
         public const string DisableNewAnalyzerRuleId = "ET0031";
+
+        public const string DisableNormalClassDeclaratonInModelAssemblyAnalyzerRuleId = "ET0032";
     }
 }

+ 18 - 0
Share/Analyzer/Config/DiagnosticRules.cs

@@ -468,4 +468,22 @@ namespace ET.Analyzer
                     true,
                     Description);
     }
+    
+    public static class EntityClassDeclarationAnalyzerrRule
+    {
+        private const string Title = "Model/ModelView程序集禁止声明非实体类";
+
+        private const string MessageFormat = "Model/ModelView程序集禁止声明非实体类:{0}";
+
+        private const string Description = "Model/ModelView程序集禁止声明非实体类.";
+
+        public static readonly DiagnosticDescriptor Rule =
+                new DiagnosticDescriptor(DiagnosticIds.DisableNormalClassDeclaratonInModelAssemblyAnalyzerRuleId,
+                    Title,
+                    MessageFormat,
+                    DiagnosticCategories.All,
+                    DiagnosticSeverity.Error,
+                    true,
+                    Description);
+    }
 }