ClassDeclarationInHotfixAnalyzer.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Collections.Immutable;
  2. using System.Linq;
  3. using Microsoft.CodeAnalysis;
  4. using Microsoft.CodeAnalysis.CSharp;
  5. using Microsoft.CodeAnalysis.CSharp.Syntax;
  6. using Microsoft.CodeAnalysis.Diagnostics;
  7. namespace ET.Analyzer
  8. {
  9. [DiagnosticAnalyzer(LanguageNames.CSharp)]
  10. public class ClassDeclarationInHotfixAnalyzer: DiagnosticAnalyzer
  11. {
  12. private const string Title = "Hotfix程序集中 只能声明含有BaseAttribute子类特性的类或静态类";
  13. private const string MessageFormat = "Hotfix程序集中 只能声明含有BaseAttribute子类特性的类或静态类 类: {0}";
  14. private const string Description = "Hotfix程序集中 只能声明含有BaseAttribute子类特性的类或静态类.";
  15. private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticIds.ClassDeclarationInHotfixAnalyzerRuleId,
  16. Title,
  17. MessageFormat,
  18. DiagnosticCategories.Hotfix,
  19. DiagnosticSeverity.Error, true, Description);
  20. public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
  21. public override void Initialize(AnalysisContext context)
  22. {
  23. if (!AnalyzerGlobalSetting.EnableAnalyzer)
  24. {
  25. return;
  26. }
  27. context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
  28. context.EnableConcurrentExecution();
  29. context.RegisterCompilationStartAction((analysisContext =>
  30. {
  31. if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.Compilation.AssemblyName,AnalyzeAssembly.AllHotfix))
  32. {
  33. analysisContext.RegisterSemanticModelAction((this.AnalyzeSemanticModel));
  34. }
  35. } ));
  36. }
  37. private void AnalyzeSemanticModel(SemanticModelAnalysisContext analysisContext)
  38. {
  39. foreach (var classDeclarationSyntax in analysisContext.SemanticModel.SyntaxTree.GetRoot().DescendantNodes<ClassDeclarationSyntax>())
  40. {
  41. var classTypeSymbol = analysisContext.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax);
  42. if (classTypeSymbol!=null)
  43. {
  44. Analyzer(analysisContext, classTypeSymbol);
  45. }
  46. }
  47. }
  48. private void Analyzer(SemanticModelAnalysisContext context, INamedTypeSymbol namedTypeSymbol)
  49. {
  50. if (namedTypeSymbol.IsStatic)
  51. {
  52. return;
  53. }
  54. if (!this.CheckIsTypeOrBaseTypeHasBaseAttributeInherit(namedTypeSymbol))
  55. {
  56. foreach (SyntaxReference? declaringSyntaxReference in namedTypeSymbol.DeclaringSyntaxReferences)
  57. {
  58. Diagnostic diagnostic = Diagnostic.Create(Rule, declaringSyntaxReference.GetSyntax()?.GetLocation(), namedTypeSymbol.Name);
  59. //Diagnostic diagnostic = Diagnostic.Create(Rule, declaringSyntaxReference.GetSyntax()?.GetLocation(), context.SemanticModel.SyntaxTree.FilePath);
  60. context.ReportDiagnostic(diagnostic);
  61. }
  62. }
  63. }
  64. /// <summary>
  65. /// 检查该类或其基类是否有BaseAttribute的子类特性标记
  66. /// </summary>
  67. private bool CheckIsTypeOrBaseTypeHasBaseAttributeInherit(INamedTypeSymbol namedTypeSymbol)
  68. {
  69. INamedTypeSymbol? typeSymbol = namedTypeSymbol;
  70. while (typeSymbol != null)
  71. {
  72. if (typeSymbol.HasBaseAttribute(Definition.BaseAttribute))
  73. {
  74. return true;
  75. }
  76. typeSymbol = typeSymbol.BaseType;
  77. }
  78. return false;
  79. }
  80. }
  81. }