Esempio n. 1
0
 private IEnumerable <SyntaxNode> VisitDoLoopBlock(DoLoopBlockSyntax node)
 {
     if (!node.Statements.Any() && NoCommentsBefore(node.LoopStatement))
     {
         yield return(node.DoStatement);
     }
 }
 public override void VisitDoLoopBlock(DoLoopBlockSyntax node) =>
 counter.CheckNesting(node.DoStatement.DoKeyword, () => base.VisitDoLoopBlock(node));
Esempio n. 3
0
 public override void VisitDoLoopBlock(DoLoopBlockSyntax node)
 {
     State.IncreaseComplexityByNestingPlusOne(node.DoStatement.DoKeyword);
     State.VisitWithNesting(node, base.VisitDoLoopBlock);
 }
Esempio n. 4
0
 public override void VisitDoLoopBlock(DoLoopBlockSyntax node)
 {
     LogicalLineCount++;
     base.VisitDoLoopBlock(node);
 }
Esempio n. 5
0
        public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            var document = context.Document;

            if (document.Project.Solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles)
            {
                return;
            }
            var span = context.Span;

            if (!span.IsEmpty)
            {
                return;
            }
            var cancellationToken = context.CancellationToken;

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }
            var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            if (model.IsFromGeneratedCode(cancellationToken))
            {
                return;
            }
            var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);

            var node = root.FindNode(span);

            if (node == null)
            {
                return;
            }

            DoLoopBlockSyntax parentBlockNode = null;

            if ((node is DoStatementSyntax) || (node is LoopStatementSyntax))
            {
                parentBlockNode = node.Parent as DoLoopBlockSyntax;
            }
            else
            {
                return;
            }

            string     description;
            SyntaxKind blockKindAfterConversion;
            SyntaxKind doKindAfterConversion;

            if (parentBlockNode.Kind() == SyntaxKind.DoLoopWhileBlock)
            {
                description = "To 'Do While ... Loop'";
                blockKindAfterConversion = SyntaxKind.DoWhileLoopBlock;
                doKindAfterConversion    = SyntaxKind.DoWhileStatement;
            }
            else if (parentBlockNode.Kind() == SyntaxKind.DoLoopUntilBlock)
            {
                description = "To 'Do Until ... Loop'";
                blockKindAfterConversion = SyntaxKind.DoUntilLoopBlock;
                doKindAfterConversion    = SyntaxKind.DoUntilStatement;
            }
            else
            {
                return;
            }

            context.RegisterRefactoring(
                CodeActionFactory.Create(
                    span,
                    DiagnosticSeverity.Info,
                    GettextCatalog.GetString(GettextCatalog.GetString(description)),
                    t2 =>
            {
                var newRoot = root.ReplaceNode(
                    (SyntaxNode)parentBlockNode,
                    SyntaxFactory.DoLoopBlock(blockKindAfterConversion,
                                              SyntaxFactory.DoStatement(doKindAfterConversion, parentBlockNode.LoopStatement.WhileOrUntilClause),
                                              parentBlockNode.Statements, SyntaxFactory.SimpleLoopStatement().WithTrailingTrivia(parentBlockNode.LoopStatement.GetTrailingTrivia()))
                    .WithLeadingTrivia(parentBlockNode.GetLeadingTrivia())
                    .WithAdditionalAnnotations(Formatter.Annotation));
                return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
            }
                    )
                );
        }
Esempio n. 6
0
        private DoWhileLoop TraverseDoWhileLoop(DoLoopBlockSyntax dbs, ref int returnCnt, bool nested = false)
        {
            DoWhileLoop doWhileLoop = new DoWhileLoop();
            DoStatementSyntax dss = dbs.DoStatement;
            doWhileLoop.IsNested = nested;
            foreach (SyntaxNode sn in dss.DescendantNodesAndSelf())
            {
                if (sn is BinaryExpressionSyntax)
                {
                    doWhileLoop.ConditionCount++;
                }
                else if (sn is IdentifierNameSyntax)
                {
                    Variables variable = new Variables();
                    variable.IsReferenced = true;

                    variable.Name = (sn as IdentifierNameSyntax).Identifier.ValueText;
                    doWhileLoop.AccessedVars.Add(variable);
                }
            }

            foreach (StatementSyntax ss in dbs.Statements)
            {
                if (ss is AssignmentStatementSyntax)
                {
                    //TODO: need to look at more than just then name!
                    Method tempMethod = TraverseAccessVars(ss as AssignmentStatementSyntax);

                    doWhileLoop.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    doWhileLoop.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                }
                else if (ss is LocalDeclarationStatementSyntax)
                {
                    Method tempMethod = TraverseVarDecls(ss as LocalDeclarationStatementSyntax);
                    doWhileLoop.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    doWhileLoop.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                }
                else if (ss is SingleLineIfStatementSyntax)
                {
                    Decisions decision = TraverseIfStatement(ss as SingleLineIfStatementSyntax, ref returnCnt, true);
                    doWhileLoop.Nested.AddRange(decision.IfStatements);
                    doWhileLoop.Nested.AddRange(decision.ElseStatements);
                }
                else if (ss is MultiLineIfBlockSyntax)
                {
                    Decisions decisions = TraverseMultiIfStatement(ss as MultiLineIfBlockSyntax, ref returnCnt, true);
                    doWhileLoop.Nested.AddRange(decisions.IfStatements);
                    doWhileLoop.Nested.AddRange(decisions.ElseStatements);
                }
                else if (ss is ElseStatementSyntax)
                {
                    doWhileLoop.Nested.Add(TravsereElseStatement(ss as ElseStatementSyntax, ref returnCnt, true));
                }
                else if (ss is ForBlockSyntax)
                {
                    Decisions tempDecision = TraverseForStatement(ss as ForBlockSyntax, ref returnCnt, true);
                    doWhileLoop.Nested.AddRange(tempDecision.IfStatements);
                    doWhileLoop.Nested.AddRange(tempDecision.ElseStatements);
                    doWhileLoop.Nested.AddRange(tempDecision.ForEachStatements);
                    doWhileLoop.Nested.AddRange(tempDecision.ForStatements);
                    doWhileLoop.Nested.AddRange(tempDecision.WhileLoops);
                    doWhileLoop.Nested.AddRange(tempDecision.DoWhileLoops);
                    doWhileLoop.Nested.AddRange(tempDecision.Catches);
                    doWhileLoop.Nested.AddRange(tempDecision.SwitchStatements);
                }
                else if (ss is SelectBlockSyntax)
                {
                    doWhileLoop.Nested.Add(TraverseSwitchStatement(ss as SelectBlockSyntax, ref returnCnt, true));
                }
                else if (ss is DoLoopBlockSyntax)
                {
                    doWhileLoop.Nested.Add(TraverseDoWhileLoop(ss as DoLoopBlockSyntax, ref returnCnt, true));
                }
                else if (ss is WhileBlockSyntax)
                {
                    doWhileLoop.Nested.Add(TraverseWhileLoop(ss as WhileBlockSyntax, ref returnCnt, true));
                }
                else if (ss is CallStatementSyntax)
                {
                    doWhileLoop.InvokedMethods.Add(TraverseInvokedMethod(ss as CallStatementSyntax));
                }
                else if (ss is ReturnStatementSyntax)
                {
                    Method tempMethod = TraverseReturnStatement(ss as ReturnStatementSyntax);
                    doWhileLoop.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                    doWhileLoop.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    returnCnt++;
                }
            }

            return doWhileLoop;
        }