lib/transform.js
function identifierFactory (ast, reserved) {
  reserved = reserved || new Set

  ast.find(j.Identifier).forEach(path => {
    reserved.add(path.node.name)
  })

  ast.find(j.FunctionDeclaration).forEach(path => {
    reserved.add(path.node.id.name)
  })

  function identifier (name, suffix) {
    return j.identifier(identifier.string(name, suffix))
  }

  identifier.string = function (name, suffix) {
    let sfx = suffix == null ? '' : suffix
      , unique = name + sfx
      , n = 0

    while (reserved.has(unique)) {
      unique = name + (n++) + sfx
    }

    return unique
  }

  return identifier
}
function identifierFactory (ast, reserved) {
  reserved = reserved || new Set

  ast.find(j.Identifier).forEach(path => {
    reserved.add(path.node.name)
  })

  ast.find(j.FunctionDeclaration).forEach(path => {
    reserved.add(path.node.id.name)
  })

  function identifier (name, suffix) {
    return j.identifier(identifier.string(name, suffix))
  }

  identifier.string = function (name, suffix) {
    let sfx = suffix == null ? '' : suffix
      , unique = name + sfx
      , n = 0

    while (reserved.has(unique)) {
      unique = name + (n++) + sfx
    }

    return unique
  }

  return identifier
}
function identifierFactory ( ast , reserved ) { reserved = reserved || new Set ast . find ( j . Identifier ) . forEach ( path => { reserved . add ( path . node . name ) } ) ast . find ( j . FunctionDeclaration ) . forEach ( path => { reserved . add ( path . node . id . name ) } ) function identifier ( name , suffix ) { return j . identifier ( identifier . string ( name , suffix ) ) } identifier . string = function ( name , suffix ) { let sfx = suffix == null ? '' : suffix , unique = name + sfx , n = 0 while ( reserved . has ( unique ) ) { unique = name + ( n ++ ) + sfx } return unique } return identifier }
Make identifiers with unique names
Make identifiers with unique names