|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Immutable; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.Operations; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.Analyzers |
| 10 | +{ |
| 11 | + internal class MiddlewareAnalysis : ConfigureMethodAnalysis |
| 12 | + { |
| 13 | + public static MiddlewareAnalysis CreateAndInitialize(StartupAnalysisContext context) |
| 14 | + { |
| 15 | + if (context == null) |
| 16 | + { |
| 17 | + throw new ArgumentNullException(nameof(context)); |
| 18 | + } |
| 19 | + |
| 20 | + var symbols = context.StartupSymbols; |
| 21 | + var analysis = new MiddlewareAnalysis((IMethodSymbol)context.OperationBlockStartAnalysisContext.OwningSymbol); |
| 22 | + |
| 23 | + var middleware = ImmutableArray.CreateBuilder<MiddlewareItem>(); |
| 24 | + |
| 25 | + context.OperationBlockStartAnalysisContext.RegisterOperationAction(context => |
| 26 | + { |
| 27 | + // We're looking for usage of extension methods, so we need to look at the 'this' parameter |
| 28 | + // rather than invocation.Instance. |
| 29 | + if (context.Operation is IInvocationOperation invocation && |
| 30 | + invocation.Instance == null && |
| 31 | + invocation.Arguments.Length >= 1 && |
| 32 | + invocation.Arguments[0].Parameter?.Type == symbols.IApplicationBuilder) |
| 33 | + { |
| 34 | + middleware.Add(new MiddlewareItem(invocation)); |
| 35 | + } |
| 36 | + }, OperationKind.Invocation); |
| 37 | + |
| 38 | + context.OperationBlockStartAnalysisContext.RegisterOperationBlockEndAction(context => |
| 39 | + { |
| 40 | + analysis.Middleware = middleware.ToImmutable(); |
| 41 | + }); |
| 42 | + |
| 43 | + return analysis; |
| 44 | + } |
| 45 | + |
| 46 | + public MiddlewareAnalysis(IMethodSymbol configureMethod) |
| 47 | + : base(configureMethod) |
| 48 | + { |
| 49 | + } |
| 50 | + |
| 51 | + public ImmutableArray<MiddlewareItem> Middleware { get; private set; } = ImmutableArray<MiddlewareItem>.Empty; |
| 52 | + } |
| 53 | +} |
0 commit comments