Debian Codegraph @

src/com/google/javascript/jscomp/CheckProvides.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 package com.google.javascript.jscomp;
17
18 import com.google.common.collect.Maps;
19 import com.google.javascript.jscomp.CheckLevel;
20 import com.google.javascript.jscomp.NodeTraversal.AbstractShallowCallback;
21 import com.google.javascript.rhino.JSDocInfo;
22 import com.google.javascript.rhino.JSDocInfo.Visibility;
23 import com.google.javascript.rhino.Node;
24 import com.google.javascript.rhino.Token;
25
26 import java.util.Map;
27
28 /**
29 * Insures '@constructor X' has a 'goog.provide("X")' .
30 *
31 */
32 class CheckProvides implements HotSwapCompilerPass {
33 private final AbstractCompiler compiler;
34 private final CheckLevel checkLevel;
35 private final CodingConvention codingConvention;
36
37 static final DiagnosticType MISSING_PROVIDE_WARNING = DiagnosticType.disabled(
38 "JSC_MISSING_PROVIDE",
39 "missing goog.provide(''{0}'')");
40
41 CheckProvides(AbstractCompiler compiler, CheckLevel checkLevel) {
42 this.compiler = compiler;
43 this.checkLevel = checkLevel;
44 this.codingConvention = compiler.getCodingConvention();
45 }
46
47 @Override
48 public void process(Node externs, Node root) {
49 hotSwapScript(root, null);
50 }
51
52 @Override
53 public void hotSwapScript(Node scriptRoot, Node originalRoot) {
54 CheckProvidesCallback callback =
55 new CheckProvidesCallback(codingConvention);
56 new NodeTraversal(compiler, callback).traverse(scriptRoot);
57 }
58
59 private class CheckProvidesCallback extends AbstractShallowCallback {
60 private final Map<String, Node> provides = Maps.newHashMap();
61 private final Map<String, Node> ctors = Maps.newHashMap();
62 private final CodingConvention convention;
63
64 CheckProvidesCallback(CodingConvention convention){
65 this.convention = convention;
66 }
67
68 @Override
69 public void visit(NodeTraversal t, Node n, Node parent) {
70 switch (n.getType()) {
71 case Token.CALL:
72 String providedClassName =
73 codingConvention.extractClassNameIfProvide(n, parent);
74 if (providedClassName != null) {
75 provides.put(providedClassName, n);
76 }
77 break;
78 case Token.FUNCTION:
79 visitFunctionNode(n, parent);
80 break;
81 case Token.SCRIPT:
82 visitScriptNode();
83 }
84 }
85
86 private void visitFunctionNode(Node n, Node parent) {
87 Node name = null;
88 JSDocInfo info = parent.getJSDocInfo();
89 if (info != null && info.isConstructor()) {
90 name = parent.getFirstChild();
91 } else {
92 // look to the child, maybe it's a named function
93 info = n.getJSDocInfo();
94 if (info != null && info.isConstructor()) {
95 name = n.getFirstChild();
96 }
97 }
98 if (name != null && name.isQualifiedName()) {
99 String qualifiedName = name.getQualifiedName();
100 if (!this.convention.isPrivate(qualifiedName)) {
101 Visibility visibility = info.getVisibility();
102 if (!visibility.equals(JSDocInfo.Visibility.PRIVATE)) {
103 ctors.put(qualifiedName, name);
104 }
105 }
106 }
107 }
108
109 private void visitScriptNode() {
110 for (Map.Entry<String, Node> ctorEntry : ctors.entrySet()) {
111 String ctor = ctorEntry.getKey();
112 int index = -1;
113 boolean found = false;
114 do {
115 index = ctor.indexOf('.', index +1);
116 String provideKey = index == -1 ? ctor : ctor.substring(0, index);
117 if (provides.containsKey(provideKey)) {
118 found = true;
119 break;
120 }
121 } while (index != -1);
122
123 if (!found) {
124 Node n = ctorEntry.getValue();
125 compiler.report(
126 JSError.make(n.getSourceFileName(), n,
127 checkLevel, MISSING_PROVIDE_WARNING, ctorEntry.getKey()));
128 }
129 }
130 provides.clear();
131 ctors.clear();
132 }
133 }
134 }