private ParamsWithStepsAttribute GetParamsWithStepsAttribute(string fieldOrPropertyName, SyntaxList <AttributeListSyntax> attributeLists)
        {
            foreach (var attribute in attributeLists.SelectMany(attributeList =>
                                                                attributeList.Attributes.Where(attribute => attribute.Name.ToString() == paramsWithStepsAttribute)))
            {
                var arguments = (from argument in attribute.ArgumentList.Arguments
                                 select argument.Expression as LiteralExpressionSyntax
                                 into expression
                                 where expression != null && expression.Token.Value is int
                                 select(int) expression.Token.Value).ToList();

                const int expectedArgCount = 3;
                if (arguments.Count != expectedArgCount)
                {
                    var rawArgs = String.Join(", ", attribute.ArgumentList.Arguments.Select(
                                                  arg =>
                    {
                        var literalExpressionSyntax = arg.Expression as LiteralExpressionSyntax;
                        return(literalExpressionSyntax != null
                                       ? ((int)(literalExpressionSyntax.Token.Value)).ToString(CultureInfo.InvariantCulture)
                                       : arg.Expression.ToString());
                    }));
                    Console.WriteLine("{0} -> {1} - Wrong number of args, Expected {2}, Got {3}",
                                      fieldOrPropertyName, rawArgs, expectedArgCount, arguments.Count);
                    continue;
                }

                return(new ParamsWithStepsAttribute(arguments[0], arguments[1], arguments[2]));
            }

            return(null);
        }
Esempio n. 2
0
        private static bool IsTestRelatedMethod(SemanticModel semanticModel, SyntaxList <AttributeListSyntax> attributeLists)
        {
            var allAttributes = attributeLists.SelectMany(al => al.Attributes);

            return(allAttributes.Any(a => a.IsSetUpOrTearDownMethodAttribute(semanticModel) ||
                                     a.IsTestMethodAttribute(semanticModel)));
        }
Esempio n. 3
0
        public override SyntaxList <TNode> VisitList <TNode>(SyntaxList <TNode> list)
        {
            Console.WriteLine("Visiting Syntax List {0} : {1}", typeof(TNode).Name, list.Count);
            var result = List(list.SelectMany(visitor.Visit).Cast <TNode>());

            return(base.VisitList(result));
        }
Esempio n. 4
0
 public static IEnumerable <AttributeSyntax> GetAllAttributes(this SyntaxList <AttributeListSyntax> attributeListSyntaxes, string name)
 {
     return(attributeListSyntaxes
            .SelectMany(al => al.Attributes)
            .Where(a => a.Name.ToString() == name)
            .ToList());
 }
 private bool HasExcludedCodeAttribute(SyntaxList <AttributeListSyntax> attributeSyntaxLists)
 {
     return(attributeSyntaxLists
            .SelectMany(attributeList => attributeList.Attributes)
            .Select(GetAttributeName)
            .Any(IsExcludedAttribute));
 }
Esempio n. 6
0
        private SyntaxList <StatementSyntax> ConvertStatements(SyntaxList <Microsoft.CodeAnalysis.CSharp.Syntax.StatementSyntax> statements,
                                                               MethodBodyVisitor iteratorState = null)
        {
            var methodBodyVisitor = CreateMethodBodyVisitor(iteratorState);

            return(SyntaxFactory.List(statements.SelectMany(s => ConvertStatement(s, methodBodyVisitor))));
        }
 public static AttributeSyntax FindAttributeWithName(
     this SyntaxList <AttributeListSyntax> @this,
     string name)
 {
     return(@this
            .SelectMany(d => d.Attributes)
            .FirstOrDefault(d => HasName(d, name)));
 }
 public static AttributeSyntax GetAttribute(this SyntaxList <AttributeListSyntax> attributes, string attributeName)
 => attributes
 .SelectMany(a => a.Attributes)
 .FirstOrDefault(a =>
 {
     var name = a.Name.ToString();
     return(name == attributeName || name == attributeName + "Attribute");
 });
 public static AttributeSyntax SelectAttributeOfType(SyntaxList <AttributeListSyntax> attributeLists, Type type, SemanticModel semanticModel)
 {
     return(attributeLists.SelectMany(
                attributeListSyntax => attributeListSyntax.Attributes)
            .FirstOrDefault(
                attributeSyntax =>
                new AttributeInspector(attributeSyntax, semanticModel).IsAttributeTypeOf(type)));
 }
 static string MetaLiteral(SyntaxList <AttributeListSyntax> attributes)
 {
     return(attributes.SelectMany(attrs => attrs.Attributes)
            .Where(atr => atr.Name.As <SimpleNameSyntax>()?.Identifier.ValueText == "Meta")
            .Select(meta => meta.ArgumentList.Arguments.FirstOrDefault().Expression)
            .OfType <LiteralExpressionSyntax>()
            .Select(_literal => _literal.Token.ValueText)
            .FirstOrDefault());
 }
        public static string[] AttributeFullNames(this SyntaxList <AttributeListSyntax> collection,
                                                  SyntaxNodeAnalysisContext analysisContext)
        {
            var attributeSyntaxes = collection.SelectMany(e => e.Attributes);

            return(attributeSyntaxes
                   .Select(e => analysisContext.SemanticModel.GetTypeInfo(e).Type.ToString())
                   .ToArray());
        }
