Debian Codegraph @

src/com/google/javascript/jscomp/NodeNameExtractor.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 import com.google.javascript.rhino.Token;
21 import com.google.javascript.rhino.TokenStream;
22
23 /**
24 * Utility class that extracts the qualified name out of a node.
25 * Useful when trying to get a human-friendly string representation of
26 * a property node that can be used to describe the node or name
27 * related nodes based on it (as done by the NameAnonymousFunctions
28 * compiler pass).
29 *
30 */
31 class NodeNameExtractor {
32 private final char delimiter;
33 private int nextUniqueInt = 0;
34
35 NodeNameExtractor(char delimiter) {
36 this.delimiter = delimiter;
37 }
38
39 /**
40 * Returns a qualified name of the specified node. Dots and brackets
41 * are changed to the delimiter passed in when constructing the
42 * NodeNameExtractor object. We also replace ".prototype" with the
43 * delimiter to keep names short, while still differentiating them
44 * from static properties. (Prototype properties will end up
45 * looking like "a$b$$c" if this.delimiter = '$'.)
46 */
47 String getName(Node node) {
48 switch (node.getType()) {
49 case Token.FUNCTION:
50 Node functionNameNode = node.getFirstChild();
51 return functionNameNode.getString();
52 case Token.GETPROP:
53 Node lhsOfDot = node.getFirstChild();
54 Node rhsOfDot = lhsOfDot.getNext();
55 String lhsOfDotName = getName(lhsOfDot);
56 String rhsOfDotName = getName(rhsOfDot);
57 if ("prototype".equals(rhsOfDotName)) {
58 return lhsOfDotName + delimiter;
59 } else {
60 return lhsOfDotName + delimiter + rhsOfDotName;
61 }
62 case Token.GETELEM:
63 Node outsideBrackets = node.getFirstChild();
64 Node insideBrackets = outsideBrackets.getNext();
65 String nameOutsideBrackets = getName(outsideBrackets);
66 String nameInsideBrackets = getName(insideBrackets);
67 if ("prototype".equals(nameInsideBrackets)) {
68 return nameOutsideBrackets + delimiter;
69 } else {
70 return nameOutsideBrackets + delimiter + nameInsideBrackets;
71 }
72 case Token.NAME:
73 return node.getString();
74 case Token.STRING:
75 case Token.STRING_KEY:
76 return TokenStream.isJSIdentifier(node.getString()) ?
77 node.getString() : ("__" + nextUniqueInt++);
78 case Token.NUMBER:
79 return NodeUtil.getStringValue(node);
80 case Token.THIS:
81 return "this";
82 case Token.CALL:
83 return getName(node.getFirstChild());
84 default:
85 StringBuilder sb = new StringBuilder();
86 for (Node child = node.getFirstChild(); child != null;
87 child = child.getNext()) {
88 if (sb.length() > 0) {
89 sb.append(delimiter);
90 }
91 sb.append(getName(child));
92 }
93 return sb.toString();
94 }
95 }
96 }