| 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 |
public class CheckGlobalThisTest extends CompilerTestCase { |
| 23 |
public CheckGlobalThisTest() { |
| 24 |
this.parseTypeInfo = true; |
| 25 |
} |
| 26 |
|
| 27 |
@Override |
| 28 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 29 |
return new CombinedCompilerPass( |
| 30 |
compiler, new CheckGlobalThis(compiler)); |
| 31 |
} |
| 32 |
|
| 33 |
private void testFailure(String js) { |
| 34 |
testSame(js, CheckGlobalThis.GLOBAL_THIS); |
| 35 |
} |
| 36 |
|
| 37 |
public void testGlobalThis1() throws Exception { |
| 38 |
testSame("var a = this;"); |
| 39 |
} |
| 40 |
|
| 41 |
public void testGlobalThis2() { |
| 42 |
testFailure("this.foo = 5;"); |
| 43 |
} |
| 44 |
|
| 45 |
public void testGlobalThis3() { |
| 46 |
testFailure("this[foo] = 5;"); |
| 47 |
} |
| 48 |
|
| 49 |
public void testGlobalThis4() { |
| 50 |
testFailure("this['foo'] = 5;"); |
| 51 |
} |
| 52 |
|
| 53 |
public void testGlobalThis5() { |
| 54 |
testFailure("(a = this).foo = 4;"); |
| 55 |
} |
| 56 |
|
| 57 |
public void testGlobalThis6() { |
| 58 |
testSame("a = this;"); |
| 59 |
} |
| 60 |
|
| 61 |
public void testGlobalThis7() { |
| 62 |
testFailure("var a = this.foo;"); |
| 63 |
} |
| 64 |
|
| 65 |
public void testStaticFunction1() { |
| 66 |
testSame("function a() { return this; }"); |
| 67 |
} |
| 68 |
|
| 69 |
public void testStaticFunction2() { |
| 70 |
testFailure("function a() { this.complex = 5; }"); |
| 71 |
} |
| 72 |
|
| 73 |
public void testStaticFunction3() { |
| 74 |
testSame("var a = function() { return this; }"); |
| 75 |
} |
| 76 |
|
| 77 |
public void testStaticFunction4() { |
| 78 |
testFailure("var a = function() { this.foo.bar = 6; }"); |
| 79 |
} |
| 80 |
|
| 81 |
public void testStaticFunction5() { |
| 82 |
testSame("function a() { return function() { return this; } }"); |
| 83 |
} |
| 84 |
|
| 85 |
public void testStaticFunction6() { |
| 86 |
testSame("function a() { return function() { this.x = 8; } }"); |
| 87 |
} |
| 88 |
|
| 89 |
public void testStaticFunction7() { |
| 90 |
testSame("var a = function() { return function() { this.x = 8; } }"); |
| 91 |
} |
| 92 |
|
| 93 |
public void testStaticFunction8() { |
| 94 |
testFailure("var a = function() { return this.foo; };"); |
| 95 |
} |
| 96 |
|
| 97 |
public void testConstructor1() { |
| 98 |
testSame("/** @constructor */function A() { this.m2 = 5; }"); |
| 99 |
} |
| 100 |
|
| 101 |
public void testConstructor2() { |
| 102 |
testSame("/** @constructor */var A = function() { this.m2 = 5; }"); |
| 103 |
} |
| 104 |
|
| 105 |
public void testConstructor3() { |
| 106 |
testSame("/** @constructor */a.A = function() { this.m2 = 5; }"); |
| 107 |
} |
| 108 |
|
| 109 |
public void testInterface1() { |
| 110 |
testSame( |
| 111 |
"/** @interface */function A() { /** @type {string} */ this.m2; }"); |
| 112 |
} |
| 113 |
|
| 114 |
public void testOverride1() { |
| 115 |
testSame("/** @constructor */function A() { } var a = new A();" + |
| 116 |
"/** @override */ a.foo = function() { this.bar = 5; };"); |
| 117 |
} |
| 118 |
|
| 119 |
public void testThisJSDoc1() throws Exception { |
| 120 |
testSame("/** @this whatever */function h() { this.foo = 56; }"); |
| 121 |
} |
| 122 |
|
| 123 |
public void testThisJSDoc2() throws Exception { |
| 124 |
testSame("/** @this whatever */var h = function() { this.foo = 56; }"); |
| 125 |
} |
| 126 |
|
| 127 |
public void testThisJSDoc3() throws Exception { |
| 128 |
testSame("/** @this whatever */foo.bar = function() { this.foo = 56; }"); |
| 129 |
} |
| 130 |
|
| 131 |
public void testThisJSDoc4() throws Exception { |
| 132 |
testSame("/** @this whatever */function f() { this.foo = 56; }"); |
| 133 |
} |
| 134 |
|
| 135 |
public void testThisJSDoc5() throws Exception { |
| 136 |
testSame("function a() { /** @this x */function f() { this.foo = 56; } }"); |
| 137 |
} |
| 138 |
|
| 139 |
public void testMethod1() { |
| 140 |
testSame("A.prototype.m1 = function() { this.m2 = 5; }"); |
| 141 |
} |
| 142 |
|
| 143 |
public void testMethod2() { |
| 144 |
testSame("a.B.prototype.m1 = function() { this.m2 = 5; }"); |
| 145 |
} |
| 146 |
|
| 147 |
public void testMethod3() { |
| 148 |
testSame("a.b.c.D.prototype.m1 = function() { this.m2 = 5; }"); |
| 149 |
} |
| 150 |
|
| 151 |
public void testMethod4() { |
| 152 |
testSame("a.prototype['x' + 'y'] = function() { this.foo = 3; };"); |
| 153 |
} |
| 154 |
|
| 155 |
public void testPropertyOfMethod() { |
| 156 |
testFailure("a.protoype.b = {}; " + |
| 157 |
"a.prototype.b.c = function() { this.foo = 3; };"); |
| 158 |
} |
| 159 |
|
| 160 |
public void testStaticMethod1() { |
| 161 |
testFailure("a.b = function() { this.m2 = 5; }"); |
| 162 |
} |
| 163 |
|
| 164 |
public void testStaticMethod2() { |
| 165 |
testSame("a.b = function() { return function() { this.m2 = 5; } }"); |
| 166 |
} |
| 167 |
|
| 168 |
public void testStaticMethod3() { |
| 169 |
testSame("a.b.c = function() { return function() { this.m2 = 5; } }"); |
| 170 |
} |
| 171 |
|
| 172 |
public void testMethodInStaticFunction() { |
| 173 |
testSame("function f() { A.prototype.m1 = function() { this.m2 = 5; } }"); |
| 174 |
} |
| 175 |
|
| 176 |
public void testStaticFunctionInMethod1() { |
| 177 |
testSame("A.prototype.m1 = function() { function me() { this.m2 = 5; } }"); |
| 178 |
} |
| 179 |
|
| 180 |
public void testStaticFunctionInMethod2() { |
| 181 |
testSame("A.prototype.m1 = function() {" + |
| 182 |
" function me() {" + |
| 183 |
" function myself() {" + |
| 184 |
" function andI() { this.m2 = 5; } } } }"); |
| 185 |
} |
| 186 |
|
| 187 |
public void testInnerFunction1() { |
| 188 |
testFailure("function f() { function g() { return this.x; } }"); |
| 189 |
} |
| 190 |
|
| 191 |
public void testInnerFunction2() { |
| 192 |
testFailure("function f() { var g = function() { return this.x; } }"); |
| 193 |
} |
| 194 |
|
| 195 |
public void testInnerFunction3() { |
| 196 |
testFailure( |
| 197 |
"function f() { var x = {}; x.y = function() { return this.x; } }"); |
| 198 |
} |
| 199 |
|
| 200 |
public void testInnerFunction4() { |
| 201 |
testSame( |
| 202 |
"function f() { var x = {}; x.y(function() { return this.x; }); }"); |
| 203 |
} |
| 204 |
|
| 205 |
public void testIssue182a() { |
| 206 |
testFailure("var NS = {read: function() { return this.foo; }};"); |
| 207 |
} |
| 208 |
|
| 209 |
public void testIssue182b() { |
| 210 |
testFailure("var NS = {write: function() { this.foo = 3; }};"); |
| 211 |
} |
| 212 |
|
| 213 |
public void testIssue182c() { |
| 214 |
testFailure("var NS = {}; NS.write2 = function() { this.foo = 3; };"); |
| 215 |
} |
| 216 |
|
| 217 |
public void testIssue182d() { |
| 218 |
testSame("function Foo() {} " + |
| 219 |
"Foo.prototype = {write: function() { this.foo = 3; }};"); |
| 220 |
} |
| 221 |
|
| 222 |
public void testLendsAnnotation1() { |
| 223 |
testFailure("/** @constructor */ function F() {}" + |
| 224 |
"dojo.declare(F, {foo: function() { return this.foo; }});"); |
| 225 |
} |
| 226 |
|
| 227 |
public void testLendsAnnotation2() { |
| 228 |
testFailure("/** @constructor */ function F() {}" + |
| 229 |
"dojo.declare(F, /** @lends {F.bar} */ (" + |
| 230 |
" {foo: function() { return this.foo; }}));"); |
| 231 |
} |
| 232 |
|
| 233 |
public void testLendsAnnotation3() { |
| 234 |
testSame("/** @constructor */ function F() {}" + |
| 235 |
"dojo.declare(F, /** @lends {F.prototype} */ (" + |
| 236 |
" {foo: function() { return this.foo; }}));"); |
| 237 |
} |
| 238 |
|
| 239 |
public void testSuppressWarning() { |
| 240 |
testFailure("var x = function() { this.complex = 5; };"); |
| 241 |
testSame("/** @suppress {globalThis} */" + |
| 242 |
"var x = function() { this.complex = 5; };"); |
| 243 |
} |
| 244 |
} |