Esempio n. 12
0
 public static IEnumerable <AttributeSyntax> GetExpectedExceptionFreeTestCaseAttribute(SyntaxList <AttributeListSyntax> attrs,
                                                                                       bool alreadyHasExpectedExceptionAttribute = false)
 {
     return(attrs
            .SelectMany(x => x.Attributes)
            .Where(x => x.Name.ToString() == "TestCase" || x.Name.ToString() == "TestCaseAttribute")
            .Where(x => !alreadyHasExpectedExceptionAttribute &&
                   x.ArgumentList.Arguments.All(a =>
                                                a.NameEquals.Name.ToString() != "ExpectedExceptionName" && a.NameEquals.Name.ToString() != "ExpectedException")));
 }
        /// <summary>
        /// Parses an attribute
        /// </summary>
        /// <param name="attributeLists">The list of attributes to parse</param>
        /// <param name="separator">The separator to use between attributes</param>
        /// <returns>A tuple where the first value is the Swift attributes and the second contains the value of an ExportAttribute</returns>
        private static Tuple <string, string> ParseAttributes(SyntaxList <AttributeListSyntax> attributeLists,
                                                              string separator = " ")
        {
            if (!attributeLists.Any())
            {
                return(new Tuple <string, string>("", null));
            }

            var    output   = "";
            string exportAs = null;

            foreach (var attribute in attributeLists.SelectMany(attrList => attrList.Attributes))
            {
                if (attribute.Name.ToString() == nameof(UsingRef))
                {
                    continue;
                }
                if (IsProtocolBuilderAttribute(attribute, "ExportAttribute"))
                {
                    exportAs = SyntaxNode(attribute.ArgumentList.Arguments[0].Expression).Trim().Trim('"');
                    continue;
                }

                var attOutput = default(string);
                if (attribute.Name.ToString().ToLower().Contains("outputas"))
                {
                    var attLanguage = (Languages)Enum.Parse(typeof(Languages),
                                                            attribute.ArgumentList.Arguments[0].Expression.ToString().Split('.').Last());
                    if (attLanguage == Builder.Instance.Language)
                    {
                        attOutput = attribute.ArgumentList.Arguments[1].Expression.ToString().Trim('"');
                    }
                    if (string.IsNullOrWhiteSpace(attOutput))
                    {
                        continue;
                    }
                }

                if (string.IsNullOrWhiteSpace(attOutput))
                {
                    var syntaxNodeResult = SyntaxNode(attribute);
                    if (!string.IsNullOrWhiteSpace(syntaxNodeResult))
                    {
                        attOutput = SyntaxNode(attribute);
                    }
                }

                if (!string.IsNullOrWhiteSpace(attOutput))
                {
                    output += attOutput + separator;
                }
            }

            return(new Tuple <string, string>(output, exportAs));
        }
 private List<LuaExpressionSyntax> BuildAttributes(SyntaxList<AttributeListSyntax> attributeLists) {
   List<LuaExpressionSyntax> expressions = new List<LuaExpressionSyntax>();
   var attributes = attributeLists.SelectMany(i => i.Attributes);
   foreach (var node in attributes) {
     var expression = (LuaExpressionSyntax)node.Accept(this);
     if (expression != null) {
       expressions.Add(expression);
     }
   }
   return expressions;
 }
        private static bool HasAttribute(SyntaxList <AttributeListSyntax> source, string fullName, SemanticModel model, GeneratorExecutionContext ctx)
        {
            return(source.SelectMany(list => list.Attributes).Any(
                       atr =>
            {
                TypeInfo typeInfo = model.GetTypeInfo(atr, ctx.CancellationToken);
                string typeName = typeInfo.Type?.ToDisplayString(TypeNameFormat);

                return string.Equals(typeName, fullName, StringComparison.Ordinal);
            }));
        }
