1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
* Copyright 2007 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
public class InlineSimpleMethodsTest extends CompilerTestCase {
public InlineSimpleMethodsTest() {
super("", false);
}
@Override
protected void setUp() throws Exception {
super.setUp();
super.enableLineNumberCheck(true);
}
@Override
protected CompilerPass getProcessor(Compiler compiler) {
return new InlineSimpleMethods(compiler);
}
/**
* Helper for tests that expects definitions to remain unchanged, such
* that {@code definitions+js} is converted to {@code definitions+expected}.
*/
private void testWithPrefix(String definitions, String js, String expected) {
test(definitions + js, definitions + expected);
}
public void testSimpleInline1() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){return this.baz};",
"var x=(new Foo).bar();var y=(new Foo).bar();",
"var x=(new Foo).baz;var y=(new Foo).baz");
}
public void testSimpleInline2() {
testWithPrefix("function Foo(){}" +
"Foo.prototype={bar:function(){return this.baz}};",
"var x=(new Foo).bar();var y=(new Foo).bar();",
"var x=(new Foo).baz;var y=(new Foo).baz");
}
public void testSimpleGetterInline1() {
// TODO(johnlenz): Support this case.
testSame("function Foo(){}" +
"Foo.prototype={get bar(){return this.baz}};" +
"var x=(new Foo).bar;var y=(new Foo).bar");
// Verify we are not confusing calling the result of an ES5 getter
// with call the getter.
testSame("function Foo(){}" +
"Foo.prototype={get bar(){return this.baz}};" +
"var x=(new Foo).bar();var y=(new Foo).bar()");
}
public void testSimpleSetterInline1() {
// Verify 'get' and 'set' are not confused.
testSame("function Foo(){}" +
"Foo.prototype={set bar(a){return this.baz}};" +
"var x=(new Foo).bar;var y=(new Foo).bar");
testSame("function Foo(){}" +
"Foo.prototype={set bar(a){return this.baz}};" +
"var x=(new Foo).bar();var y=(new Foo).bar()");
}
public void testSelfInline() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){return this.baz};",
"Foo.prototype.meth=function(){this.bar();}",
"Foo.prototype.meth=function(){this.baz}");
}
public void testCallWithArgs() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){return this.baz};",
"var x=(new Foo).bar(3,new Foo)",
"var x=(new Foo).bar(3,new Foo)");
}
public void testCallWithConstArgs() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(a){return this.baz};",
"var x=(new Foo).bar(3, 4)",
"var x=(new Foo).baz");
}
public void testNestedProperties() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){return this.baz.ooka};",
"(new Foo).bar()",
"(new Foo).baz.ooka");
}
public void testSkipComplexMethods() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){return this.baz};" +
"Foo.prototype.condy=function(){return this.baz?this.baz:1};",
"var x=(new Foo).argy()",
"var x=(new Foo).argy()");
}
public void testSkipConflictingMethods() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){return this.baz};" +
"Foo.prototype.bar=function(){return this.bazz};",
"var x=(new Foo).bar()",
"var x=(new Foo).bar()");
}
public void testSameNamesDifferentDefinitions() {
testWithPrefix("function A(){}" +
"A.prototype.g=function(){return this.a};" +
"function B(){}" +
"B.prototype.g=function(){return this.b};",
"var x=(new A).g();" +
"var y=(new B).g();" +
"var a=new A;" +
"var ag=a.g();",
"var x=(new A).g();" +
"var y=(new B).g();" +
"var a=new A;" +
"var ag=a.g()");
}
public void testSameNamesSameDefinitions() {
testWithPrefix("function A(){}" +
"A.prototype.g=function(){return this.a};" +
"function B(){}" +
"B.prototype.g=function(){return this.a};",
"var x=(new A).g();" +
"var y=(new B).g();" +
"var a=new A;" +
"var ag=a.g();",
"var x=(new A).a;" +
"var y=(new B).a;" +
"var a=new A;" +
"var ag=a.a");
}
public void testConfusingNames() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){return this.baz};",
"function bar(){var bar=function(){};bar()}",
"function bar(){var bar=function(){};bar()}");
}
public void testConstantInline() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){return 3};",
"var f=new Foo;var x=f.bar()",
"var f=new Foo;var x=3");
}
public void testConstantArrayInline() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){return[3,4]};",
"var f=new Foo;var x=f.bar()",
"var f=new Foo;var x=[3,4]");
}
public void testConstantInlineWithSideEffects() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){return 3};",
"var x=(new Foo).bar()",
"var x=(new Foo).bar()");
}
public void testEmptyMethodInline() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(a){};",
"var x=new Foo; x.bar();",
"var x=new Foo");
}
public void testEmptyMethodInlineWithSideEffects() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){};",
"(new Foo).bar();var y=new Foo;y.bar(new Foo)",
"(new Foo).bar();var y=new Foo;y.bar(new Foo)");
}
public void testEmptyMethodInlineInAssign1() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){};",
"var x=new Foo;var y=x.bar()",
"var x=new Foo;var y=void 0");
}
public void testEmptyMethodInlineInAssign2() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){};",
"var x=new Foo;var y=x.bar().toString()",
"var x=new Foo;var y=(void 0).toString()");
}
public void testNormalMethod() {
testWithPrefix("function Foo(){}" +
"Foo.prototype.bar=function(){var x=1};",
"var x=new Foo;x.bar()",
"var x=new Foo;x.bar()");
}
public void testNoInlineOfExternMethods1() {
testSame("var external={};external.charAt;",
"external.charAt()", (DiagnosticType) null);
}
public void testNoInlineOfExternMethods2() {
testSame("var external={};external.charAt=function(){};",
"external.charAt()", (DiagnosticType) null);
}
public void testNoInlineOfExternMethods3() {
testSame("var external={};external.bar=function(){};",
"function Foo(){}Foo.prototype.bar=function(){};(new Foo).bar()",
(DiagnosticType) null);
}
public void testNoInlineOfDangerousProperty() {
testSame("function Foo(){this.bar=3}" +
"Foo.prototype.bar=function(){};" +
"var x=new Foo;var y=x.bar()");
}
// Don't warn about argument naming conventions (this is done in another pass)
// opt_ parameters must not be followed by non-optional parameters.
// var_args must be last
public void testNoWarn() {
testSame("function Foo(){}" +
"Foo.prototype.bar=function(opt_a,b){var x=1};" +
"var x=new Foo;x.bar()");
testSame("function Foo(){}" +
"Foo.prototype.bar=function(var_args,b){var x=1};" +
"var x=new Foo;x.bar()");
}
public void testObjectLit() {
testSame("Foo.prototype.bar=function(){return this.baz_};" +
"var blah={bar:function(){}};" +
"(new Foo).bar()");
}
public void testObjectLit2() {
testSame("var blah={bar:function(){}};" +
"(new Foo).bar()");
}
public void testObjectLitExtern() {
String externs = "window.bridge={_sip:function(){}};";
testSame(externs, "window.bridge._sip()", null);
}
public void testExternFunction() {
String externs = "function emptyFunction() {}";
testSame(externs,
"function Foo(){this.empty=emptyFunction}" +
"(new Foo).empty()", null);
}
public void testIssue2508576_1() {
// Method defined by an extern should be left alone.
String externs = "function alert(a) {}";
testSame(externs, "({a:alert,b:alert}).a(\"a\")", null);
}
public void testIssue2508576_2() {
// Anonymous object definition with a side-effect should be left alone.
testSame("({a:function(){},b:x()}).a(\"a\")");
}
public void testIssue2508576_3() {
// Anonymous object definition without side-effect should be removed.
test("({a:function(){},b:alert}).a(\"a\")", "");
}
public void testAnonymousGet() {
// Anonymous object definition without side-effect should be removed.
testSame("({get a(){return function(){}},b:alert}).a(\"a\")");
testSame("({get a(){},b:alert}).a(\"a\")");
testSame("({get a(){},b:alert}).a");
}
public void testAnonymousSet() {
// Anonymous object definition without side-effect should be removed.
testSame("({set a(b){return function(){}},b:alert}).a(\"a\")");
testSame("({set a(b){},b:alert}).a(\"a\")");
testSame("({set a(b){},b:alert}).a");
}
}
|