Example #1
0
        static void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            var generator = context.Compilation.GetTypeByMetadataName(typeof(ProxyGeneratorAttribute).FullName);

            if (generator == null)
            {
                return;
            }

            var symbol = context.Compilation.GetSemanticModel(context.Node.SyntaxTree).GetSymbolInfo(context.Node);

            if (symbol.Symbol?.Kind == SymbolKind.Method)
            {
                var method = (IMethodSymbol)symbol.Symbol;
                if (method.GetAttributes().Any(x => x.AttributeClass == generator) &&
                    // Skip generic method definitions since they are typically usability overloads
                    // like Mock.Of<T>(...)
                    !method.TypeArguments.Any(x => x.Kind == SymbolKind.TypeParameter))
                {
                    var name = ProxyGenerator.GetProxyFullName(method.TypeArguments);

                    // See if the proxy already exists
                    var proxy = context.Compilation.GetTypeByMetadataName(name);
                    if (proxy == null)
                    {
                        var diagnostic = Diagnostic.Create(Rule, context.Node.GetLocation(),
                                                           new [] { new KeyValuePair <string, string>("Name", name) }.ToImmutableDictionary(),
                                                           name);

                        context.ReportDiagnostic(diagnostic);
                    }
                }
            }
        }
Example #2
0
        private static void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            var symbol = context.Compilation.GetSemanticModel(context.Node.SyntaxTree).GetSymbolInfo(context.Node);

            if (symbol.Symbol?.Kind == SymbolKind.Method)
            {
                var generator = context.Compilation.GetTypeByMetadataName(typeof(ProxyGeneratorAttribute).FullName);
                var method    = (IMethodSymbol)symbol.Symbol;
                if (method.GetAttributes().Any(x => x.AttributeClass == generator) &&
                    // Skip generic method definitions since they are typically usability overloads
                    // like Mock.Of<T>(...)
                    !method.TypeArguments.Any(x => x.Kind == SymbolKind.TypeParameter))
                {
                    var name = ProxyGenerator.GetProxyFullName(method.TypeArguments);

                    // See if the proxy already exists
                    var proxy = context.Compilation.GetTypeByMetadataName(name);
                    if (proxy != null)
                    {
                        // See if the symbol has any diagnostics associated
                        var diag      = context.Compilation.GetDiagnostics();
                        var proxyPath = proxy.Locations[0].GetLineSpan().Path;

                        Func <Location, bool> isProxyLoc = (loc) => loc.IsInSource && loc.GetLineSpan().Path == proxyPath;

                        var proxyDiag = diag
                                        .Where(d => isProxyLoc(d.Location) || d.AdditionalLocations.Any(a => isProxyLoc(a)))
                                        .Where(d => d.Id == "CS0535");

                        if (proxyDiag.Any())
                        {
                            // If there are compilation errors, we should update the proxy.
                            var diagnostic = Diagnostic.Create(Rule, context.Node.GetLocation(), name);

                            context.ReportDiagnostic(diagnostic);
                        }
                    }
                }
            }
        }