| 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.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 |
|
| 28 |
|
| 29 |
|
| 30 |
class SuppressDocWarningsGuard extends WarningsGuard { |
| 31 |
private static final long serialVersionUID = 1L; |
| 32 |
|
| 33 |
|
| 34 |
private final Map<String, DiagnosticGroupWarningsGuard> suppressors = |
| 35 |
Maps.newHashMap(); |
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 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 |
|
| 89 |
|
| 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 |
|
| 106 |
|
| 107 |
return WarningsGuard.Priority.SUPPRESS_DOC.value; |
| 108 |
} |
| 109 |
} |