public override object TrackedVisitBlockStatement(BlockStatement blockStatement, object data)
 {
     BlockStatement replaced = blockStatement;
     InsertionBlockData insertionBlockData = new InsertionBlockData();
     base.TrackedVisitBlockStatement(blockStatement, insertionBlockData);
     if (insertionBlockData.Block != null && insertionBlockData.Statements.Count > 0)
     {
         if (blockStatement.GetHashCode() == insertionBlockData.Block.GetHashCode())
         {
             IList<INode> nodes = new List<INode>();
             foreach (Statement node in insertionBlockData.Statements)
                 nodes.Add(node);
             replaced.Children.InsertRange(insertionBlockData.BlockChildIndex, nodes);
             ReplaceCurrentNode(replaced);
         }
     }
     return null;
 }
Esempio n. 2
0
        public override object TrackedVisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data)
        {
            string            variableName       = GetVariableName(arrayCreateExpression);
            List <Expression> initializerList    = arrayCreateExpression.ArrayInitializer.CreateExpressions;
            Expression        replacedExpression = arrayCreateExpression;

            if (initializerList.Count > 0 && initializerList[0] is CollectionInitializerExpression && data is InsertionBlockData)
            {
                ArrayCreateExpression replacedArrayCreateExpression = arrayCreateExpression;
                replacedArrayCreateExpression.ArrayInitializer = null;
                replacedArrayCreateExpression.Arguments.Add(new PrimitiveExpression(initializerList.Count, initializerList.Count.ToString()));

                string   arrayTypeName = arrayCreateExpression.CreateType.Type + "s";
                Position position      = Position.After;
                if (variableName == null)
                {
                    variableName = arrayTypeName;
                    position     = Position.Before;
                }

                List <Statement>   initStatements     = GetArrayInitStatements(replacedArrayCreateExpression, variableName, initializerList);
                InsertionBlockData insertionBlockData = (InsertionBlockData)data;
                insertionBlockData.Block           = (BlockStatement)AstUtil.GetParentOfType(replacedArrayCreateExpression, typeof(BlockStatement));
                insertionBlockData.BlockChildIndex = GetBlockChildIndex(replacedArrayCreateExpression, position);
                insertionBlockData.Statements      = initStatements;

                if (variableName == arrayTypeName)
                {
                    IdentifierExpression identifierExpression = new IdentifierExpression(variableName);
                    replacedExpression = identifierExpression;

                    VariableDeclaration      variableDeclaration = new VariableDeclaration(variableName, arrayCreateExpression);
                    LocalVariableDeclaration localVariable       = new LocalVariableDeclaration(variableDeclaration);
                    localVariable.TypeReference = arrayCreateExpression.CreateType;

                    initStatements.Insert(0, localVariable);
                }

                ReplaceCurrentNode(replacedExpression);
            }

            return(base.TrackedVisitArrayCreateExpression(arrayCreateExpression, data));
        }
Esempio n. 3
0
        public override object TrackedVisitBlockStatement(BlockStatement blockStatement, object data)
        {
            BlockStatement     replaced           = blockStatement;
            InsertionBlockData insertionBlockData = new InsertionBlockData();

            base.TrackedVisitBlockStatement(blockStatement, insertionBlockData);
            if (insertionBlockData.Block != null && insertionBlockData.Statements.Count > 0)
            {
                if (blockStatement.GetHashCode() == insertionBlockData.Block.GetHashCode())
                {
                    IList <INode> nodes = new List <INode>();
                    foreach (Statement node in insertionBlockData.Statements)
                    {
                        nodes.Add(node);
                    }
                    replaced.Children.InsertRange(insertionBlockData.BlockChildIndex, nodes);
                    ReplaceCurrentNode(replaced);
                }
            }
            return(null);
        }