Explorar o código

更新ETSystem SourceGenerator 判断静态函数是否在静态分部类中

wuwenchao %!s(int64=2) %!d(string=hai) anos
pai
achega
8f99efbf1f

+ 16 - 0
Share/Analyzer/Extension/AnalyzerHelper.cs

@@ -359,5 +359,21 @@ namespace ET.Analyzer
             BasicBlock? block = controlFlowGraph.Blocks.FirstOrDefault(x => x.Operations.Any(y => y.Syntax.Contains(statementSyntax)));
             return block;
         }
+        
+        /// <summary>
+        /// 判断类是否为partial类
+        /// </summary>
+        public static bool IsPartial(this ClassDeclarationSyntax classDeclaration)
+        {
+            foreach (var modifier in classDeclaration.Modifiers)
+            {
+                if (modifier.IsKind(SyntaxKind.PartialKeyword))
+                {
+                    return true;
+                }
+            }
+
+            return false;
+        }
     }
 }

+ 6 - 0
Share/Share.SourceGenerator/Config/DiagnosticCategories.cs

@@ -0,0 +1,6 @@
+namespace ET.Generator;
+
+public static class DiagnosticCategories
+{
+    public const string Generator = "ETGeneratorAnalyzers";
+}

+ 6 - 0
Share/Share.SourceGenerator/Config/DiagnosticIds.cs

@@ -0,0 +1,6 @@
+namespace ET.Generator;
+
+public static class DiagnosticIds
+{
+    public const string ETSystemMethodIsInStaticPartialClassRuleId = "ET1001";
+}

+ 23 - 0
Share/Share.SourceGenerator/Config/DiagnosticRules.cs

@@ -0,0 +1,23 @@
+using Microsoft.CodeAnalysis;
+
+namespace ET.Generator;
+
+
+public static class ETSystemMethodIsInStaticPartialClassRule
+{
+    private const string Title = "ETSystem函数必须声明在静态分部类中";
+
+    private const string MessageFormat = "ETSystem函数所在的类:{0} 不是静态分部类";
+
+    private const string Description = "ETSystem函数必须声明在静态类中.";
+
+    public static readonly DiagnosticDescriptor Rule =
+            new DiagnosticDescriptor(DiagnosticIds.ETSystemMethodIsInStaticPartialClassRuleId,
+                Title,
+                MessageFormat,
+                DiagnosticCategories.Generator,
+                DiagnosticSeverity.Error,
+                true,
+                Description);
+}
+

+ 15 - 7
Share/Share.SourceGenerator/Generator/ETSystemGenerator/ETSystemGenerator.cs

@@ -37,9 +37,17 @@ public class ETSystemGenerator : ISourceGenerator
         var classTypeSymbol = semanticModel.GetDeclaredSymbol(classDeclarationSyntax) as INamedTypeSymbol;
         if (classTypeSymbol==null)
         {
-            context.AddSource($"{className}.g.cs", "classTypeSymbol==null");
             return;
         }
+
+        if (!classTypeSymbol.IsStatic || !classDeclarationSyntax.IsPartial())
+        {
+            Diagnostic diagnostic = Diagnostic.Create(ETSystemMethodIsInStaticPartialClassRule.Rule, classDeclarationSyntax.GetLocation(),
+                classDeclarationSyntax.Identifier.Text);
+            context.ReportDiagnostic(diagnostic);
+            return;
+        }
+        
         var namespaceSymbol = classTypeSymbol?.ContainingNamespace;
         var namespaceName = namespaceSymbol?.Name;
         while (namespaceSymbol?.ContainingNamespace != null)
@@ -55,7 +63,6 @@ public class ETSystemGenerator : ISourceGenerator
         var inClassSb = new StringBuilder();
 
         this.GenerateSystemCodeByTemplate(inClassSb, classDeclarationSyntax, methodDeclarationSyntaxes, context,semanticModel);
-
         string code = $$"""
 namespace {{namespaceName}}{
     public static partial class {{className}}
@@ -64,7 +71,7 @@ namespace {{namespaceName}}{
     }
 }
 """;
-        context.AddSource($"{className}.g.cs",code);
+        context.AddSource($"{namespaceName}.{className}.g.cs",code);
     }
 
     /// <summary>
@@ -74,16 +81,17 @@ namespace {{namespaceName}}{
     {
         foreach (var methodDeclarationSyntax in methodDeclarationSyntaxes)
         {
-            var componentParam = methodDeclarationSyntax.ParameterList.Parameters.FirstOrDefault();
-            if (componentParam==null)
+            var methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclarationSyntax) as IMethodSymbol;
+            if (methodSymbol==null)
             {
                 continue;
             }
-            var methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclarationSyntax) as IMethodSymbol;
-            if (methodSymbol==null)
+            var componentParam = methodDeclarationSyntax.ParameterList.Parameters.FirstOrDefault();
+            if (componentParam==null)
             {
                 continue;
             }
+            
             var methodName = methodDeclarationSyntax.Identifier.Text;
             var componentName = componentParam.Type?.ToString();
             

+ 8 - 1
Share/Share.SourceGenerator/Share.SourceGenerator.csproj

@@ -5,10 +5,17 @@
         <IncludeBuildOutput>false</IncludeBuildOutput>
         <Nullable>enable</Nullable>
         <LangVersion>11</LangVersion>
+        <IncludeBuildOutput>false</IncludeBuildOutput>
+        <DevelopmentDependency>true</DevelopmentDependency>
+        <IncludeSymbols>false</IncludeSymbols>
+        <NoWarn>1701;1702;RS2008</NoWarn>
+        
     </PropertyGroup>
 
     <ItemGroup>
-        <Compile Include="../Analyzer/Extension/*.cs" />
+        <Compile Include="../Analyzer/Extension/*.cs">
+            <Link>Extension\%(RecursiveDir)%(FileName)%(Extension)</Link>
+        </Compile>
     </ItemGroup>
 
     <ItemGroup>

BIN=BIN
Unity/Assets/Plugins/Share.SourceGenerator.dll