lib/parser.js
function pr_autolog(options) {

    function espectRewrite(filepath, outpath, template) {
      var autologret = require(template);
      autologret(filepath, outpath);
    }

    function insertReturnLog(source, endLine) {
      var sourceRet;
      // Insert ending log at return statement.
      sourceRet = source.replace(/(\s|;)return(\s|;)/g, "$1" + endLine + "return$2");

      // Remove any ending log that added twice.
      while(sourceRet.indexOf(endLine + endLine) !== -1) {
        sourceRet = sourceRet.replace(endLine + endLine, endLine);
      }
      return sourceRet;
    }

    function insertEnterAndLeaveLog(item, node, functionName) {
      var fnoutLine = 'var DEBOUT="debuguy,"' +
        ' + (new Date()).getTime() + ",' +
        item.substring(0, item.lastIndexOf('.')) + '.' +
        functionName + '@' +
        node.loc.start.line + '";console.log(DEBOUT + ",ENTER," + Date.now());';
      var fnEndLine = 'console.log(DEBOUT + ",LEAVE," + Date.now());';

      var startLine = node.getSource().indexOf('{') + 1;
      var endLine = node.getSource().lastIndexOf('}');
      var source = node.getSource().substr(0, startLine) + fnoutLine +
                   node.getSource().substr(startLine, endLine - startLine) + fnEndLine +
                   node.getSource().substr(endLine);
      source = insertReturnLog(source, fnEndLine);
      return source;
    }

    function insertEnterLog(item, node, functionName) {
      var fnoutLine = '\nconsole.log("debuguy,"' +
        ' + (new Date()).getTime() + ",' +
        item.substring(0, item.lastIndexOf('.')) + '.' +
        functionName + '@' +
        node.loc.start.line + '");';
      var startLine = node.getSource().indexOf('{') + 1;
      var source = node.getSource().substr(0, startLine) + fnoutLine +
                   node.getSource().substr(startLine);
      return source;
    }

    this.traverse(options, function insertLog (dir, item, out) {
      if (options.xEspect) {
        var templatefile = __dirname + '/autolog.esp.js';
        if ('string' === typeof options.xEspect) {
          templatefile = options.xEspect;
        }
        espectRewrite(dir + item, out + item, templatefile);
        return;
      }
      fs.readFile(dir + item, function(err, data) {
        console.log('adding log ' + dir + item);

        if (err) {
          console.log(err);
          console.log(data);
          return;
        }

        var text = data.toString();
        var output;

        try {
          output = esprimaTraversal(text, {loc:true})
            .traverse(function (node) {
            if (node.type === 'ArrowFunctionExpression' ||
                node.type === 'FunctionDeclaration' ||
                node.type === 'FunctionExpression') {
              // Get the name of the function
              var functionName = 'anonymous';
              if (node.id) {
                functionName = node.id.name;
              } else if (node.parent) {
                switch (node.parent.type) {
                case 'VariableDeclarator':
                  functionName = node.parent.id.name;
                  break;
                case 'Property':
                  functionName = node.parent.key.name;
                  break;
                case 'AssignmentExpression':
                  if (node.parent.left.type === 'MemberExpression') {
                    functionName = node.parent.left.property.name ||
                                   node.parent.left.property.value;
                  }
                  else if (node.parent.left.type === 'Identifier') {
                    functionName = node.parent.left.name;
                  }
                  break;
                }
              }
              // Build the console log output
              var source;
              if (options.callStackGraph) {
                source = insertEnterAndLeaveLog(item, node, functionName);
              } else {
                source = insertEnterLog(item, node, functionName);
              }
              node.updateSource(source);
            }
          });

          fs.writeFile(out + item, output, function(err) {
            if (err) {
              console.log(err);
            } else {
              console.log(out + item + ' was saved!');
            }
          });
        }
        catch (e) {
          console.log('parsing failed: ' + dir + item);
          console.log(e);
        }
      });
    });
  }
