| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class RescopeGlobalSymbolsTest extends CompilerTestCase { |
| 24 |
|
| 25 |
private final String namespace = "_"; |
| 26 |
|
| 27 |
public RescopeGlobalSymbolsTest() { |
| 28 |
} |
| 29 |
|
| 30 |
@Override protected CompilerPass getProcessor(Compiler compiler) { |
| 31 |
return new RescopeGlobalSymbols(compiler, namespace, false); |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
protected int getNumRepetitions() { |
| 36 |
return 1; |
| 37 |
} |
| 38 |
|
| 39 |
public void testVarDeclarations() { |
| 40 |
test("var a = 1;", "_.a = 1;"); |
| 41 |
test("var a = 1, b = 2, c = 3;", "_.a = 1; _.b = 2; _.c = 3;"); |
| 42 |
test("var a = 'str', b = 1, c = { foo: 'bar' }, d = function() {};", |
| 43 |
"_.a = 'str'; _.b = 1; _.c = { foo: 'bar' }; _.d = function() {};"); |
| 44 |
test("if(1){var x = 1;}", "if(1){_.x = 1;}"); |
| 45 |
test("var x;", ""); |
| 46 |
test("var a, b = 1;", "_.b = 1"); |
| 47 |
} |
| 48 |
|
| 49 |
public void testForLoops() { |
| 50 |
test("for (var i = 0; i < 1000; i++);", |
| 51 |
"for (_.i = 0; _.i < 1000; _.i++);"); |
| 52 |
test("for (var i = 0, c = 2; i < 1000; i++);", |
| 53 |
"for (_.i = 0, _.c = 2; _.i < 1000; _.i++);"); |
| 54 |
test("for (var i = 0, c = 2, d = 3; i < 1000; i++);", |
| 55 |
"for (_.i = 0, _.c = 2, _.d = 3; _.i < 1000; _.i++);"); |
| 56 |
test("for (var i = 0, c = 2, d = 3, e = 4; i < 1000; i++);", |
| 57 |
"for (_.i = 0, _.c = 2, _.d = 3, _.e = 4; _.i < 1000; _.i++);"); |
| 58 |
test("for (var i = 0; i < 1000;)i++;", |
| 59 |
"for (_.i = 0; _.i < 1000;)_.i++;"); |
| 60 |
test("for (var i = 0,b; i < 1000;)i++;b++", |
| 61 |
"for (_.i = 0,_.b; _.i < 1000;)_.i++;_.b++"); |
| 62 |
test("var o={};for (var i in o)i++;", |
| 63 |
"_.o={};for (_.i in _.o)_.i++;"); |
| 64 |
} |
| 65 |
|
| 66 |
public void testFunctionStatements() { |
| 67 |
test("function test(){}", |
| 68 |
"_.test=function (){}"); |
| 69 |
test("if(1)function test(){}", |
| 70 |
"if(1)_.test=function (){}"); |
| 71 |
new StringCompare().testFreeCallSemantics(); |
| 72 |
} |
| 73 |
|
| 74 |
public void testDeeperScopes() { |
| 75 |
test("var a = function(b){return b}", |
| 76 |
"_.a = function(b){return b}"); |
| 77 |
test("var a = function(b){var a; return a+b}", |
| 78 |
"_.a = function(b){var a; return a+b}"); |
| 79 |
test("var a = function(a,b){return a+b}", |
| 80 |
"_.a = function(a,b){return a+b}"); |
| 81 |
test("var x=1,a = function(b){var a; return a+b+x}", |
| 82 |
"_.x=1;_.a = function(b){var a; return a+b+_.x}"); |
| 83 |
test("var x=1,a = function(b){return function(){var a;return a+b+x}}", |
| 84 |
"_.x=1;_.a = function(b){return function(){var a; return a+b+_.x}}"); |
| 85 |
} |
| 86 |
|
| 87 |
public void testTryCatch() { |
| 88 |
test("try{var a = 1}catch(e){throw e}", |
| 89 |
"try{_.a = 1}catch(e){throw e}"); |
| 90 |
} |
| 91 |
|
| 92 |
public void testShadow() { |
| 93 |
test("var _ = 1; (function () { _ = 2 })()", |
| 94 |
"_._ = 1; (function () { _._ = 2 })()"); |
| 95 |
test("function foo() { var _ = {}; _.foo = foo; _.bar = 1; }", |
| 96 |
"_.foo = function () { var _$ = {}; _$.foo = _.foo; _$.bar = 1}"); |
| 97 |
test("function foo() { var _ = {}; _.foo = foo; _.bar = 1; " + |
| 98 |
"(function() { var _ = 0;})() }", |
| 99 |
"_.foo = function () { var _$ = {}; _$.foo = _.foo; _$.bar = 1; " + |
| 100 |
"(function() { var _$ = 0;})() }"); |
| 101 |
test("function foo() { var _ = {}; _.foo = foo; _.bar = 1; " + |
| 102 |
"var _$ = 1; }", |
| 103 |
"_.foo = function () { var _$ = {}; _$.foo = _.foo; _$.bar = 1; " + |
| 104 |
"var _$$ = 1; }"); |
| 105 |
test("function foo() { var _ = {}; _.foo = foo; _.bar = 1; " + |
| 106 |
"var _$ = 1; (function() { _ = _$ })() }", |
| 107 |
"_.foo = function () { var _$ = {}; _$.foo = _.foo; _$.bar = 1; " + |
| 108 |
"var _$$ = 1; (function() { _$ = _$$ })() }"); |
| 109 |
test("function foo() { var _ = {}; _.foo = foo; _.bar = 1; " + |
| 110 |
"var _$ = 1, _$$ = 2 (function() { _ = _$ = _$$; " + |
| 111 |
"var _$, _$$$ })() }", |
| 112 |
"_.foo = function () { var _$ = {}; _$.foo = _.foo; _$.bar = 1; " + |
| 113 |
"var _$$ = 1, _$$$ = 2 (function() { _$ = _$$ = _$$$; " + |
| 114 |
"var _$$, _$$$$ })() }"); |
| 115 |
test("function foo() { var _a = 1;}", |
| 116 |
"_.foo = function () { var _a = 1;}"); |
| 117 |
|
| 118 |
|
| 119 |
test("function foo() { var _$a = 1;}", |
| 120 |
"_.foo = function () { var _$a$ = 1;}"); |
| 121 |
} |
| 122 |
|
| 123 |
public void testExterns() { |
| 124 |
test("var document;", |
| 125 |
"document", |
| 126 |
"window.document", null, null); |
| 127 |
test("var document;", |
| 128 |
"document.getElementsByTagName('test')", |
| 129 |
"window.document.getElementsByTagName('test')", null, null); |
| 130 |
test("var document;", |
| 131 |
"window.document.getElementsByTagName('test')", |
| 132 |
"window.document.getElementsByTagName('test')", null, null); |
| 133 |
test("var document;document.getElementsByTagName", |
| 134 |
"document.getElementsByTagName('test')", |
| 135 |
"window.document.getElementsByTagName('test')", null, null); |
| 136 |
test("var document,navigator", |
| 137 |
"document.navigator;navigator", |
| 138 |
"window.document.navigator;window.navigator", null, null); |
| 139 |
test("var iframes", |
| 140 |
"function test() { iframes.resize(); }", |
| 141 |
"_.test = function() { window.iframes.resize(); }", null, null); |
| 142 |
test("var iframes", |
| 143 |
"var foo = iframes;", |
| 144 |
"_.foo = window.iframes;", null, null); |
| 145 |
|
| 146 |
test("var arguments, window, eval;", |
| 147 |
"arguments;window;eval;", |
| 148 |
"arguments;window;eval;", null, null); |
| 149 |
|
| 150 |
test("", |
| 151 |
"document", |
| 152 |
"window.document", null, null); |
| 153 |
} |
| 154 |
|
| 155 |
private class StringCompare extends CompilerTestCase { |
| 156 |
|
| 157 |
StringCompare() { |
| 158 |
super("", false); |
| 159 |
} |
| 160 |
|
| 161 |
@Override protected CompilerPass getProcessor(Compiler compiler) { |
| 162 |
return new RescopeGlobalSymbols(compiler, namespace, false); |
| 163 |
} |
| 164 |
|
| 165 |
public void testFreeCallSemantics() { |
| 166 |
test("function x(){};var y=function(){var val=x()||{}}", |
| 167 |
"_.x=function(){};_.y=function(){var val=(0,_.x)()||{}}"); |
| 168 |
test("function x(){x()}", |
| 169 |
"_.x=function(){(0,_.x)()}"); |
| 170 |
} |
| 171 |
} |
| 172 |
} |