<commit_before>$('#compute').on('click', function(e){
  compute(
    $('#sourceInput').text(),
    function(codes){
      $('#sourceOutput').text(codes.source);
      $('#ASTOutput').text(codes.AST);
      $('#errorOutput').text(codes.errors);
    }
  ); //TODO: Can we pipe this at some point?
});

var outputPanes = $('#sourceOutput, #ASTOutput, #errorOutput');
var paneButtons = $('BUTTON#sourcePane, BUTTON#ASTPane, BUTTON#errorPane')
paneButtons.on('click', function(e){
  outputPanes.hide();
  $('#' + $(e.target).attr('data-pane')).show();
});

function compute(oldSource, callback){
  var tolerant = [];

  var originalAST = esprima.parse(oldSource, {tolerant:tolerant});
  var newAST = getGoofy(_.clone(originalAST, true));

  var newSource = escodegen.generate(newAST);
  callback({
    source:newSource,
    AST:JSON.stringify(originalAST,null, 4),
    errors:JSON.stringify(tolerant,null, 4)
  });
}

function getGoofy(AST){

  for (var i = 0; i < AST.body.length; i++){
    var body = AST.body[i];
    goofBody(body, function(){});
  }

  return AST;
}

function goofBody(body, closureMap){
  var newClosureMap = function(){};
  newClosureMap.prototype = closureMap;
  newClosureMap = new newClosureMap();

  if (Array.isArray(body)){
    for(var i = 0; i < body.length; i++){
      goofBody(body[i], newClosureMap);
    }
    return;
  }

  if (body.type === 'BlockStatement'){
    return goofBody(body.body, newClosureMap);
  }

  if (body.type === 'FunctionDeclaration'){
    body.id.name = getWord('V');
    return goofBody(body.body, newClosureMap);
  }

  if (body.type === 'VariableDeclaration'){
    for (var i = 0; i < body.declarations.length; i++){
      var variable = body.declarations[i];
      var newVariableName = getWord('N');
      newClosureMap[variable.id.name] = newVariableName;
      variable.id.name = newVariableName;
    }

    return body;
  }

  if (body.type === 'ExpressionStatement'){
    var arguments =
      (body.expression.right || body.expression).arguments;
    for (var i = 0; i < arguments.length; i++){
      var variable = arguments[i];
      var variableId = variable.id || variable;
      //FIXME: Mapping doesn't work because newClosureMap is never sent to a deeper call
      if (newClosureMap[variableId.name]){
        variable.name = newClosureMap[variableId.name];
      }
      else {
        var newVariableName = getWord('N');
        newClosureMap[variableId.name] = newVariableName;
        variableId.name = newVariableName;
      }
    }
  }
}

function getWord(partOfSpeech){
  //start at random spot in array then wrap around looking
  var r = Math.floor(Math.random() * __words.length);
  var i = r;
  while (i++){
    var word = __words[i];
    if (word[1] == partOfSpeech){
      return word[0];
    }

    if (i >= __words.length){
      i = 0;
    }
    else {
      if (i == (r - 1)){
        return 'nothing'; //End of wrap around loop
      }
    }
  }
}<commit_msg>Closure deep variable names match now.<commit_after>$('#compute').on('click', function(e){
  compute(
    $('#sourceInput').text(),
    function(codes){
      $('#sourceOutput').text(codes.source);
      $('#ASTOutput').text(codes.AST);
      $('#errorOutput').text(codes.errors);
    }
  ); //TODO: Can we pipe this at some point?
});

var outputPanes = $('#sourceOutput, #ASTOutput, #errorOutput');
var paneButtons = $('BUTTON#sourcePane, BUTTON#ASTPane, BUTTON#errorPane')
paneButtons.on('click', function(e){
  outputPanes.hide();
  $('#' + $(e.target).attr('data-pane')).show();
});

function compute(oldSource, callback){
  var tolerant = [];

  var originalAST = esprima.parse(oldSource, {tolerant:tolerant});
  var newAST = getGoofy(_.clone(originalAST, true));

  var newSource = escodegen.generate(newAST);
  callback({
    source:newSource,
    AST:JSON.stringify(originalAST,null, 4),
    errors:JSON.stringify(tolerant,null, 4)
  });
}

function getGoofy(AST){

  for (var i = 0; i < AST.body.length; i++){
    var body = AST.body[i];
    goofBody(body, function(){});
  }

  return AST;
}

function getNewClosureMap(cm){
  var newClosureMap = function(){};
  newClosureMap.prototype = cm;
  newClosureMap = new newClosureMap();
  return newClosureMap;
}

function goofBody(body, closureMap){
  if (Array.isArray(body)){
    //Body items need to share a closure map
    var newClosureMap = getNewClosureMap(closureMap);

    for(var i = 0; i < body.length; i++){
      goofBody(body[i], newClosureMap);
    }
    return;
  }

  if (body.type === 'BlockStatement'){
    return goofBody(body.body, getNewClosureMap(closureMap));
  }

  if (body.type === 'FunctionDeclaration'){
    body.id.name = getWord('V');
    return goofBody(body.body, getNewClosureMap(closureMap));
  }

  if (body.type === 'VariableDeclaration'){
    for (var i = 0; i < body.declarations.length; i++){
      var variable = body.declarations[i];
      var newVariableName = getWord('N');
      closureMap[variable.id.name] = newVariableName;
      variable.id.name = newVariableName;
    }

    return body;
  }

  if (body.type === 'ExpressionStatement'){
    var arguments =
      (body.expression.right || body.expression).arguments;
    for (var i = 0; i < arguments.length; i++){
      var variable = arguments[i];
      var variableId = variable.id || variable;
      if (closureMap[variableId.name]){
        variable.name = closureMap[variableId.name];
      }
      else {
        var newVariableName = getWord('N');
        closureMap[variableId.name] = newVariableName;
        variableId.name = newVariableName;
      }
    }
  }
}

function getWord(partOfSpeech){
  //start at random spot in array then wrap around looking
  var r = Math.floor(Math.random() * __words.length);
  var i = r;
  while (i++){
    var word = __words[i];
    if (word[1] == partOfSpeech){
      return word[0];
    }

    if (i >= __words.length){
      i = 0;
    }
    else {
      if (i == (r - 1)){
        return 'nothing'; //End of wrap around loop
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.16074704018676006, 'fraction_numerical': 0.0033350008337502085, 'mean_word_length': 3.223943661971831, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '5339136', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1984, 'n_words': 443}