You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

GuildAccessAnalyzer.cs 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. using Discord.Commands;
  9. namespace Discord.Analyzers
  10. {
  11. [DiagnosticAnalyzer(LanguageNames.CSharp)]
  12. public sealed class GuildAccessAnalyzer : DiagnosticAnalyzer
  13. {
  14. private const string DiagnosticId = "DNET0001";
  15. private const string Title = "Limit command to Guild contexts.";
  16. private const string MessageFormat = "Command method '{0}' is accessing 'Context.Guild' but is not restricted to Guild contexts.";
  17. private const string Description = "Accessing 'Context.Guild' in a command without limiting the command to run only in guilds.";
  18. private const string Category = "API Usage";
  19. private static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
  20. public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
  21. public override void Initialize(AnalysisContext context)
  22. {
  23. context.EnableConcurrentExecution();
  24. context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
  25. context.RegisterSyntaxNodeAction(AnalyzeMemberAccess, SyntaxKind.SimpleMemberAccessExpression);
  26. }
  27. private static void AnalyzeMemberAccess(SyntaxNodeAnalysisContext context)
  28. {
  29. // Bail out if the accessed member isn't named 'Guild'
  30. var memberAccessSymbol = context.SemanticModel.GetSymbolInfo(context.Node).Symbol;
  31. if (memberAccessSymbol.Name != "Guild")
  32. return;
  33. // Bail out if it happens to be 'ContextType.Guild' in the '[RequireContext]' argument
  34. if (context.Node.Parent is AttributeArgumentSyntax)
  35. return;
  36. // Bail out if the containing class doesn't derive from 'ModuleBase<T>'
  37. var typeNode = context.Node.FirstAncestorOrSelf<TypeDeclarationSyntax>();
  38. var typeSymbol = context.SemanticModel.GetDeclaredSymbol(typeNode);
  39. if (!typeSymbol.DerivesFromModuleBase())
  40. return;
  41. // Bail out if the containing method isn't marked with '[Command]'
  42. var methodNode = context.Node.FirstAncestorOrSelf<MethodDeclarationSyntax>();
  43. var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
  44. var methodAttributes = methodSymbol.GetAttributes();
  45. if (!methodAttributes.Any(a => a.AttributeClass.Name == nameof(CommandAttribute)))
  46. return;
  47. // Is the '[RequireContext]' attribute not applied to either the
  48. // method or the class, or its argument isn't 'ContextType.Guild'?
  49. var ctxAttribute = methodAttributes.SingleOrDefault(_attributeDataPredicate)
  50. ?? typeSymbol.GetAttributes().SingleOrDefault(_attributeDataPredicate);
  51. if (ctxAttribute == null || ctxAttribute.ConstructorArguments.Any(arg => !arg.Value.Equals((int)ContextType.Guild)))
  52. {
  53. // Report the diagnostic
  54. var diagnostic = Diagnostic.Create(Rule, context.Node.GetLocation(), methodSymbol.Name);
  55. context.ReportDiagnostic(diagnostic);
  56. }
  57. }
  58. private static readonly Func<AttributeData, bool> _attributeDataPredicate =
  59. (a => a.AttributeClass.Name == nameof(RequireContextAttribute));
  60. }
  61. }