function pr_autolog(options) {

    function espectRewrite(filepath, outpath, template) {
      var autologret = require(template);
      autologret(filepath, outpath);
    }

    function insertReturnLog(source, endLine) {
      var sourceRet;
      // Insert ending log at return statement.
      sourceRet = source.replace(/(\s|;)return(\s|;)/g, "$1" + endLine + "return$2");

      // Remove any ending log that added twice.
      while(sourceRet.indexOf(endLine + endLine) !== -1) {
        sourceRet = sourceRet.replace(endLine + endLine, endLine);
      }
      return sourceRet;
    }

    function insertEnterAndLeaveLog(item, node, functionName) {
      var fnoutLine = 'var DEBOUT="debuguy,"' +
        ' + (new Date()).getTime() + ",' +
        item.substring(0, item.lastIndexOf('.')) + '.' +
        functionName + '@' +
        node.loc.start.line + '";console.log(DEBOUT + ",ENTER," + Date.now());';
      var fnEndLine = 'console.log(DEBOUT + ",LEAVE," + Date.now());';

      var startLine = node.getSource().indexOf('{') + 1;
      var endLine = node.getSource().lastIndexOf('}');
      var source = node.getSource().substr(0, startLine) + fnoutLine +
                   node.getSource().substr(startLine, endLine - startLine) + fnEndLine +
                   node.getSource().substr(endLine);
      source = insertReturnLog(source, fnEndLine);
      return source;
    }

    function insertEnterLog(item, node, functionName) {
      var fnoutLine = '\nconsole.log("debuguy,"' +
        ' + (new Date()).getTime() + ",' +
        item.substring(0, item.lastIndexOf('.')) + '.' +
        functionName + '@' +
        node.loc.start.line + '");';
      var startLine = node.getSource().indexOf('{') + 1;
      var source = node.getSource().substr(0, startLine) + fnoutLine +
                   node.getSource().substr(startLine);
      return source;
    }

    this.traverse(options, function insertLog (dir, item, out) {
      if (options.xEspect) {
        var templatefile = __dirname + '/autolog.esp.js';
        if ('string' === typeof options.xEspect) {
          templatefile = options.xEspect;
        }
        espectRewrite(dir + item, out + item, templatefile);
        return;
      }
      fs.readFile(dir + item, function(err, data) {
        console.log('adding log ' + dir + item);

        if (err) {
          console.log(err);
          console.log(data);
          return;
        }

        var text = data.toString();
        var output;

        try {
          output = esprimaTraversal(text, {loc:true})
            .traverse(function (node) {
            if (node.type === 'ArrowFunctionExpression' ||
                node.type === 'FunctionDeclaration' ||
                node.type === 'FunctionExpression') {
              // Get the name of the function
              var functionName = 'anonymous';
              if (node.id) {
                functionName = node.id.name;
              } else if (node.parent) {
                switch (node.parent.type) {
                case 'VariableDeclarator':
                  functionName = node.parent.id.name;
                  break;
                case 'Property':
                  functionName = node.parent.key.name;
                  break;
                case 'AssignmentExpression':
                  if (node.parent.left.type === 'MemberExpression') {
                    functionName = node.parent.left.property.name ||
                                   node.parent.left.property.value;
                  }
                  else if (node.parent.left.type === 'Identifier') {
                    functionName = node.parent.left.name;
                  }
                  break;
                }
              }
              // Build the console log output
              var source;
              if (options.callStackGraph) {
                source = insertEnterAndLeaveLog(item, node, functionName);
              } else {
                source = insertEnterLog(item, node, functionName);
              }
              node.updateSource(source);
            }
          });

          fs.writeFile(out + item, output, function(err) {
            if (err) {
              console.log(err);
            } else {
              console.log(out + item + ' was saved!');
            }
          });
        }
        catch (e) {
          console.log('parsing failed: ' + dir + item);
          console.log(e);
        }
      });
    });
  }
