AddChildTypeAnalyzer.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 AddChildTypeAnalyzer: DiagnosticAnalyzer
  12. {
  13. public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(AddChildTypeAnalyzerRule.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. // 筛选出 AddChild函数syntax
  35. string methodName = memberAccessExpressionSyntax.Name.Identifier.Text;
  36. if (!Definition.AddChildMethods.Contains(methodName))
  37. {
  38. return;
  39. }
  40. if (!(memberAccessExpressionSyntax?.Parent is InvocationExpressionSyntax invocationExpressionSyntax) ||
  41. !(context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax).Symbol is IMethodSymbol addChildMethodSymbol))
  42. {
  43. return;
  44. }
  45. // 获取AddChild函数的调用者类型
  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. // 获取 child实体类型
  63. ISymbol? childTypeSymbol = null;
  64. // addChild为泛型调用
  65. if (addChildMethodSymbol.IsGenericMethod)
  66. {
  67. GenericNameSyntax? genericNameSyntax = memberAccessExpressionSyntax?.GetFirstChild<GenericNameSyntax>();
  68. TypeArgumentListSyntax? typeArgumentList = genericNameSyntax?.GetFirstChild<TypeArgumentListSyntax>();
  69. var childTypeSyntax = typeArgumentList?.Arguments.First();
  70. if (childTypeSyntax == null)
  71. {
  72. Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
  73. context.ReportDiagnostic(diagnostic);
  74. throw new Exception("childTypeSyntax==null");
  75. }
  76. childTypeSymbol = context.SemanticModel.GetSymbolInfo(childTypeSyntax).Symbol;
  77. }
  78. // addChild为非泛型调用
  79. else
  80. {
  81. SyntaxNode? firstArgumentSyntax = invocationExpressionSyntax.GetFirstChild<ArgumentListSyntax>()?.GetFirstChild<ArgumentSyntax>()
  82. ?.ChildNodes().First();
  83. if (firstArgumentSyntax == null)
  84. {
  85. Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
  86. context.ReportDiagnostic(diagnostic);
  87. return;
  88. }
  89. ISymbol? firstArgumentSymbol = context.SemanticModel.GetSymbolInfo(firstArgumentSyntax).Symbol;
  90. if (firstArgumentSymbol is ILocalSymbol childLocalSymbol)
  91. {
  92. childTypeSymbol = childLocalSymbol.Type;
  93. }
  94. else if (firstArgumentSymbol is IParameterSymbol childParamaterSymbol)
  95. {
  96. childTypeSymbol = childParamaterSymbol.Type;
  97. }
  98. else if (firstArgumentSymbol is IMethodSymbol methodSymbol)
  99. {
  100. childTypeSymbol = methodSymbol.ReturnType;
  101. }
  102. else if (firstArgumentSymbol is IFieldSymbol fieldSymbol)
  103. {
  104. childTypeSymbol = fieldSymbol.Type;
  105. }
  106. else if (firstArgumentSymbol is IPropertySymbol propertySymbol)
  107. {
  108. childTypeSymbol = propertySymbol.Type;
  109. }
  110. else if (firstArgumentSymbol != null)
  111. {
  112. Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
  113. firstArgumentSymbol.Name, parentTypeSymbol.Name);
  114. context.ReportDiagnostic(diagnostic);
  115. return;
  116. }
  117. else
  118. {
  119. Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
  120. firstArgumentSyntax.GetText(), parentTypeSymbol.Name);
  121. context.ReportDiagnostic(diagnostic);
  122. return;
  123. }
  124. }
  125. if (childTypeSymbol == null)
  126. {
  127. return;
  128. }
  129. // 忽略 child类型为泛型类型
  130. if (childTypeSymbol is ITypeParameterSymbol typeParameterSymbol)
  131. {
  132. return;
  133. }
  134. // 获取ChildOf标签的约束类型
  135. if (!(childTypeSymbol is ITypeSymbol childType))
  136. {
  137. throw new Exception($"{childTypeSymbol} 不是typeSymbol");
  138. }
  139. INamedTypeSymbol? availableParentType = null;
  140. bool hasAttribute = false;
  141. foreach (AttributeData? attributeData in childType.GetAttributes())
  142. {
  143. if (attributeData.AttributeClass?.ToString() == Definition.ChildOfAttribute)
  144. {
  145. hasAttribute = true;
  146. availableParentType = attributeData.ConstructorArguments[0].Value as INamedTypeSymbol;
  147. break;
  148. }
  149. }
  150. if (hasAttribute && availableParentType==null)
  151. {
  152. return;
  153. }
  154. // 判断父级类型是否属于child约束的父级类型
  155. if (availableParentType?.ToString() == parentTypeSymbol.ToString())
  156. {
  157. return;
  158. }
  159. {
  160. Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), childTypeSymbol?.Name,
  161. parentTypeSymbol?.Name);
  162. context.ReportDiagnostic(diagnostic);
  163. }
  164. }
  165. private void HandleAcessEntityChild(SyntaxNodeAnalysisContext context)
  166. {
  167. var memberAccessExpressionSyntax = context.Node as MemberAccessExpressionSyntax;
  168. //在方法体内
  169. var methodDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<MethodDeclarationSyntax>();
  170. if (methodDeclarationSyntax!=null)
  171. {
  172. var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclarationSyntax);
  173. bool? enableAccessEntiyChild = methodSymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
  174. if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
  175. {
  176. Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
  177. context.ReportDiagnostic(diagnostic);
  178. }
  179. return;
  180. }
  181. //在属性内
  182. var propertyDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<PropertyDeclarationSyntax>();
  183. if (propertyDeclarationSyntax!=null)
  184. {
  185. var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax);
  186. bool? enableAccessEntiyChild = propertySymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
  187. if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
  188. {
  189. Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
  190. context.ReportDiagnostic(diagnostic);
  191. }
  192. return;
  193. }
  194. }
  195. }
  196. }