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.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.RegisterSyntaxNodeAction(AnalyzeMemberAccess, SyntaxKind.SimpleMemberAccessExpression);
  24. }
  25. private static void AnalyzeMemberAccess(SyntaxNodeAnalysisContext context)
  26. {
  27. // Bail out if the accessed member isn't named 'Guild'
  28. var memberAccessSymbol = context.SemanticModel.GetSymbolInfo(context.Node).Symbol;
  29. if (memberAccessSymbol.Name != "Guild")
  30. return;
  31. // Bail out if it happens to be 'ContextType.Guild' in the '[RequireContext]' argument
  32. if (context.Node.Parent is AttributeArgumentSyntax)
  33. return;
  34. // Bail out if the containing class doesn't derive from 'ModuleBase<T>'
  35. var typeNode = context.Node.FirstAncestorOrSelf<TypeDeclarationSyntax>();
  36. var typeSymbol = context.SemanticModel.GetDeclaredSymbol(typeNode);
  37. if (!typeSymbol.DerivesFromModuleBase())
  38. return;
  39. // Bail out if the containing method isn't marked with '[Command]'
  40. var methodNode = context.Node.FirstAncestorOrSelf<MethodDeclarationSyntax>();
  41. var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
  42. var methodAttributes = methodSymbol.GetAttributes();
  43. if (!methodAttributes.Any(a => a.AttributeClass.Name == nameof(CommandAttribute)))
  44. return;
  45. // Is the '[RequireContext]' attribute not applied to either the
  46. // method or the class, or its argument isn't 'ContextType.Guild'?
  47. var ctxAttribute = methodAttributes.SingleOrDefault(_attributeDataPredicate)
  48. ?? typeSymbol.GetAttributes().SingleOrDefault(_attributeDataPredicate);
  49. if (ctxAttribute == null || ctxAttribute.ConstructorArguments.Any(arg => !arg.Value.Equals((int)ContextType.Guild)))
  50. {
  51. // Report the diagnostic
  52. var diagnostic = Diagnostic.Create(Rule, context.Node.GetLocation(), methodSymbol.Name);
  53. context.ReportDiagnostic(diagnostic);
  54. }
  55. }
  56. private static readonly Func<AttributeData, bool> _attributeDataPredicate =
  57. (a => a.AttributeClass.Name == nameof(RequireContextAttribute));
  58. }
  59. }