Debian Codegraph @

src/com/google/javascript/jscomp/ComposeWarningsGuard.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.Joiner;
20 import com.google.common.collect.Lists;
21 import com.google.common.collect.Maps;
22 import com.google.javascript.jscomp.CheckLevel;
23
24 import java.io.Serializable;
25 import java.util.*;
26 import java.util.Map;
27 import java.util.TreeSet;
28
29 /**
30 * WarningsGuard that represents just a chain of other guards. For example we
31 * could have following chain
32 * 1) all warnings outside of /foo/ should be suppressed
33 * 2) errors with key JSC_BAR should be marked as warning
34 * 3) the rest should be reported as error
35 *
36 * This class is designed for such behavior.
37 *
38 * @author [email protected] (Anatol Pomazau)
39 */
40 public class ComposeWarningsGuard extends WarningsGuard {
41
42 private static final long serialVersionUID = 1L;
43
44 // The order that the guards were added in.
45 private final Map<WarningsGuard, Integer> orderOfAddition = Maps.newHashMap();
46 private int numberOfAdds = 0;
47
48 private final Comparator<WarningsGuard> guardComparator =
49 new GuardComparator(orderOfAddition);
50 private boolean demoteErrors = false;
51
52 private static class GuardComparator
53 implements Comparator<WarningsGuard>, Serializable {
54 private static final long serialVersionUID = 1L;
55
56 private final Map<WarningsGuard, Integer> orderOfAddition;
57 private GuardComparator(Map<WarningsGuard, Integer> orderOfAddition) {
58 this.orderOfAddition = orderOfAddition;
59 }
60
61 @Override
62 public int compare(WarningsGuard a, WarningsGuard b) {
63 int priorityDiff = a.getPriority() - b.getPriority();
64 if (priorityDiff != 0) {
65 return priorityDiff;
66 }
67
68 // If the warnings guards have the same priority, the one that
69 // was added last wins.
70 return orderOfAddition.get(b).intValue() -
71 orderOfAddition.get(a).intValue();
72 }
73 }
74
75 // The order that the guards are applied in.
76 private final TreeSet<WarningsGuard> guards =
77 new TreeSet<WarningsGuard>(guardComparator);
78
79 public ComposeWarningsGuard(List<WarningsGuard> guards) {
80 addGuards(guards);
81 }
82
83 public ComposeWarningsGuard(WarningsGuard... guards) {
84 this(Lists.newArrayList(guards));
85 }
86
87 void addGuard(WarningsGuard guard) {
88 if (guard instanceof ComposeWarningsGuard) {
89 ComposeWarningsGuard composeGuard = (ComposeWarningsGuard) guard;
90 if (composeGuard.demoteErrors) {
91 this.demoteErrors = composeGuard.demoteErrors;
92 }
93
94 // Reverse the guards, so that they have the same order in the result.
95 addGuards(Lists.newArrayList(composeGuard.guards.descendingSet()));
96 } else {
97 numberOfAdds++;
98 orderOfAddition.put(guard, numberOfAdds);
99 guards.remove(guard);
100 guards.add(guard);
101 }
102 }
103
104 private void addGuards(Iterable<WarningsGuard> guards) {
105 for (WarningsGuard guard : guards) {
106 addGuard(guard);
107 }
108 }
109
110 @Override
111 public CheckLevel level(JSError error) {
112 for (WarningsGuard guard : guards) {
113 CheckLevel newLevel = guard.level(error);
114 if (newLevel != null) {
115 if (demoteErrors && newLevel == CheckLevel.ERROR) {
116 return CheckLevel.WARNING;
117 }
118 return newLevel;
119 }
120 }
121 return null;
122 }
123
124 @Override
125 public boolean disables(DiagnosticGroup group) {
126 nextSingleton:
127 for (DiagnosticType type : group.getTypes()) {
128 DiagnosticGroup singleton = DiagnosticGroup.forType(type);
129
130 for (WarningsGuard guard : guards) {
131 if (guard.disables(singleton)) {
132 continue nextSingleton;
133 } else if (guard.enables(singleton)) {
134 return false;
135 }
136 }
137
138 return false;
139 }
140
141 return true;
142 }
143
144 /**
145 * Determines whether this guard will "elevate" the status of any disabled
146 * diagnostic type in the group to a warning or an error.
147 */
148 @Override
149 public boolean enables(DiagnosticGroup group) {
150 for (WarningsGuard guard : guards) {
151 if (guard.enables(group)) {
152 return true;
153 } else if (guard.disables(group)) {
154 return false;
155 }
156 }
157
158 return false;
159 }
160
161 List<WarningsGuard> getGuards() {
162 return Collections.unmodifiableList(Lists.newArrayList(guards));
163 }
164
165 /**
166 * Make a warnings guard that's the same as this one but with
167 * all escalating guards turned down.
168 */
169 ComposeWarningsGuard makeEmergencyFailSafeGuard() {
170 ComposeWarningsGuard safeGuard = new ComposeWarningsGuard();
171 safeGuard.demoteErrors = true;
172 for (WarningsGuard guard : guards.descendingSet()) {
173 safeGuard.addGuard(guard);
174 }
175 return safeGuard;
176 }
177
178 @Override
179 public String toString() {
180 return Joiner.on(", ").join(guards);
181 }
182 }