<commit_before>define(function(require, exports, module) {

    var esprima = require('lib/esprima'),
        eswalker = require('lib/walker'),
        custom_template = require('../templates/default.js'),
        SmartComments = {

        initialize: function(config) {
            var instance = SmartComments,
                template_path;

            //initializing our own walker 
            instance.SmartCommentsWalker = eswalker.createWalker();

            //esprima obj
            instance.esprima = esprima;

            instance.config = config;

            template_path = config.template || '../templates/default.js';

            //template_path = path.join(template_path);

            try {
                //var custom_template = require(template_path);
                instance.template = BaseTemplate;
                //TODO: Esto deberia ser dinamico pero quedaria el problema de los parametros
                instance.template.walkFunctions = custom_template.walkFunctions;
                instance.template.tags = custom_template.tags(instance.template);
            } catch (e) {
                console.log('Cannot find template:');
                console.log(template_path);
            }

            if (instance.template) {
                instance.template.initialize(config);

                //build users defined functions to walker
                instance.buildWalkFunctions();
                return true;
            }

            return false;
        },

        //esto es para que walker sepa que funciones son las que tiene que ejecuctar
        buildWalkFunctions: function() {
            var instance = this,
                template = instance.template,
                SmartCommentsWalker = instance.SmartCommentsWalker,
                default_function,
                walkFunctions = template.walkFunctions();

            walkFunctions.forEach(function(item, index){
                default_function = function(node, parent, fieldName, siblings, index) {
                        var instance = template,
                            params = {
                                node: node,
                                parent: parent,
                                fieldName: fieldName,
                                siblings: siblings,
                                index: index
                            };
                        instance.executeWalk(item.name, params);
                };

                if(item.func){
                    //user custom function
                    default_function = func;
                }
                SmartCommentsWalker[item.name] = default_function;
            });
       
        },

        generate: function(source) {
            var instance = SmartComments,
                ast,
                result = {};

            try {
                ast = instance.esprima.parse(source, {
                    tokens: true,
                    range: true,
                    tolerant: true
                });
            } catch (e) {
                result.error = { 
                    message: 'Sorry, we have been some problems to parse your code',
                    details: e
                };
            }
            if (ast) {
                instance.comments_list = [];

                instance.template.setCommentsList(instance.comments_list);

                instance.SmartCommentsWalker.walk(ast);

                instance.comments_list = instance.template.getCommentsList();

                return SmartComments.applyComments(instance.comments_list, source);
            } else {
                return result;
            }
        },

        applyComments: function(comments_list, source) {
            var instance = SmartComments,
                size = comments_list.length,
                i = size - 1,
                comment;

            for (i; i >= 0; i--) {
                comment_value = instance.createComment(comments_list[i], source);
                source = instance.insertString(comment_value, comments_list[i].pos, source);
            };

            return source;
        },

        createComment: function(comment, source) {
            var value = '/**\n',
                tag_list = comment.tags,
                size = tag_list.length,
                i = 0,
                iterator = comment.pos - 1,
                buffer = '',
                currentChar,
                especialChars = "!@#$%^&*()+=-[]\\';,./{}|\":<>?\n";

            if(iterator > 0){

                while (iterator > 0 ) {
                    currentChar = source[iterator]
                    if(especialChars.indexOf(currentChar) != -1 ){
                        if(currentChar !== '\n'){
                            value = '\n' + value;
                        }
                        break;
                    }
                    buffer += currentChar;
                    iterator--;
                }

                for (i; i < size; i++) {
                    value += buffer + ' * ' + tag_list[i].name;
                    if (tag_list[i].name !== '') {
                        value += ' ';
                    }
                    value += tag_list[i].value + '\n';

                };

                value += buffer + ' */\n' + buffer;                
            }

            return value;
        },

        insertString: function(value, pos, source) {
            var last = source.slice(0, pos) + value + source.slice(pos);
            return last;
        },
<commit_msg>B3 Comment generation for zero position elements in the source.<commit_after>define(function(require, exports, module) {

    var esprima = require('lib/esprima'),
        eswalker = require('lib/walker'),
        custom_template = require('../templates/default.js'),
        SmartComments = {

        initialize: function(config) {
            var instance = SmartComments,
                template_path;

            //initializing our own walker 
            instance.SmartCommentsWalker = eswalker.createWalker();

            //esprima obj
            instance.esprima = esprima;

            instance.config = config;

            template_path = config.template || '../templates/default.js';

            //template_path = path.join(template_path);

            try {
                //var custom_template = require(template_path);
                instance.template = BaseTemplate;
                //TODO: Esto deberia ser dinamico pero quedaria el problema de los parametros
                instance.template.walkFunctions = custom_template.walkFunctions;
                instance.template.tags = custom_template.tags(instance.template);
            } catch (e) {
                console.log('Cannot find template:');
                console.log(template_path);
            }

            if (instance.template) {
                instance.template.initialize(config);

                //build users defined functions to walker
                instance.buildWalkFunctions();
                return true;
            }

            return false;
        },

        //esto es para que walker sepa que funciones son las que tiene que ejecuctar
        buildWalkFunctions: function() {
            var instance = this,
                template = instance.template,
                SmartCommentsWalker = instance.SmartCommentsWalker,
                default_function,
                walkFunctions = template.walkFunctions();

            walkFunctions.forEach(function(item, index){
                default_function = function(node, parent, fieldName, siblings, index) {
                        var instance = template,
                            params = {
                                node: node,
                                parent: parent,
                                fieldName: fieldName,
                                siblings: siblings,
                                index: index
                            };
                        instance.executeWalk(item.name, params);
                };

                if(item.func){
                    //user custom function
                    default_function = func;
                }
                SmartCommentsWalker[item.name] = default_function;
            });
       
        },

        generate: function(source) {
            var instance = SmartComments,
                ast,
                result = {};

            try {
                ast = instance.esprima.parse(source, {
                    tokens: true,
                    range: true,
                    tolerant: true
                });
            } catch (e) {
                result.error = { 
                    message: 'Sorry, we have been some problems to parse your code',
                    details: e
                };
            }
            if (ast) {
                instance.comments_list = [];

                instance.template.setCommentsList(instance.comments_list);

                instance.SmartCommentsWalker.walk(ast);

                instance.comments_list = instance.template.getCommentsList();

                return SmartComments.applyComments(instance.comments_list, source);
            } else {
                return result;
            }
        },

        applyComments: function(comments_list, source) {
            var instance = SmartComments,
                size = comments_list.length,
                i = size - 1,
                comment;

            for (i; i >= 0; i--) {
                comment_value = instance.createComment(comments_list[i], source);
                source = instance.insertString(comment_value, comments_list[i].pos, source);
            };

            return source;
        },

        createComment: function(comment, source) {
            var value = '/**\n',
                tag_list = comment.tags,
                size = tag_list.length,
                i = 0,
                iterator = comment.pos - 1,
                buffer = '',
                currentChar,
                especialChars = "!@#$%^&*()+=-[]\\';,./{}|\":<>?\n";

            if(comment.pos === 0)
                iterator = 0;
            while (iterator > 0 ) {
                currentChar = source[iterator]
                if(especialChars.indexOf(currentChar) != -1 ){
                    if(currentChar !== '\n'){
                        value = '\n' + value;
                    }
                    break;
                }
                buffer += currentChar;
                iterator--;
            }

            for (i; i < size; i++) {
                value += buffer + ' * ' + tag_list[i].name;
                if (tag_list[i].name !== '') {
                    value += ' ';
                }
                value += tag_list[i].value + '\n';

            };

            value += buffer + ' */\n' + buffer;                

            return value;
        },

        insertString: function(value, pos, source) {
            var last = source.slice(0, pos) + value + source.slice(pos);
            return last;
        },

{'fraction_non_alphanumeric': 0.09678291430116248, 'fraction_numerical': 0.0016220600162206002, 'mean_word_length': 1.1495254696881658, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 10, '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': '4313009', 'n_tokens_mistral': 2722, 'n_tokens_neox': 2448, 'n_words': 559}