Debian Codegraph @

src/com/google/javascript/jscomp/ControlStructureCheck.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.javascript.rhino.JSDocInfo;
20 import com.google.javascript.rhino.Node;
21 import com.google.javascript.rhino.Token;
22
23 /**
24 * Check for usage of 'with'.
25 *
26 */
27 class ControlStructureCheck implements HotSwapCompilerPass {
28
29 private final AbstractCompiler compiler;
30
31 static final DiagnosticType USE_OF_WITH = DiagnosticType.warning(
32 "JSC_USE_OF_WITH",
33 "The use of the 'with' structure should be avoided.");
34
35 ControlStructureCheck(AbstractCompiler compiler) {
36 this.compiler = compiler;
37 }
38
39 @Override
40 public void process(Node externs, Node root) {
41 check(root);
42 }
43
44 @Override
45 public void hotSwapScript(Node scriptRoot, Node originalRoot) {
46 check(scriptRoot);
47 }
48
49 /**
50 * Reports errors for any invalid use of control structures.
51 *
52 * @param node Current node to check.
53 */
54 private void check(Node node) {
55 switch (node.getType()) {
56 case Token.WITH:
57 JSDocInfo info = node.getJSDocInfo();
58 boolean allowWith =
59 info != null && info.getSuppressions().contains("with");
60 if (!allowWith) {
61 report(node, USE_OF_WITH);
62 }
63 break;
64 }
65
66 for (Node bChild = node.getFirstChild(); bChild != null;) {
67 Node next = bChild.getNext();
68 check(bChild);
69 bChild = next;
70 }
71 }
72
73 private void report(Node n, DiagnosticType error) {
74 compiler.report(JSError.make(n.getSourceFileName(), n, error));
75 }
76 }