Exemplo n.º 1
0
        public static ClassDeclarationSyntax CreateWithSuppressMessageAttributeByStyleCopCheckId(string classTypeName, int checkId, string justification = "")
        {
            if (classTypeName == null)
            {
                throw new ArgumentNullException(nameof(classTypeName));
            }

            return(Create(classTypeName)
                   .AddSuppressMessageAttribute(SuppressMessageAttributeFactory.CreateStyleCopSuppression(checkId, justification)));
        }
Exemplo n.º 2
0
        public static ClassDeclarationSyntax CreateWithInheritClassTypeAndSuppressMessageAttributeByCodeAnalysisCheckId(string classTypeName, string inheritClassTypeName, int checkId, string justification = "")
        {
            if (classTypeName == null)
            {
                throw new ArgumentNullException(nameof(classTypeName));
            }

            if (inheritClassTypeName == null)
            {
                throw new ArgumentNullException(nameof(inheritClassTypeName));
            }

            return(CreateWithInheritClassType(classTypeName, inheritClassTypeName)
                   .AddSuppressMessageAttribute(SuppressMessageAttributeFactory.CreateCodeAnalysisSuppression(checkId, justification)));
        }
Exemplo n.º 3
0
        public static EnumDeclarationSyntax Create(string title, IList <IOpenApiAny> apiSchemaEnums)
        {
            if (title == null)
            {
                throw new ArgumentNullException(nameof(title));
            }

            if (apiSchemaEnums == null)
            {
                throw new ArgumentNullException(nameof(apiSchemaEnums));
            }

            // Create an enum
            var enumDeclaration = SyntaxFactory.EnumDeclaration(title)
                                  .AddModifiers(SyntaxTokenFactory.PublicKeyword());

            // Find values to the enum
            var containTypeName = false;
            var lines           = new List <string>();
            var intValues       = new List <int>();

            foreach (var item in apiSchemaEnums)
            {
                if (!(item is OpenApiString openApiString))
                {
                    continue;
                }

                lines.Add(openApiString.Value.Trim());

                if (!openApiString.Value.Contains("=", StringComparison.Ordinal))
                {
                    continue;
                }

                var sa = openApiString.Value.Split('=', StringSplitOptions.RemoveEmptyEntries);
                var s  = sa.Last().Trim();
                if (int.TryParse(s, out int val))
                {
                    intValues.Add(val);
                }
            }

            // Add values to the enum
            foreach (var line in lines)
            {
                enumDeclaration = enumDeclaration.AddMembers(SyntaxFactory.EnumMemberDeclaration(line));

                var sa = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                if (sa.Length > 0 && sa.First().Equals("Object", StringComparison.Ordinal))
                {
                    containTypeName = true;
                }
            }

            // Add SuppressMessageAttribute
            if (containTypeName)
            {
                enumDeclaration = enumDeclaration
                                  .AddSuppressMessageAttribute(SuppressMessageAttributeFactory.Create(1720, null));
            }

            // Add FlagAttribute
            if (intValues.Count > 0)
            {
                bool isAllValidBinarySequence = intValues
                                                .Where(x => x != 0)
                                                .All(x => x.IsBinarySequence());
                if (isAllValidBinarySequence)
                {
                    enumDeclaration = enumDeclaration.AddFlagAttribute();
                }
            }

            return(enumDeclaration);
        }