Example #1
0
        public override SyntaxNode VisitParenthesizedExpression(ParenthesizedExpressionSyntax node)
        {
            var newExpression = (ExpressionSyntax)node.Expression.Accept(this);

            // Remove unnecessary parentheses around non-binary expressions
            if (!(newExpression is BinaryExpressionSyntax))
            {
                return(newExpression
                       .WithLeadingTrivia(node.GetLeadingTrivia())
                       .WithTrailingTrivia(node.GetTrailingTrivia()));
            }
            else
            {
                return(node.Expression != newExpression?
                       node.WithExpression(newExpression) : node);
            }
        }
Example #2
0
        public static Task <Document> RefactorAsync(
            Document document,
            ParenthesizedExpressionSyntax parenthesizedExpression,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ExpressionSyntax expression = parenthesizedExpression.Expression;

            IEnumerable <SyntaxTrivia> leading = parenthesizedExpression.GetLeadingTrivia()
                                                 .Concat(parenthesizedExpression.OpenParenToken.TrailingTrivia)
                                                 .Concat(expression.GetLeadingTrivia());

            IEnumerable <SyntaxTrivia> trailing = expression.GetTrailingTrivia()
                                                  .Concat(parenthesizedExpression.CloseParenToken.LeadingTrivia)
                                                  .Concat(parenthesizedExpression.GetTrailingTrivia());

            ExpressionSyntax newNode = expression.WithLeadingTrivia(leading).WithTrailingTrivia(trailing);

            return(document.ReplaceNodeAsync(parenthesizedExpression, newNode, cancellationToken));
        }
Example #3
0
        private static async Task <Document> RefactorAsync(
            Document document,
            ParenthesizedExpressionSyntax parenthesizedExpression,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            ExpressionSyntax expression = parenthesizedExpression.Expression;

            IEnumerable <SyntaxTrivia> leading = parenthesizedExpression.GetLeadingTrivia()
                                                 .Concat(parenthesizedExpression.OpenParenToken.TrailingTrivia)
                                                 .Concat(expression.GetLeadingTrivia());

            IEnumerable <SyntaxTrivia> trailing = expression.GetTrailingTrivia()
                                                  .Concat(parenthesizedExpression.CloseParenToken.LeadingTrivia)
                                                  .Concat(parenthesizedExpression.GetTrailingTrivia());

            SyntaxNode newRoot = root.ReplaceNode(
                parenthesizedExpression,
                expression.WithTrivia(leading, trailing));

            return(document.WithSyntaxRoot(newRoot));
        }
        public override SyntaxNode VisitParenthesizedExpression(ParenthesizedExpressionSyntax node)
        {
            var newExpression = (ExpressionSyntax)node.Expression.Accept(this);

            // Remove unnecessary parentheses around non-binary expressions
            if (!(newExpression is BinaryExpressionSyntax))
            {
                return newExpression
                    .WithLeadingTrivia(node.GetLeadingTrivia())
                    .WithTrailingTrivia(node.GetTrailingTrivia());
            }
            else
            {
                return node.Expression != newExpression ?
                    node.WithExpression(newExpression) : node;
            }
        }
        public static Task <Document> RemoveParenthesesAsync(
            Document document,
            ParenthesizedExpressionSyntax parenthesizedExpression,
            CancellationToken cancellationToken = default)
        {
            ExpressionSyntax expression = parenthesizedExpression.Expression;

            SyntaxTriviaList leading = parenthesizedExpression.GetLeadingTrivia()
                                       .Concat(parenthesizedExpression.OpenParenToken.TrailingTrivia)
                                       .Concat(expression.GetLeadingTrivia())
                                       .ToSyntaxTriviaList();

            SyntaxTriviaList trailing = expression.GetTrailingTrivia()
                                        .Concat(parenthesizedExpression.CloseParenToken.LeadingTrivia)
                                        .Concat(parenthesizedExpression.GetTrailingTrivia())
                                        .ToSyntaxTriviaList();

            ExpressionSyntax newExpression = expression
                                             .WithLeadingTrivia(leading)
                                             .WithTrailingTrivia(trailing)
                                             .WithFormatterAnnotation();

            if (!leading.Any())
            {
                SyntaxNode parent = parenthesizedExpression.Parent;

                switch (parent.Kind())
                {
                case SyntaxKind.ReturnStatement:
                {
                    var returnStatement = (ReturnStatementSyntax)parent;

                    SyntaxToken returnKeyword = returnStatement.ReturnKeyword;

                    if (!returnKeyword.TrailingTrivia.Any())
                    {
                        ReturnStatementSyntax newNode = returnStatement.Update(returnKeyword.WithTrailingTrivia(Space), newExpression, returnStatement.SemicolonToken);

                        return(document.ReplaceNodeAsync(returnStatement, newNode, cancellationToken));
                    }

                    break;
                }

                case SyntaxKind.YieldReturnStatement:
                {
                    var yieldReturn = (YieldStatementSyntax)parent;

                    SyntaxToken returnKeyword = yieldReturn.ReturnOrBreakKeyword;

                    if (!returnKeyword.TrailingTrivia.Any())
                    {
                        YieldStatementSyntax newNode = yieldReturn.Update(yieldReturn.YieldKeyword, returnKeyword.WithTrailingTrivia(Space), newExpression, yieldReturn.SemicolonToken);

                        return(document.ReplaceNodeAsync(yieldReturn, newNode, cancellationToken));
                    }

                    break;
                }

                case SyntaxKind.AwaitExpression:
                {
                    var awaitExpression = (AwaitExpressionSyntax)parent;

                    SyntaxToken awaitKeyword = awaitExpression.AwaitKeyword;

                    if (!awaitKeyword.TrailingTrivia.Any())
                    {
                        AwaitExpressionSyntax newNode = awaitExpression.Update(awaitKeyword.WithTrailingTrivia(Space), newExpression);

                        return(document.ReplaceNodeAsync(awaitExpression, newNode, cancellationToken));
                    }

                    break;
                }
                }
            }

            return(document.ReplaceNodeAsync(parenthesizedExpression, newExpression, cancellationToken));
        }