|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. 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.Diagnostics; |
| 8 | + |
| 9 | +namespace Microsoft.Azure.Functions.Worker.Sdk.Analyzers |
| 10 | +{ |
| 11 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 12 | + public class DeferredBindingAttributeNotSupported : DiagnosticAnalyzer |
| 13 | + { |
| 14 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(DiagnosticDescriptors.DeferredBindingAttributeNotSupported); } } |
| 15 | + |
| 16 | + public override void Initialize(AnalysisContext context) |
| 17 | + { |
| 18 | + context.EnableConcurrentExecution(); |
| 19 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze); |
| 20 | + context.RegisterSymbolAction(AnalyzeMethod, SymbolKind.NamedType); |
| 21 | + } |
| 22 | + |
| 23 | + private static void AnalyzeMethod(SymbolAnalysisContext symbolAnalysisContext) |
| 24 | + { |
| 25 | + var symbol = (INamedTypeSymbol)symbolAnalysisContext.Symbol; |
| 26 | + |
| 27 | + var attributes = symbol.GetAttributes(); |
| 28 | + |
| 29 | + if (attributes.IsEmpty) |
| 30 | + { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + foreach (var attribute in attributes) |
| 35 | + { |
| 36 | + if (attribute.IsSupportsDeferredBindingAttribute() && !IsInputOrTriggerBinding(symbol)) |
| 37 | + { |
| 38 | + var location = Location.Create(attribute.ApplicationSyntaxReference.SyntaxTree, attribute.ApplicationSyntaxReference.Span); |
| 39 | + var diagnostic = Diagnostic.Create(DiagnosticDescriptors.DeferredBindingAttributeNotSupported, location, attribute.AttributeClass.Name); |
| 40 | + symbolAnalysisContext.ReportDiagnostic(diagnostic); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static bool IsInputOrTriggerBinding(INamedTypeSymbol symbol) |
| 46 | + { |
| 47 | + var baseType = symbol.BaseType?.ToDisplayString(); |
| 48 | + |
| 49 | + if (string.Equals(baseType,Constants.Types.InputBindingAttribute, StringComparison.Ordinal) |
| 50 | + || string.Equals(baseType,Constants.Types.TriggerBindingAttribute, StringComparison.Ordinal)) |
| 51 | + { |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments