Exemplo n.º 1
0
        public static IndexerDeclarationSyntax ToStatementBody(IndexerDeclarationSyntax property)
        {
            var accessor = SyntaxFactory
                           .AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                           .WithBody(SyntaxFactory.Block(SyntaxFactory.ReturnStatement(property.ExpressionBody.Expression.WithLeadingTrivia(SyntaxFactory.Space))));

            var accessorDeclList = new SyntaxList <AccessorDeclarationSyntax>();

            accessorDeclList = accessorDeclList.Add(accessor);

            return(property
                   .WithAccessorList(SyntaxFactory.AccessorList(accessorDeclList))
                   .WithExpressionBody(null)
                   .WithSemicolonToken(SyntaxFactory.MissingToken(SyntaxKind.SemicolonToken))
                   .WithTrailingTrivia(property.GetTrailingTrivia()));
        }
Exemplo n.º 2
0
            public override SyntaxNode VisitIndexerDeclaration(IndexerDeclarationSyntax node)
            {
                if (generator.GetAttributes(node).Any(attr => generator.GetName(attr) == "CompilerGenerated"))
                {
                    return(base.VisitIndexerDeclaration(node));
                }

                var trivia = node.GetTrailingTrivia();

                // NOTE: Most of this code could be shared with VisitPropertyDeclaration but the mutating With*
                // and props like ExpressionBody aren't available in the shared base BasePropertyDeclarationSyntax type :(
                var canRead  = (node.ExpressionBody != null || node.AccessorList?.Accessors.Any(x => x.IsKind(SyntaxKind.GetAccessorDeclaration)) == true);
                var canWrite = node.AccessorList?.Accessors.Any(x => x.IsKind(SyntaxKind.SetAccessorDeclaration)) == true;

                if (node.ExpressionBody != null)
                {
                    node = node.RemoveNode(node.ExpressionBody, SyntaxRemoveOptions.KeepNoTrivia);
                }
                if (node.AccessorList?.Accessors.Any() == true)
                {
                    node = node.RemoveNodes(node.AccessorList.Accessors, SyntaxRemoveOptions.KeepNoTrivia);
                }

                if (canRead && !canWrite)
                {
                    node = node.WithExpressionBody(
                        ArrowExpressionClause(ExecutePipeline(node.Type, node.ParameterList.Parameters)));
                }
                else
                {
                    if (canRead)
                    {
                        node = node.AddAccessorListAccessors(AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                                                             .WithExpressionBody(ArrowExpressionClause(ExecutePipeline(node.Type, node.ParameterList.Parameters)))
                                                             .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
                    }
                    if (canWrite)
                    {
                        node = node.AddAccessorListAccessors(AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
                                                             .WithExpressionBody(ArrowExpressionClause(
                                                                                     ExecutePipeline(null, node.ParameterList.Parameters.Concat(new[] { Parameter(Identifier("value")).WithType(node.Type) }))))
                                                             .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
                    }
                }

                return(base.VisitIndexerDeclaration(node.WithTrailingTrivia(trivia)));
            }