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.

ConfigureAwaitAnalyzer.cs 2.6 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Immutable;
  3. using Microsoft.CodeAnalysis;
  4. using Microsoft.CodeAnalysis.CSharp;
  5. using Microsoft.CodeAnalysis.CSharp.Syntax;
  6. using Microsoft.CodeAnalysis.Diagnostics;
  7. namespace RegexAnalyzer
  8. {
  9. [DiagnosticAnalyzer(LanguageNames.CSharp)]
  10. public class ConfigureAwaitAnalyzer : DiagnosticAnalyzer
  11. {
  12. public const string DiagnosticId = "ConfigureAwait";
  13. internal const string Title = "ConfigureAwait was not specified";
  14. internal const string MessageFormat = "ConfigureAwait error {0}";
  15. internal const string Description = "ConfigureAwait(false) should be used.";
  16. internal const string Category = "Usage";
  17. internal static DiagnosticDescriptor Rule =
  18. new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat,
  19. Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
  20. public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
  21. public override void Initialize(AnalysisContext context)
  22. {
  23. context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.InvocationExpression);
  24. }
  25. private void AnalyzeNode(SyntaxNodeAnalysisContext context)
  26. {
  27. /*var invocationExpr = (InvocationExpressionSyntax)context.Node;
  28. var memberAccessExpr = invocationExpr.Expression as MemberAccessExpressionSyntax;
  29. if (memberAccessExpr?.Name.ToString() != "Match") return;
  30. var memberSymbol = context.SemanticModel.GetSymbolInfo(memberAccessExpr).Symbol as IMethodSymbol;
  31. if (!memberSymbol?.ToString().StartsWith("System.Text.RegularExpressions.Regex.Match") ?? true) return;
  32. var argumentList = invocationExpr.ArgumentList as ArgumentListSyntax;
  33. if ((argumentList?.Arguments.Count ?? 0) < 2) return;
  34. var regexLiteral = argumentList.Arguments[1].Expression as LiteralExpressionSyntax;
  35. if (regexLiteral == null) return;
  36. var regexOpt = context.SemanticModel.GetConstantValue(regexLiteral);
  37. if (!regexOpt.HasValue) return;
  38. var regex = regexOpt.Value as string;
  39. if (regex == null) return;
  40. try
  41. {
  42. System.Text.RegularExpressions.Regex.Match("", regex);
  43. }
  44. catch (ArgumentException e)
  45. {
  46. var diagnostic = Diagnostic.Create(Rule, regexLiteral.GetLocation(), e.Message);
  47. context.ReportDiagnostic(diagnostic);
  48. }*/
  49. }
  50. }
  51. }