|  | 
|  | 1 | +using System.Collections.Immutable; | 
|  | 2 | +using System.Linq; | 
|  | 3 | +using Microsoft.CodeAnalysis; | 
|  | 4 | +using Microsoft.CodeAnalysis.CSharp; | 
|  | 5 | +using Microsoft.CodeAnalysis.CSharp.Syntax; | 
|  | 6 | +using Microsoft.CodeAnalysis.Diagnostics; | 
|  | 7 | + | 
|  | 8 | +namespace RubberduckCodeAnalysis | 
|  | 9 | +{ | 
|  | 10 | +    [DiagnosticAnalyzer(LanguageNames.CSharp)] | 
|  | 11 | +    public class ChainedWrapperAnalyzer : DiagnosticAnalyzer | 
|  | 12 | +    { | 
|  | 13 | +        private const string ChainedWrapperId = "ChainedWrapper"; | 
|  | 14 | +        private static readonly DiagnosticDescriptor ChainedWrapperRule = new DiagnosticDescriptor( | 
|  | 15 | +            ChainedWrapperId, | 
|  | 16 | +            new LocalizableResourceString(nameof(Resources.ChainedWrapperTitle), Resources.ResourceManager, typeof(Resources)), | 
|  | 17 | +            new LocalizableResourceString(nameof(Resources.ChainedWrapperMessageFormat), Resources.ResourceManager, typeof(Resources)), | 
|  | 18 | +            new LocalizableResourceString(nameof(Resources.AnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),  | 
|  | 19 | +            DiagnosticSeverity.Error,  | 
|  | 20 | +            true, | 
|  | 21 | +            new LocalizableResourceString(nameof(Resources.ChainedWrapperDescription), Resources.ResourceManager, typeof(Resources)) | 
|  | 22 | +        ); | 
|  | 23 | + | 
|  | 24 | +        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => | 
|  | 25 | +            ImmutableArray.Create(ChainedWrapperRule); | 
|  | 26 | + | 
|  | 27 | +        public override void Initialize(AnalysisContext context) | 
|  | 28 | +        { | 
|  | 29 | +            context.RegisterSyntaxNodeAction(AnalyzeSymbol, SyntaxKind.SimpleMemberAccessExpression); | 
|  | 30 | +        } | 
|  | 31 | +        | 
|  | 32 | +        private static void AnalyzeSymbol(SyntaxNodeAnalysisContext context) | 
|  | 33 | +        { | 
|  | 34 | +            var node = (MemberAccessExpressionSyntax)context.Node; | 
|  | 35 | + | 
|  | 36 | +            if (!(node.Expression is InvocationExpressionSyntax || node.Expression is MemberAccessExpressionSyntax)) | 
|  | 37 | +            { | 
|  | 38 | +                return; | 
|  | 39 | +            } | 
|  | 40 | + | 
|  | 41 | +            var expInterfaces = context.SemanticModel.GetTypeInfo(node.Expression).Type?.AllInterfaces; | 
|  | 42 | +            var nameInterfaces = context.SemanticModel.GetTypeInfo(node.Name).Type?.AllInterfaces; | 
|  | 43 | + | 
|  | 44 | +            if (expInterfaces?.Any(a => a.ToDisplayString() == "Rubberduck.VBEditor.SafeComWrappers.Abstract.ISafeComWrapper") == true || | 
|  | 45 | +                nameInterfaces?.Any(a => a.ToDisplayString() == "Rubberduck.VBEditor.SafeComWrappers.Abstract.ISafeComWrapper") == true) | 
|  | 46 | +            { | 
|  | 47 | +                var diagnostic = Diagnostic.Create(ChainedWrapperRule, node.GetLocation()); | 
|  | 48 | +                context.ReportDiagnostic(diagnostic); | 
|  | 49 | +            } | 
|  | 50 | +        } | 
|  | 51 | +    } | 
|  | 52 | +} | 
0 commit comments