EntityComponentAnalyzer.cs 11 KB

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