public static IEnumerable <AttributeListSyntax> SplitAttributes(AttributeListSyntax attributeList)
        {
            if (attributeList == null)
            {
                throw new ArgumentNullException(nameof(attributeList));
            }

            SeparatedSyntaxList <AttributeSyntax> attributes = attributeList.Attributes;

            for (int i = 0; i < attributes.Count; i++)
            {
                AttributeListSyntax list = AttributeList(attributes[i]);

                if (i == 0)
                {
                    list = list.WithLeadingTrivia(attributeList.GetLeadingTrivia());
                }

                if (i == attributes.Count - 1)
                {
                    list = list.WithTrailingTrivia(attributeList.GetTrailingTrivia());
                }

                yield return(list.WithFormatterAnnotation());
            }
        }
        public static IEnumerable <AttributeListSyntax> SplitAttributes(AttributeListSyntax attributeList)
        {
            if (attributeList == null)
            {
                throw new ArgumentNullException(nameof(attributeList));
            }

            for (int i = 0; i < attributeList.Attributes.Count; i++)
            {
                AttributeListSyntax list = SyntaxFactory.AttributeList(
                    SyntaxFactory.SingletonSeparatedList(attributeList.Attributes[i]));

                if (i == 0)
                {
                    list = list.WithLeadingTrivia(attributeList.GetLeadingTrivia());
                }

                if (i == attributeList.Attributes.Count - 1)
                {
                    list = list.WithTrailingTrivia(attributeList.GetTrailingTrivia());
                }

                yield return(list);
            }
        }
예제 #3
0
            AttributeListSyntax FindInList(AttributeListSyntax originalAttributeList, bool add)
            {
                if (found)
                {
                    return(originalAttributeList);
                }

                List <AttributeSyntax> attributes = new List <AttributeSyntax>();

                foreach (var attribute in originalAttributeList.Attributes)
                {
                    if (found)
                    {
                        attributes.Add(attribute);
                        continue;
                    }

                    var p = semanticModel.GetSymbolInfo(attribute);
                    if (p.Symbol.ContainingType.Equals(mocklisSymbols.GeneratedCodeAttribute))
                    {
                        found = true;
                        attributes.Add(typesForSymbols.GeneratedCodeAttribute());
                    }
                    else if (add && p.Symbol.ContainingType.Equals(mocklisSymbols.MocklisClassAttribute))
                    {
                        found = true;
                        attributes.Add(attribute);
                        attributes.Add(typesForSymbols.GeneratedCodeAttribute());
                    }
                    else
                    {
                        attributes.Add(attribute);
                    }
                }

                if (found)
                {
                    var newAttributeList = F.AttributeList(F.SeparatedList(attributes));
                    if (originalAttributeList.HasLeadingTrivia)
                    {
                        newAttributeList = newAttributeList.WithLeadingTrivia(originalAttributeList.GetLeadingTrivia());
                    }

                    if (originalAttributeList.HasTrailingTrivia)
                    {
                        newAttributeList = newAttributeList.WithTrailingTrivia(originalAttributeList.GetTrailingTrivia());
                    }

                    return(newAttributeList);
                }

                return(found ? F.AttributeList(F.SeparatedList(attributes)) : originalAttributeList);
            }
            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);
            }
        private async Task <Document> AddNewLineToAttribute(Document document, AttributeListSyntax attribute, CancellationToken cancellationToken)
        {
            var leadingWhitespace = attribute.HasLeadingTrivia ?
                                    attribute.GetLeadingTrivia().Last()
                : SyntaxFactory.ElasticWhitespace("");

            var newAttribute = GetNodeWithTrailingEOL(attribute.WithoutTrailingTrivia());

            newAttribute = newAttribute
                           .WithTrailingTrivia(newAttribute
                                               .GetTrailingTrivia()
                                               .Add(leadingWhitespace));

            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            root = root.ReplaceNode(attribute, newAttribute);

            return(document.WithSyntaxRoot(root));
        }
