| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.collect.Lists; |
| 20 |
import com.google.javascript.jscomp.MakeDeclaredNamesUnique.InlineRenamer; |
| 21 |
import com.google.javascript.rhino.Node; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class MakeDeclaredNamesUniqueTest extends CompilerTestCase { |
| 27 |
|
| 28 |
private boolean useDefaultRenamer = false; |
| 29 |
private boolean invert = false; |
| 30 |
private boolean removeConst = false; |
| 31 |
private final String localNamePrefix = "unique_"; |
| 32 |
|
| 33 |
@Override |
| 34 |
public CompilerPass getProcessor(final Compiler compiler) { |
| 35 |
if (!invert) { |
| 36 |
return new CompilerPass() { |
| 37 |
@Override |
| 38 |
public void process(Node externs, Node root) { |
| 39 |
compiler.resetUniqueNameId(); |
| 40 |
MakeDeclaredNamesUnique renamer = null; |
| 41 |
if (useDefaultRenamer) { |
| 42 |
renamer = new MakeDeclaredNamesUnique(); |
| 43 |
} else { |
| 44 |
renamer = new MakeDeclaredNamesUnique( |
| 45 |
new InlineRenamer( |
| 46 |
compiler.getCodingConvention(), |
| 47 |
compiler.getUniqueNameIdSupplier(), |
| 48 |
localNamePrefix, |
| 49 |
removeConst)); |
| 50 |
} |
| 51 |
NodeTraversal.traverseRoots( |
| 52 |
compiler, Lists.newArrayList(externs, root), renamer); |
| 53 |
} |
| 54 |
}; |
| 55 |
} else { |
| 56 |
return MakeDeclaredNamesUnique.getContextualRenameInverter(compiler); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
protected int getNumRepetitions() { |
| 62 |
|
| 63 |
return 1; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void setUp() { |
| 68 |
removeConst = false; |
| 69 |
invert = false; |
| 70 |
useDefaultRenamer = false; |
| 71 |
} |
| 72 |
|
| 73 |
private void testWithInversion(String original, String expected) { |
| 74 |
invert = false; |
| 75 |
test(original, expected); |
| 76 |
invert = true; |
| 77 |
test(expected, original); |
| 78 |
invert = false; |
| 79 |
} |
| 80 |
|
| 81 |
private void testSameWithInversion(String externs, String original) { |
| 82 |
invert = false; |
| 83 |
testSame(externs, original, null); |
| 84 |
invert = true; |
| 85 |
testSame(externs, original, null); |
| 86 |
invert = false; |
| 87 |
} |
| 88 |
|
| 89 |
private void testSameWithInversion(String original) { |
| 90 |
testSameWithInversion("", original); |
| 91 |
} |
| 92 |
|
| 93 |
private String wrapInFunction(String s) { |
| 94 |
return "function f(){" + s + "}"; |
| 95 |
} |
| 96 |
|
| 97 |
private void testInFunction(String original, String expected) { |
| 98 |
test(wrapInFunction(original), wrapInFunction(expected)); |
| 99 |
} |
| 100 |
|
| 101 |
public void testMakeLocalNamesUniqueWithContext1() { |
| 102 |
|
| 103 |
this.useDefaultRenamer = true; |
| 104 |
|
| 105 |
invert = true; |
| 106 |
test( |
| 107 |
"var a;function foo(){var a$$inline_1; a = 1}", |
| 108 |
"var a;function foo(){var a$$0; a = 1}"); |
| 109 |
test( |
| 110 |
"var a;function foo(){var a$$inline_1;}", |
| 111 |
"var a;function foo(){var a;}"); |
| 112 |
} |
| 113 |
|
| 114 |
public void testMakeLocalNamesUniqueWithContext2() { |
| 115 |
|
| 116 |
this.useDefaultRenamer = true; |
| 117 |
|
| 118 |
|
| 119 |
testSameWithInversion("var a;"); |
| 120 |
|
| 121 |
|
| 122 |
testSameWithInversion("a;"); |
| 123 |
|
| 124 |
|
| 125 |
testWithInversion( |
| 126 |
"var a;function foo(a){var b;a}", |
| 127 |
"var a;function foo(a$$1){var b;a$$1}"); |
| 128 |
testWithInversion( |
| 129 |
"var a;function foo(){var b;a}function boo(){var b;a}", |
| 130 |
"var a;function foo(){var b;a}function boo(){var b$$1;a}"); |
| 131 |
testWithInversion( |
| 132 |
"function foo(a){var b}" + |
| 133 |
"function boo(a){var b}", |
| 134 |
"function foo(a){var b}" + |
| 135 |
"function boo(a$$1){var b$$1}"); |
| 136 |
|
| 137 |
|
| 138 |
testWithInversion( |
| 139 |
"var a = function foo(){foo()};var b = function foo(){foo()};", |
| 140 |
"var a = function foo(){foo()};var b = function foo$$1(){foo$$1()};"); |
| 141 |
|
| 142 |
|
| 143 |
testWithInversion( |
| 144 |
"try { } catch(e) {e;}", |
| 145 |
"try { } catch(e) {e;}"); |
| 146 |
|
| 147 |
|
| 148 |
test( |
| 149 |
"try { } catch(e) {e;}; try { } catch(e) {e;}", |
| 150 |
"try { } catch(e) {e;}; try { } catch(e$$1) {e$$1;}"); |
| 151 |
test( |
| 152 |
"try { } catch(e) {e; try { } catch(e) {e;}};", |
| 153 |
"try { } catch(e) {e; try { } catch(e$$1) {e$$1;} }; "); |
| 154 |
} |
| 155 |
|
| 156 |
public void testMakeLocalNamesUniqueWithContext3() { |
| 157 |
|
| 158 |
this.useDefaultRenamer = true; |
| 159 |
|
| 160 |
String externs = "var extern1 = {};"; |
| 161 |
|
| 162 |
|
| 163 |
testSameWithInversion(externs, "var extern1 = extern1 || {};"); |
| 164 |
|
| 165 |
|
| 166 |
testSame(externs, "var extern1 = extern1 || {};", null); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public void testMakeLocalNamesUniqueWithContext4() { |
| 172 |
|
| 173 |
this.useDefaultRenamer = true; |
| 174 |
|
| 175 |
|
| 176 |
testInFunction( |
| 177 |
"var e; try { } catch(e) {e;}; try { } catch(e) {e;}", |
| 178 |
"var e; try { } catch(e$$1) {e$$1;}; try { } catch(e$$2) {e$$2;}"); |
| 179 |
testInFunction( |
| 180 |
"var e; try { } catch(e) {e; try { } catch(e) {e;}}", |
| 181 |
"var e; try { } catch(e$$1) {e$$1; try { } catch(e$$2) {e$$2;} }"); |
| 182 |
testInFunction( |
| 183 |
"try { } catch(e) {e;}; try { } catch(e) {e;} var e;", |
| 184 |
"try { } catch(e$$1) {e$$1;}; try { } catch(e$$2) {e$$2;} var e;"); |
| 185 |
testInFunction( |
| 186 |
"try { } catch(e) {e; try { } catch(e) {e;}} var e;", |
| 187 |
"try { } catch(e$$1) {e$$1; try { } catch(e$$2) {e$$2;} } var e;"); |
| 188 |
|
| 189 |
invert = true; |
| 190 |
|
| 191 |
testInFunction( |
| 192 |
"var e; try { } catch(e$$0) {e$$0;}; try { } catch(e$$1) {e$$1;}", |
| 193 |
"var e; try { } catch(e$$2) {e$$2;}; try { } catch(e$$0) {e$$0;}"); |
| 194 |
testInFunction( |
| 195 |
"var e; try { } catch(e$$1) {e$$1; try { } catch(e$$2) {e$$2;} };", |
| 196 |
"var e; try { } catch(e$$0) {e$$0; try { } catch(e$$1) {e$$1;} };"); |
| 197 |
testInFunction( |
| 198 |
"try { } catch(e) {e;}; try { } catch(e$$1) {e$$1;};var e$$2;", |
| 199 |
"try { } catch(e) {e;}; try { } catch(e$$0) {e$$0;};var e$$1;"); |
| 200 |
testInFunction( |
| 201 |
"try { } catch(e) {e; try { } catch(e$$1) {e$$1;} };var e$$2", |
| 202 |
"try { } catch(e) {e; try { } catch(e$$0) {e$$0;} };var e$$1"); |
| 203 |
} |
| 204 |
|
| 205 |
public void testMakeLocalNamesUniqueWithContext5() { |
| 206 |
|
| 207 |
this.useDefaultRenamer = true; |
| 208 |
|
| 209 |
testWithInversion( |
| 210 |
"function f(){var f; f = 1}", |
| 211 |
"function f(){var f$$1; f$$1 = 1}"); |
| 212 |
testWithInversion( |
| 213 |
"function f(f){f = 1}", |
| 214 |
"function f(f$$1){f$$1 = 1}"); |
| 215 |
testWithInversion( |
| 216 |
"function f(f){var f; f = 1}", |
| 217 |
"function f(f$$1){var f$$1; f$$1 = 1}"); |
| 218 |
|
| 219 |
test( |
| 220 |
"var fn = function f(){var f; f = 1}", |
| 221 |
"var fn = function f(){var f$$1; f$$1 = 1}"); |
| 222 |
test( |
| 223 |
"var fn = function f(f){f = 1}", |
| 224 |
"var fn = function f(f$$1){f$$1 = 1}"); |
| 225 |
test( |
| 226 |
"var fn = function f(f){var f; f = 1}", |
| 227 |
"var fn = function f(f$$1){var f$$1; f$$1 = 1}"); |
| 228 |
} |
| 229 |
|
| 230 |
public void testArguments() { |
| 231 |
|
| 232 |
this.useDefaultRenamer = true; |
| 233 |
|
| 234 |
|
| 235 |
testSameWithInversion( |
| 236 |
"function foo(){var arguments;function bar(){var arguments;}}"); |
| 237 |
|
| 238 |
invert = true; |
| 239 |
|
| 240 |
|
| 241 |
test( |
| 242 |
"function foo(){var arguments$$1;}", |
| 243 |
"function foo(){var arguments$$0;}"); |
| 244 |
} |
| 245 |
|
| 246 |
public void testMakeLocalNamesUniqueWithoutContext() { |
| 247 |
|
| 248 |
this.useDefaultRenamer = false; |
| 249 |
|
| 250 |
test("var a;", |
| 251 |
"var a$$unique_0"); |
| 252 |
|
| 253 |
|
| 254 |
testSame("a;"); |
| 255 |
|
| 256 |
|
| 257 |
test("var a;" + |
| 258 |
"function foo(a){var b;a}", |
| 259 |
"var a$$unique_0;" + |
| 260 |
"function foo$$unique_1(a$$unique_2){var b$$unique_3;a$$unique_2}"); |
| 261 |
test("var a;" + |
| 262 |
"function foo(){var b;a}" + |
| 263 |
"function boo(){var b;a}", |
| 264 |
"var a$$unique_0;" + |
| 265 |
"function foo$$unique_1(){var b$$unique_3;a$$unique_0}" + |
| 266 |
"function boo$$unique_2(){var b$$unique_4;a$$unique_0}"); |
| 267 |
|
| 268 |
|
| 269 |
test("var a = function foo(){foo()};", |
| 270 |
"var a$$unique_0 = function foo$$unique_1(){foo$$unique_1()};"); |
| 271 |
|
| 272 |
|
| 273 |
test("try { } catch(e) {e;}", |
| 274 |
"try { } catch(e$$unique_0) {e$$unique_0;}"); |
| 275 |
test("try { } catch(e) {e;};" + |
| 276 |
"try { } catch(e) {e;}", |
| 277 |
"try { } catch(e$$unique_0) {e$$unique_0;};" + |
| 278 |
"try { } catch(e$$unique_1) {e$$unique_1;}"); |
| 279 |
test("try { } catch(e) {e; " + |
| 280 |
"try { } catch(e) {e;}};", |
| 281 |
"try { } catch(e$$unique_0) {e$$unique_0; " + |
| 282 |
"try { } catch(e$$unique_1) {e$$unique_1;} }; "); |
| 283 |
} |
| 284 |
|
| 285 |
public void testMakeLocalNamesUniqueWithoutContext2() { |
| 286 |
|
| 287 |
this.useDefaultRenamer = false; |
| 288 |
|
| 289 |
test("var _a;", |
| 290 |
"var JSCompiler__a$$unique_0"); |
| 291 |
test("var _a = function _b(_c) { var _d; };", |
| 292 |
"var JSCompiler__a$$unique_0 = function JSCompiler__b$$unique_1(" + |
| 293 |
"JSCompiler__c$$unique_2) { var JSCompiler__d$$unique_3; };"); |
| 294 |
} |
| 295 |
|
| 296 |
public void testOnlyInversion() { |
| 297 |
invert = true; |
| 298 |
test("function f(a, a$$1) {}", |
| 299 |
"function f(a, a$$0) {}"); |
| 300 |
test("function f(a$$1, b$$2) {}", |
| 301 |
"function f(a, b) {}"); |
| 302 |
test("function f(a$$1, a$$2) {}", |
| 303 |
"function f(a, a$$0) {}"); |
| 304 |
testSame("try { } catch(e) {e;}; try { } catch(e$$1) {e$$1;}"); |
| 305 |
testSame("try { } catch(e) {e; try { } catch(e$$1) {e$$1;} }; "); |
| 306 |
testSame("var a$$1;"); |
| 307 |
testSame("function f() { var $$; }"); |
| 308 |
test("var CONST = 3; var b = CONST;", |
| 309 |
"var CONST = 3; var b = CONST;"); |
| 310 |
test("function f() {var CONST = 3; var ACONST$$1 = 2;}", |
| 311 |
"function f() {var CONST = 3; var ACONST = 2;}"); |
| 312 |
} |
| 313 |
|
| 314 |
public void testOnlyInversion2() { |
| 315 |
invert = true; |
| 316 |
test("function f() {try { } catch(e) {e;}; try { } catch(e$$0) {e$$0;}}", |
| 317 |
"function f() {try { } catch(e) {e;}; try { } catch(e$$1) {e$$1;}}"); |
| 318 |
} |
| 319 |
|
| 320 |
public void testOnlyInversion3() { |
| 321 |
invert = true; |
| 322 |
test( |
| 323 |
"function x1() {" + |
| 324 |
" var a$$1;" + |
| 325 |
" function x2() {" + |
| 326 |
" var a$$2;" + |
| 327 |
" }" + |
| 328 |
" function x3() {" + |
| 329 |
" var a$$3;" + |
| 330 |
" }" + |
| 331 |
"}", |
| 332 |
"function x1() {" + |
| 333 |
" var a$$0;" + |
| 334 |
" function x2() {" + |
| 335 |
" var a;" + |
| 336 |
" }" + |
| 337 |
" function x3() {" + |
| 338 |
" var a;" + |
| 339 |
" }" + |
| 340 |
"}"); |
| 341 |
} |
| 342 |
|
| 343 |
public void testOnlyInversion4() { |
| 344 |
invert = true; |
| 345 |
test( |
| 346 |
"function x1() {" + |
| 347 |
" var a$$0;" + |
| 348 |
" function x2() {" + |
| 349 |
" var a;a$$0++" + |
| 350 |
" }" + |
| 351 |
"}", |
| 352 |
"function x1() {" + |
| 353 |
" var a$$1;" + |
| 354 |
" function x2() {" + |
| 355 |
" var a;a$$1++" + |
| 356 |
" }" + |
| 357 |
"}"); |
| 358 |
} |
| 359 |
|
| 360 |
public void testConstRemovingRename1() { |
| 361 |
removeConst = true; |
| 362 |
test("(function () {var CONST = 3; var ACONST$$1 = 2;})", |
| 363 |
"(function () {var CONST$$unique_0 = 3; var ACONST$$unique_1 = 2;})"); |
| 364 |
} |
| 365 |
|
| 366 |
public void testConstRemovingRename2() { |
| 367 |
removeConst = true; |
| 368 |
test("var CONST = 3; var b = CONST;", |
| 369 |
"var CONST$$unique_0 = 3; var b$$unique_1 = CONST$$unique_0;"); |
| 370 |
} |
| 371 |
} |