예제 #1
0
            public override SyntaxNode VisitAttributeList(AttributeListSyntax node)
            {
                var ordered = node.Attributes.OrderBy(x => x.Name.GetText().ToString());
                var result  = node.WithAttributes(SyntaxFactory.SeparatedList(ordered));

                return(base.VisitAttributeList(result));
            }
 private static AttributeListSyntax RemoveFromList(AttributeListSyntax list, AttributeSyntax attributeSyntax)
 {
     return(list.WithAttributes(
                SeparatedList(
                    list.Attributes
                    .Where(attr => attr.Name.ToString() != attributeSyntax.Name.ToString())
                    )));
 }
    // remove useless attributes
    public override SyntaxNode VisitAttributeList(AttributeListSyntax node)
    {
        var atts = new SeparatedSyntaxList <AttributeSyntax>().AddRange(FilterAttributes(node.Attributes));

        if (atts.Count == 0)     // nothing left, remove this node
        {
            return(null);
        }
        if (atts.Count == node.Attributes.Count)     // no change, don't change the tree
        {
            return(base.VisitAttributeList(node));
        }
        return(node.WithAttributes(atts));    // rewrite
    }
            public override SyntaxNode VisitAttributeList(AttributeListSyntax attributeListNode)
            {
                _cancellationToken.ThrowIfCancellationRequested();

                if (_visitedAttributeListCounter < MaxVisitedAttributesCount)
                {
                    _visitedAttributeListCounter++;
                }

                var attributesToCheck  = attributeListNode.Attributes;
                var modifiedAttributes = new List <AttributeSyntax>(attributesToCheck.Count);

                foreach (AttributeSyntax attribute in attributesToCheck)
                {
                    _cancellationToken.ThrowIfCancellationRequested();

                    if (!IsAttributeToRemove(attribute))
                    {
                        modifiedAttributes.Add(attribute);
                    }
                }

                bool allPreviousAttributeListWereRemoved = _attributeListsRemovedCounter > 0 &&
                                                           _attributeListsRemovedCounter == (_visitedAttributeListCounter - 1);

                if (modifiedAttributes.Count == attributesToCheck.Count && !allPreviousAttributeListWereRemoved)
                {
                    return(attributeListNode);
                }
                else if (modifiedAttributes.Count == 0)
                {
                    if (_attributeListsRemovedCounter < MaxRemovedAttributesCount)
                    {
                        _attributeListsRemovedCounter++;
                    }

                    return(null);
                }

                AttributeListSyntax modifiedAttributeListNode = attributeListNode.WithAttributes(SyntaxFactory.SeparatedList(modifiedAttributes));

                if (allPreviousAttributeListWereRemoved)
                {
                    var trivia = modifiedAttributeListNode.GetLeadingTrivia().Prepend(SyntaxFactory.CarriageReturnLineFeed);
                    modifiedAttributeListNode = modifiedAttributeListNode.WithLeadingTrivia(trivia);
                }

                return(modifiedAttributeListNode);
            }
예제 #5
0
            /// <inheritdoc />
            public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
            {
                CancellationToken token  = cancellationToken;
                INamedTypeSymbol  symbol = semanticModel.GetDeclaredSymbol(node, token);

                if (symbol == null)
                {
                    return(node);
                }

                int totalAttributes = 0;
                ImmutableArray <AttributeData> attrs = symbol.GetAttributes();

                if (attrs.IsDefaultOrEmpty)
                {
                    return(node);
                }

                SyntaxList <AttributeListSyntax> attributeLists = node.AttributeLists;

                for (int i = 0; i < attributeLists.Count; i++)
                {
                    AttributeListSyntax attributeList = attributeLists[i];
                    SeparatedSyntaxList <AttributeSyntax> attributes = attributeList.Attributes;

                    for (int j = 0; j < attributes.Count; j++, totalAttributes++)
                    {
                        AttributeSyntax attr     = attributes[j];
                        string          attrName = attr.Name.ToString();

                        if (attrName == "Component" || attrName == nameof(ComponentAttribute))
                        {
                            // There is a 'Component' attribute: Specify its content.
                            string contentStr = node.ToString();

                            // TODO: Use b64 and serialization
                            // It works, except Roslyn <= 2.0.0 has a bug with serialization
                            //using (MemoryStream ms = new MemoryStream())
                            //{
                            //    node.SerializeTo(ms, cancellationToken);

                            //    contentStr = ms.TryGetBuffer(out ArraySegment<byte> buffer)
                            //        ? Convert.ToBase64String(buffer.Array, buffer.Offset, buffer.Count)
                            //        : Convert.ToBase64String(ms.ToArray());
                            //}

                            AttributeArgumentSyntax contentArg = SyntaxFactory.AttributeArgument(
                                SyntaxFactory.LiteralExpression(
                                    SyntaxKind.StringLiteralExpression,
                                    SyntaxFactory.Literal(contentStr)
                                    )
                                );

                            node = node.WithAttributeLists(
                                attributeLists.Replace(
                                    attributeList,
                                    attributeList.WithAttributes(
                                        attributes.Replace(attr, attr.WithArgumentList(
                                                               SyntaxFactory.AttributeArgumentList().AddArguments(contentArg)
                                                               ))
                                        )
                                    )
                                );
                        }

                        // Maybe a component?
                        AttributeData attrData = attrs[totalAttributes];

                        if (attrData.AttributeClass.MetadataName == nameof(CopyFromAttribute))
                        {
                            // CopyFrom component: copy members
                            node = CopyMembers(node, attrData, token);
                        }
                        else if (InheritsCompositionAttribute(attrData.AttributeClass))
                        {
                            // Component: apply it
                            CompositionAttribute builtAttr = attrData.Construct <CompositionAttribute>();

                            try
                            {
                                node = builtAttr.Component.Apply(node, symbol, token);

                                if (node == null)
                                {
                                    throw new NullReferenceException("A component cannot return null.");
                                }
                            }
                            catch (Exception e)
                            {
                                throw new DiagnosticException($"Error applying the {builtAttr.Component} component.", e, attr.GetLocation());
                            }
                        }
                    }
                }

                return(node);
            }