private void maybeUpdateTopLevelName(NodeTraversal t, Node nameNode) {
    String name = nameNode.getString();
    if (!currentScript.isModule || !currentScript.topLevelNames.contains(name)) {
      return;
    }
    Var var = t.getScope().getVar(name);
    // If the name refers to a var that is not from the top level scope.
    if (var == null || var.getScope().getRootNode() != currentScript.rootNode) {
      // Then it shouldn't be renamed.
      return;
    }

    // If the name is part of a destructuring import, the import rewriting will take care of it
    if (var.getNameNode() == nameNode
        && nameNode.getParent().isStringKey()
        && nameNode.getGrandparent().isObjectPattern()) {
      Node destructuringLhsNode = nameNode.getGrandparent().getParent();
      if (isCallTo(destructuringLhsNode.getLastChild(), GOOG_REQUIRE)
          || isCallTo(destructuringLhsNode.getLastChild(), GOOG_REQUIRETYPE)) {
        return;
      }
    }

    // If the name is an alias for an imported namespace rewrite from
    // "new Foo;" to "new module$exports$Foo;"
    boolean nameIsAnAlias = currentScript.namesToInlineByAlias.containsKey(name);
    if (nameIsAnAlias && var.getNode() != nameNode) {
      maybeAddAliasToSymbolTable(nameNode, currentScript.legacyNamespace);

      String namespaceToInline = currentScript.namesToInlineByAlias.get(name);
      if (namespaceToInline.equals(currentScript.getBinaryNamespace())) {
        currentScript.hasCreatedExportObject = true;
      }
      safeSetMaybeQualifiedString(nameNode, namespaceToInline);

      // Make sure this action won't shadow a local variable.
      if (namespaceToInline.indexOf('.') != -1) {
        String firstQualifiedName = namespaceToInline.substring(0, namespaceToInline.indexOf('.'));
        Var shadowedVar = t.getScope().getVar(firstQualifiedName);
        if (shadowedVar == null
            || shadowedVar.isGlobal()
            || shadowedVar.getScope().isModuleScope()) {
          return;
        }
        t.report(
            shadowedVar.getNode(),
            IMPORT_INLINING_SHADOWS_VAR,
            shadowedVar.getName(),
            namespaceToInline);
      }
      return;
    }

    // For non-import alias names rewrite from
    // "var foo; console.log(foo);" to
    // "var module$contents$Foo_foo; console.log(module$contents$Foo_foo);"
    safeSetString(nameNode, currentScript.contentsPrefix + name);
  }
private void maybeUpdateTopLevelName ( NodeTraversal t , Node nameNode ) { String name = nameNode . getString ( ) ; if ( ! currentScript . isModule || ! currentScript . topLevelNames . contains ( name ) ) { return ; } Var var = t . getScope ( ) . getVar ( name ) ; if ( var == null || var . getScope ( ) . getRootNode ( ) != currentScript . rootNode ) { return ; } if ( var . getNameNode ( ) == nameNode && nameNode . getParent ( ) . isStringKey ( ) && nameNode . getGrandparent ( ) . isObjectPattern ( ) ) { Node destructuringLhsNode = nameNode . getGrandparent ( ) . getParent ( ) ; if ( isCallTo ( destructuringLhsNode . getLastChild ( ) , GOOG_REQUIRE ) || isCallTo ( destructuringLhsNode . getLastChild ( ) , GOOG_REQUIRETYPE ) ) { return ; } } boolean nameIsAnAlias = currentScript . namesToInlineByAlias . containsKey ( name ) ; if ( nameIsAnAlias && var . getNode ( ) != nameNode ) { maybeAddAliasToSymbolTable ( nameNode , currentScript . legacyNamespace ) ; String namespaceToInline = currentScript . namesToInlineByAlias . get ( name ) ; if ( namespaceToInline . equals ( currentScript . getBinaryNamespace ( ) ) ) { currentScript . hasCreatedExportObject = true ; } safeSetMaybeQualifiedString ( nameNode , namespaceToInline ) ; if ( namespaceToInline . indexOf ( '.' ) != - 1 ) { String firstQualifiedName = namespaceToInline . substring ( 0 , namespaceToInline . indexOf ( '.' ) ) ; Var shadowedVar = t . getScope ( ) . getVar ( firstQualifiedName ) ; if ( shadowedVar == null || shadowedVar . isGlobal ( ) || shadowedVar . getScope ( ) . isModuleScope ( ) ) { return ; } t . report ( shadowedVar . getNode ( ) , IMPORT_INLINING_SHADOWS_VAR , shadowedVar . getName ( ) , namespaceToInline ) ; } return ; } safeSetString ( nameNode , currentScript . contentsPrefix + name ) ; }
Rewrites top level var names from
"var foo; console.log(foo);" to
"var module$contents$Foo_foo; console.log(module$contents$Foo_foo);"