Debian Codegraph @

test/com/google/javascript/jscomp/ShadowVariablesTest.java

raw symbols SCIP
1 /*
2 * Copyright 2011 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 /**
20 * Unit tests for {@link ShadowVariables}.
21 *
22 *
23 */
24 public class ShadowVariablesTest extends CompilerTestCase{
25 // Use pseudo names to make test easier to read.
26 private boolean generatePseudoNames = false;
27 private RenameVars pass = null;
28
29 @Override
30 protected CompilerPass getProcessor(Compiler compiler) {
31 pass = new RenameVars(
32 compiler, "", false, false,
33 generatePseudoNames, true, null, null, null);
34 return pass;
35 }
36
37 @Override
38 protected int getNumRepetitions() {
39 return 1;
40 }
41
42 @Override
43 protected void setUp() throws Exception {
44 super.setUp();
45 generatePseudoNames = false;
46 }
47
48 @Override
49 protected void tearDown() throws Exception {
50 super.tearDown();
51 pass = null;
52 }
53
54 public void testShadowSimple1() {
55 test("function foo(x) { return function (y) {} }",
56 "function b(a) { return function (a) {} }");
57
58 generatePseudoNames = true;
59
60 test("function foo ( x ) { return function ( y ) {} }",
61 "function $foo$$($x$$) { return function ($x$$) {} }");
62
63 }
64
65 public void testShadowSimple2() {
66 test("function foo(x,y) { return function (y,z) {} }",
67 "function c(a,b) { return function (a,b) {} }");
68
69 generatePseudoNames = true;
70
71 test("function foo ( x , y ) { return function ( y , z ) {} }",
72 "function $foo$$($x$$,$y$$) { return function ($x$$,$y$$) {} }");
73 }
74
75 /**
76 * If we have a choice, pick out the most used variable to shadow.
77 */
78 public void testShadowMostUsedVar() {
79 generatePseudoNames = true;
80 test("function foo () {var x ; var y ; y ; y ; y ; x ;" +
81 " return function ( k ) {} }",
82
83 "function $foo$$() {var $x$$; var $y$$; $y$$;$y$$;$y$$;$x$$;" +
84 " return function ($y$$) {} }");
85 }
86
87 public void testNoShadowReferencedVariables() {
88 generatePseudoNames = true;
89 test("function f1 () { var x ; x ; x ; x ;" +
90 " return function f2 ( y ) {" +
91 " return function f3 () { x } }}",
92 "function $f1$$() { var $x$$;$x$$;$x$$;$x$$;" +
93 " return function $f2$$($y$$) {" +
94 " return function $f3$$() {$x$$} }}");
95 }
96
97 public void testNoShadowGlobalVariables() {
98 generatePseudoNames = true;
99 test("var x ; x ; function foo () { return function ( y ) {}}",
100 "var $x$$; $x$$; function $foo$$() { return function ($y$$) {}}");
101 }
102
103 public void testShadowBleedInFunctionName() {
104 generatePseudoNames = true;
105 test("function foo () { function b ( y ) { y } b ; b ;}",
106 "function $foo$$() { function $b$$($b$$) {$b$$} $b$$; $b$$;}");
107 }
108
109 public void testNoShadowLessPopularName() {
110 generatePseudoNames = true;
111 // We make sure that y doesn't pick x as a shadow and remains to be renamed
112 // to 'a'.
113 // If we do shadow y with whatever x renames to (say b) we will
114 // get 4 b's and 7 a's while currently we get 3 b's and 8 a's.
115 // I believe this arrangement will always be better for gzipping.
116 test("function f1 ( x ) {" +
117 " function f2 ( y ) {} x ; x ;}" +
118 "function f3 ( i ) {" +
119 " var k ; var j ; j ; j ; j ; j ; j ; j ;}",
120
121 "function $f1$$($x$$) {" +
122 " function $f2$$($y$$) {} $x$$;$x$$;}" +
123 "function $f3$$($i$$) {" +
124 " var $k$$; var $j$$;$j$$;$j$$;$j$$;$j$$;$j$$;$j$$;}");
125 }
126
127 public void testShadowFunctionName() {
128 generatePseudoNames = true;
129 test("var g = function() {" +
130 " var x ; return function(){function y (){}}}",
131 "var $g$$ = function() {" +
132 " var $x$$; return function(){function $x$$(){}}}");
133 }
134
135 public void testShadowLotsOfScopes1() {
136 generatePseudoNames = true;
137 test("var g = function( x ) { return function() { return function() {" +
138 " return function() { var y }}}}",
139 "var $g$$ = function($x$$) { return function() { return function() {" +
140 " return function() { var $x$$ }}}}");
141 }
142
143 public void testShadowLotsOfScopes2() {
144 generatePseudoNames = true;
145 // 'y' doesn't have a candidate to shadow due to upward referencing.
146 test("var g = function( x ) { return function( y ) " +
147 " {return function() {return function() { x }}}}",
148 "var $g$$ = function($x$$) { return function($y$$) " +
149 " {return function() {return function() { $x$$ }}}}");
150
151 test("var g = function( x ) { return function() " +
152 " {return function( y ) {return function() { x }}}}",
153 "var $g$$ = function($x$$) { return function() " +
154 " {return function($y$$) {return function() { $x$$ }}}}");
155
156 test("var g = function( x ) { return function() " +
157 " {return function() {return function( y ) { x }}}}",
158 "var $g$$ = function($x$$) { return function() " +
159 " {return function() {return function($y$$) { $x$$ }}}}");
160 }
161
162 public void testShadowLotsOfScopes3() {
163 generatePseudoNames = true;
164 // 'y' doesn't have a candidate to shadow due to upward referencing.
165 test("var g = function( x ) { return function() " +
166 " {return function() {return function() { x }; var y }}}",
167 "var $g$$ = function($x$$) { return function() " +
168 " {return function() {return function() { $x$$ }; var $y$$}}}");
169 test("var g = function( x ) { return function() " +
170 " {return function() {return function() { x }}; var y }}",
171 "var $g$$ = function($x$$) { return function() " +
172 " {return function() {return function() { $x$$ }}; var $y$$}}");
173 test("var g = function( x ) { return function() " +
174 " {return function() {return function() { x }}}; var y }",
175 "var $g$$ = function($x$$) { return function() " +
176 " {return function() {return function() { $x$$ }}}; var $y$$}");
177 }
178
179 public void testShadowLotsOfScopes4() {
180 // Make sure we do get the optimal shadowing scheme where
181 test("var g = function(x) { return function() { return function() {" +
182 " return function(){return function(){};var m};var n};var o}}",
183 "var b = function(a) { return function() { return function() {" +
184 " return function(){return function(){};var a};var a};var a}}");
185 }
186
187 public void testShadowLotsOfScopes5() {
188 generatePseudoNames = true;
189 test("var g = function( x ) {" +
190 " return function() { return function() {" +
191 " return function() { return function() {" +
192 " x }; o };var n };var o };var p }",
193 "var $g$$ = function($x$$) {" +
194 " return function() { return function() {" +
195 " return function() { return function() {" +
196 " $x$$};$o$$};var $p$$};var $o$$};var $p$$}");
197
198 test("var g = function( x ) {" +
199 " return function() { return function() {" +
200 " return function() { return function() {" +
201 " x }; p };var n };var o };var p }",
202 "var $g$$ = function($x$$) {" +
203 " return function() { return function() {" +
204 " return function() { return function() {" +
205 " $x$$};$p$$};var $o$$};var $o$$};var $p$$}");
206 }
207
208 public void testShadowWithShadowAlready() {
209 test("var g = function(x) { return function() { return function() {" +
210 " return function(){return function(){x}};var p};var o};var p}",
211 "var c = function(b) { return function() { return function() {" +
212 " return function(){return function(){b}};var a};var a};var a}");
213
214 test("var g = function(x) { return function() { return function() {" +
215 " return function(){return function(){x};p};var p};var o};var p}",
216 "var c = function(b) { return function() { return function() {" +
217 " return function(){return function(){b};a};var a};var a};var a}");
218 }
219
220 public void testShadowBug1() {
221 generatePseudoNames = true;
222 test("function f ( x ) { return function( y ) {" +
223 " return function( x ) { x + y ; }}}",
224 "function $f$$($x$$) { return function($y$$) {" +
225 " return function($x$$) { $x$$ + $y$$; }}}");
226 }
227
228 public void testOptimal() {
229 // A test for a case that wasn't optimal in a single pass algorithm.
230 test("function f(x) { function g(y) { function h(x) {}}}",
231 "function c(a) { function b(a) { function b(a) {}}}");
232 }
233
234 public void testSharingAcrossInnerScopes() {
235 test("function f() {var f=function g(){g()}; var x=function y(){y()}}",
236 "function c() {var d=function a(){a()}; var e=function b(){b()}}");
237 test("function f(x) { return x ? function(y){} : function(z) {} }",
238 "function b(a) { return a ? function(a){} : function(a) {} }");
239 }
240
241 public void testExportedLocal1() {
242 test("function f(a) { a();a();a(); return function($super){} }",
243 "function b(a) { a();a();a(); return function($super){} }");
244 }
245
246 public void testExportedLocal2() {
247 test("function f($super) { $super();$super(); return function(a){} }",
248 "function a($super) { $super();$super(); return function(b){} }");
249 }
250
251 public void testRenameMapHasNoDuplicates() {
252 test("function foo(x) { return function (y) {} }",
253 "function b(a) { return function (a) {} }");
254
255 VariableMap vm = pass.getVariableMap();
256 try {
257 vm.getNewNameToOriginalNameMap();
258 } catch (java.lang.IllegalArgumentException unexpected) {
259 fail("Invalid VariableMap generated: " +
260 vm.getOriginalNameToNewNameMap().toString());
261 }
262 }
263
264 public void testBug4172539() {
265 // All the planets must line up. When we look at the 2nd inner function,
266 // y can shadow x, also m can shadow x as well. Now all that is left for
267 // n to shadow is 'y'. Now because y has already shadowed x, the pseudo
268 // name maps has already updated y gets $x$$. This mean n will be updated
269 // with "$x$$" in the name map which is incorrect. That is the reason
270 // why we can't update the pseudo name map on-the-fly.
271
272 generatePseudoNames = true;
273 test("function f(x) {" +
274 " x;x;x;" +
275 " return function (y) { y; x };" +
276 " return function (y) {" +
277 " y;" +
278 " return function (m, n) {" +
279 " m;m;m;" +
280 " };" +
281 " };" +
282 "}",
283
284 "function $f$$($x$$) {" +
285 " $x$$;$x$$;$x$$;" +
286 " return function ($y$$) { $y$$; $x$$ };" +
287 " return function ($x$$) {" +
288 " $x$$;" +
289 " return function ($x$$, $y$$) {" +
290 " $x$$;$x$$;$x$$;" +
291 " };" +
292 " };" +
293 "}");
294 }
295 }