| 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.Sets; |
| 20 |
|
| 21 |
import java.util.Set; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class StripCodeTest extends CompilerTestCase { |
| 28 |
|
| 29 |
private static final String EXTERNS = ""; |
| 30 |
|
| 31 |
public StripCodeTest() { |
| 32 |
super(EXTERNS, true); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private static StripCode createLoggerInstance(Compiler compiler) { |
| 42 |
Set<String> stripTypes = Sets.newHashSet( |
| 43 |
"goog.debug.DebugWindow", |
| 44 |
"goog.debug.FancyWindow", |
| 45 |
"goog.debug.Formatter", |
| 46 |
"goog.debug.HtmlFormatter", |
| 47 |
"goog.debug.TextFormatter", |
| 48 |
"goog.debug.Logger", |
| 49 |
"goog.debug.LogManager", |
| 50 |
"goog.debug.LogRecord", |
| 51 |
"goog.net.BrowserChannel.LogSaver", |
| 52 |
"GA_GoogleDebugger"); |
| 53 |
|
| 54 |
Set<String> stripNames = Sets.newHashSet( |
| 55 |
"logger", |
| 56 |
"logger_", |
| 57 |
"debugWindow", |
| 58 |
"debugWindow_", |
| 59 |
"logFormatter_", |
| 60 |
"logBuffer_"); |
| 61 |
|
| 62 |
Set<String> stripNamePrefixes = Sets.newHashSet("trace"); |
| 63 |
Set<String> stripTypePrefixes = Sets.newHashSet("e.f.Trace"); |
| 64 |
|
| 65 |
return new StripCode(compiler, stripTypes, stripNames, stripTypePrefixes, |
| 66 |
stripNamePrefixes); |
| 67 |
} |
| 68 |
|
| 69 |
@Override public CompilerPass getProcessor(Compiler compiler) { |
| 70 |
return createLoggerInstance(compiler); |
| 71 |
} |
| 72 |
|
| 73 |
public void testLoggerDefinedInConstructor() { |
| 74 |
test("a.b.c = function() {" + |
| 75 |
" this.logger = goog.debug.Logger.getLogger('a.b.c');" + |
| 76 |
"};", |
| 77 |
"a.b.c=function(){}"); |
| 78 |
} |
| 79 |
|
| 80 |
public void testLoggerDefinedInPrototype1() { |
| 81 |
test("a.b.c = function() {};" + |
| 82 |
"a.b.c.prototype.logger = goog.debug.Logger.getLogger('a.b.c');", |
| 83 |
"a.b.c=function(){}"); |
| 84 |
} |
| 85 |
|
| 86 |
public void testLoggerDefinedInPrototype2() { |
| 87 |
test("a.b.c = function() {};" + |
| 88 |
"a.b.c.prototype = {logger: goog.debug.Logger.getLogger('a.b.c')}", |
| 89 |
"a.b.c = function() {};" + |
| 90 |
"a.b.c.prototype = {}"); |
| 91 |
} |
| 92 |
|
| 93 |
public void testLoggerDefinedInPrototype3() { |
| 94 |
test("a.b.c = function() {};" + |
| 95 |
"a.b.c.prototype = { " + |
| 96 |
" get logger() {return goog.debug.Logger.getLogger('a.b.c')}" + |
| 97 |
"}", |
| 98 |
"a.b.c = function() {};" + |
| 99 |
"a.b.c.prototype = {}"); |
| 100 |
} |
| 101 |
|
| 102 |
public void testLoggerDefinedInPrototype4() { |
| 103 |
test("a.b.c = function() {};" + |
| 104 |
"a.b.c.prototype = { " + |
| 105 |
" set logger(a) {this.x = goog.debug.Logger.getLogger('a.b.c')}" + |
| 106 |
"}", |
| 107 |
"a.b.c = function() {};" + |
| 108 |
"a.b.c.prototype = {}"); |
| 109 |
} |
| 110 |
|
| 111 |
public void testLoggerDefinedInPrototype5() { |
| 112 |
test("a.b.c = function() {};" + |
| 113 |
"a.b.c.prototype = { " + |
| 114 |
" get f() {return this.x;}," + |
| 115 |
" set f(a) {this.x = goog.debug.Logger.getLogger('a.b.c')}" + |
| 116 |
"}", |
| 117 |
"a.b.c = function() {};" + |
| 118 |
"a.b.c.prototype = { " + |
| 119 |
" get f() {return this.x;}," + |
| 120 |
" set f(a) {this.x = null}" + |
| 121 |
"}"); |
| 122 |
} |
| 123 |
|
| 124 |
public void testLoggerDefinedStatically() { |
| 125 |
test("a.b.c = function() {};" + |
| 126 |
"a.b.c.logger = goog.debug.Logger.getLogger('a.b.c');", |
| 127 |
"a.b.c=function(){}"); |
| 128 |
} |
| 129 |
|
| 130 |
public void testLoggerDefinedInObjectLiteral1() { |
| 131 |
test("a.b.c = {" + |
| 132 |
" x: 0," + |
| 133 |
" logger: goog.debug.Logger.getLogger('a.b.c')" + |
| 134 |
"};", |
| 135 |
"a.b.c={x:0}"); |
| 136 |
} |
| 137 |
|
| 138 |
public void testLoggerDefinedInObjectLiteral2() { |
| 139 |
test("a.b.c = {" + |
| 140 |
" x: 0," + |
| 141 |
" get logger() {return goog.debug.Logger.getLogger('a.b.c')}" + |
| 142 |
"};", |
| 143 |
"a.b.c={x:0}"); |
| 144 |
} |
| 145 |
|
| 146 |
public void testLoggerDefinedInObjectLiteral3() { |
| 147 |
test("a.b.c = {" + |
| 148 |
" x: null," + |
| 149 |
" get logger() {return this.x}," + |
| 150 |
" set logger(a) {this.x = goog.debug.Logger.getLogger(a)}" + |
| 151 |
"};", |
| 152 |
"a.b.c={x:null}"); |
| 153 |
} |
| 154 |
|
| 155 |
public void testLoggerDefinedInObjectLiteral4() { |
| 156 |
test("a.b.c = {" + |
| 157 |
" x: null," + |
| 158 |
" get y() {return this.x}," + |
| 159 |
" set y(a) {this.x = goog.debug.Logger.getLogger(a)}" + |
| 160 |
"};", |
| 161 |
"a.b.c = {" + |
| 162 |
" x: null," + |
| 163 |
" get y() {return this.x}," + |
| 164 |
" set y(a) {this.x = null}" + |
| 165 |
"};"); |
| 166 |
} |
| 167 |
|
| 168 |
public void testLoggerDefinedInPrototypeAndUsedInConstructor() { |
| 169 |
test("a.b.c = function(level) {" + |
| 170 |
" if (!this.logger.isLoggable(level)) {" + |
| 171 |
" this.logger.setLevel(level);" + |
| 172 |
" }" + |
| 173 |
" this.logger.log(level, 'hi');" + |
| 174 |
"};" + |
| 175 |
"a.b.c.prototype.logger = goog.debug.Logger.getLogger('a.b.c');" + |
| 176 |
"a.b.c.prototype.go = function() { this.logger.finer('x'); };", |
| 177 |
"a.b.c=function(level){if(!null);};" + |
| 178 |
"a.b.c.prototype.go=function(){}"); |
| 179 |
} |
| 180 |
|
| 181 |
public void testLoggerDefinedStaticallyAndUsedInConstructor() { |
| 182 |
test("a.b.c = function(level) {" + |
| 183 |
" if (!a.b.c.logger.isLoggable(level)) {" + |
| 184 |
" a.b.c.logger.setLevel(level);" + |
| 185 |
" }" + |
| 186 |
" a.b.c.logger.log(level, 'hi');" + |
| 187 |
"};" + |
| 188 |
"a.b.c.logger = goog.debug.Logger.getLogger('a.b.c');", |
| 189 |
"a.b.c=function(level){if(!null);}"); |
| 190 |
} |
| 191 |
|
| 192 |
public void testLoggerVarDeclaration() { |
| 193 |
test("var logger = opt_logger || goog.debug.LogManager.getRoot();", ""); |
| 194 |
} |
| 195 |
|
| 196 |
public void testLoggerMethodCallByVariableType() { |
| 197 |
test("var x = goog.debug.Logger.getLogger('a.b.c'); y.info(a); x.info(a);", |
| 198 |
"y.info(a)"); |
| 199 |
} |
| 200 |
|
| 201 |
public void testSubPropertyAccessByVariableName() { |
| 202 |
test("var x, y = goog.debug.Logger.getLogger('a.b.c');" + |
| 203 |
"var logger = x;" + |
| 204 |
"var curlevel = logger.level_ ? logger.getLevel().name : 3;", |
| 205 |
"var x;var curlevel=null?null:3"); |
| 206 |
} |
| 207 |
|
| 208 |
public void testPrefixedVariableName() { |
| 209 |
test("this.blcLogger_ = goog.debug.Logger.getLogger('a.b.c');" + |
| 210 |
"this.blcLogger_.fine('Raised dirty states.');", ""); |
| 211 |
} |
| 212 |
|
| 213 |
public void testPrefixedPropertyName() { |
| 214 |
test("a.b.c.staticLogger_ = goog.debug.Logger.getLogger('a.b.c');" + |
| 215 |
"a.b.c.staticLogger_.fine('-' + a.b.c.d_())", ""); |
| 216 |
} |
| 217 |
|
| 218 |
public void testPrefixedClassName() { |
| 219 |
test("a.b.MyLogger = function(logger) {" + |
| 220 |
" this.logger_ = logger;" + |
| 221 |
"};" + |
| 222 |
"a.b.MyLogger.prototype.shout = function(msg, opt_x) {" + |
| 223 |
" this.logger_.log(goog.debug.Logger.Level.SHOUT, msg, opt_x);" + |
| 224 |
"};", |
| 225 |
"a.b.MyLogger=function(logger){};" + |
| 226 |
"a.b.MyLogger.prototype.shout=function(msg,opt_x){}"); |
| 227 |
} |
| 228 |
|
| 229 |
public void testLoggerClassDefinition() { |
| 230 |
test("goog.debug.Logger=function(name){this.name_=name}", ""); |
| 231 |
} |
| 232 |
|
| 233 |
public void testStaticLoggerPropertyDefinition() { |
| 234 |
test("goog.debug.Logger.Level.SHOUT=" + |
| 235 |
"new goog.debug.Logger.Level(x,1200)", ""); |
| 236 |
} |
| 237 |
|
| 238 |
public void testStaticLoggerMethodDefinition() { |
| 239 |
test("goog.debug.Logger.getLogger=function(name){" + |
| 240 |
"return goog.debug.LogManager.getLogger(name)" + |
| 241 |
"};", ""); |
| 242 |
} |
| 243 |
|
| 244 |
public void testPrototypeFieldDefinition() { |
| 245 |
test("goog.debug.Logger.prototype.level_=null;", ""); |
| 246 |
} |
| 247 |
|
| 248 |
public void testPrototypeFieldDefinitionWithoutAssignment() { |
| 249 |
test("goog.debug.Logger.prototype.level_;", ""); |
| 250 |
} |
| 251 |
|
| 252 |
public void testPrototypeMethodDefinition() { |
| 253 |
test("goog.debug.Logger.prototype.addHandler=" + |
| 254 |
"function(handler){this.handlers_.push(handler)};", ""); |
| 255 |
} |
| 256 |
|
| 257 |
public void testPublicPropertyAssignment() { |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
testSame("rootLogger.someProperty=3"); |
| 262 |
testSame("this.blcLogger_.level=x"); |
| 263 |
testSame("goog.ui.Component.logger.prop=y"); |
| 264 |
} |
| 265 |
|
| 266 |
public void testGlobalCallWithStrippedType() { |
| 267 |
testSame("window.alert(goog.debug.Logger)"); |
| 268 |
} |
| 269 |
|
| 270 |
public void testClassDefiningCallWithStripType1() { |
| 271 |
test("goog.debug.Logger.inherits(Object)", ""); |
| 272 |
} |
| 273 |
|
| 274 |
public void testClassDefiningCallWithStripType2() { |
| 275 |
test("goog.formatter=function(){};" + |
| 276 |
"goog.inherits(goog.debug.Formatter,goog.formatter)", |
| 277 |
"goog.formatter=function(){}"); |
| 278 |
} |
| 279 |
|
| 280 |
public void testClassDefiningCallWithStripType3() { |
| 281 |
test("goog.formatter=function(){};" + |
| 282 |
"goog.inherits(goog.formatter,goog.debug.Formatter)", |
| 283 |
null, StripCode.STRIP_TYPE_INHERIT_ERROR); |
| 284 |
} |
| 285 |
|
| 286 |
public void testClassDefiningCallWithStripType4() { |
| 287 |
test("goog.formatter=function(){};" + |
| 288 |
"goog.formatter.inherits(goog.debug.Formatter)", |
| 289 |
null, StripCode.STRIP_TYPE_INHERIT_ERROR); |
| 290 |
} |
| 291 |
|
| 292 |
public void testClassDefiningCallWithStripType5() { |
| 293 |
testSame("goog.formatter=function(){};" + |
| 294 |
"goog.formatter.inherits(goog.debug.FormatterFoo)"); |
| 295 |
} |
| 296 |
|
| 297 |
public void testClassDefiningCallWithStripType6() { |
| 298 |
test("goog.formatter=function(){};" + |
| 299 |
"goog.formatter.inherits(goog.debug.Formatter.Foo)", |
| 300 |
null, StripCode.STRIP_TYPE_INHERIT_ERROR); |
| 301 |
} |
| 302 |
|
| 303 |
public void testClassDefiningCallWithStripType7() { |
| 304 |
test("goog.inherits(goog.debug.TextFormatter,goog.debug.Formatter)", ""); |
| 305 |
} |
| 306 |
|
| 307 |
public void testClassDefiningCallWithStripType8() { |
| 308 |
|
| 309 |
test("goog.debug.DebugWindow = function(){}", ""); |
| 310 |
test("goog.inherits(goog.debug.DebugWindow,Base)", ""); |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
testSame("goog.debug.DebugWindowFoo=function(){}"); |
| 315 |
testSame("goog.inherits(goog.debug.DebugWindowFoo,Base)"); |
| 316 |
testSame("goog.debug.DebugWindowFoo"); |
| 317 |
testSame("goog.debug.DebugWindowFoo=1"); |
| 318 |
|
| 319 |
|
| 320 |
test("goog.debug.DebugWindow.Foo=function(){}", ""); |
| 321 |
test("goog.inherits(goog.debug.DebugWindow.Foo,Base)", ""); |
| 322 |
test("goog.debug.DebugWindow.Foo", ""); |
| 323 |
test("goog.debug.DebugWindow.Foo=1", ""); |
| 324 |
} |
| 325 |
|
| 326 |
public void testPropertyWithEmptyStringKey() { |
| 327 |
test("goog.format.NUMERIC_SCALES_BINARY_ = {'': 1};", |
| 328 |
"goog.format.NUMERIC_SCALES_BINARY_={\"\":1}"); |
| 329 |
} |
| 330 |
|
| 331 |
public void testVarinIf() { |
| 332 |
test("if(x)var logger=null;else foo()", "if(x);else foo()"); |
| 333 |
} |
| 334 |
|
| 335 |
public void testGetElemInIf() { |
| 336 |
test("var logger=null;if(x)logger[f];else foo()", "if(x);else foo()"); |
| 337 |
} |
| 338 |
|
| 339 |
public void testAssignInIf() { |
| 340 |
test("var logger=null;if(x)logger=1;else foo()", |
| 341 |
"if(x);else foo()"); |
| 342 |
} |
| 343 |
|
| 344 |
public void testNamePrefix() { |
| 345 |
test("a = function(traceZZZ) {}; a.prototype.traceXXX = {x: 1};" + |
| 346 |
"a.prototype.z = function() { this.traceXXX.f(); };" + |
| 347 |
"var traceYYY = 0;", |
| 348 |
"a=function(traceZZZ){};a.prototype.z=function(){}"); |
| 349 |
} |
| 350 |
|
| 351 |
public void testTypePrefix() { |
| 352 |
test("e.f.TraceXXX = function() {}; " + |
| 353 |
"e.f.TraceXXX.prototype.yyy = 2;", ""); |
| 354 |
} |
| 355 |
|
| 356 |
public void testStripCallsToStrippedNames() { |
| 357 |
test("a = function() { this.logger_ = function(msg){}; };" + |
| 358 |
"a.prototype.b = function() { this.logger_('hi'); }", |
| 359 |
"a=function(){};a.prototype.b=function(){}"); |
| 360 |
test("a = function() {};" + |
| 361 |
"a.prototype.logger_ = function(msg) {};" + |
| 362 |
"a.prototype.b = function() { this.logger_('hi'); }", |
| 363 |
"a=function(){};a.prototype.b=function(){}"); |
| 364 |
} |
| 365 |
|
| 366 |
public void testStripVarsInitializedFromStrippedNames() { |
| 367 |
test("a = function() { this.logger_ = function() { return 1; }; };" + |
| 368 |
"a.prototype.b = function() { " + |
| 369 |
" var one = this.logger_(); if (one) foo() }", |
| 370 |
"a=function(){};a.prototype.b=function(){if(null)foo()}"); |
| 371 |
} |
| 372 |
|
| 373 |
public void testReportErrorOnStripInNestedAssignment() { |
| 374 |
|
| 375 |
test("(foo.logger_ = 7) + 8", |
| 376 |
"(foo.logger_ = 7) + 8", |
| 377 |
StripCode.STRIP_ASSIGNMENT_ERROR); |
| 378 |
|
| 379 |
|
| 380 |
test("(goog.debug.Logger.foo = 7) + 8", |
| 381 |
"(goog.debug.Logger.foo = 7) + 8", |
| 382 |
StripCode.STRIP_ASSIGNMENT_ERROR); |
| 383 |
|
| 384 |
|
| 385 |
test("(GA_GoogleDebugger.foo = 7) + 8", |
| 386 |
"(GA_GoogleDebugger.foo = 7) + 8", |
| 387 |
StripCode.STRIP_ASSIGNMENT_ERROR); |
| 388 |
} |
| 389 |
|
| 390 |
public void testNewOperatior1() { |
| 391 |
test("function foo() {} foo.bar = new goog.debug.Logger();", |
| 392 |
"function foo() {} foo.bar = null;"); |
| 393 |
} |
| 394 |
|
| 395 |
public void testNewOperatior2() { |
| 396 |
test("function foo() {} foo.bar = (new goog.debug.Logger()).foo();", |
| 397 |
"function foo() {} foo.bar = null;"); |
| 398 |
} |
| 399 |
|
| 400 |
public void testCrazyNesting1() { |
| 401 |
test("var x = {}; x[new goog.debug.Logger()] = 3;", |
| 402 |
"var x = {}; x[null] = 3;"); |
| 403 |
} |
| 404 |
|
| 405 |
public void testCrazyNesting2() { |
| 406 |
test("var x = {}; x[goog.debug.Logger.getLogger()] = 3;", |
| 407 |
"var x = {}; x[null] = 3;"); |
| 408 |
} |
| 409 |
|
| 410 |
public void testCrazyNesting3() { |
| 411 |
test("var x = function() {}; x(new goog.debug.Logger());", |
| 412 |
"var x = function() {}; x(null);"); |
| 413 |
} |
| 414 |
|
| 415 |
public void testCrazyNesting4() { |
| 416 |
test("var x = function() {}; x(goog.debug.Logger.getLogger());", |
| 417 |
"var x = function() {}; x(null);"); |
| 418 |
} |
| 419 |
|
| 420 |
public void testCrazyNesting5() { |
| 421 |
test("var x = function() {}; var y = {}; " + |
| 422 |
"var z = goog.debug.Logger.getLogger(); x(y[z['foo']]);", |
| 423 |
"var x = function() {}; var y = {}; x(y[null]);"); |
| 424 |
} |
| 425 |
} |