public void Visit(BlockStatement blockStatement) { if (_innerVisitor != null) { blockStatement.Accept(_innerVisitor); } foreach (var statement in blockStatement.Body) { statement.Accept(this); } }
public static void Apply(BlockStatement block, JSParser parser) { if (parser == null) { throw new ArgumentNullException("parser"); } if (block != null) { // create a new instance of the visitor and apply it to the block var visitor = new ReorderScopeVisitor(parser); block.Accept(visitor); // if there were any module directive prologues we need to promote, do them first var insertAt = 0; if (visitor.m_moduleDirectives != null) { foreach (var directivePrologue in visitor.m_moduleDirectives) { insertAt = RelocateDirectivePrologue(block, insertAt, directivePrologue); } } // Make sure that we skip over any remaining comments and directive prologues. // we do NOT want to insert anything between the start of the scope and any directive prologues. while (insertAt < block.Count && (block[insertAt] is DirectivePrologue || block[insertAt] is ImportantComment)) { ++insertAt; } // first, we want to move all function declarations to the top of this block if (visitor.m_functionDeclarations != null) { foreach (var funcDecl in visitor.m_functionDeclarations) { insertAt = RelocateFunction(block, insertAt, funcDecl); } } // special case: if there is only one var statement in the entire scope, // then just leave it alone because we will only add bytes by moving it around, // or be byte-neutral at best (no initializers and not in a for-statement). if (visitor.m_varStatements != null && visitor.m_varStatements.Count > 1) { // then we want to move all variable declarations after to the top (after the functions) foreach (var varStatement in visitor.m_varStatements) { insertAt = RelocateVar(block, insertAt, varStatement); } } // then we want to do the same thing for all child functions (declarations AND other) if (visitor.m_functionDeclarations != null) { foreach (var funcDecl in visitor.m_functionDeclarations) { Apply(funcDecl.Body, parser); } } if (visitor.m_functionExpressions != null) { foreach (var funcExpr in visitor.m_functionExpressions) { Apply(funcExpr.Body, parser); } } // and then recurse the modules if (visitor.m_moduleDeclarations != null) { foreach (var moduleDecl in visitor.m_moduleDeclarations) { Apply(moduleDecl.Body, parser); } } } }