Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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.base.Preconditions;
20 import com.google.common.collect.Lists;
21 import com.google.common.collect.Sets;
22 import com.google.javascript.jscomp.CheckLevel;
23 import com.google.javascript.jscomp.NodeTraversal.Callback;
24 import com.google.javascript.rhino.JSDocInfo;
25 import com.google.javascript.rhino.Node;
26 import com.google.javascript.rhino.Token;
27
28 import java.util.List;
29 import java.util.Set;
30
31
32 /**
33 * This pass walks the AST to create a Collection of 'new' nodes and
34 * 'goog.require' nodes. It reconciles these Collections, creating a
35 * warning for each discrepancy.
36 *
37 */
38 class CheckRequiresForConstructors implements HotSwapCompilerPass {
39 private final AbstractCompiler compiler;
40 private final CodingConvention codingConvention;
41 private final CheckLevel level;
42
43 // Warnings
44 static final DiagnosticType MISSING_REQUIRE_WARNING = DiagnosticType.disabled(
45 "JSC_MISSING_REQUIRE_WARNING",
46 "''{0}'' used but not goog.require''d");
47
48 CheckRequiresForConstructors(AbstractCompiler compiler,
49 CheckLevel level) {
50 this.compiler = compiler;
51 this.codingConvention = compiler.getCodingConvention();
52 this.level = level;
53 }
54
55 /**
56 * Uses Collections of new and goog.require nodes to create a compiler warning
57 * for each new class name without a corresponding goog.require().
58 */
59 @Override
60 public void process(Node externs, Node root) {
61 Callback callback = new CheckRequiresForConstructorsCallback();
62 new NodeTraversal(compiler, callback).traverseRoots(externs, root);
63 }
64
65 @Override
66 public void hotSwapScript(Node scriptRoot, Node originalRoot) {
67 Callback callback = new CheckRequiresForConstructorsCallback();
68 new NodeTraversal(compiler, callback).traverseWithScope(scriptRoot,
69 SyntacticScopeCreator.generateUntypedTopScope(compiler));
70 }
71
72 // Return true if the name is a class name (starts with an uppercase
73 // character, but is not in all caps).
74 private static boolean isClassName(String name) {
75 return (name != null && name.length() > 1
76 && Character.isUpperCase(name.charAt(0))
77 && !name.equals(name.toUpperCase()));
78 }
79
80 // Return the shortest prefix of the className that refers to a class,
81 // or null if no part refers to a class.
82 private static String getOutermostClassName(String className) {
83 for (String part : className.split("\\.")) {
84 if (isClassName(part)) {
85 return className.substring(0, className.indexOf(part) +
86 part.length());
87 }
88 }
89
90 return null;
91 }
92
93 /**
94 * This class "records" each constructor and goog.require visited and creates
95 * a warning for each new node without an appropriate goog.require node.
96 *
97 */
98 private class CheckRequiresForConstructorsCallback implements Callback {
99 private final List<String> constructors = Lists.newArrayList();
100 private final List<String> requires = Lists.newArrayList();
101 private final List<Node> newNodes = Lists.newArrayList();
102
103 @Override
104 public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
105 return parent == null || !parent.isScript() ||
106 !t.getInput().isExtern();
107 }
108
109 @Override
110 public void visit(NodeTraversal t, Node n, Node parent) {
111 JSDocInfo info;
112 switch (n.getType()) {
113 case Token.ASSIGN:
114 info = (JSDocInfo) n.getProp(Node.JSDOC_INFO_PROP);
115 if (info != null && info.isConstructor()) {
116 String qualifiedName = n.getFirstChild().getQualifiedName();
117 constructors.add(qualifiedName);
118 }
119 break;
120 case Token.FUNCTION:
121 if (NodeUtil.isFunctionExpression(n)) {
122 if (parent.isName()) {
123 String functionName = parent.getString();
124 info = (JSDocInfo) parent.getProp(Node.JSDOC_INFO_PROP);
125 if (info != null && info.isConstructor()) {
126 constructors.add(functionName);
127 } else {
128 Node gramps = parent.getParent();
129 Preconditions.checkState(
130 gramps != null && gramps.isVar());
131 info = (JSDocInfo) gramps.getProp(Node.JSDOC_INFO_PROP);
132 if (info != null && info.isConstructor()) {
133 constructors.add(functionName);
134 }
135 }
136 }
137 } else {
138 info = (JSDocInfo) n.getProp(Node.JSDOC_INFO_PROP);
139 if (info != null && info.isConstructor()) {
140 String functionName = n.getFirstChild().getString();
141 constructors.add(functionName);
142 }
143 }
144 break;
145 case Token.CALL:
146 visitCallNode(n, parent);
147 break;
148 case Token.SCRIPT:
149 visitScriptNode(t);
150 break;
151 case Token.NEW:
152 visitNewNode(t, n);
153 }
154 }
155
156 private void visitScriptNode(NodeTraversal t) {
157 Set<String> classNames = Sets.newHashSet();
158 for (Node node : newNodes) {
159 String className = node.getFirstChild().getQualifiedName();
160 String outermostClassName = getOutermostClassName(className);
161 boolean notProvidedByConstructors =
162 (constructors == null || !constructors.contains(className));
163 boolean notProvidedByRequires =
164 (requires == null || (!requires.contains(className)
165 && !requires.contains(outermostClassName)));
166 if (notProvidedByConstructors && notProvidedByRequires
167 && !classNames.contains(className)) {
168 compiler.report(
169 t.makeError(node, level, MISSING_REQUIRE_WARNING, className));
170 classNames.add(className);
171 }
172 }
173 // for the next script, if there is one, we don't want the new, ctor, and
174 // require nodes to spill over.
175 this.newNodes.clear();
176 this.requires.clear();
177 this.constructors.clear();
178 }
179
180 private void visitCallNode(Node n, Node parent) {
181 String required = codingConvention.extractClassNameIfRequire(n, parent);
182 if (required != null) {
183 requires.add(required);
184 }
185 }
186
187 private void visitNewNode(NodeTraversal t, Node n) {
188 Node qNameNode = n.getFirstChild();
189
190 // If the ctor is something other than a qualified name, ignore it.
191 if (!qNameNode.isQualifiedName()) {
192 return;
193 }
194
195 // Grab the root ctor namespace.
196 Node nameNode = qNameNode;
197 for (; nameNode.hasChildren(); nameNode = nameNode.getFirstChild()) {}
198
199 // We only consider programmer-defined constructors that are
200 // global variables, or are defined on global variables.
201 if (!nameNode.isName()) {
202 return;
203 }
204
205 String name = nameNode.getString();
206 Scope.Var var = t.getScope().getVar(name);
207 if (var == null || var.isLocal() || var.isExtern()) {
208 return;
209 }
210 newNodes.add(n);
211 }
212 }
213 }