Esempio n. 16
0
        private static AttributeSyntax GetAttribute <TAttribute>(SyntaxList <AttributeListSyntax> attributeList)
        {
            var attributeType = typeof(TAttribute);

            return
                (attributeList.SelectMany(a => a.Attributes).FirstOrDefault(
                     a =>
                     ((IdentifierNameSyntax)a.Name).Identifier.Text == attributeType.Name ||
                     attributeType.Name.EndsWith("Attribute") &&
                     ((IdentifierNameSyntax)a.Name).Identifier.Text ==
                     attributeType.Name.Substring(0, attributeType.Name.Length - "Attribute".Length)));
        }
Esempio n. 17
0
        private static AttributeSyntax GetAttribute <TAttribute>(SyntaxList <AttributeDeclarationSyntax> attributeList)
        {
            var attributeType = typeof(TAttribute);

            return
                (attributeList.SelectMany(a => a.Attributes).FirstOrDefault(
                     a =>
                     a.Name.PlainName.Split('.').Last() == attributeType.Name ||
                     attributeType.Name.EndsWith("Attribute") &&
                     a.Name.PlainName.Split('.').Last() ==
                     attributeType.Name.Substring(0, attributeType.Name.Length - "Attribute".Length)));
        }
Esempio n. 18
0
        private static bool HasMemberJsonIgnoreAttribute(PropertyDeclarationSyntax propertySyntax)
        {
            SyntaxList <AttributeListSyntax> propertyAttributes = propertySyntax.AttributeLists;

            if (propertyAttributes.Any())
            {
                var attributes = propertyAttributes.SelectMany(x => x.Attributes);

                return(attributes.Any(a => a.Name.ToString() == "JsonIgnore"));
            }

            return(false);
        }
        private bool HasAttributeWithInitGuarantee(SyntaxList <AttributeListSyntax> attributes)
        {
            if (attributes.Count == 0)
            {
                return(false);
            }

            foreach (var attribute in attributes.SelectMany(x => x.Attributes))
            {
                var attributeName = attribute.Name.ToFullString().Replace("Attribute", "");
                if (attributeName.EndsWith("InitOnly") || attributeName.EndsWith("InitRequired"))
                {
                    return(true);
                }
            }

            return(false);
        }
        private static IEnumerable <RuleName> GetRuleNamesToIgnore(SyntaxList <AttributeListSyntax> attributeLists)
        {
            string[] ignoreNames = new []
            {
                "SuppressMessage",
                "CodeAnalysis.SuppressMessage",
                "Diagnostics.CodeAnalysis.SuppressMessage",
                "System.Diagnostics.CodeAnalysis.SuppressMessage"
            };

            return(attributeLists
                   .SelectMany(x => x.Attributes)
                   .Where(a => ignoreNames.Contains(a.Name.ToString()) && a.ArgumentList.Arguments.Count == 2)
                   .Where(a => a.ArgumentList.Arguments[0].Expression.Kind == SyntaxKind.StringLiteralExpression && a.ArgumentList.Arguments[1].Expression.Kind == SyntaxKind.StringLiteralExpression)
                   .Select(a => new RuleName(
                               ((LiteralExpressionSyntax)a.ArgumentList.Arguments[0].Expression).ToFullString().Replace("\"", ""),
                               ((LiteralExpressionSyntax)a.ArgumentList.Arguments[1].Expression).ToFullString().Replace("\"", "")
                               )));
        }
