Debian Codegraph @

test/com/google/javascript/jscomp/StrictModeCheckTest.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
17 package com.google.javascript.jscomp;
18
19 public class StrictModeCheckTest extends CompilerTestCase {
20 private static final String EXTERNS = "var arguments; function eval(str) {}";
21
22 private boolean noVarCheck;
23 private boolean noCajaChecks;
24
25 public StrictModeCheckTest() {
26 super(EXTERNS);
27 }
28
29 @Override
30 protected void setUp() throws Exception {
31 super.setUp();
32 noVarCheck = false;
33 noCajaChecks = false;
34 }
35
36 @Override
37 protected CompilerPass getProcessor(Compiler compiler) {
38 return new StrictModeCheck(compiler, noVarCheck, noCajaChecks);
39 }
40
41 @Override
42 protected int getNumRepetitions() {
43 return 1;
44 }
45
46 public void testEval() {
47 test("function foo() { eval('a'); }", null,
48 StrictModeCheck.EVAL_USE);
49 }
50
51 public void testEval2() {
52 testSame("function foo(eval) {}",
53 StrictModeCheck.EVAL_DECLARATION);
54 }
55
56 public void testEval3() {
57 testSame("function foo() {} foo.eval = 3;");
58 }
59
60 public void testEval4() {
61 testSame("function foo() { var eval = 3; }",
62 StrictModeCheck.EVAL_DECLARATION);
63 }
64
65 public void testEval5() {
66 testSame("function eval() {}", StrictModeCheck.EVAL_DECLARATION);
67 }
68
69 public void testEval6() {
70 testSame("try {} catch (eval) {}", StrictModeCheck.EVAL_DECLARATION);
71 }
72
73 public void testEval7() {
74 testSame("var o = {eval: 3};");
75 }
76
77 public void testEval8() {
78 testSame("var a; eval: while (true) { a = 3; }");
79 }
80
81 public void testUnknownVariable() {
82 testSame("function foo(a) { a = b; }", StrictModeCheck.UNKNOWN_VARIABLE);
83 }
84
85 public void testUnknownVariable2() {
86 testSame("a: while (true) { a = 3; }", StrictModeCheck.UNKNOWN_VARIABLE);
87 }
88
89 public void testUnknownVariable3() {
90 testSame("try {} catch (ex) { ex = 3; }");
91 }
92
93 public void testArguments() {
94 testSame("function foo(arguments) {}",
95 StrictModeCheck.ARGUMENTS_DECLARATION);
96 }
97
98 public void testArguments2() {
99 testSame("function foo() { var arguments = 3; }",
100 StrictModeCheck.ARGUMENTS_DECLARATION);
101 }
102
103 public void testArguments3() {
104 testSame("function arguments() {}",
105 StrictModeCheck.ARGUMENTS_DECLARATION);
106 }
107
108 public void testArguments4() {
109 testSame("try {} catch (arguments) {}",
110 StrictModeCheck.ARGUMENTS_DECLARATION);
111 }
112
113 public void testArguments5() {
114 testSame("var o = {arguments: 3};");
115 }
116
117 public void testEvalAssignment() {
118 noCajaChecks = true;
119 testSame("function foo() { eval = []; }",
120 StrictModeCheck.EVAL_ASSIGNMENT);
121 }
122
123 public void testEvalAssignment2() {
124 test("function foo() { eval = []; }", null, StrictModeCheck.EVAL_USE);
125 }
126
127 public void testAssignToArguments() {
128 testSame("function foo() { arguments = []; }",
129 StrictModeCheck.ARGUMENTS_ASSIGNMENT);
130 }
131
132 public void testDeleteVar() {
133 testSame("var a; delete a", StrictModeCheck.DELETE_VARIABLE);
134 }
135
136 public void testDeleteFunction() {
137 testSame("function a() {} delete a", StrictModeCheck.DELETE_VARIABLE);
138 }
139
140 public void testDeleteArgument() {
141 testSame("function b(a) { delete a; }",
142 StrictModeCheck.DELETE_VARIABLE);
143 }
144
145 public void testDeleteProperty() {
146 testSame("function f(obj) { delete obj.a; }");
147 }
148
149 public void testIllegalName() {
150 test("var a__ = 3;", null, StrictModeCheck.ILLEGAL_NAME);
151 }
152
153 public void testIllegalName2() {
154 test("function a__() {}", null, StrictModeCheck.ILLEGAL_NAME);
155 }
156
157 public void testIllegalName3() {
158 test("function f(a__) {}", null, StrictModeCheck.ILLEGAL_NAME);
159 }
160
161 public void testIllegalName4() {
162 test("try {} catch (a__) {}", null, StrictModeCheck.ILLEGAL_NAME);
163 }
164
165 public void testIllegalName5() {
166 noVarCheck = true;
167 test("var a = b__;", null, StrictModeCheck.ILLEGAL_NAME);
168 }
169
170 public void testIllegalName6() {
171 test("function f(obj) { return obj.a__; }", null,
172 StrictModeCheck.ILLEGAL_NAME);
173 }
174
175 public void testIllegalName7() {
176 noCajaChecks = true;
177 testSame("var a__ = 3;");
178 }
179
180 public void testIllegalName8() {
181 test("var o = {a__: 3};", null, StrictModeCheck.ILLEGAL_NAME);
182 test("var o = {b: 3, a__: 4};", null, StrictModeCheck.ILLEGAL_NAME);
183 test("var o = {b: 3, get a__() {}};", null, StrictModeCheck.ILLEGAL_NAME);
184 test("var o = {b: 3, set a__(c) {}};", null, StrictModeCheck.ILLEGAL_NAME);
185 }
186
187 public void testIllegalName9() {
188 test("a__: while (true) { var b = 3; }", null,
189 StrictModeCheck.ILLEGAL_NAME);
190 }
191
192 public void testIllegalName10() {
193 // Validate that number as objlit key
194 testSame("var o = {1: 3, 2: 4};");
195 }
196
197 public void testDuplicateObjectLiteralKey() {
198 testSame("var o = {a: 1, b: 2, c: 3};");
199 testSame("var x = { get a() {}, set a(p) {} };");
200
201 testSame("var o = {a: 1, b: 2, a: 3};",
202 StrictModeCheck.DUPLICATE_OBJECT_KEY);
203 testSame("var x = { get a() {}, get a() {} };",
204 StrictModeCheck.DUPLICATE_OBJECT_KEY);
205 testSame("var x = { get a() {}, a: 1 };",
206 StrictModeCheck.DUPLICATE_OBJECT_KEY);
207 testSame("var x = { set a(p) {}, a: 1 };",
208 StrictModeCheck.DUPLICATE_OBJECT_KEY);
209
210 testSame(
211 "'use strict';\n" +
212 "function App() {}\n" +
213 "App.prototype = {\n" +
214 " get appData() { return this.appData_; },\n" +
215 " set appData(data) { this.appData_ = data; }\n" +
216 "};");
217 }
218
219 public void testFunctionDecl() {
220 testSame("function g() {}");
221 testSame("var g = function() {};");
222 testSame("(function() {})();");
223 testSame("(function() {});");
224 testSame(inFn("function g() {}"));
225 testSame(inFn("var g = function() {};"));
226 testSame(inFn("(function() {})();"));
227 testSame(inFn("(function() {});"));
228
229 test("{function g() {}}", null, StrictModeCheck.BAD_FUNCTION_DECLARATION);
230 testSame("{var g = function () {}}");
231 testSame("{(function g() {})()}");
232
233 test("var x;if (x) { function g(){} }", null,
234 StrictModeCheck.BAD_FUNCTION_DECLARATION);
235 testSame("var x;if (x) {var g = function () {}}");
236 testSame("var x;if (x) {(function g() {})()}");
237 }
238
239 public void testFunctionDecl2() {
240 test("{function g() {}}", null, StrictModeCheck.BAD_FUNCTION_DECLARATION);
241 }
242
243 private String inFn(String body) {
244 return "function func() {" + body + "}";
245 }
246 }