Debian Codegraph @

src/com/google/javascript/jscomp/AnonymousFunctionNamingCallback.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.jscomp.NodeTraversal.AbstractPostOrderCallback;
20 import com.google.javascript.rhino.Node;
21 import com.google.javascript.rhino.Token;
22
23
24 /**
25 * Visitor that performs naming operations on anonymous functions by
26 * means of the FunctionNamer interface. Anonymous functions are
27 * named based on context. For example, the anonymous function on the
28 * RHS based on the property at the LHS of the assignment operator.
29 *
30 * goog.string.htmlEscape = function(str) {
31 * }
32 *
33 */
34 class AnonymousFunctionNamingCallback
35 extends AbstractPostOrderCallback {
36 private final FunctionNamer namer;
37
38 /**
39 * Interface used by AnonymousFunctionNamingCallback to set the name
40 * of anonymous functions.
41 */
42 interface FunctionNamer {
43 /**
44 * Generates a string representation of a node for use by
45 * setFunctionName.
46 */
47 String getName(Node node);
48
49 /**
50 * Sets the name of an anonymous function.
51 * @param fnNode The function node to update
52 * @param name The name
53 */
54 void setFunctionName(String name, Node fnNode);
55
56 /**
57 * Generate a name by "concatenating" the output of multiple calls
58 * to getName.
59 */
60 String getCombinedName(String lhs, String rhs);
61 }
62
63 AnonymousFunctionNamingCallback(FunctionNamer namer) {
64 this.namer = namer;
65 }
66
67 @Override
68 public void visit(NodeTraversal t, Node n, Node parent) {
69 switch (n.getType()) {
70 case Token.FUNCTION:
71 // this handles functions that are assigned to variables or
72 // properties
73 // e.g. goog.string.htmlEscape = function(str) {
74 // }
75
76 // get the function name and see if it's empty
77 Node functionNameNode = n.getFirstChild();
78 String functionName = functionNameNode.getString();
79 if (functionName.length() == 0) {
80 if (parent.isAssign()) {
81 // this is an assignment to a property, generally either a
82 // static function or a prototype function
83 // e.g. goog.string.htmlEscape = function() { } or
84 // goog.structs.Map.prototype.getCount = function() { }
85 Node lhs = parent.getFirstChild();
86 String name = namer.getName(lhs);
87 namer.setFunctionName(name, n);
88 } else if (parent.isName()) {
89 // this is an assignment to a variable
90 // e.g. var handler = function() {}
91 String name = namer.getName(parent);
92 namer.setFunctionName(name, n);
93 }
94 }
95 break;
96 case Token.ASSIGN:
97 // this handles functions that are assigned to a prototype through
98 // an object literal
99 // e.g. BuzzApp.prototype = {
100 // Start : function() { }
101 // }
102 Node lhs = n.getFirstChild();
103 Node rhs = lhs.getNext();
104 if (rhs.isObjectLit()) {
105 nameObjectLiteralMethods(rhs, namer.getName(lhs));
106 }
107 }
108 }
109
110 private void nameObjectLiteralMethods(Node objectLiteral, String context) {
111 for (Node keyNode = objectLiteral.getFirstChild();
112 keyNode != null;
113 keyNode = keyNode.getNext()) {
114 Node valueNode = keyNode.getFirstChild();
115
116 // Object literal keys may be STRING_KEY, GETTER_DEF, SETTER_DEF.
117 // Get and Set are skipped because they can not be named.
118 if (keyNode.isStringKey()) {
119 // concatenate the context and key name to get a new qualified name.
120 String name = namer.getCombinedName(context, namer.getName(keyNode));
121
122 int type = valueNode.getType();
123 if (type == Token.FUNCTION) {
124 // set name if function is anonymous
125 Node functionNameNode = valueNode.getFirstChild();
126 String functionName = functionNameNode.getString();
127 if (functionName.isEmpty()) {
128 namer.setFunctionName(name, valueNode);
129 }
130 } else if (type == Token.OBJECTLIT) {
131 // process nested object literal
132 nameObjectLiteralMethods(valueNode, name);
133 }
134 }
135 }
136 }
137 }