Example #1
0
 private static void DeleteNoInits(Var node, int min, string name)
 {
     // walk backwards from the end of the list down to (and including) the minimum index
     for (int ndx = node.Count - 1; ndx >= min; --ndx)
     {
         // if the name matches and there is no initializer...
         if (string.CompareOrdinal(node[ndx].Identifier, name) == 0
             && node[ndx].Initializer == null)
         {
             // ...remove it from the list
             node.RemoveAt(ndx);
         }
     }
 }
Example #2
0
        public override void Visit(Var node)
        {
            if (node != null)
            {
                // first we want to weed out duplicates that don't have initializers
                // var a=1, a=2 is okay, but var a, a=2 and var a=2, a should both be just var a=2,
                // and var a, a should just be var a
                if (m_parser.Settings.IsModificationAllowed(TreeModifications.RemoveDuplicateVar))
                {
                    // first we want to weed out duplicates that don't have initializers
                    // var a=1, a=2 is okay, but var a, a=2 and var a=2, a should both be just var a=2,
                    // and var a, a should just be var a
                    int ndx = 0;
                    while (ndx < node.Count)
                    {
                        string thisName = node[ndx].Identifier;

                        // handle differently if we have an initializer or not
                        if (node[ndx].Initializer != null)
                        {
                            // the current vardecl has an initializer, so we want to delete any other
                            // vardecls of the same name in the rest of the list with no initializer
                            // and move on to the next item afterwards
                            DeleteNoInits(node, ++ndx, thisName);
                        }
                        else
                        {
                            // this vardecl has no initializer, so we can delete it if there is ANY
                            // other vardecl with the same name (whether or not it has an initializer)
                            if (VarDeclExists(node, ndx + 1, thisName))
                            {
                                node.RemoveAt(ndx);

                                // don't increment the index; we just deleted the current item,
                                // so the next item just slid into this position
                            }
                            else
                            {
                                // nope -- it's the only one. Move on to the next
                                ++ndx;
                            }
                        }
                    }
                }

                // recurse the analyze
                base.Visit(node);
            }
        }