/*
 * Copyright 2008 The Closure Compiler Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.javascript.jscomp;

import com.google.common.collect.Lists;
import com.google.javascript.jscomp.NodeTraversal.Callback;
import com.google.javascript.rhino.Node;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Moves top-level function declarations to the top.
 *
 * Enable this pass if a try catch block wraps the output after compilation,
 * and the output runs on Firefox because function declarations are only
 * defined when reached inside a try catch block on Firefox.
 *
 * On Firefox, this code works:
 *
 * var g = f;
 * function f() {}
 *
 * but this code does not work:
 *
 * try {
 *   var g = f;
 *   function f() {}
 * } catch(e) {}
 *
 */
class MoveFunctionDeclarations implements Callback, CompilerPass {
  private final AbstractCompiler compiler;
  private final Map<JSModule, List<Node>> functions;

  MoveFunctionDeclarations(AbstractCompiler compiler) {
    this.compiler = compiler;
    functions = new HashMap<>();
  }

  @Override
  public void process(Node externs, Node root) {
    NodeTraversal.traverseEs6(compiler, root, this);
    for (Entry<JSModule, List<Node>> entry : functions.entrySet()) {
      JSModule module = entry.getKey();
      Node addingRoot = compiler.getNodeForCodeInsertion(module);
      for (Node n : Lists.reverse(entry.getValue())) {
        addingRoot.addChildToFront(n);
      }
    }
  }

  @Override
  public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
    Node grandparent = n.getAncestor(2);
    return grandparent == null || !grandparent.isScript();
  }

  @Override
  public void visit(NodeTraversal t, Node n, Node parent) {
    if (parent == null || !parent.isScript()) {
      return;
    }

    if (NodeUtil.isFunctionDeclaration(n)) {
      parent.removeChild(n);
      compiler.reportCodeChange();

      JSModule module = t.getModule();
      List<Node> moduleFunctions = functions.get(module);
      if (moduleFunctions == null) {
        moduleFunctions = new ArrayList<>();
        functions.put(module, moduleFunctions);
      }
      moduleFunctions.add(n);
    }
  }
}

@@ -679,20 +679,163 @@
 collect.
-List
+ArrayListMultimap;%0Aimport com.google.common.collect.ListMultimap;%0Aimport com.google.common.collect.Lists;%0Aimport com.google.common.collect.Multimap
 s;%0Aimpor
@@ -935,62 +935,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.HashMap;%0A
 impo
@@ -958,30 +958,8 @@
 st;%0A
-import java.util.Map;%0A
 impo
@@ -1559,17 +1559,26 @@
 e final 
-M
+ListMultim
 ap%3CJSMod
@@ -1582,27 +1582,21 @@
 Module, 
-List%3C
 Node%3E
-%3E
  functio
@@ -1706,21 +1706,32 @@
 s = 
-new HashMap%3C%3E
+ArrayListMultimap.create
 ();%0A
@@ -1894,16 +1894,32 @@
 entry : 
+Multimaps.asMap(
 function
@@ -1919,16 +1919,17 @@
 unctions
+)
 .entrySe
@@ -1939,48 +1939,8 @@
 ) %7B%0A
-      JSModule module = entry.getKey();%0A
     
@@ -1992,22 +1992,30 @@
 sertion(
-module
+entry.getKey()
 );%0A     
@@ -2572,263 +2572,37 @@
     
-JSModule module = t.getModule();%0A      List%3CNode%3E moduleFunctions = functions.get(module);%0A      if (moduleFunctions == null) %7B%0A        moduleFunctions = new ArrayList%3C%3E();%0A        functions.put(module, moduleFunctions);%0A      %7D%0A      moduleFunctions.add(
+functions.put(t.getModule(), 
 n);%0A

Use a ListMultimap to simplify the code ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=110178728