Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2010 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.collect.Maps;
20 import com.google.javascript.rhino.JSDocInfo;
21 import com.google.javascript.rhino.Node;
22 import com.google.javascript.rhino.Token;
23
24 import java.util.Map;
25
26 /**
27 * Filters warnings based on in-code {@code @suppress} annotations.
28 * @author [email protected] (Nick Santos)
29 */
30 class SuppressDocWarningsGuard extends WarningsGuard {
31 private static final long serialVersionUID = 1L;
32
33 /** Warnings guards for each suppressible warnings group, indexed by name. */
34 private final Map<String, DiagnosticGroupWarningsGuard> suppressors =
35 Maps.newHashMap();
36
37 /**
38 * The suppressible groups, indexed by name.
39 */
40 SuppressDocWarningsGuard(Map<String, DiagnosticGroup> suppressibleGroups) {
41 for (Map.Entry<String, DiagnosticGroup> entry :
42 suppressibleGroups.entrySet()) {
43 suppressors.put(
44 entry.getKey(),
45 new DiagnosticGroupWarningsGuard(
46 entry.getValue(),
47 CheckLevel.OFF));
48 }
49 }
50
51 @Override
52 public CheckLevel level(JSError error) {
53 Node node = error.node;
54 if (node != null) {
55 boolean visitedFunction = false;
56 for (Node current = node;
57 current != null;
58 current = current.getParent()) {
59 int type = current.getType();
60 JSDocInfo info = null;
61
62 if (type == Token.FUNCTION) {
63 info = NodeUtil.getBestJSDocInfo(current);
64 visitedFunction = true;
65 } else if (type == Token.SCRIPT) {
66 info = current.getJSDocInfo();
67 } else if (current.isVar() || current.isAssign()) {
68 // There's one edge case we're worried about:
69 // if the warning points to an assigment to a function, we
70 // want the suppressions on that function to apply.
71 // It's OK if we double-count some cases.
72 Node rhs = NodeUtil.getRValueOfLValue(current.getFirstChild());
73 if (rhs != null) {
74 if (rhs.isCast()) {
75 rhs = rhs.getFirstChild();
76 }
77
78 if (rhs.isFunction() && !visitedFunction) {
79 info = NodeUtil.getBestJSDocInfo(current);
80 }
81 }
82 }
83
84 if (info != null) {
85 for (String suppressor : info.getSuppressions()) {
86 WarningsGuard guard = suppressors.get(suppressor);
87
88 // Some @suppress tags are for other tools, and
89 // may not have a warnings guard.
90 if (guard != null) {
91 CheckLevel newLevel = guard.level(error);
92 if (newLevel != null) {
93 return newLevel;
94 }
95 }
96 }
97 }
98 }
99 }
100 return null;
101 }
102
103 @Override
104 public int getPriority() {
105 // Happens after path-based filtering, but before other times
106 // of filtering.
107 return WarningsGuard.Priority.SUPPRESS_DOC.value;
108 }
109 }