                                                 properties: []
                                 }],
                                 kind: "var"
+                        }
 
                         if n.ejs_env.parent?
-                                n.body.body.splice 1, 0, {
+                                prepends.push {
                                         type: syntax.ExpressionStatement
                                         expression:
                                                 type: syntax.AssignmentExpression,

                n.callee = create_identifier "%invokeClosure"
                n



exports.convert = (tree) ->
        debug.log "before:"
        debug.log -> escodegen.generate tree

        funcs_to_vars = new FuncsToVars tree
        tree2 = funcs_to_vars.visit tree

        debug.log "after FuncsToVars:"
        debug.log -> escodegen.generate tree2
        
        # first we decorate the tree with free variable usage
        free tree2

        # traverse the tree accumulating info about which function level defined a given variable.
        locate_envs = new LocateEnvVisitor
        tree3 = locate_envs.visit tree2

        debug.log "after LocateEnvVisitor:"
        debug.log -> escodegen.generate tree3
        substitute_vars = new SubstituteVariables tree3
        tree4 = substitute_vars.visit tree3

        debug.log "after SubstituteVariables:"
        debug.log -> escodegen.generate tree4
        
        lambda_lift = new LambdaLift tree4
        tree5 = lambda_lift.visit tree4

        debug.log "after LambdaLift:"
        debug.log -> escodegen.generate tree5

        tree5
        
                                        property: create_identifier sym

                        @mappings.unshift new_mapping
                        @visitFunctionBody n
                        n.body.body = prepends.concat n.body.body
                        @mappings = @mappings.slice(1)

                # we need to replace function declarations of the form:
