| OLD | NEW |
| (Empty) | |
| 1 this.foo = this.foo + 1; // OK - outside of function. |
| 2 |
| 3 function f() { |
| 4 this.foo = this.foo + 1; // OK - global |this|. |
| 5 } |
| 6 |
| 7 /** |
| 8 * @constructor |
| 9 */ |
| 10 function TypeOne() { |
| 11 this.foo = this.foo + 1; // OK - object field in ctor. |
| 12 |
| 13 /** |
| 14 * @this {TypeOne} |
| 15 */ |
| 16 function callbackOne() { |
| 17 this.foo = this.foo + 1; // OK - @this declared. |
| 18 |
| 19 function badInnerCallback() { |
| 20 this.foo = this.foo + 2; // ERROR - @this not declared. |
| 21 } |
| 22 } |
| 23 |
| 24 function badCallbackInCtor() { |
| 25 this.foo = this.foo + 1; // ERROR - @this not declared. |
| 26 } |
| 27 } |
| 28 |
| 29 TypeOne.prototype = { |
| 30 addListener: function(callback) |
| 31 { |
| 32 if (typeof callback !== "function") |
| 33 throw "addListener: callback is not a function"; |
| 34 if (this._listeners.length === 0) |
| 35 extensionServer.sendRequest({ command: commands.Subscribe, type: thi
s._type }); |
| 36 this._listeners.push(callback); |
| 37 extensionServer.registerHandler("notify-" + this._type, this._dispatch.b
ind(this)); |
| 38 }, |
| 39 |
| 40 funcOne: function() { |
| 41 this.foo = this.foo + 1; // OK - in method. |
| 42 }, |
| 43 |
| 44 funcTwo: function() { |
| 45 /** |
| 46 * @this {TypeOne} |
| 47 */ |
| 48 function callback() { |
| 49 this.foo = this.foo + 1; // OK - @this declared. |
| 50 } |
| 51 }, |
| 52 |
| 53 funcThree: function() { |
| 54 function badCallbackInMethod() { |
| 55 this.foo = this.foo + 1; // ERROR - @this not declared. |
| 56 } |
| 57 } |
| 58 } |
| 59 |
| 60 |
| 61 /** |
| 62 * @constructor |
| 63 */ |
| 64 TypeTwo = function() { |
| 65 this.bar = this.bar + 1; // OK - object field in ctor. |
| 66 |
| 67 /** |
| 68 * @this {TypeTwo} |
| 69 */ |
| 70 function callbackOne() { |
| 71 this.bar = this.bar + 1; // OK - @this declared. |
| 72 |
| 73 function badInnerCallback() { |
| 74 this.bar = this.bar + 2; // ERROR - @this not declared. |
| 75 } |
| 76 } |
| 77 |
| 78 function badCallbackInCtor() { |
| 79 this.bar = this.bar + 1; // ERROR - @this not declared. |
| 80 } |
| 81 } |
| 82 |
| 83 TypeTwo.prototype = { |
| 84 funcOne: function() { |
| 85 this.bar = this.bar + 1; // OK - in method. |
| 86 }, |
| 87 |
| 88 funcTwo: function() { |
| 89 /** |
| 90 * @this {TypeTwo} |
| 91 */ |
| 92 function callback() { |
| 93 this.bar = this.bar + 1; // OK - @this declared. |
| 94 } |
| 95 }, |
| 96 |
| 97 funcThree: function() { |
| 98 function badCallbackInMethod() { |
| 99 this.bar = this.bar + 1; // ERROR - @this not declared. |
| 100 } |
| 101 } |
| 102 } |
| 103 |
| 104 /** |
| 105 * @return {!Object} |
| 106 */ |
| 107 function returnConstructedObject() { |
| 108 |
| 109 /** |
| 110 * @constructor |
| 111 */ |
| 112 TypeThree = function() { |
| 113 this.bar = this.bar + 1; // OK - object field in ctor. |
| 114 |
| 115 /** |
| 116 * @this {TypeThree} |
| 117 */ |
| 118 function callbackOne() { |
| 119 this.bar = this.bar + 1; // OK - @this declared. |
| 120 |
| 121 function badInnerCallback() { |
| 122 this.bar = this.bar + 2; // ERROR - @this not declared. |
| 123 } |
| 124 } |
| 125 |
| 126 function badCallbackInCtor() { |
| 127 this.bar = this.bar + 1; // ERROR - @this not declared. |
| 128 } |
| 129 } |
| 130 |
| 131 TypeThree.prototype = { |
| 132 funcOne: function() { |
| 133 this.bar = this.bar + 1; // OK - in method. |
| 134 }, |
| 135 |
| 136 funcTwo: function() { |
| 137 /** |
| 138 * @this {TypeThree} |
| 139 */ |
| 140 function callback() { |
| 141 this.bar = this.bar + 1; // OK - @this declared. |
| 142 } |
| 143 }, |
| 144 |
| 145 funcThree: function() { |
| 146 function badCallbackInMethod() { |
| 147 this.bar = this.bar + 1; // ERROR - @this not declared. |
| 148 } |
| 149 |
| 150 /** |
| 151 * @this {TypeOne} |
| 152 */ |
| 153 function callbackNotReferencingThis() { |
| 154 return 3; // ERROR - @this for a function not referencing |this|. |
| 155 } |
| 156 } |
| 157 } |
| 158 |
| 159 return new TypeThree(); |
| 160 } |
| 161 |
| 162 var object = { |
| 163 /** |
| 164 * @this {MyType} |
| 165 */ |
| 166 value: function() |
| 167 { |
| 168 this.foo = 1; // OK - @this annotated. |
| 169 } |
| 170 }; |
| 171 |
| 172 (function() { |
| 173 var object = { |
| 174 /** |
| 175 * @this {MyType} |
| 176 */ |
| 177 value: function() |
| 178 { |
| 179 this.foo = 1; // OK - @this annotated. |
| 180 } |
| 181 }; |
| 182 })(); |
| 183 |
| 184 /** |
| 185 * @constructor |
| 186 */ |
| 187 var ReceiverTest = function() {} |
| 188 |
| 189 ReceiverTest.prototype = { |
| 190 memberOne: function() { |
| 191 var badMemberBinding1 = this.memberTwo.bind(null); // ERROR - Member not
bound to |this| receiver. |
| 192 var badMemberBinding2 = this.memberTwo.bind(bar); // ERROR - Member not
bound to |this| receiver. |
| 193 var goodMemberBinding = this.memberTwo.bind(this); |
| 194 |
| 195 /** @this {ReceiverTest} */ |
| 196 function callbackWithThis() |
| 197 { |
| 198 this.memberTwo(); |
| 199 } |
| 200 |
| 201 function callbackNoThis() |
| 202 { |
| 203 return 42; |
| 204 } |
| 205 |
| 206 callbackWithThis.call(this); |
| 207 callbackWithThis.call(foo); |
| 208 callbackNoThis(); |
| 209 callbackNoThis.call(null, 1); |
| 210 callbackNoThis.apply(null, [2]); |
| 211 callbackNoThis.bind(null, 1); |
| 212 this.memberTwo(callbackWithThis.bind(this, 1)); |
| 213 this.memberTwo(callbackWithThis.bind(foo, 1)); |
| 214 this.memberTwo(callbackNoThis); |
| 215 this.memberTwo(callbackNoThis.bind(null)); |
| 216 |
| 217 callbackWithThis(); // ERROR - No receiver. |
| 218 callbackWithThis.call(); // ERROR - No receiver. |
| 219 callbackWithThis.call(null); // ERROR - No receiver. |
| 220 callbackWithThis.apply(); // ERROR - No receiver. |
| 221 callbackWithThis.apply(null); // ERROR - No receiver. |
| 222 callbackNoThis.call(this); // ERROR - Function has no @this annotation. |
| 223 callbackNoThis.call(foo); // ERROR - Function has no @this annotation. |
| 224 callbackNoThis.apply(this); // ERROR - Function has no @this annotation. |
| 225 callbackNoThis.bind(this); // ERROR - Function has no @this annotation. |
| 226 |
| 227 this.memberTwo(callbackWithThis); // ERROR - Used as argument with no bo
und receiver. |
| 228 this.memberTwo(callbackWithThis.bind(null, 2)); // ERROR - Used as argum
ent with no bound receiver (null means "no receiver"). |
| 229 this.memberTwo(callbackNoThis.bind(this)); // ERROR - Bound to a receive
r but has no @this annotation. |
| 230 this.memberTwo(callbackNoThis.bind(foo)); // ERROR - Bound to a receiver
but has no @this annotation. |
| 231 |
| 232 // Callback receivers specified as arguments. |
| 233 |
| 234 array.forEach(callbackWithThis, this); |
| 235 array.forEach(callbackNoThis); |
| 236 |
| 237 array.forEach(callbackWithThis); // ERROR - No receiver. |
| 238 array.forEach(callbackNoThis, this); // ERROR - Receiver for callback wi
th no @this annotation. |
| 239 |
| 240 var isMultiline = false; |
| 241 |
| 242 element.addEventListener("click", callbackNoThis); |
| 243 element.addEventListener("click", callbackNoThis, true); |
| 244 element.addEventListener("click", callbackNoThis, false); |
| 245 element.addEventListener("click", callbackNoThis, isMultiline); // OK -
ignored. |
| 246 |
| 247 element.addEventListener("click", callbackNoThis, this); // ERROR. |
| 248 |
| 249 element.addEventListener("click", callbackWithThis, this); |
| 250 element.addEventListener("click", callbackWithThis, foo); // OK - ignore
d. |
| 251 element.addEventListener("click", callbackWithThis, isMultiline); // OK
- ignored. |
| 252 |
| 253 element.addEventListener("click", callbackWithThis, true); // ERROR. |
| 254 element.addEventListener("click", callbackWithThis, false); // ERROR. |
| 255 |
| 256 // DevTools-specific. |
| 257 |
| 258 /** |
| 259 * @suppressReceiverCheck |
| 260 * @this {Object} |
| 261 */ |
| 262 function ignoredCallbackWithThis() |
| 263 { |
| 264 this.foo = 1; |
| 265 } |
| 266 object.callFunction(func, [], ignoredCallbackWithThis); // OK - ignored. |
| 267 |
| 268 function callbackReferencingThisNotAnnotated() |
| 269 { |
| 270 this.foo = 2; |
| 271 } |
| 272 this.memberTwo(callbackReferencingThisNotAnnotated.bind(this)); // OK -
No @this annotation, but references |this|. |
| 273 |
| 274 /** |
| 275 * @this {Object} |
| 276 */ |
| 277 function callbackNotReferencingThisAnnotated() |
| 278 { |
| 279 } |
| 280 this.memberTwo(callbackNotReferencingThisAnnotated); // OK - Has @this a
nnotation, but does not reference |this|. |
| 281 }, |
| 282 |
| 283 memberTwo: function(arg) {} |
| 284 } |
| 285 |
| 286 /** |
| 287 * @constructor |
| 288 */ |
| 289 function ReceiverWithArrowsTest() |
| 290 { |
| 291 this._q = []; |
| 292 } |
| 293 |
| 294 ReceiverWithArrowsTest.prototype = { |
| 295 /** |
| 296 * @param {string} s |
| 297 */ |
| 298 memberOne: function(s) |
| 299 { |
| 300 /** |
| 301 * @param {string} a |
| 302 * @this {C} |
| 303 */ |
| 304 function testArrowFunctionReferencingThis(a) |
| 305 { |
| 306 (x => this._q.push(x))(a); |
| 307 } |
| 308 |
| 309 /** |
| 310 * @param {string} a |
| 311 * @this {C} |
| 312 */ |
| 313 function testNestedArrowFunctionReferencingThis(a) |
| 314 { |
| 315 (x => (y => this._q.push(y))(x))(a); |
| 316 } |
| 317 |
| 318 /** |
| 319 * @param {string} a |
| 320 */ |
| 321 function testArrowFunctionReferencingThisMissingAnnotation(a) |
| 322 { |
| 323 (x => this._q.push(x))(a); |
| 324 } |
| 325 |
| 326 } |
| 327 }; |
| OLD | NEW |