예제 #6
0
        public static IEnumerable <AttributeListSyntax> SplitAttributeList(AttributeListSyntax attributeList)
        {
            SeparatedSyntaxList <AttributeSyntax> attributes = attributeList.Attributes;

            for (int i = 0; i < attributes.Count; i++)
            {
                AttributeListSyntax list = AttributeList(attributes[i]);

                if (i == 0)
                {
                    list = list.WithLeadingTrivia(attributeList.GetLeadingTrivia());
                }

                if (i == attributes.Count - 1)
                {
                    list = list.WithTrailingTrivia(attributeList.GetTrailingTrivia());
                }

                yield return(list);
            }
        }
예제 #7
0
 /// <summary>
 /// Return a "null attribute list" that only has trivia, no actual syntax.
 /// This is used when we want to remove an attribute list but keep nearby comments
 /// (e.g. the class documentation comments that can appear above an attribute).
 /// If we just used "null" we would lose those comments.
 /// </summary>
 private static AttributeListSyntax NullAttributeList(AttributeListSyntax node)
 {
     return(AttributeList(
                MissingToken(SyntaxKind.OpenBracketToken),
                null, new SeparatedSyntaxList <AttributeSyntax>(),
                MissingToken(SyntaxKind.CloseBracketToken)
                )
            // get rid of the indentation that would appear between the attribute list and the
            // doc comments, but we still want the indentation before the doc comments.
            .WithLeadingTrivia(
                node
                .GetLeadingTrivia()
                .Reverse()
                .SkipWhile(t => t.Kind() == SyntaxKind.WhitespaceTrivia)
                .Reverse()
                )
            .WithTrailingTrivia(
                node
                .GetTrailingTrivia()
                .Where(t => t.Kind() != SyntaxKind.EndOfLineTrivia)
                ));
 }
