<commit_before>const chars = 'abcdefghijklmnopqrstuvwxyz' +
              'ABCDEFGHIJKLMNOPQRSTUVWXYZ$'.split('');

module.exports = ({ Plugin, types: t }) => {
  const mangleNamesVisitors = {
    Scope({ scope }) {
      const bindings = scope.bindings;
      scope.bindings = {};

      const names = Object.keys(bindings);

      let i = 0;
      for (let name of names) {
        let binding = bindings[name];
        let bindingRefs = this.refs.get(binding);
        if (!bindingRefs) {
          continue;
        }

        let newName;

        do {
          newName = getIdentifier(i);
          i += 1;
        } while (!(t.isValidIdentifier(newName)
            && canUse(newName, scope, bindingRefs, this.refs)));

        scope.bindings[newName] = binding;
        for (let ref of bindingRefs) {
          ref.node.name = newName;
        }
      }
    },
  };

  return {
    pre() {
      this.refs = new Map();
    },

    visitor: {
      Program: {
        exit(path) {
          path.traverse(mangleNamesVisitors, {
            refs: this.refs,
          });
        },
      },

      'ReferencedIdentifier|BindingIdentifier'(path) {
        const { scope, node } = path;
        recordRef(this.refs, scope.getBinding(node.name), path);
      },
    },
  };

  function canUse(name, scope, refs, refsMap) {
    // Competing binding in the definition scope.
    const competingBinding = scope.getBinding(name);
    if (competingBinding) {
      /**
       * Go through all references then crawl their scopes upwards,
       * looking to see if one of these references is in this scope.
       */
      const bindingRefs = refsMap.get(competingBinding);
      for (let ref of bindingRefs) {
        let myScope = ref.scope;
        do {
          if (myScope === scope) {
            return false;
          }
          myScope = myScope.parent;
        } while (myScope);<commit_msg>Make sure the naming plugin run first<commit_after>const chars = 'abcdefghijklmnopqrstuvwxyz' +
              'ABCDEFGHIJKLMNOPQRSTUVWXYZ$'.split('');

module.exports = ({ Plugin, types: t }) => {
  const mangleNamesVisitors = {
    Scope({ scope }) {
      const bindings = scope.bindings;
      scope.bindings = {};

      const names = Object.keys(bindings);

      let i = 0;
      for (let name of names) {
        let binding = bindings[name];
        let bindingRefs = this.refs.get(binding);
        if (!bindingRefs) {
          continue;
        }

        let newName;

        do {
          newName = getIdentifier(i);
          i += 1;
        } while (!(t.isValidIdentifier(newName)
            && canUse(newName, scope, bindingRefs, this.refs)));

        scope.bindings[newName] = binding;
        for (let ref of bindingRefs) {
          ref.node.name = newName;
        }
      }
    },
  };

  const collectVisitor = {
    'ReferencedIdentifier|BindingIdentifier'(path) {
      const { scope, node } = path;
      recordRef(this.refs, scope.getBinding(node.name), path);
    },
  };

  return {
    visitor: {
      Program(path) {
        // We want to take control of the sequencing and make sure our visitors
        // run in isolation.
        const refs = new Map();
        path.traverse(collectVisitor, { refs });
        path.traverse(mangleNamesVisitors, { refs });
      },
    },
  };

  function canUse(name, scope, refs, refsMap) {
    // Competing binding in the definition scope.
    const competingBinding = scope.getBinding(name);
    if (competingBinding) {
      /**
       * Go through all references then crawl their scopes upwards,
       * looking to see if one of these references is in this scope.
       */
      const bindingRefs = refsMap.get(competingBinding);
      for (let ref of bindingRefs) {
        let myScope = ref.scope;
        do {
          if (myScope === scope) {
            return false;
          }
          myScope = myScope.parent;
        } while (myScope);
{'fraction_non_alphanumeric': 0.11520854526958291, 'fraction_numerical': 0.001017293997965412, 'mean_word_length': 1.9372666168782673, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6119423', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1155, 'n_words': 293}