Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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 package com.google.javascript.jscomp;
17
18 import com.google.common.collect.Lists;
19 import com.google.common.collect.Sets;
20 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
21 import com.google.javascript.jscomp.Scope.Var;
22 import com.google.javascript.rhino.Node;
23 import com.google.javascript.rhino.Token;
24
25 import java.util.Set;
26
27 /**
28 * Checks that the code obeys the static restrictions of strict mode:
29 * <ol>
30 * <li> No use of "with".
31 * <li> No deleting variables, functions, or arguments.
32 * <li> No re-declarations or assignments of "eval" or arguments.
33 * <li> No use of "eval" (optional check for Caja).
34 * </ol>
35 *
36 */
37 class StrictModeCheck extends AbstractPostOrderCallback
38 implements CompilerPass {
39
40 static final DiagnosticType UNKNOWN_VARIABLE = DiagnosticType.warning(
41 "JSC_UNKNOWN_VARIABLE", "unknown variable {0}");
42
43 static final DiagnosticType EVAL_USE = DiagnosticType.error(
44 "JSC_EVAL_USE", "\"eval\" cannot be used in Caja");
45
46 static final DiagnosticType EVAL_DECLARATION = DiagnosticType.warning(
47 "JSC_EVAL_DECLARATION",
48 "\"eval\" cannot be redeclared in ES5 strict mode");
49
50 static final DiagnosticType EVAL_ASSIGNMENT = DiagnosticType.warning(
51 "JSC_EVAL_ASSIGNMENT",
52 "the \"eval\" object cannot be reassigned in ES5 strict mode");
53
54 static final DiagnosticType ARGUMENTS_DECLARATION = DiagnosticType.warning(
55 "JSC_ARGUMENTS_DECLARATION",
56 "\"arguments\" cannot be redeclared in ES5 strict mode");
57
58 static final DiagnosticType ARGUMENTS_ASSIGNMENT = DiagnosticType.warning(
59 "JSC_ARGUMENTS_ASSIGNMENT",
60 "the \"arguments\" object cannot be reassigned in ES5 strict mode");
61
62 static final DiagnosticType DELETE_VARIABLE = DiagnosticType.warning(
63 "JSC_DELETE_VARIABLE",
64 "variables, functions, and arguments cannot be deleted in "
65 + "ES5 strict mode");
66
67 static final DiagnosticType ILLEGAL_NAME = DiagnosticType.error(
68 "JSC_ILLEGAL_NAME",
69 "identifiers ending in '__' cannot be used in Caja");
70
71 static final DiagnosticType DUPLICATE_OBJECT_KEY = DiagnosticType.warning(
72 "JSC_DUPLICATE_OBJECT_KEY",
73 "object literals cannot contain duplicate keys in ES5 strict mode");
74
75 static final DiagnosticType BAD_FUNCTION_DECLARATION = DiagnosticType.error(
76 "JSC_BAD_FUNCTION_DECLARATION",
77 "functions can only be declared at top level or immediately within " +
78 "another function in ES5 strict mode");
79
80 private final AbstractCompiler compiler;
81 private final boolean noVarCheck;
82 private final boolean noCajaChecks;
83
84 StrictModeCheck(AbstractCompiler compiler) {
85 this(compiler, false, false);
86 }
87
88 StrictModeCheck(
89 AbstractCompiler compiler, boolean noVarCheck, boolean noCajaChecks) {
90 this.compiler = compiler;
91 this.noVarCheck = noVarCheck;
92 this.noCajaChecks = noCajaChecks;
93 }
94
95 @Override public void process(Node externs, Node root) {
96 NodeTraversal.traverseRoots(
97 compiler, Lists.newArrayList(externs, root), this);
98 NodeTraversal.traverse(compiler, root, new NonExternChecks());
99 }
100
101 @Override public void visit(NodeTraversal t, Node n, Node parent) {
102 if (n.isFunction()) {
103 checkFunctionUse(t, n);
104 } else if (n.isName()) {
105 if (!isDeclaration(n)) {
106 checkNameUse(t, n);
107 }
108 } else if (n.isAssign()) {
109 checkAssignment(t, n);
110 } else if (n.isDelProp()) {
111 checkDelete(t, n);
112 } else if (n.isObjectLit()) {
113 checkObjectLiteral(t, n);
114 } else if (n.isLabel()) {
115 checkLabel(t, n);
116 }
117 }
118
119 /** Checks that the function is used legally. */
120 private void checkFunctionUse(NodeTraversal t, Node n) {
121 if (NodeUtil.isFunctionDeclaration(n) && !NodeUtil.isHoistedFunctionDeclaration(n)) {
122 t.report(n, BAD_FUNCTION_DECLARATION);
123 }
124 }
125
126 /**
127 * Determines if the given name is a declaration, which can be a declaration
128 * of a variable, function, or argument.
129 */
130 private static boolean isDeclaration(Node n) {
131 switch (n.getParent().getType()) {
132 case Token.VAR:
133 case Token.FUNCTION:
134 case Token.CATCH:
135 return true;
136
137 case Token.PARAM_LIST:
138 return n.getParent().getParent().isFunction();
139
140 default:
141 return false;
142 }
143 }
144
145 /** Checks that the given name is used legally. */
146 private void checkNameUse(NodeTraversal t, Node n) {
147 Var v = t.getScope().getVar(n.getString());
148 if (v == null) {
149 // In particular, this prevents creating a global variable by assigning
150 // to it without a declaration.
151 if (!noVarCheck) {
152 t.report(n, UNKNOWN_VARIABLE, n.getString());
153 }
154 }
155
156 if (!noCajaChecks) {
157 if ("eval".equals(n.getString())) {
158 t.report(n, EVAL_USE);
159 } else if (n.getString().endsWith("__")) {
160 t.report(n, ILLEGAL_NAME);
161 }
162 }
163 }
164
165 /** Checks that an assignment is not to the "arguments" object. */
166 private void checkAssignment(NodeTraversal t, Node n) {
167 if (n.getFirstChild().isName()) {
168 if ("arguments".equals(n.getFirstChild().getString())) {
169 t.report(n, ARGUMENTS_ASSIGNMENT);
170 } else if ("eval".equals(n.getFirstChild().getString())) {
171 // Note that assignment to eval is already illegal because any use of
172 // that name is illegal.
173 if (noCajaChecks) {
174 t.report(n, EVAL_ASSIGNMENT);
175 }
176 }
177 }
178 }
179
180 /** Checks that variables, functions, and arguments are not deleted. */
181 private void checkDelete(NodeTraversal t, Node n) {
182 if (n.getFirstChild().isName()) {
183 Var v = t.getScope().getVar(n.getFirstChild().getString());
184 if (v != null) {
185 t.report(n, DELETE_VARIABLE);
186 }
187 }
188 }
189
190 /** Checks that object literal keys are valid. */
191 private void checkObjectLiteral(NodeTraversal t, Node n) {
192 Set<String> getters = Sets.newHashSet();
193 Set<String> setters = Sets.newHashSet();
194 for (Node key = n.getFirstChild();
195 key != null;
196 key = key.getNext()) {
197 if (!noCajaChecks && key.getString().endsWith("__")) {
198 t.report(key, ILLEGAL_NAME);
199 }
200 if (!key.isSetterDef()) {
201 // normal property and getter cases
202 if (getters.contains(key.getString())) {
203 t.report(key, DUPLICATE_OBJECT_KEY);
204 } else {
205 getters.add(key.getString());
206 }
207 }
208 if (!key.isGetterDef()) {
209 // normal property and setter cases
210 if (setters.contains(key.getString())) {
211 t.report(key, DUPLICATE_OBJECT_KEY);
212 } else {
213 setters.add(key.getString());
214 }
215 }
216 }
217 }
218
219 /** Checks that label names are valid. */
220 private void checkLabel(NodeTraversal t, Node n) {
221 if (n.getFirstChild().getString().endsWith("__")) {
222 if (!noCajaChecks) {
223 t.report(n.getFirstChild(), ILLEGAL_NAME);
224 }
225 }
226 }
227
228 /** Checks that are performed on non-extern code only. */
229 private class NonExternChecks extends AbstractPostOrderCallback {
230 @Override public void visit(NodeTraversal t, Node n, Node parent) {
231 if ((n.isName()) && isDeclaration(n)) {
232 checkDeclaration(t, n);
233 } else if (n.isGetProp()) {
234 checkProperty(t, n);
235 }
236 }
237
238 /** Checks for illegal declarations. */
239 private void checkDeclaration(NodeTraversal t, Node n) {
240 if ("eval".equals(n.getString())) {
241 t.report(n, EVAL_DECLARATION);
242 } else if ("arguments".equals(n.getString())) {
243 t.report(n, ARGUMENTS_DECLARATION);
244 } else if (n.getString().endsWith("__")) {
245 if (!noCajaChecks) {
246 t.report(n, ILLEGAL_NAME);
247 }
248 }
249 }
250
251 /** Checks for illegal property accesses. */
252 private void checkProperty(NodeTraversal t, Node n) {
253 if (n.getLastChild().getString().endsWith("__")) {
254 if (!noCajaChecks) {
255 t.report(n.getLastChild(), ILLEGAL_NAME);
256 }
257 }
258 }
259 }
260 }