-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathAddDownstreamWebApiAnalyzer.cs
More file actions
68 lines (56 loc) · 2.59 KB
/
AddDownstreamWebApiAnalyzer.cs
File metadata and controls
68 lines (56 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Microsoft.Identity.Web.Analyzers;
/// <summary>
/// Analyzer that detects usage of obsolete AddDownstreamWebApi extension method.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AddDownstreamWebApiAnalyzer : DiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor Rule = new(
id: DiagnosticIds.AddDownstreamWebApiObsolete,
title: "AddDownstreamWebApi is obsolete",
messageFormat: "AddDownstreamWebApi is obsolete. Use AddDownstreamApi instead. See https://aka.ms/id-web-downstream-api-v2 for migration details.",
category: "Usage",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: "AddDownstreamWebApi has been replaced by AddDownstreamApi from Microsoft.Identity.Abstractions. Update your code to use the new API.");
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeInvocation, SyntaxKind.InvocationExpression);
}
private static void AnalyzeInvocation(SyntaxNodeAnalysisContext context)
{
var invocation = (InvocationExpressionSyntax)context.Node;
if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess)
{
return;
}
var methodSymbol = context.SemanticModel.GetSymbolInfo(invocation, context.CancellationToken).Symbol as IMethodSymbol;
if (methodSymbol is null)
{
return;
}
if (IsAddDownstreamWebApiMethod(methodSymbol))
{
var diagnostic = Diagnostic.Create(Rule, memberAccess.Name.GetLocation());
context.ReportDiagnostic(diagnostic);
}
}
private static bool IsAddDownstreamWebApiMethod(IMethodSymbol methodSymbol)
{
return methodSymbol.Name == "AddDownstreamWebApi" &&
methodSymbol.ContainingType?.Name == "DownstreamWebApiExtensions" &&
methodSymbol.ContainingNamespace?.ToDisplayString() == "Microsoft.Identity.Web";
}
}