Debian Codegraph @

src/com/google/javascript/jscomp/NameAnonymousFunctionsMapped.java

raw symbols SCIP
1 /*
2 * Copyright 2004 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.collect.Maps;
21 import com.google.javascript.rhino.Node;
22 import com.google.javascript.rhino.Token;
23
24 import java.util.*;
25 import java.util.logging.*;
26
27 /**
28 * Gives anonymous function names that are optimized to be small and provides a
29 * mapping back to the original names. This makes it way easier to debug because
30 * debuggers and stack traces use the function names. So if you have
31 *
32 * goog.string.htmlEscape = function(str) {
33 * }
34 *
35 * It will become
36 *
37 * goog.string.htmlEscape = function $qv(str) {
38 * }
39 *
40 * And there will be mapping from $qv to goog.string.htmlEscape
41 *
42 */
43 class NameAnonymousFunctionsMapped implements CompilerPass {
44
45 private static Logger logger = Logger.getLogger(
46 NameAnonymousFunctionsMapped.class.getName());
47
48 static final char PREFIX = '$';
49 static final String PREFIX_STRING = "$";
50
51 private final AbstractCompiler compiler;
52 private final NameGenerator nameGenerator;
53 private final VariableMap previousMap;
54 private final Map<String, String> renameMap;
55
56 private int namedCount = 0;
57 private int bytesUsed = 0;
58
59 NameAnonymousFunctionsMapped(
60 AbstractCompiler compiler, VariableMap previousMap) {
61 this.compiler = compiler;
62 Set<String> reserved =
63 previousMap != null ?
64 previousMap.getNewNameToOriginalNameMap().keySet() :
65 Collections.<String>emptySet();
66 this.nameGenerator = new NameGenerator(reserved, PREFIX_STRING, null);
67 this.previousMap = previousMap;
68 this.renameMap = Maps.newHashMap();
69 }
70
71 @Override
72 public void process(Node externs, Node root) {
73 AnonymousFunctionNamingCallback namingCallback =
74 new AnonymousFunctionNamingCallback(new MappedFunctionNamer());
75 NodeTraversal.traverse(compiler, root, namingCallback);
76 logger.fine("Named " + namedCount + " anon functions using " +
77 bytesUsed + " bytes");
78 if (namedCount > 0) {
79 compiler.reportCodeChange();
80 }
81 }
82
83 /**
84 * Names anonymous functions. The function names don't have to be globally
85 * unique or even locally unique. We make them somewhat unique because of a
86 * bug in IE (and there may be other bugs we haven't found). See unit test for
87 * more info.
88 */
89 private class MappedFunctionNamer
90 implements AnonymousFunctionNamingCallback.FunctionNamer {
91 static final char DELIMITER = '.';
92
93 @Override
94 public final String getName(Node node) {
95 switch (node.getType()) {
96 case Token.NAME:
97 case Token.STRING:
98 case Token.STRING_KEY:
99 return node.getString();
100 default:
101 return new CodePrinter.Builder(node).build();
102 }
103 }
104
105 @Override
106 public final void setFunctionName(String name, Node fnNode) {
107 Node fnNameNode = fnNode.getFirstChild();
108 String newName = getAlternateName(name);
109 fnNameNode.setString(newName);
110 namedCount++;
111 bytesUsed += newName.length();
112 }
113
114 String getAlternateName(String name) {
115 String newName = renameMap.get(name);
116 if (newName == null) {
117 // Use the previously used name, if possible.
118 if (previousMap != null) {
119 newName = previousMap.lookupNewName(name);
120 }
121 if (newName == null) {
122 // otherwise generate a new name.
123 newName = nameGenerator.generateNextName();
124 }
125 renameMap.put(name, newName);
126 }
127 return newName;
128 }
129
130 @Override
131 public final String getCombinedName(String lhs, String rhs) {
132 return lhs + DELIMITER + rhs;
133 }
134 }
135
136 /**
137 * Gets the function renaming map (the "answer key").
138 *
139 * @return A mapping from original names to new names
140 */
141 VariableMap getFunctionMap() {
142 return new VariableMap(ImmutableMap.copyOf(renameMap));
143 }
144 }