Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2007 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.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 * Checks for certain uses of the {@code this} keyword that are considered
26 * unsafe because they are likely to reference the global {@code this} object
27 * unintentionally.
28 *
29 * <p>A use of {@code this} is considered unsafe if it's on the left side of an
30 * assignment or a property access, and not inside one of the following:
31 * <ol>
32 * <li>a prototype method
33 * <li>a function annotated with {@code @constructor}
34 * <li>a function annotated with {@code @this}.
35 * <li>a function where there's no logical place to put a
36 * {@code this} annotation.
37 * </ol>
38 *
39 * <p>Note that this check does not track assignments of {@code this} to
40 * variables or objects. The code
41 * <pre>
42 * function evil() {
43 * var a = this;
44 * a.useful = undefined;
45 * }
46 * </pre>
47 * will not get flagged, even though it is semantically equivalent to
48 * <pre>
49 * function evil() {
50 * this.useful = undefined;
51 * }
52 * </pre>
53 * which would get flagged.
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 * If {@code assignLhsChild != null}, then the node being traversed is
66 * a descendant of the first child of an ASSIGN node. assignLhsChild's
67 * parent is this ASSIGN node.
68 */
69 private Node assignLhsChild = null;
70
71 CheckGlobalThis(AbstractCompiler compiler) {
72 this.compiler = compiler;
73 }
74
75 /**
76 * Since this pass reports errors only when a global {@code this} keyword
77 * is encountered, there is no reason to traverse non global contexts.
78 */
79 @Override
80 public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
81
82 if (n.isFunction()) {
83 // Don't traverse functions that are constructors or have the @this
84 // or @override annotation.
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 // Don't traverse functions unless they would normally
95 // be able to have a @this annotation associated with them. e.g.,
96 // var a = function() { }; // or
97 // function a() {} // or
98 // a.x = function() {}; // or
99 // var a = {x: function() {}};
100 int pType = parent.getType();
101 if (!(pType == Token.BLOCK ||
102 pType == Token.SCRIPT ||
103 pType == Token.NAME ||
104 pType == Token.ASSIGN ||
105
106 // object literal keys
107 pType == Token.STRING_KEY)) {
108 return false;
109 }
110
111 // Don't traverse functions that are getting lent to a prototype.
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 // Always traverse the left side of the assignment. To handle
128 // nested assignments properly (e.g., (a = this).property = c;),
129 // assignLhsChild should not be overridden.
130 if (assignLhsChild == null) {
131 assignLhsChild = lhs;
132 }
133 } else {
134 // Only traverse the right side if it's not an assignment to a prototype
135 // property or subproperty.
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 // Always report a THIS on the left side of an assign.
167 return true;
168 }
169
170 // Also report a THIS with a property access.
171 return parent != null && NodeUtil.isGet(parent);
172 }
173
174 /**
175 * Gets a function's JSDoc information, if it has any. Checks for a few
176 * patterns (ellipses show where JSDoc would be):
177 * <pre>
178 * ... function() {}
179 * ... x = function() {};
180 * var ... x = function() {};
181 * ... var x = function() {};
182 * </pre>
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 }