예제 #8
0
        private static List <AttributeListSyntax> GetNewAttributeList(AttributeListSyntax attributeList, SyntaxTrivia indentationTrivia)
        {
            var newAttributeLists = new List <AttributeListSyntax>();

            for (var i = 0; i < attributeList.Attributes.Count; i++)
            {
                var newAttributes = SyntaxFactory.SingletonSeparatedList(
                    attributeList.Attributes[i].WithLeadingTrivia(
                        attributeList.Attributes[i].GetLeadingTrivia().WithoutLeadingWhitespace()));
                var newAttributeList = SyntaxFactory.AttributeList(attributeList.Target, newAttributes);

                newAttributeList = (i == 0)
                    ? newAttributeList.WithLeadingTrivia(attributeList.GetLeadingTrivia())
                    : newAttributeList.WithLeadingTrivia(indentationTrivia);

                newAttributeList = (i == (attributeList.Attributes.Count - 1))
                    ? newAttributeList.WithTrailingTrivia(attributeList.GetTrailingTrivia())
                    : newAttributeList.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);

                newAttributeLists.Add(newAttributeList);
            }

            return(newAttributeLists);
        }
        private static List<AttributeListSyntax> GetNewAttributeList(AttributeListSyntax attributeList, SyntaxTrivia indentationTrivia)
        {
            var newAttributeLists = new List<AttributeListSyntax>();

            for (var i = 0; i < attributeList.Attributes.Count; i++)
            {
                var newAttributes = SyntaxFactory.SingletonSeparatedList(
                    attributeList.Attributes[i].WithLeadingTrivia(
                        attributeList.Attributes[i].GetLeadingTrivia().WithoutLeadingWhitespace()));
                var newAttributeList = SyntaxFactory.AttributeList(attributeList.Target, newAttributes);

                newAttributeList = (i == 0)
                    ? newAttributeList.WithLeadingTrivia(attributeList.GetLeadingTrivia())
                    : newAttributeList.WithLeadingTrivia(indentationTrivia);

                newAttributeList = (i == (attributeList.Attributes.Count - 1))
                    ? newAttributeList.WithTrailingTrivia(attributeList.GetTrailingTrivia())
                    : newAttributeList.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);

                newAttributeLists.Add(newAttributeList);
            }

            return newAttributeLists;
        }
        public MethodTransformerResult Transform(IMethodOrAccessorTransformationResult transformResult,
                                                 ITypeTransformationMetadata typeMetadata, INamespaceTransformationMetadata namespaceMetadata)
        {
            var methodResult = transformResult.AnalyzationResult;

            if (!methodResult.Missing || !methodResult.Conversion.HasFlag(MethodConversion.ToAsync) || methodResult.Symbol.IsObsolete())
            {
                return(MethodTransformerResult.Skip);
            }
            var methodNode = transformResult.Transformed;

            var baseMethod = methodResult.RelatedMethods
                             .Where(o =>
                                    (methodResult.BaseOverriddenMethod != null && o.Symbol.Equals(methodResult.BaseOverriddenMethod)) ||
                                    methodResult.ImplementedInterfaces.Any(i => o.Symbol.Equals(i)))
                             .FirstOrDefault(o => o.AsyncCounterpartSymbol?.IsObsolete() == true);

            if (baseMethod == null)
            {
                return(MethodTransformerResult.Skip);
            }

            namespaceMetadata.AddUsing("System");
            AttributeListSyntax obsoleteAttribute = null;
            var          syntaxReference          = baseMethod.AsyncCounterpartSymbol.DeclaringSyntaxReferences.SingleOrDefault();
            SyntaxTrivia?documentationTrivia      = null;

            if (syntaxReference != null)
            {
                var baseMethodNode = syntaxReference.GetSyntax() as MethodDeclarationSyntax;
                obsoleteAttribute   = baseMethodNode?.AttributeLists.FirstOrDefault(o => o.Attributes.Count == 1 && o.Attributes.First().Name.ToString() == "Obsolete");
                obsoleteAttribute   = (AttributeListSyntax)_directiveRemover.VisitAttributeList(obsoleteAttribute);
                documentationTrivia = obsoleteAttribute.GetLeadingTrivia()
                                      .Select(o => o.IsKind(SyntaxKind.SingleLineDocumentationCommentTrivia) ? o : (SyntaxTrivia?)null)
                                      .FirstOrDefault(o => o.HasValue);
            }

            if (obsoleteAttribute == null)
            {
                obsoleteAttribute = AttributeList(SingletonSeparatedList(Attribute(IdentifierName("Obsolete"))))
                                    .WithOpenBracketToken(Token(TriviaList(transformResult.LeadingWhitespaceTrivia), SyntaxKind.OpenBracketToken, TriviaList()))
                                    .WithCloseBracketToken(Token(TriviaList(), SyntaxKind.CloseBracketToken, TriviaList(transformResult.EndOfLineTrivia)));
            }

            var inheritDocTrivia = Trivia(GetInheritdoc(transformResult.EndOfLineTrivia.ToFullString()));

            if (documentationTrivia.HasValue)
            {
                obsoleteAttribute = obsoleteAttribute.WithLeadingTrivia(obsoleteAttribute.GetLeadingTrivia()
                                                                        .Replace(documentationTrivia.Value, inheritDocTrivia));
            }
            else
            {
                // Append <inheritdoc />
                var leadingTrivia = obsoleteAttribute.GetLeadingTrivia();
                var trivias       = new List <SyntaxTrivia>();
                if (leadingTrivia.Count == 0 || !leadingTrivia.Last().IsKind(SyntaxKind.WhitespaceTrivia))
                {
                    trivias.Add(transformResult.LeadingWhitespaceTrivia);
                }

                trivias.Add(inheritDocTrivia);
                trivias.Add(transformResult.LeadingWhitespaceTrivia);
                obsoleteAttribute = obsoleteAttribute.WithLeadingTrivia(leadingTrivia.AddRange(trivias));
            }

            methodNode = methodNode
                         .WithLeadingTrivia(TriviaList(transformResult.LeadingWhitespaceTrivia))
                         .WithAttributeLists(methodNode.AttributeLists.Add(obsoleteAttribute));

            return(MethodTransformerResult.Update(methodNode));
        }