<commit_before>/// <reference path="../../util/idGenerator.ts" />
/// <reference path="../../estree.ts" />

namespace Styx.Parser.AstPreprocessing {
    interface FunctionExpressionToRewrite {
        name: string;
        functionExpression: ESTree.FunctionExpression;
    }
    
    export function rewriteFunctionExpressions(program: ESTree.Program): ESTree.Program {
        let functionIdGenerator = Util.IdGenerator.create();
        let functionExpressionsToRewrite: FunctionExpressionToRewrite[] = [];
        
        let stringifiedProgram = JSON.stringify(program, visitNode);
        let clonedProgram: ESTree.Program = JSON.parse(stringifiedProgram);
        
        for (let funcToRewrite of functionExpressionsToRewrite) {
            let functionDeclaration: ESTree.Function = {
                type: ESTree.NodeType.FunctionDeclaration,
                id: { type: ESTree.NodeType.Identifier, name: funcToRewrite.name },
                params: clone(funcToRewrite.functionExpression.params),
                body: clone(funcToRewrite.functionExpression.body)
            };
            
            clonedProgram.body.unshift(functionDeclaration);
        }
        
        return clonedProgram;
    
        function visitNode(key: string, value: any): any {
            return value && value.type === ESTree.NodeType.FunctionExpression
                ? rewriteFunctionExpression(value)
                : value;
        }
        
        function rewriteFunctionExpression(functionExpression: ESTree.FunctionExpression): ESTree.Identifier {
            let funcId = functionIdGenerator.generateId();
            let nameSuffix = functionExpression.id ? "_" + functionExpression.id.name : "";
            let funcName = `$$func${funcId}${nameSuffix}`
            
            functionExpressionsToRewrite.push({
                name: funcName,
                functionExpression: functionExpression
            });
            
            return {
                type: ESTree.NodeType.Identifier,
                name: funcName
            }
        }
        
        function clone<T>(object: T): T {
            return JSON.parse(JSON.stringify(object));
        }
    }
}<commit_msg>Renaming<commit_after>/// <reference path="../../util/idGenerator.ts" />
/// <reference path="../../estree.ts" />

namespace Styx.Parser.AstPreprocessing {
    interface RewrittenFunction {
        name: string;
        functionExpression: ESTree.FunctionExpression;
    }
    
    export function rewriteFunctionExpressions(program: ESTree.Program): ESTree.Program {
        let functionIdGenerator = Util.IdGenerator.create();
        let functionExpressionsToRewrite: RewrittenFunction[] = [];
        
        let stringifiedProgram = JSON.stringify(program, visitNode);
        let clonedProgram: ESTree.Program = JSON.parse(stringifiedProgram);
        
        prependFunctionDeclarationsToProgramBody(functionExpressionsToRewrite, clonedProgram);
        
        return clonedProgram;
    
        function visitNode(key: string, value: any): any {
            return value && value.type === ESTree.NodeType.FunctionExpression
                ? rewriteFunctionExpression(value)
                : value;
        }
        
        function rewriteFunctionExpression(functionExpression: ESTree.FunctionExpression): ESTree.Identifier {
            let funcId = functionIdGenerator.generateId();
            let nameSuffix = functionExpression.id ? "_" + functionExpression.id.name : "";
            let funcName = `$$func${funcId}${nameSuffix}`
            
            functionExpressionsToRewrite.push({
                name: funcName,
                functionExpression: functionExpression
            });
            
            return {
                type: ESTree.NodeType.Identifier,
                name: funcName
            }
        }
    }
    
    function prependFunctionDeclarationsToProgramBody(rewrittenFunctions: RewrittenFunction[], program: ESTree.Program) {
        for (let rewrittenFunc of rewrittenFunctions) {
            let functionDeclaration: ESTree.Function = {
                type: ESTree.NodeType.FunctionDeclaration,
                id: {
                    type: ESTree.NodeType.Identifier,
                    name: rewrittenFunc.name
                },
                params: clone(rewrittenFunc.functionExpression.params),
                body: clone(rewrittenFunc.functionExpression.body)
            };
            
            program.body.unshift(functionDeclaration);
        }
    }
        
    function clone<T>(object: T): T {
        return JSON.parse(JSON.stringify(object));
    }
}
{'fraction_non_alphanumeric': 0.09327315710294434, 'fraction_numerical': 0.0, 'mean_word_length': 2.026007802340702, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2650325', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1081, 'n_words': 199}