lib/plugins/js/babel-plugin-transform-flatten.js
function shouldRenameDeclaration(name, path, binding, rootScope) {
  const isRootScope = path.scope === rootScope;
  const isGlobal = isRootScope && name in rootScope.globals;
  const program = rootScope.path;

  if (isGlobal) return false;

  if (!isRootScope) {
    const functionParent = path.getFunctionParent();
    const declarationParent = getDeclarationParent(path);

    // Bail if not a declaration or is property/key of object
    if (!declarationParent || isPropertyOrKey(path)) return false;

    if (declarationParent.isBlockScoped()) {
      // Handle function declaration (which defines it's own scope)
      if (declarationParent.isFunctionDeclaration()) {
        return declarationParent.parentPath.scope === rootScope && declarationParent.node.id === path.node;
      }
      // Skip let/const in block scope
      return false;
      // Handle re-assignment of root bound declaration (ignore function expressions)
    } else if (declarationParent.isAssignmentExpression() && !path.parentPath.isFunctionExpression()) {
      const declarationBinding = declarationParent.scope.getBinding(name);

      return declarationBinding ? declarationBinding.scope === rootScope : false;
      // Skip if in function scope
    } else if (functionParent !== program) {
      return false;
    }
  }

  // Skip if property or key of object
  return !isPropertyOrKey(path);
}
function shouldRenameDeclaration(name, path, binding, rootScope) {
  const isRootScope = path.scope === rootScope;
  const isGlobal = isRootScope && name in rootScope.globals;
  const program = rootScope.path;

  if (isGlobal) return false;

  if (!isRootScope) {
    const functionParent = path.getFunctionParent();
    const declarationParent = getDeclarationParent(path);

    // Bail if not a declaration or is property/key of object
    if (!declarationParent || isPropertyOrKey(path)) return false;

    if (declarationParent.isBlockScoped()) {
      // Handle function declaration (which defines it's own scope)
      if (declarationParent.isFunctionDeclaration()) {
        return declarationParent.parentPath.scope === rootScope && declarationParent.node.id === path.node;
      }
      // Skip let/const in block scope
      return false;
      // Handle re-assignment of root bound declaration (ignore function expressions)
    } else if (declarationParent.isAssignmentExpression() && !path.parentPath.isFunctionExpression()) {
      const declarationBinding = declarationParent.scope.getBinding(name);

      return declarationBinding ? declarationBinding.scope === rootScope : false;
      // Skip if in function scope
    } else if (functionParent !== program) {
      return false;
    }
  }

  // Skip if property or key of object
  return !isPropertyOrKey(path);
}
function shouldRenameDeclaration ( name , path , binding , rootScope ) { const isRootScope = path . scope === rootScope ; const isGlobal = isRootScope && name in rootScope . globals ; const program = rootScope . path ; if ( isGlobal ) return false ; if ( ! isRootScope ) { const functionParent = path . getFunctionParent ( ) ; const declarationParent = getDeclarationParent ( path ) ; // Bail if not a declaration or is property/key of object if ( ! declarationParent || isPropertyOrKey ( path ) ) return false ; if ( declarationParent . isBlockScoped ( ) ) { // Handle function declaration (which defines it's own scope) if ( declarationParent . isFunctionDeclaration ( ) ) { return declarationParent . parentPath . scope === rootScope && declarationParent . node . id === path . node ; } // Skip let/const in block scope return false ; // Handle re-assignment of root bound declaration (ignore function expressions) } else if ( declarationParent . isAssignmentExpression ( ) && ! path . parentPath . isFunctionExpression ( ) ) { const declarationBinding = declarationParent . scope . getBinding ( name ) ; return declarationBinding ? declarationBinding . scope === rootScope : false ; // Skip if in function scope } else if ( functionParent !== program ) { return false ; } } // Skip if property or key of object return ! isPropertyOrKey ( path ) ; }
Determine if declaration 'path' should be renamed
@param {String} name
@param {NodePath} path
@param {Binding} binding
@param {Scope} rootScope
@returns {Boolean}
Determine if declaration path should be renamed