Esempio n. 1
0
        public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            var newMethod = VisitMemberDeclaration(node, base.VisitMethodDeclaration(node));

            if (node.HasAttribute <ExpectedOutputAttribute>())
            {
                ExerciseClassName       = ExerciseClassName ?? FindParentClassName(node);
                Exercise.ExpectedOutput = node.GetAttributes <ExpectedOutputAttribute>().Select(attr => attr.GetArgument(0)).FirstOrDefault();
            }
            if (node.HasAttribute <HideExpectedOutputOnErrorAttribute>())
            {
                Exercise.HideExpectedOutputOnError = true;
            }
            if (node.HasAttribute <HintAttribute>())
            {
                Exercise.HintsMd.AddRange(node.GetAttributes <HintAttribute>().Select(attr => attr.GetArgument(0)));
            }
            if (node.HasAttribute <CommentAfterExerciseIsSolved>())
            {
                Exercise.CommentAfterExerciseIsSolved = node.GetAttributes <CommentAfterExerciseIsSolved>().Single().GetArgument(0);
            }
            if (node.HasAttribute <ExerciseAttribute>())
            {
                ExerciseClassName            = ExerciseClassName ?? FindParentClassName(node);
                Exercise.EthalonSolution    += node.WithoutAttributes();
                Exercise.ExerciseInitialCode = GetExerciseCode(node);
                if (node.HasAttribute <SingleStatementMethodAttribute>())
                {
                    Exercise.ValidatorName += " SingleStatementMethod";
                }
            }
            return(newMethod);
        }
Esempio n. 2
0
        public static List <AttributeSyntax> OutputAttribute(this MethodDeclarationSyntax node)
        {
            if (node == null)
            {
                return(new List <AttributeSyntax>());
            }

            return(node.GetAttributes("Output"));
        }
Esempio n. 3
0
        public static List <AttributeSyntax> DescriptionAttribute(this MethodDeclarationSyntax node)
        {
            if (node == null)
            {
                return(new List <AttributeSyntax>());
            }

            return(node.GetAttributes("Description"));
        }
Esempio n. 4
0
        public static Span HasValidPreviousVersionAttribute(this MethodDeclarationSyntax node)
        {
            if (node == null || node.IsDeprecated() || !node.HasAttribute("PreviousVersion"))
            {
                return(null); //Don't care about deprecated methods or if the method does not have the attribute at all
            }
            List <AttributeSyntax> previousVersionAttributes = node.GetAttributes("PreviousVersion");

            string currentVersion = BH.Engine.Test.CodeCompliance.Query.CurrentAssemblyFileVersion();

            foreach (AttributeSyntax a in previousVersionAttributes)
            {
                string givenVersion = a.ArgumentList.Arguments[0].Expression.GetFirstToken().Value.ToString();

                if (givenVersion != currentVersion)
                {
                    return(node.Identifier.Span.ToSpan());
                }
            }

            return(null); //All ok
        }
        public static Span HasUniqueMultiOutputAttributes(this MethodDeclarationSyntax node)
        {
            if (node == null)
            {
                return(null);
            }

            bool isvoid = false;

            if (node.ReturnType is PredefinedTypeSyntax)
            {
                isvoid = ((PredefinedTypeSyntax)node.ReturnType).Keyword.Kind() == SyntaxKind.VoidKeyword;
            }

            if (isvoid || node.IsDeprecated() || node.HasOutputAttribute() != null)
            {
                return(null); //Don't care about void return types or deprecated methods or methods with no output attribute at all
            }
            string returnType = node.ReturnType.ToString();

            if (!returnType.StartsWith("Output<"))
            {
                return(null);                                            //Not a multi output return type
            }
            returnType = returnType.Substring(7);                        //Trim the 'Output<' from the string
            returnType = returnType.Substring(0, returnType.Length - 1); //Trim the final '>' from the string

            List <string> returnOptions = new List <string>();
            int           split         = 0;
            string        builtString   = "";

            foreach (char x in returnType)
            {
                if (x == '<')
                {
                    split++;
                }

                if (x == '>')
                {
                    split--;
                }

                if (x == ',' && split == 0)
                {
                    returnOptions.Add(builtString);
                    builtString = "";
                }
                else
                {
                    builtString += x;
                }
            }
            returnOptions.Add(builtString); //Add the last built string that wasn't separated by a comma

            List <AttributeSyntax> multiOutAttrs = node.GetAttributes("MultiOutput");

            var distinctOutputNumbers = multiOutAttrs.Select(x =>
            {
                string t         = x.ToString();
                string newString = "";
                int bracketCount = 0;
                foreach (char i in t)
                {
                    if (i == '<')
                    {
                        bracketCount++;
                    }

                    if (i == '>')
                    {
                        bracketCount--;
                    }

                    if (i == ',' && bracketCount == 0)
                    {
                        return(newString);
                    }
                    else
                    {
                        newString += i;
                    }
                }

                return(newString); //As a backup
            });

            distinctOutputNumbers = distinctOutputNumbers.Distinct();

            if (returnOptions.Count != distinctOutputNumbers.Count())
            {
                return(node.Identifier.Span.ToSpan()); //If someone has used the same index more than once in declaring a multi output
            }
            else
            {
                return(null);
            }
        }
        public static Span HasValidMultiOutputAttributes(this MethodDeclarationSyntax node)
        {
            if (node == null)
            {
                return(null);
            }

            bool isvoid = false;

            if (node.ReturnType is PredefinedTypeSyntax)
            {
                isvoid = ((PredefinedTypeSyntax)node.ReturnType).Keyword.Kind() == SyntaxKind.VoidKeyword;
            }

            if (isvoid || node.IsDeprecated() || node.HasOutputAttribute() != null)
            {
                return(null); //Don't care about void return types or deprecated methods or methods with no output attribute at all
            }
            string returnType = node.ReturnType.ToString();

            if (!returnType.StartsWith("Output<"))
            {
                return(null);                                            //Not a multi output return type
            }
            returnType = returnType.Substring(7);                        //Trim the 'Output<' from the string
            returnType = returnType.Substring(0, returnType.Length - 1); //Trim the final '>' from the string

            List <string> returnOptions = new List <string>();
            int           split         = 0;
            string        builtString   = "";

            foreach (char x in returnType)
            {
                if (x == '<')
                {
                    split++;
                }

                if (x == '>')
                {
                    split--;
                }

                if (x == ',' && split == 0)
                {
                    //Either splitting at a comma, or end of the string
                    returnOptions.Add(builtString);
                    builtString = "";
                }
                else
                {
                    builtString += x;
                }
            }
            returnOptions.Add(builtString); //Add the last built string that wasn't separated by a comma

            List <AttributeSyntax> multiOutAttrs = node.GetAttributes("MultiOutput");

            if (returnOptions.Count != multiOutAttrs.Count)
            {
                return(node.Identifier.Span.ToSpan());
            }
            else
            {
                return(null);
            }
        }