function pr_autolog ( options ) { function espectRewrite ( filepath , outpath , template ) { var autologret = require ( template ) ; autologret ( filepath , outpath ) ; } function insertReturnLog ( source , endLine ) { var sourceRet ; // Insert ending log at return statement. sourceRet = source . replace ( / (\s|;)return(\s|;) / g , "$1" + endLine + "return$2" ) ; // Remove any ending log that added twice. while ( sourceRet . indexOf ( endLine + endLine ) !== - 1 ) { sourceRet = sourceRet . replace ( endLine + endLine , endLine ) ; } return sourceRet ; } function insertEnterAndLeaveLog ( item , node , functionName ) { var fnoutLine = 'var DEBOUT="debuguy,"' + ' + (new Date()).getTime() + ",' + item . substring ( 0 , item . lastIndexOf ( '.' ) ) + '.' + functionName + '@' + node . loc . start . line + '";console.log(DEBOUT + ",ENTER," + Date.now());' ; var fnEndLine = 'console.log(DEBOUT + ",LEAVE," + Date.now());' ; var startLine = node . getSource ( ) . indexOf ( '{' ) + 1 ; var endLine = node . getSource ( ) . lastIndexOf ( '}' ) ; var source = node . getSource ( ) . substr ( 0 , startLine ) + fnoutLine + node . getSource ( ) . substr ( startLine , endLine - startLine ) + fnEndLine + node . getSource ( ) . substr ( endLine ) ; source = insertReturnLog ( source , fnEndLine ) ; return source ; } function insertEnterLog ( item , node , functionName ) { var fnoutLine = '\nconsole.log("debuguy,"' + ' + (new Date()).getTime() + ",' + item . substring ( 0 , item . lastIndexOf ( '.' ) ) + '.' + functionName + '@' + node . loc . start . line + '");' ; var startLine = node . getSource ( ) . indexOf ( '{' ) + 1 ; var source = node . getSource ( ) . substr ( 0 , startLine ) + fnoutLine + node . getSource ( ) . substr ( startLine ) ; return source ; } this . traverse ( options , function insertLog ( dir , item , out ) { if ( options . xEspect ) { var templatefile = __dirname + '/autolog.esp.js' ; if ( 'string' === typeof options . xEspect ) { templatefile = options . xEspect ; } espectRewrite ( dir + item , out + item , templatefile ) ; return ; } fs . readFile ( dir + item , function ( err , data ) { console . log ( 'adding log ' + dir + item ) ; if ( err ) { console . log ( err ) ; console . log ( data ) ; return ; } var text = data . toString ( ) ; var output ; try { output = esprimaTraversal ( text , { loc : true } ) . traverse ( function ( node ) { if ( node . type === 'ArrowFunctionExpression' || node . type === 'FunctionDeclaration' || node . type === 'FunctionExpression' ) { // Get the name of the function var functionName = 'anonymous' ; if ( node . id ) { functionName = node . id . name ; } else if ( node . parent ) { switch ( node . parent . type ) { case 'VariableDeclarator' : functionName = node . parent . id . name ; break ; case 'Property' : functionName = node . parent . key . name ; break ; case 'AssignmentExpression' : if ( node . parent . left . type === 'MemberExpression' ) { functionName = node . parent . left . property . name || node . parent . left . property . value ; } else if ( node . parent . left . type === 'Identifier' ) { functionName = node . parent . left . name ; } break ; } } // Build the console log output var source ; if ( options . callStackGraph ) { source = insertEnterAndLeaveLog ( item , node , functionName ) ; } else { source = insertEnterLog ( item , node , functionName ) ; } node . updateSource ( source ) ; } } ) ; fs . writeFile ( out + item , output , function ( err ) { if ( err ) { console . log ( err ) ; } else { console . log ( out + item + ' was saved!' ) ; } } ) ; } catch ( e ) { console . log ( 'parsing failed: ' + dir + item ) ; console . log ( e ) ; } } ) ; } ) ; }
Automatically insert console logs in javascript functions.
@param  {[type]} options [description]
@return {[type]}         [description]
Automatically insert console logs in javascript functions .