Debian Codegraph @

src/com/google/javascript/jscomp/NameAnonymousFunctions.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.javascript.rhino.Node;
20
21 import java.util.logging.*;
22
23 /**
24 * Gives anonymous function names. This makes it way easier to debug because
25 * debuggers and stack traces use the function names. So if you have
26 *
27 * goog.string.htmlEscape = function(str) {
28 * }
29 *
30 * It will become
31 *
32 * goog.string.htmlEscape = function $goog$string$htmlEscape$(str) {
33 * }
34 *
35 */
36 class NameAnonymousFunctions implements CompilerPass {
37 private static final Logger logger = Logger.getLogger(
38 NameAnonymousFunctions.class.getName());
39
40 static final char DELIMITER = '$';
41
42 private final AbstractCompiler compiler;
43
44 private int namedCount = 0;
45 private int bytesUsed = 0;
46
47 NameAnonymousFunctions(AbstractCompiler compiler) {
48 this.compiler = compiler;
49 }
50
51 @Override
52 public void process(Node externs, Node root) {
53 AnonymousFunctionNamingCallback namingCallback =
54 new AnonymousFunctionNamingCallback(new AnonymousFunctionNamer());
55 NodeTraversal.traverse(compiler, root, namingCallback);
56 logger.fine("Named " + namedCount + " anon functions using " +
57 bytesUsed + " bytes");
58 }
59
60 /**
61 * Names anonymous functions. The function names don't have to be globally
62 * unique or even locally unique. We make them somewhat unique because of a
63 * bug in IE (and there may be other bugs we haven't found). See unit test for
64 * more info.
65 */
66 private class AnonymousFunctionNamer
67 implements AnonymousFunctionNamingCallback.FunctionNamer {
68 private NodeNameExtractor nameExtractor;
69
70 AnonymousFunctionNamer() {
71 this.nameExtractor = new NodeNameExtractor(DELIMITER);
72 }
73
74 /**
75 * Returns a likely not conflicting name to make IE happy. See unit test
76 * for more info.
77 */
78 private String getLikelyNonConflictingName(String name) {
79 return DELIMITER + name + DELIMITER;
80 }
81
82 @Override
83 public final String getName(Node node) {
84 return nameExtractor.getName(node);
85 }
86
87 @Override
88 public final void setFunctionName(String name, Node fnNode) {
89 Node fnNameNode = fnNode.getFirstChild();
90 String uniqueName = getLikelyNonConflictingName(name);
91 fnNameNode.setString(uniqueName);
92 compiler.reportCodeChange();
93 namedCount++;
94 bytesUsed += uniqueName.length();
95 }
96
97 @Override
98 public final String getCombinedName(String lhs, String rhs) {
99 return lhs + DELIMITER + rhs;
100 }
101 }
102 }