| 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.javascript.jscomp.NodeTraversal.Callback; |
| 20 |
import com.google.javascript.rhino.JSDocInfo; |
| 21 |
import com.google.javascript.rhino.Node; |
| 22 |
import com.google.javascript.rhino.Token; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
final class CheckGlobalThis implements Callback { |
| 57 |
|
| 58 |
static final DiagnosticType GLOBAL_THIS = DiagnosticType.warning( |
| 59 |
"JSC_USED_GLOBAL_THIS", |
| 60 |
"dangerous use of the global 'this' object"); |
| 61 |
|
| 62 |
private final AbstractCompiler compiler; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private Node assignLhsChild = null; |
| 70 |
|
| 71 |
CheckGlobalThis(AbstractCompiler compiler) { |
| 72 |
this.compiler = compiler; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
@Override |
| 80 |
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { |
| 81 |
|
| 82 |
if (n.isFunction()) { |
| 83 |
|
| 84 |
|
| 85 |
JSDocInfo jsDoc = getFunctionJsDocInfo(n); |
| 86 |
if (jsDoc != null && |
| 87 |
(jsDoc.isConstructor() || |
| 88 |
jsDoc.isInterface() || |
| 89 |
jsDoc.hasThisType() || |
| 90 |
jsDoc.isOverride())) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
int pType = parent.getType(); |
| 101 |
if (!(pType == Token.BLOCK || |
| 102 |
pType == Token.SCRIPT || |
| 103 |
pType == Token.NAME || |
| 104 |
pType == Token.ASSIGN || |
| 105 |
|
| 106 |
|
| 107 |
pType == Token.STRING_KEY)) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
Node gramps = parent.getParent(); |
| 113 |
if (NodeUtil.isObjectLitKey(parent)) { |
| 114 |
JSDocInfo maybeLends = gramps.getJSDocInfo(); |
| 115 |
if (maybeLends != null && |
| 116 |
maybeLends.getLendsName() != null && |
| 117 |
maybeLends.getLendsName().endsWith(".prototype")) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if (parent != null && parent.isAssign()) { |
| 124 |
Node lhs = parent.getFirstChild(); |
| 125 |
|
| 126 |
if (n == lhs) { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
if (assignLhsChild == null) { |
| 131 |
assignLhsChild = lhs; |
| 132 |
} |
| 133 |
} else { |
| 134 |
|
| 135 |
|
| 136 |
if (NodeUtil.isGet(lhs)) { |
| 137 |
if (lhs.isGetProp() && |
| 138 |
lhs.getLastChild().getString().equals("prototype")) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
Node llhs = lhs.getFirstChild(); |
| 142 |
if (llhs.isGetProp() && |
| 143 |
llhs.getLastChild().getString().equals("prototype")) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
@Override |
| 154 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 155 |
if (n.isThis() && shouldReportThis(n)) { |
| 156 |
compiler.report(t.makeError(n, GLOBAL_THIS)); |
| 157 |
} |
| 158 |
if (n == assignLhsChild) { |
| 159 |
assignLhsChild = null; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
private boolean shouldReportThis(Node n) { |
| 164 |
Node parent = n.getParent(); |
| 165 |
if (assignLhsChild != null) { |
| 166 |
|
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
return parent != null && NodeUtil.isGet(parent); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
private JSDocInfo getFunctionJsDocInfo(Node n) { |
| 185 |
JSDocInfo jsDoc = n.getJSDocInfo(); |
| 186 |
Node parent = n.getParent(); |
| 187 |
if (jsDoc == null) { |
| 188 |
int parentType = parent.getType(); |
| 189 |
if (parentType == Token.NAME || parentType == Token.ASSIGN) { |
| 190 |
jsDoc = parent.getJSDocInfo(); |
| 191 |
if (jsDoc == null && parentType == Token.NAME) { |
| 192 |
Node gramps = parent.getParent(); |
| 193 |
if (gramps.isVar()) { |
| 194 |
jsDoc = gramps.getJSDocInfo(); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
return jsDoc; |
| 200 |
} |
| 201 |
} |