| 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.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 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class ComposeWarningsGuard extends WarningsGuard { |
| 41 |
|
| 42 |
private static final long serialVersionUID = 1L; |
| 43 |
|
| 44 |
|
| 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 |
|
| 69 |
|
| 70 |
return orderOfAddition.get(b).intValue() - |
| 71 |
orderOfAddition.get(a).intValue(); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 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 |
|
| 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 |
|
| 146 |
|
| 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 |
|
| 167 |
|
| 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 |
} |