Exemple #1
0
        public static Task <Document> RefactorAsync(
            Document document,
            IfStatementSyntax ifStatement,
            CancellationToken cancellationToken)
        {
            var block = (BlockSyntax)ifStatement.Parent;

            SyntaxNode node = block;

            while (!node.IsParentKind(SyntaxKind.MethodDeclaration, SyntaxKind.LocalFunctionStatement))
            {
                node = node.Parent;
            }

            bool containsYield = node
                                 .DescendantNodes(f => !f.IsNestedMethod())
                                 .Any(f => f.IsKind(SyntaxKind.YieldBreakStatement, SyntaxKind.YieldReturnStatement));

            StatementSyntax jumpStatement = (containsYield)
                ? (StatementSyntax)YieldBreakStatement()
                : ReturnStatement();

            var rewriter = new IfStatementRewriter(jumpStatement, visitLocalFunction: node.IsParentKind(SyntaxKind.LocalFunctionStatement));

            SyntaxNode newNode = rewriter.VisitBlock(block);

            return(document.ReplaceNodeAsync(block, newNode, cancellationToken));
        }
        public static Task <Document> RefactorAsync(
            Document document,
            IfStatementSyntax ifStatement,
            bool recursive,
            CancellationToken cancellationToken)
        {
            var block = (BlockSyntax)ifStatement.Parent;

            SyntaxNode outermostBlock = block;

            while (!outermostBlock.IsParentKind(
                       SyntaxKind.MethodDeclaration,
                       SyntaxKind.LocalFunctionStatement,
                       SyntaxKind.SimpleLambdaExpression,
                       SyntaxKind.ParenthesizedLambdaExpression,
                       SyntaxKind.AnonymousMethodExpression))
            {
                outermostBlock = outermostBlock.Parent;
            }

            StatementSyntax jumpStatement = null;

            if (outermostBlock.IsParentKind(SyntaxKind.MethodDeclaration, SyntaxKind.LocalFunctionStatement) &&
                outermostBlock
                .DescendantNodes(f => !f.IsNestedMethod())
                .Any(f => f.IsKind(SyntaxKind.YieldBreakStatement, SyntaxKind.YieldReturnStatement)))
            {
                jumpStatement = YieldBreakStatement();
            }
            else
            {
                jumpStatement = ReturnStatement();
            }

            var rewriter = new IfStatementRewriter(jumpStatement, recursive);

            SyntaxNode newNode = rewriter.VisitBlock(block);

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