private static SyntaxTokenList AddAsyncModifierWithCorrectedTrivia(SyntaxTokenList modifiers, ref TypeSyntax newReturnType)
        {
            if (modifiers.Any())
            {
                return(modifiers.Add(s_asyncToken));
            }

            // Move the leading trivia from the return type to the new modifiers list.
            SyntaxTokenList result = SyntaxFactory.TokenList(s_asyncToken.WithLeadingTrivia(newReturnType.GetLeadingTrivia()));

            newReturnType = newReturnType.WithoutLeadingTrivia();
            return(result);
        }
Example #2
0
 private static TypeSyntax WrapInEnumerable(TypeSyntax type)
 {
     // very rough heuristic, if we have something like object[] make it IEnumerable<object[]>
     // as required by xunit. But if the data type is already enumerable, like object[][]
     // or IList<IList<object>> don't touch it
     if (type.ToString() == "IEnumerable<object[]>" ||
         type.ToString().EndsWith("[][]") ||
         type.ToString().Count(c => c == '<') >= 2)
     {
         return(type.WithoutLeadingTrivia());
     }
     return(ParseTypeName($"IEnumerable<{type}>").WithTrailingTrivia(Space));
 }
Example #3
0
        public static PropertyDeclarationSyntax InsertModifier(PropertyDeclarationSyntax propertyDeclaration, SyntaxToken modifier, IModifierComparer comparer)
        {
            if (propertyDeclaration == null)
            {
                throw new ArgumentNullException(nameof(propertyDeclaration));
            }

            SyntaxTokenList modifiers = propertyDeclaration.Modifiers;

            if (!modifiers.Any())
            {
                TypeSyntax type = propertyDeclaration.Type;

                return(propertyDeclaration
                       .WithType(type.WithoutLeadingTrivia())
                       .WithModifiers(TokenList(modifier.WithLeadingTrivia(type.GetLeadingTrivia()))));
            }

            return(propertyDeclaration.WithModifiers(modifiers.InsertModifier(modifier, comparer)));
        }
Example #4
0
        public static OperatorDeclarationSyntax InsertModifier(OperatorDeclarationSyntax operatorDeclaration, SyntaxToken modifier, IModifierComparer comparer)
        {
            if (operatorDeclaration == null)
            {
                throw new ArgumentNullException(nameof(operatorDeclaration));
            }

            SyntaxTokenList modifiers = operatorDeclaration.Modifiers;

            if (!modifiers.Any())
            {
                TypeSyntax returnType = operatorDeclaration.ReturnType;

                return(operatorDeclaration
                       .WithReturnType(returnType.WithoutLeadingTrivia())
                       .WithModifiers(TokenList(modifier.WithLeadingTrivia(returnType.GetLeadingTrivia()))));
            }

            return(operatorDeclaration.WithModifiers(modifiers.InsertModifier(modifier, comparer)));
        }
Example #5
0
        public static FieldDeclarationSyntax InsertModifier(FieldDeclarationSyntax fieldDeclaration, SyntaxToken modifier, IModifierComparer comparer)
        {
            if (fieldDeclaration == null)
            {
                throw new ArgumentNullException(nameof(fieldDeclaration));
            }

            SyntaxTokenList modifiers = fieldDeclaration.Modifiers;

            if (!modifiers.Any())
            {
                TypeSyntax type = fieldDeclaration.Declaration?.Type;

                return(fieldDeclaration
                       .ReplaceNode(type, type.WithoutLeadingTrivia())
                       .WithModifiers(TokenList(modifier.WithLeadingTrivia(type.GetLeadingTrivia()))));
            }

            return(fieldDeclaration.WithModifiers(modifiers.InsertModifier(modifier, comparer)));
        }
Example #6
0
        internal override Task <Document> GetUpdatedDocumentAsync(
            Document document, SemanticModel model, SyntaxNode root,
            SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
        {
            ArrayTypeSyntax arrayType = GetArrayType(nodeToFix);

            if (arrayType != null)
            {
                TypeSyntax elementType = arrayType.ElementType.WithoutLeadingTrivia().WithoutTrailingTrivia();
                if (arrayType.RankSpecifiers.Count > 1)
                {
                    elementType = SyntaxFactory.ArrayType(elementType, SyntaxFactory.List(arrayType.RankSpecifiers.Skip(1)));
                }

                InvocationExpressionSyntax syntax = InvokeStaticGenericParameterlessMethod(
                    model.Compilation.GetTypeByMetadataName("System.Array"),
                    EmptyArrayDiagnosticAnalyzer.ArrayEmptyMethodName,
                    elementType.WithoutLeadingTrivia().WithoutTrailingTrivia());

                if (nodeToFix.HasLeadingTrivia)
                {
                    syntax = syntax.WithLeadingTrivia(nodeToFix.GetLeadingTrivia());
                }

                if (nodeToFix.HasTrailingTrivia)
                {
                    syntax = syntax.WithTrailingTrivia(nodeToFix.GetTrailingTrivia());
                }

                if (syntax != null)
                {
                    root     = root.ReplaceNode(nodeToFix, syntax);
                    document = document.WithSyntaxRoot(root);
                }
            }

            return(Task.FromResult(document));
        }