Debian Codegraph @

test/com/google/javascript/jscomp/ProcessDefinesTest.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 static com.google.javascript.jscomp.SyntacticScopeCreator.VAR_MULTIPLY_DECLARED_ERROR;
20
21 import com.google.common.collect.Maps;
22 import com.google.javascript.jscomp.GlobalNamespace.Name;
23 import com.google.javascript.rhino.Node;
24 import com.google.javascript.rhino.Token;
25
26 import java.util.Map;
27
28 /**
29 * @author [email protected] (Nick Santos)
30 */
31 public class ProcessDefinesTest extends CompilerTestCase {
32
33 public ProcessDefinesTest() {
34 super("var externMethod;");
35
36 // ProcessDefines emits warnings if the user tries to re-define a constant,
37 // but the constant is not defined anywhere in the binary.
38 allowSourcelessWarnings();
39 }
40
41 private Map<String, Node> overrides = Maps.newHashMap();
42 private GlobalNamespace namespace;
43
44 @Override
45 public void setUp() throws Exception {
46 super.setUp();
47 overrides.clear();
48 }
49
50 @Override
51 protected CompilerPass getProcessor(Compiler compiler) {
52 return new ProcessDefinesWithInjectedNamespace(compiler);
53 }
54
55 @Override
56 protected int getNumRepetitions() {
57 // Only do one repetition, so that we can make sure the first pass keeps
58 // GlobalNamespace up to date.
59 return 1;
60 }
61
62 /**
63 * Helper for tests that expects definitions to remain unchanged, such
64 * that {@code definitions+js} is converted to {@code definitions+expected}.
65 */
66 private void testWithPrefix(String definitions, String js, String expected) {
67 test(definitions + js, definitions + expected);
68 }
69
70 public void testBasicDefine1() {
71 test("/** @define {boolean} */ var DEF = true", "var DEF=true");
72 }
73
74 public void testBasicDefine2() {
75 test("/** @define {string} */ var DEF = 'a'", "var DEF=\"a\"");
76 }
77
78 public void testBasicDefine3() {
79 test("/** @define {number} */ var DEF = 0", "var DEF=0");
80 }
81
82 public void testDefineBadType() {
83 test("/** @define {Object} */ var DEF = {}",
84 null, ProcessDefines.INVALID_DEFINE_TYPE_ERROR);
85 }
86
87 public void testDefineWithBadValue1() {
88 test("/** @define {boolean} */ var DEF = new Boolean(true);", null,
89 ProcessDefines.INVALID_DEFINE_INIT_ERROR);
90 }
91
92 public void testDefineWithBadValue2() {
93 test("/** @define {string} */ var DEF = 'x' + y;", null,
94 ProcessDefines.INVALID_DEFINE_INIT_ERROR);
95 }
96
97 public void testDefineWithDependentValue() {
98 test("/** @define {boolean} */ var BASE = false;\n" +
99 "/** @define {boolean} */ var DEF = !BASE;",
100 "var BASE=false;var DEF=!BASE");
101 test("var a = {};\n" +
102 "/** @define {boolean} */ a.BASE = false;\n" +
103 "/** @define {boolean} */ a.DEF = !a.BASE;",
104 "var a={};a.BASE=false;a.DEF=!a.BASE");
105 }
106
107
108 public void testDefineWithInvalidDependentValue() {
109 test("var BASE = false;\n" +
110 "/** @define {boolean} */ var DEF = !BASE;",
111 null,
112 ProcessDefines.INVALID_DEFINE_INIT_ERROR);
113 }
114
115 public void testOverriding1() {
116 overrides.put("DEF_OVERRIDE_TO_TRUE", new Node(Token.TRUE));
117 overrides.put("DEF_OVERRIDE_TO_FALSE", new Node(Token.FALSE));
118 test(
119 "/** @define {boolean} */ var DEF_OVERRIDE_TO_TRUE = false;" +
120 "/** @define {boolean} */ var DEF_OVERRIDE_TO_FALSE = true",
121 "var DEF_OVERRIDE_TO_TRUE=true;var DEF_OVERRIDE_TO_FALSE=false");
122 }
123
124 public void testOverriding2() {
125 overrides.put("DEF_OVERRIDE_TO_TRUE", new Node(Token.TRUE));
126 String normalConst = "var DEF_OVERRIDE_TO_FALSE=true;";
127 testWithPrefix(
128 normalConst,
129 "/** @define {boolean} */ var DEF_OVERRIDE_TO_TRUE = false",
130 "var DEF_OVERRIDE_TO_TRUE=true");
131 }
132
133 public void testOverriding3() {
134 overrides.put("DEF_OVERRIDE_TO_TRUE", new Node(Token.TRUE));
135 test(
136 "/** @define {boolean} */ var DEF_OVERRIDE_TO_TRUE = true;",
137 "var DEF_OVERRIDE_TO_TRUE=true");
138 }
139
140 public void testOverridingString0() {
141 test(
142 "/** @define {string} */ var DEF_OVERRIDE_STRING = 'x';",
143 "var DEF_OVERRIDE_STRING=\"x\"");
144 }
145
146 public void testOverridingString1() {
147 test(
148 "/** @define {string} */ var DEF_OVERRIDE_STRING = 'x' + 'y';",
149 "var DEF_OVERRIDE_STRING=\"x\" + \"y\"");
150 }
151
152 public void testOverridingString2() {
153 overrides.put("DEF_OVERRIDE_STRING", Node.newString("foo"));
154 test(
155 "/** @define {string} */ var DEF_OVERRIDE_STRING = 'x';",
156 "var DEF_OVERRIDE_STRING=\"foo\"");
157 }
158
159 public void testOverridingString3() {
160 overrides.put("DEF_OVERRIDE_STRING", Node.newString("foo"));
161 test(
162 "/** @define {string} */ var DEF_OVERRIDE_STRING = 'x' + 'y';",
163 "var DEF_OVERRIDE_STRING=\"foo\"");
164 }
165
166 public void testMisspelledOverride() {
167 overrides.put("DEF_BAD_OVERIDE", new Node(Token.TRUE));
168 test("/** @define {boolean} */ var DEF_BAD_OVERRIDE = true",
169 "var DEF_BAD_OVERRIDE=true", null,
170 ProcessDefines.UNKNOWN_DEFINE_WARNING);
171 }
172
173 public void testCompiledIsKnownDefine() {
174 overrides.put("COMPILED", new Node(Token.TRUE));
175 testSame("");
176 }
177
178 public void testSimpleReassign1() {
179 test("/** @define {boolean} */ var DEF = false; DEF = true;",
180 "var DEF=true;true");
181 }
182
183 public void testSimpleReassign2() {
184 test("/** @define {number|boolean} */ var DEF=false;DEF=true;DEF=3",
185 "var DEF=3;true;3");
186
187 Name def = namespace.getNameIndex().get("DEF");
188 assertEquals(1, def.getRefs().size());
189 assertEquals(1, def.globalSets);
190 assertNotNull(def.getDeclaration());
191 }
192
193 public void testSimpleReassign3() {
194 test("/** @define {boolean} */ var DEF = false;var x;x = DEF = true;",
195 "var DEF=true;var x;x=true");
196 }
197
198 public void testDuplicateVar() {
199 test("/** @define {boolean} */ var DEF = false; var DEF = true;",
200 null, VAR_MULTIPLY_DECLARED_ERROR);
201 }
202
203 public void testAssignBeforeDeclaration1() {
204 test("DEF=false;var b=false,/** @define {boolean} */DEF=true,c=false",
205 null, ProcessDefines.INVALID_DEFINE_INIT_ERROR);
206 }
207
208 public void testAssignBeforeDeclaration2() {
209 overrides.put("DEF_OVERRIDE_TO_TRUE", new Node(Token.TRUE));
210 test(
211 "DEF_OVERRIDE_TO_TRUE = 3;" +
212 "/** @define {boolean|number} */ var DEF_OVERRIDE_TO_TRUE = false;",
213 null, ProcessDefines.INVALID_DEFINE_INIT_ERROR);
214 }
215
216 public void testEmptyDeclaration() {
217 test("/** @define {boolean} */ var DEF;",
218 null, ProcessDefines.INVALID_DEFINE_INIT_ERROR);
219 }
220
221 public void testReassignAfterCall() {
222 test("/** @define {boolean} */var DEF=true;externMethod();DEF=false",
223 null, ProcessDefines.DEFINE_NOT_ASSIGNABLE_ERROR);
224 }
225
226 public void testReassignAfterRef() {
227 test("/** @define {boolean} */var DEF=true;var x = DEF;DEF=false",
228 null, ProcessDefines.DEFINE_NOT_ASSIGNABLE_ERROR);
229 }
230
231 public void testReassignWithExpr() {
232 test("/** @define {boolean} */var DEF=true;var x;DEF=x=false",
233 null, ProcessDefines.INVALID_DEFINE_INIT_ERROR);
234 }
235
236 public void testReassignAfterNonGlobalRef() {
237 test(
238 "/** @define {boolean} */var DEF=true;" +
239 "var x=function(){var y=DEF}; DEF=false",
240 "var DEF=false;var x=function(){var y=DEF};false");
241
242 Name def = namespace.getNameIndex().get("DEF");
243 assertEquals(2, def.getRefs().size());
244 assertEquals(1, def.globalSets);
245 assertNotNull(def.getDeclaration());
246 }
247
248 public void testReassignAfterRefInConditional() {
249 test(
250 "/** @define {boolean} */var DEF=true;" +
251 "if (false) {var x=DEF} DEF=false;",
252 null, ProcessDefines.DEFINE_NOT_ASSIGNABLE_ERROR);
253 }
254
255 public void testAssignInNonGlobalScope() {
256 test("/** @define {boolean} */var DEF=true;function foo() {DEF=false};",
257 null, ProcessDefines.NON_GLOBAL_DEFINE_INIT_ERROR);
258 }
259
260 public void testDeclareInNonGlobalScope() {
261 test("function foo() {/** @define {boolean} */var DEF=true;};",
262 null, ProcessDefines.NON_GLOBAL_DEFINE_INIT_ERROR);
263 }
264
265 public void testDefineAssignmentInLoop() {
266 test("/** @define {boolean} */var DEF=true;var x=0;while (x) {DEF=false;}",
267 null, ProcessDefines.NON_GLOBAL_DEFINE_INIT_ERROR);
268 }
269
270 public void testWithNoDefines() {
271 testSame("var DEF=true;var x={};x.foo={}");
272 }
273
274 public void testNamespacedDefine1() {
275 test("var a = {}; /** @define {boolean} */ a.B = false; a.B = true;",
276 "var a = {}; a.B = true; true;");
277
278 Name aDotB = namespace.getNameIndex().get("a.B");
279 assertEquals(1, aDotB.getRefs().size());
280 assertEquals(1, aDotB.globalSets);
281 assertNotNull(aDotB.getDeclaration());
282 }
283
284 public void testNamespacedDefine2a() {
285 overrides.put("a.B", new Node(Token.TRUE));
286 test("var a = {}; /** @define {boolean} */ a.B = false;",
287 "var a = {}; a.B = true;");
288 }
289
290 public void testNamespacedDefine2b() {
291 // TODO(johnlenz): We should either reject the define as invalid
292 // or replace its value.
293 overrides.put("a.B", new Node(Token.TRUE));
294 test("var a = { /** @define {boolean} */ B : false };",
295 "var a = {B : false};",
296 null, ProcessDefines.UNKNOWN_DEFINE_WARNING);
297 }
298
299 public void testNamespacedDefine2c() {
300 // TODO(johnlenz): We should either reject the define as invalid
301 // or replace its value.
302 overrides.put("a.B", new Node(Token.TRUE));
303 test("var a = { /** @define {boolean} */ get B() { return false } };",
304 "var a = {get B() { return false } };",
305 null, ProcessDefines.UNKNOWN_DEFINE_WARNING);
306 }
307
308 public void testNamespacedDefine3() {
309 overrides.put("a.B", new Node(Token.TRUE));
310 test("var a = {};", "var a = {};", null,
311 ProcessDefines.UNKNOWN_DEFINE_WARNING);
312 }
313
314 public void testNamespacedDefine4() {
315 overrides.put("a.B", new Node(Token.TRUE));
316 test("var a = {}; /** @define {boolean} */ a.B = false;",
317 "var a = {}; a.B = true;");
318 }
319
320
321 public void testOverrideAfterAlias() {
322 test("var x; /** @define {boolean} */var DEF=true; x=DEF; DEF=false;",
323 null, ProcessDefines.DEFINE_NOT_ASSIGNABLE_ERROR);
324 }
325
326 private class ProcessDefinesWithInjectedNamespace implements CompilerPass {
327 private final Compiler compiler;
328
329 public ProcessDefinesWithInjectedNamespace(Compiler compiler) {
330 this.compiler = compiler;
331 }
332
333 @Override
334 public void process(Node externs, Node js) {
335 namespace = new GlobalNamespace(compiler, js);
336 new ProcessDefines(compiler, overrides)
337 .injectNamespace(namespace)
338 .process(externs, js);
339 }
340 }
341 }