| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class CheckRequiresForConstructors implements HotSwapCompilerPass { |
| 39 |
private final AbstractCompiler compiler; |
| 40 |
private final CodingConvention codingConvention; |
| 41 |
private final CheckLevel level; |
| 42 |
|
| 43 |
|
| 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 |
|
| 57 |
|
| 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 |
|
| 73 |
|
| 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 |
|
| 81 |
|
| 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 |
|
| 95 |
|
| 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 |
|
| 174 |
|
| 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 |
|
| 191 |
if (!qNameNode.isQualifiedName()) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
Node nameNode = qNameNode; |
| 197 |
for (; nameNode.hasChildren(); nameNode = nameNode.getFirstChild()) {} |
| 198 |
|
| 199 |
|
| 200 |
|
| 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 |
} |