EntityComponentAnalyzer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using System.Collections.Immutable;
  3. using System.Linq;
  4. using Microsoft.CodeAnalysis;
  5. using Microsoft.CodeAnalysis.CSharp;
  6. using Microsoft.CodeAnalysis.CSharp.Syntax;
  7. using Microsoft.CodeAnalysis.Diagnostics;
  8. namespace ET.Analyzer
  9. {
  10. [DiagnosticAnalyzer(LanguageNames.CSharp)]
  11. public class EntityComponentAnalyzer:DiagnosticAnalyzer
  12. {
  13. public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(EntityComponentAnalyzerRule.Rule,DisableAccessEntityChildAnalyzerRule.Rule);
  14. public override void Initialize(AnalysisContext context)
  15. {
  16. if (!AnalyzerGlobalSetting.EnableAnalyzer)
  17. {
  18. return;
  19. }
  20. context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
  21. context.EnableConcurrentExecution();
  22. context.RegisterSyntaxNodeAction(this.AnalyzeMemberAccessExpression, SyntaxKind.SimpleMemberAccessExpression);
  23. }
  24. private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
  25. {
  26. if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllModelHotfix))
  27. {
  28. return;
  29. }
  30. if (!(context.Node is MemberAccessExpressionSyntax memberAccessExpressionSyntax))
  31. {
  32. return;
  33. }
  34. // 筛选出 Component函数syntax
  35. string methodName = memberAccessExpressionSyntax.Name.Identifier.Text;
  36. if (!Definition.ComponentMethod.Contains(methodName))
  37. {
  38. return;
  39. }
  40. if (!(memberAccessExpressionSyntax?.Parent is InvocationExpressionSyntax invocationExpressionSyntax) ||
  41. !(context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax).Symbol is IMethodSymbol addComponentMethodSymbol))
  42. {
  43. return;
  44. }
  45. // 获取AComponent函数的调用者类型
  46. ITypeSymbol? parentTypeSymbol = memberAccessExpressionSyntax.GetMemberAccessSyntaxParentType(context.SemanticModel);
  47. if (parentTypeSymbol==null)
  48. {
  49. return;
  50. }
  51. // 对于Entity基类会报错 除非标记了EnableAccessEntiyChild
  52. if (parentTypeSymbol.ToString()==Definition.EntityType)
  53. {
  54. HandleAcessEntityChild(context);
  55. return;
  56. }
  57. // 非Entity的子类 跳过
  58. if (parentTypeSymbol.BaseType?.ToString()!= Definition.EntityType)
  59. {
  60. return;
  61. }
  62. // 获取 component实体类型
  63. ISymbol? componentTypeSymbol = null;
  64. // Component为泛型调用
  65. if (addComponentMethodSymbol.IsGenericMethod)
  66. {
  67. GenericNameSyntax? genericNameSyntax = memberAccessExpressionSyntax?.GetFirstChild<GenericNameSyntax>();
  68. TypeArgumentListSyntax? typeArgumentList = genericNameSyntax?.GetFirstChild<TypeArgumentListSyntax>();
  69. var componentTypeSyntax = typeArgumentList?.Arguments.First();
  70. if (componentTypeSyntax == null)
  71. {
  72. Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
  73. context.ReportDiagnostic(diagnostic);
  74. throw new Exception("componentTypeSyntax==null");
  75. }
  76. componentTypeSymbol = context.SemanticModel.GetSymbolInfo(componentTypeSyntax).Symbol;
  77. }
  78. //Component为非泛型调用
  79. else
  80. {
  81. SyntaxNode? firstArgumentSyntax = invocationExpressionSyntax.GetFirstChild<ArgumentListSyntax>()?.GetFirstChild<ArgumentSyntax>()
  82. ?.ChildNodes().First();
  83. if (firstArgumentSyntax == null)
  84. {
  85. Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
  86. context.ReportDiagnostic(diagnostic);
  87. return;
  88. }
  89. // 参数为typeOf时 提取Type类型
  90. if (firstArgumentSyntax is TypeOfExpressionSyntax typeOfExpressionSyntax)
  91. {
  92. firstArgumentSyntax = typeOfExpressionSyntax.Type;
  93. }
  94. ISymbol? firstArgumentSymbol = context.SemanticModel.GetSymbolInfo(firstArgumentSyntax).Symbol;
  95. if (firstArgumentSymbol is ILocalSymbol childLocalSymbol)
  96. {
  97. componentTypeSymbol = childLocalSymbol.Type;
  98. }
  99. else if (firstArgumentSymbol is IParameterSymbol childParamaterSymbol)
  100. {
  101. componentTypeSymbol = childParamaterSymbol.Type;
  102. }
  103. else if (firstArgumentSymbol is IMethodSymbol methodSymbol)
  104. {
  105. componentTypeSymbol = methodSymbol.ReturnType;
  106. }
  107. else if (firstArgumentSymbol is IFieldSymbol fieldSymbol)
  108. {
  109. componentTypeSymbol = fieldSymbol.Type;
  110. }
  111. else if (firstArgumentSymbol is IPropertySymbol propertySymbol)
  112. {
  113. componentTypeSymbol = propertySymbol.Type;
  114. }else if (firstArgumentSymbol is INamedTypeSymbol namedTypeSymbol)
  115. {
  116. componentTypeSymbol = namedTypeSymbol;
  117. }else if (firstArgumentSymbol is ITypeParameterSymbol)
  118. {
  119. // 忽略typeof(T)参数类型
  120. return;
  121. }
  122. else if (firstArgumentSymbol != null)
  123. {
  124. Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
  125. firstArgumentSymbol.Name, parentTypeSymbol.Name);
  126. context.ReportDiagnostic(diagnostic);
  127. return;
  128. }
  129. else
  130. {
  131. Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
  132. firstArgumentSyntax.GetText(), parentTypeSymbol.Name);
  133. context.ReportDiagnostic(diagnostic);
  134. return;
  135. }
  136. }
  137. if (componentTypeSymbol==null)
  138. {
  139. return;
  140. }
  141. // 忽略 component类型为泛型类型
  142. if (componentTypeSymbol is ITypeParameterSymbol typeParameterSymbol)
  143. {
  144. return;
  145. }
  146. // 忽略 Type参数
  147. if (componentTypeSymbol.ToString()=="System.Type")
  148. {
  149. return;
  150. }
  151. // 组件类型为Entity时 忽略检查
  152. if (componentTypeSymbol.ToString()== Definition.EntityType)
  153. {
  154. return;
  155. }
  156. // 判断component类型是否属于约束类型
  157. //获取component类的parentType标记数据
  158. INamedTypeSymbol? availableParentTypeSymbol = null;
  159. bool hasParentTypeAttribute = false;
  160. foreach (AttributeData? attributeData in componentTypeSymbol.GetAttributes())
  161. {
  162. if (attributeData.AttributeClass?.ToString() == Definition.ComponentOfAttribute)
  163. {
  164. hasParentTypeAttribute = true;
  165. if (attributeData.ConstructorArguments[0].Value is INamedTypeSymbol typeSymbol)
  166. {
  167. availableParentTypeSymbol = typeSymbol;
  168. break;
  169. }
  170. }
  171. }
  172. if (hasParentTypeAttribute&&availableParentTypeSymbol==null)
  173. {
  174. return;
  175. }
  176. // 符合约束条件 通过检查
  177. if (availableParentTypeSymbol!=null && availableParentTypeSymbol.ToString()==parentTypeSymbol.ToString())
  178. {
  179. return;
  180. }
  181. {
  182. Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), componentTypeSymbol?.Name,
  183. parentTypeSymbol?.Name);
  184. context.ReportDiagnostic(diagnostic);
  185. }
  186. }
  187. private void HandleAcessEntityChild(SyntaxNodeAnalysisContext context)
  188. {
  189. var memberAccessExpressionSyntax = context.Node as MemberAccessExpressionSyntax;
  190. //在方法体内
  191. var methodDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<MethodDeclarationSyntax>();
  192. if (methodDeclarationSyntax!=null)
  193. {
  194. var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclarationSyntax);
  195. bool? enableAccessEntiyChild = methodSymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
  196. if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
  197. {
  198. Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
  199. context.ReportDiagnostic(diagnostic);
  200. }
  201. return;
  202. }
  203. //在属性内
  204. var propertyDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<PropertyDeclarationSyntax>();
  205. if (propertyDeclarationSyntax!=null)
  206. {
  207. var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax);
  208. bool? enableAccessEntiyChild = propertySymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
  209. if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
  210. {
  211. Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
  212. context.ReportDiagnostic(diagnostic);
  213. }
  214. return;
  215. }
  216. }
  217. }
  218. }