123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- using System;
- using System.Collections.Immutable;
- using System.Linq;
- using Microsoft.CodeAnalysis;
- using Microsoft.CodeAnalysis.CSharp;
- using Microsoft.CodeAnalysis.CSharp.Syntax;
- using Microsoft.CodeAnalysis.Diagnostics;
- namespace ET.Analyzer
- {
- [DiagnosticAnalyzer(LanguageNames.CSharp)]
- public class EntityComponentAnalyzer:DiagnosticAnalyzer
- {
- public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(EntityComponentAnalyzerRule.Rule,DisableAccessEntityChildAnalyzerRule.Rule);
-
- public override void Initialize(AnalysisContext context)
- {
- if (!AnalyzerGlobalSetting.EnableAnalyzer)
- {
- return;
- }
- context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
- context.EnableConcurrentExecution();
- context.RegisterSyntaxNodeAction(this.AnalyzeMemberAccessExpression, SyntaxKind.SimpleMemberAccessExpression);
- }
- private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
- {
- if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllModelHotfix))
- {
- return;
- }
- if (!(context.Node is MemberAccessExpressionSyntax memberAccessExpressionSyntax))
- {
- return;
- }
-
- // 筛选出 Component函数syntax
- string methodName = memberAccessExpressionSyntax.Name.Identifier.Text;
- if (!Definition.ComponentMethod.Contains(methodName))
- {
- return;
- }
-
- if (!(memberAccessExpressionSyntax?.Parent is InvocationExpressionSyntax invocationExpressionSyntax) ||
- !(context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax).Symbol is IMethodSymbol addComponentMethodSymbol))
- {
- return;
- }
-
- // 获取AComponent函数的调用者类型
- ITypeSymbol? parentTypeSymbol = memberAccessExpressionSyntax.GetMemberAccessSyntaxParentType(context.SemanticModel);
- if (parentTypeSymbol==null)
- {
- return;
- }
-
- // 对于Entity基类会报错 除非标记了EnableAccessEntiyChild
- if (parentTypeSymbol.ToString()==Definition.EntityType)
- {
- HandleAcessEntityChild(context);
- return;
- }
- // 非Entity的子类 跳过
- if (parentTypeSymbol.BaseType?.ToString()!= Definition.EntityType)
- {
- return;
- }
- // 获取 component实体类型
- ISymbol? componentTypeSymbol = null;
-
- // Component为泛型调用
- if (addComponentMethodSymbol.IsGenericMethod)
- {
- GenericNameSyntax? genericNameSyntax = memberAccessExpressionSyntax?.GetFirstChild<GenericNameSyntax>();
- TypeArgumentListSyntax? typeArgumentList = genericNameSyntax?.GetFirstChild<TypeArgumentListSyntax>();
- var componentTypeSyntax = typeArgumentList?.Arguments.First();
-
- if (componentTypeSyntax == null)
- {
- Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
- context.ReportDiagnostic(diagnostic);
- throw new Exception("componentTypeSyntax==null");
- }
- componentTypeSymbol = context.SemanticModel.GetSymbolInfo(componentTypeSyntax).Symbol;
- }
- //Component为非泛型调用
- else
- {
- SyntaxNode? firstArgumentSyntax = invocationExpressionSyntax.GetFirstChild<ArgumentListSyntax>()?.GetFirstChild<ArgumentSyntax>()
- ?.ChildNodes().First();
- if (firstArgumentSyntax == null)
- {
- Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
- context.ReportDiagnostic(diagnostic);
- return;
- }
- // 参数为typeOf时 提取Type类型
- if (firstArgumentSyntax is TypeOfExpressionSyntax typeOfExpressionSyntax)
- {
- firstArgumentSyntax = typeOfExpressionSyntax.Type;
- }
- ISymbol? firstArgumentSymbol = context.SemanticModel.GetSymbolInfo(firstArgumentSyntax).Symbol;
- if (firstArgumentSymbol is ILocalSymbol childLocalSymbol)
- {
- componentTypeSymbol = childLocalSymbol.Type;
- }
- else if (firstArgumentSymbol is IParameterSymbol childParamaterSymbol)
- {
- componentTypeSymbol = childParamaterSymbol.Type;
- }
- else if (firstArgumentSymbol is IMethodSymbol methodSymbol)
- {
- componentTypeSymbol = methodSymbol.ReturnType;
- }
- else if (firstArgumentSymbol is IFieldSymbol fieldSymbol)
- {
- componentTypeSymbol = fieldSymbol.Type;
- }
- else if (firstArgumentSymbol is IPropertySymbol propertySymbol)
- {
- componentTypeSymbol = propertySymbol.Type;
- }else if (firstArgumentSymbol is INamedTypeSymbol namedTypeSymbol)
- {
- componentTypeSymbol = namedTypeSymbol;
- }else if (firstArgumentSymbol is ITypeParameterSymbol)
- {
- // 忽略typeof(T)参数类型
- return;
- }
- else if (firstArgumentSymbol != null)
- {
- Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
- firstArgumentSymbol.Name, parentTypeSymbol.Name);
- context.ReportDiagnostic(diagnostic);
- return;
- }
- else
- {
- Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
- firstArgumentSyntax.GetText(), parentTypeSymbol.Name);
- context.ReportDiagnostic(diagnostic);
- return;
- }
- }
- if (componentTypeSymbol==null)
- {
- return;
- }
- // 忽略 component类型为泛型类型
- if (componentTypeSymbol is ITypeParameterSymbol typeParameterSymbol)
- {
- return;
- }
-
- // 忽略 Type参数
- if (componentTypeSymbol.ToString()=="System.Type")
- {
- return;
- }
- // 组件类型为Entity时 忽略检查
- if (componentTypeSymbol.ToString()== Definition.EntityType)
- {
- return;
- }
-
- // 判断component类型是否属于约束类型
- //获取component类的parentType标记数据
- INamedTypeSymbol? availableParentTypeSymbol = null;
- bool hasParentTypeAttribute = false;
- foreach (AttributeData? attributeData in componentTypeSymbol.GetAttributes())
- {
- if (attributeData.AttributeClass?.ToString() == Definition.ComponentOfAttribute)
- {
- hasParentTypeAttribute = true;
- if (attributeData.ConstructorArguments[0].Value is INamedTypeSymbol typeSymbol)
- {
- availableParentTypeSymbol = typeSymbol;
- break;
- }
- }
- }
- if (hasParentTypeAttribute&&availableParentTypeSymbol==null)
- {
- return;
- }
- // 符合约束条件 通过检查
- if (availableParentTypeSymbol!=null && availableParentTypeSymbol.ToString()==parentTypeSymbol.ToString())
- {
- return;
- }
- {
- Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), componentTypeSymbol?.Name,
- parentTypeSymbol?.Name);
- context.ReportDiagnostic(diagnostic);
- }
- }
-
- private void HandleAcessEntityChild(SyntaxNodeAnalysisContext context)
- {
- var memberAccessExpressionSyntax = context.Node as MemberAccessExpressionSyntax;
- //在方法体内
- var methodDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<MethodDeclarationSyntax>();
- if (methodDeclarationSyntax!=null)
- {
- var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclarationSyntax);
- bool? enableAccessEntiyChild = methodSymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
- if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
- {
- Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
- context.ReportDiagnostic(diagnostic);
- }
- return;
- }
-
- //在属性内
- var propertyDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<PropertyDeclarationSyntax>();
- if (propertyDeclarationSyntax!=null)
- {
- var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax);
-
- bool? enableAccessEntiyChild = propertySymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
- if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
- {
- Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
- context.ReportDiagnostic(diagnostic);
- }
- return;
- }
- }
- }
- }
|