public static async Task <Document> RefactorAsync(
            Document document,
            ElseClauseSyntax elseClause,
            CancellationToken cancellationToken)
        {
            if (elseClause.IsParentKind(SyntaxKind.IfStatement))
            {
                var             ifStatement = (IfStatementSyntax)elseClause.Parent;
                StatementSyntax statement   = ifStatement.Statement;

                if (statement?.GetTrailingTrivia().IsEmptyOrWhitespace() == true)
                {
                    IfStatementSyntax newIfStatement = ifStatement
                                                       .WithStatement(statement.WithTrailingTrivia(elseClause.GetTrailingTrivia()))
                                                       .WithElse(null);

                    return(await document.ReplaceNodeAsync(ifStatement, newIfStatement, cancellationToken).ConfigureAwait(false));
                }
            }

            return(await document.RemoveNodeAsync(elseClause, SyntaxRemoveOptions.KeepExteriorTrivia, cancellationToken).ConfigureAwait(false));
        }
Exemple #2
0
        private static SyntaxNode GetNewRoot(SyntaxNode oldRoot, ElseClauseSyntax elseClause)
        {
            if (elseClause.Parent?.IsKind(SyntaxKind.IfStatement) == true)
            {
                var ifStatement = (IfStatementSyntax)elseClause.Parent;

                if (ifStatement.Statement?.GetTrailingTrivia().IsWhitespaceOrEndOfLine() == true)
                {
                    IfStatementSyntax newIfStatement = ifStatement
                                                       .WithStatement(ifStatement.Statement.WithTrailingTrivia(elseClause.GetTrailingTrivia()))
                                                       .WithElse(null);

                    return(oldRoot.ReplaceNode(ifStatement, newIfStatement));
                }
            }

            return(oldRoot.RemoveNode(elseClause, SyntaxRemoveOptions.KeepExteriorTrivia));
        }
        private Decisions TraverseElseClauses(ElseClauseSyntax ecs, ref int exitPoints, bool nested = false)
        {
            Decisions retDecision = new Decisions();
            ElseStatement elseStm = new ElseStatement();

            if (ecs.HasLeadingTrivia)
            {
                SetOuterComments(elseStm, ecs.GetLeadingTrivia().ToFullString());
            }

            if (ecs.HasTrailingTrivia)
            {
                SetInnerComments(elseStm, ecs.GetTrailingTrivia().ToFullString());
            }

            elseStm.IsNested = nested;
            var binaryExpressions = from aBinaryExpression in ecs.ChildNodes().OfType<BinaryExpressionSyntax>() select aBinaryExpression;
            foreach (BinaryExpressionSyntax bes in binaryExpressions)
            {
                Method tempMethod = TraverseBinaryExpression(bes);
                elseStm.AccessedVars.AddRange(tempMethod.AccessedVariables);
                elseStm.InvokedMethods.AddRange(tempMethod.InvokedMethods);
            }
            var catches = from aCatch in ecs.ChildNodes().OfType<CatchClauseSyntax>() select aCatch;
            foreach (CatchClauseSyntax ccs in catches)
            {
                Decisions tempCatch = TraverseCatchClauses(ccs, ref exitPoints, true);
                elseStm.Nested.AddRange(tempCatch.Catches);
            }
            var elses = from aElse in ecs.ChildNodes().OfType<ElseClauseSyntax>() select aElse;
            foreach (ElseClauseSyntax ecs2 in elses)
            {
                Decisions tempElse = TraverseElseClauses(ecs2, ref exitPoints, true);
                elseStm.Nested.AddRange(tempElse.ElseStatements);
            }
            #region nested stuff
            var statements = from aStatement in ecs.ChildNodes().OfType<StatementSyntax>() select aStatement;
            foreach (StatementSyntax ss in statements)
            {
                if (ss is DoStatementSyntax)
                {
                    Decisions dwl = TraverseDoStatements(ss as DoStatementSyntax, ref exitPoints, true);
                    elseStm.Nested.AddRange(dwl.DoWhileLoops);
                }
                else if (ss is ExpressionStatementSyntax)
                {
                    Method tempMethod = TraverseExpressionStatementSyntax(ss as ExpressionStatementSyntax);
                    elseStm.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    elseStm.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                }
                else if (ss is ForEachStatementSyntax)
                {
                    Decisions fes = TraverseForEachStatements(ss as ForEachStatementSyntax, ref exitPoints, true);
                    elseStm.Nested.AddRange(fes.ForEachStatements);
                }
                else if (ss is ForStatementSyntax)
                {
                    Decisions fs = TraverseForStatements(ss as ForStatementSyntax, ref exitPoints, true);
                    elseStm.Nested.AddRange(fs.ForStatements);
                }
                else if (ss is IfStatementSyntax)
                {
                    Decisions decision = TraverseIfStatements(ss as IfStatementSyntax, ref exitPoints, true);
                    elseStm.Nested.AddRange(decision.IfStatements);
                }
                else if (ss is LocalDeclarationStatementSyntax)
                {
                    Model.Type tempType = new Model.Type();
                    LocalDeclarationStatementSyntax ldss = ss as LocalDeclarationStatementSyntax;
                    if (ldss.Declaration != null)
                    {
                        VariableDeclarationSyntax vds = ldss.Declaration;
                        tempType.Name = vds.Type.ToString();
                        tempType.IsKnownType = true;
                        tempType.IsNotUserDefined = true;
                    }
                    Method tempMethod = TransverseAccessVars(ss as LocalDeclarationStatementSyntax);
                    //NOT SURE if this will work but here goes
                    tempMethod.AccessedVariables[0].Type = tempType;
                    elseStm.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    elseStm.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                }
                else if (ss is ReturnStatementSyntax)
                {
                    exitPoints++;
                }
                else if (ss is SwitchStatementSyntax)
                {
                    Decisions switchStm = TraverseSwitchStatements(ss as SwitchStatementSyntax, ref exitPoints, true);
                    elseStm.Nested.AddRange(switchStm.SwitchStatements);
                }
                else if (ss is WhileStatementSyntax)
                {
                    Decisions wl = TraverseWhileLoops(ss as WhileStatementSyntax, ref exitPoints, true);
                    elseStm.Nested.AddRange(wl.WhileLoops);
                }
            }
            #endregion
            retDecision.ElseStatements.Add(elseStm);
            return retDecision;
        }