Esempio n. 21
0
        public static IImmutableList <AttributeUsageInfo> GetAttributeInfo(
            this SemanticModel model,
            SyntaxList <AttributeListSyntax> attributeLists)
        {
            AttributeParameterInfo GetArgumentParameter(AttributeArgumentSyntax arg, int idx)
            {
                string name = idx.ToString();

                if (arg.NameColon != null)
                {
                    name = arg.NameColon.Name.Identifier.Text;
                }
                else if (arg.NameEquals != null)
                {
                    name = arg.NameEquals.Name.Identifier.Text;
                }

                return(new AttributeParameterInfo(name, arg.Expression));
            }

            IImmutableList <AttributeParameterInfo> GetParameters(AttributeSyntax attribute)
            {
                if (attribute.ArgumentList == null)
                {
                    return(ImmutableList.Create <AttributeParameterInfo>());
                }

                return(attribute.ArgumentList.Arguments.Select(GetArgumentParameter).ToImmutableList());
            }

            AttributeUsageInfo GetSingleAttributeInfo(AttributeSyntax attribute)
            {
                ISymbol          method = model.GetSymbolInfo(attribute).Symbol;
                INamedTypeSymbol type   = method.ContainingType;

                return(new AttributeUsageInfo(type, GetParameters(attribute)));
            }

            return(attributeLists.SelectMany(l => l.Attributes).Select(GetSingleAttributeInfo).ToImmutableList());
        }
        public static bool TryGetAttribute(this SyntaxList <AttributeListSyntax> attributeLists, KnownType attributeKnownType,
                                           SemanticModel semanticModel, out AttributeSyntax searchedAttribute)
        {
            searchedAttribute = null;

            if (!attributeLists.Any())
            {
                return(false);
            }

            foreach (var attribute in attributeLists.SelectMany(attributeList => attributeList.Attributes))
            {
                var attributeType = semanticModel.GetTypeInfo(attribute).Type;

                if (attributeType.Is(attributeKnownType))
                {
                    searchedAttribute = attribute;
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 23
0
        /// <summary>
        /// Parses an attribute
        /// </summary>
        /// <param name="attributeLists">The list of attributes to parse</param>
        /// <param name="separator">The separator to use between attributes</param>
        /// <returns>A tuple where the first value is the Swift attributes and the second contains the value of an ExportAttribute</returns>
        private static Tuple <string, string> ParseAttributes(SyntaxList <AttributeListSyntax> attributeLists, string separator = " ")
        {
            if (!attributeLists.Any())
            {
                return(new Tuple <string, string>("", null));
            }

            var    output   = "";
            string exportAs = null;

            foreach (var attribute in attributeLists.SelectMany(attrList => attrList.Attributes))
            {
                if (IsSharpSwiftAttribute(attribute, "ExportAttribute"))
                {
                    exportAs = SyntaxNode(attribute.ArgumentList.Arguments[0].Expression).Trim().Trim('"');
                    continue;
                }

                output += SyntaxNode(attribute) + separator;
            }

            return(new Tuple <string, string>(output, exportAs));
        }
        private ParamsAttribute GetParamsAttribute(string fieldOrPropertyName, SyntaxList <AttributeListSyntax> attributeLists)
        {
            foreach (var attribute in attributeLists.SelectMany(attributeList =>
                                                                attributeList.Attributes.Where(attribute => attribute.Name.ToString() == paramsAttribute)))
            {
                var arguments = (from argument in attribute.ArgumentList.Arguments
                                 select argument.Expression as LiteralExpressionSyntax
                                 into expression
                                 where expression != null && expression.Token.Value is int
                                 select(int) expression.Token.Value).ToArray();

                // [Params(..)] with zero arguments is not allowed, there must be at least one!!
                if (arguments.Length < 1)
                {
                    var msg = String.Format("The [{0}] attribute must be used with at least 1 value, i.e. \"[{0}(1)]\"", paramsAttribute);
                    throw new InvalidOperationException(msg);
                }

                return(new ParamsAttribute(arguments));
            }

            return(null);
        }
Esempio n. 25
0
 public static IEnumerable <AttributeSyntax> GetExpectedExceptionAttributes(SyntaxList <AttributeListSyntax> attrs)
 {
     return(attrs
            .SelectMany(x => x.Attributes)
            .Where(x => x.Name.ToString() == "ExpectedException" || x.Name.ToString() == "ExpectedExceptionAttribute"));
 }
 public static IEnumerable <AttributeSyntax> GetAttributes(this SyntaxList <AttributeListSyntax> attributeLists,
                                                           ImmutableArray <KnownType> attributeKnownTypes, SemanticModel semanticModel) =>
 attributeLists.SelectMany(list => list.Attributes)
 .Where(a => semanticModel.GetTypeInfo(a).Type.IsAny(attributeKnownTypes));
 private static bool HasFieldAttribute(SyntaxList <AttributeListSyntax> attributeLists)
 {
     return(attributeLists.SelectMany(list => list.Attributes).Any(attribute => Vocabulary.AttributeNamesEqual(attribute.Name.ToString(), Vocabulary.FieldAttribute)));
 }
Esempio n. 28
0
 public static bool ContainsAttribute(this SyntaxList <AttributeListSyntax> attributeLists, string attributeName)
 {
     return(attributeLists.SelectMany(attr => (attr?.Attributes).GetValueOrDefault())
            .Select(GetAttributeName)
            .Any(name => string.Equals(attributeName, name, StringComparison.Ordinal)));
 }
 public static bool HasAnyAttribute(this SyntaxList <AttributeListSyntax> attributeLists, string[] attributeNames) =>
 attributeLists.SelectMany(a => a.Attributes).Select(a => a.Name.ToString()).Any(name => attributeNames.Any(attributeName =>
                                                                                                            name.EndsWith(attributeName, StringComparison.OrdinalIgnoreCase) ||
                                                                                                            name.EndsWith($"{attributeName}Attribute", StringComparison.OrdinalIgnoreCase)));
 public static bool HasAttribute(this SyntaxList <AttributeListSyntax> attributeLists, string attributeName) =>
 attributeLists.SelectMany(a => a.Attributes).Any(a => a.Name.ToString().EndsWith(attributeName, StringComparison.OrdinalIgnoreCase));