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

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