Esempio n. 1
0
        private static void CheckClientMethod(ISymbolAnalysisContext context, IMethodSymbol member)
        {
            if (!member.IsVirtual)
            {
                context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0003, member.Locations.First()), member);
            }

            var lastArgument = member.Parameters.LastOrDefault();

            if (lastArgument == null || lastArgument.Name != "cancellationToken" || lastArgument.Type.Name != "CancellationToken" || !lastArgument.HasExplicitDefaultValue)
            {
                context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0002, member.Locations.FirstOrDefault()), member);
            }
        }
Esempio n. 2
0
        private void AnalyzeClientOptionsType(ISymbolAnalysisContext context)
        {
            var typeSymbol = (INamedTypeSymbol)context.Symbol;

            var serviceVersion     = typeSymbol.GetTypeMembers(ServiceVersionName);
            var serviceVersionEnum = serviceVersion.SingleOrDefault(member => member.TypeKind == TypeKind.Enum);

            if (serviceVersionEnum == null)
            {
                context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0008, typeSymbol.Locations.First()), typeSymbol);
                return;
            }

            foreach (var serviceVersionMember in serviceVersionEnum.GetMembers().Where(member => member.Kind == SymbolKind.Field))
            {
                var parts = serviceVersionMember.Name.Split('_');
                foreach (var part in parts)
                {
                    if (part.Length == 0 || !(char.IsDigit(part[0]) || char.IsUpper(part[0])))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0016, serviceVersionMember.Locations.First()), serviceVersionMember);
                    }
                }
            }

            foreach (var constructor in typeSymbol.Constructors)
            {
                if (constructor.DeclaredAccessibility == Accessibility.Public)
                {
                    if (constructor.Parameters == null || constructor.Parameters.Length == 0)
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0009, constructor.Locations.First()), typeSymbol);
                        continue;
                    }

                    var firstParam = constructor.Parameters.FirstOrDefault();
                    if (!IsServiceVersionParameter(firstParam))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0009, firstParam.Locations.First()), typeSymbol);
                        continue;
                    }

                    var maxVersion = serviceVersionEnum.GetMembers().Where(m => m.Kind == SymbolKind.Field).Max(m => ((IFieldSymbol)m).ConstantValue);
                    if (!firstParam.HasExplicitDefaultValue || (int)firstParam.ExplicitDefaultValue != (int)maxVersion)
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0010, firstParam.Locations.First()), typeSymbol);
                    }
                }
            }
        }
        public override void Analyze(ISymbolAnalysisContext context)
        {
            var  namespaceSymbol = (INamespaceSymbol)context.Symbol;
            bool hasPublicTypes  = false;

            foreach (var member in namespaceSymbol.GetMembers())
            {
                if (member.IsType && member.DeclaredAccessibility == Accessibility.Public)
                {
                    hasPublicTypes = true;
                    break;
                }
            }

            if (!hasPublicTypes)
            {
                return;
            }

            var displayString = namespaceSymbol.ToDisplayString();

            foreach (var prefix in AllowedNamespacePrefix)
            {
                if (displayString.StartsWith(prefix))
                {
                    return;
                }
            }

            foreach (var namespaceSymbolLocation in namespaceSymbol.Locations)
            {
                context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0001, namespaceSymbolLocation, displayString), namespaceSymbol);
            }
        }
Esempio n. 4
0
        public override void AnalyzeCore(ISymbolAnalysisContext context)
        {
            INamedTypeSymbol type = (INamedTypeSymbol)context.Symbol;

            foreach (var member in type.GetMembers())
            {
                if (member is IMethodSymbol methodSymbol && methodSymbol.Name.EndsWith(AsyncSuffix) && methodSymbol.DeclaredAccessibility == Accessibility.Public)
                {
                    CheckClientMethod(context, methodSymbol);

                    var syncMemberName = member.Name.Substring(0, member.Name.Length - AsyncSuffix.Length);

                    var syncMember = FindMethod(type.GetMembers(syncMemberName).OfType <IMethodSymbol>(), methodSymbol.Parameters);

                    if (syncMember == null)
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0004, member.Locations.First()), member);
                    }
                    else
                    {
                        CheckClientMethod(context, syncMember);
                    }
                }
            }
        }
Esempio n. 5
0
        public override void AnalyzeCore(ISymbolAnalysisContext context)
        {
            var type = (INamedTypeSymbol)context.Symbol;

            if (!type.Constructors.Any(c => (c.DeclaredAccessibility == Accessibility.Protected || c.DeclaredAccessibility == Accessibility.Public) && c.Parameters.Length == 0))
            {
                context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0005, type.Locations.First()), type);
            }
        }
Esempio n. 6
0
        public override void AnalyzeCore(ISymbolAnalysisContext context)
        {
            var type = (INamedTypeSymbol)context.Symbol;

            if (!type.Constructors.Any(c => (c.DeclaredAccessibility == Accessibility.Protected || c.DeclaredAccessibility == Accessibility.Public) && c.Parameters.Length == 0))
            {
                context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0005, type.Locations.First()), type);
            }

            foreach (var constructor in type.Constructors)
            {
                if (constructor.DeclaredAccessibility == Accessibility.Public)
                {
                    var lastParameter = constructor.Parameters.LastOrDefault();

                    if (IsClientOptionsParameter(lastParameter))
                    {
                        // Allow optional options parameters
                        if (lastParameter.IsOptional)
                        {
                            continue;
                        }

                        var nonOptionsMethod = FindMethod(
                            type.Constructors, constructor.TypeParameters, constructor.Parameters.RemoveAt(constructor.Parameters.Length - 1));

                        if (nonOptionsMethod == null || nonOptionsMethod.DeclaredAccessibility != Accessibility.Public)
                        {
                            context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0006, constructor.Locations.First()), constructor);
                        }
                    }
                    else
                    {
                        var optionsMethod = FindMethod(
                            type.Constructors, constructor.TypeParameters, constructor.Parameters, IsClientOptionsParameter);

                        if (optionsMethod == null || optionsMethod.DeclaredAccessibility != Accessibility.Public)
                        {
                            context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0007, constructor.Locations.First()), constructor);
                        }
                    }
                }
            }
        }
        public override void Analyze(ISymbolAnalysisContext context)
        {
            var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;

            // Account for an `I` prefix in the interfaces.
            var minimumWordCount = namedTypeSymbol.TypeKind == TypeKind.Interface ? 2 : 1;

            if (namedTypeSymbol.DeclaredAccessibility == Accessibility.Public &&
                namedTypeSymbol.ContainingType == null &&
                CountWords(namedTypeSymbol.Name) <= minimumWordCount)
            {
                foreach (var location in namedTypeSymbol.Locations)
                {
                    context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0012, location, context.Symbol), context.Symbol);
                }
            }
        }
            static void CheckType(ISymbolAnalysisContext context, ITypeSymbol type, ISymbol symbol)
            {
                if (type is INamedTypeSymbol namedTypeSymbol)
                {
                    if (BannedAssemblies.Contains(type.ContainingAssembly.Name))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0014, symbol.Locations.First(), BannedAssembliesMessageArgs), symbol);
                    }

                    if (namedTypeSymbol.IsGenericType)
                    {
                        foreach (var typeArgument in namedTypeSymbol.TypeArguments)
                        {
                            CheckType(context, typeArgument, symbol);
                        }
                    }
                }
            }