Merge pull request #26473 from Microsoft/doGlobalImplicitThisRight

Fix bad message for captured global 'this'.
This commit is contained in:
Daniel Rosenwasser 2018-08-15 13:08:38 -07:00 committed by GitHub
commit dfef227b18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 9 deletions

View File

@ -15497,9 +15497,9 @@ namespace ts {
// With noImplicitThis, functions may not reference 'this' if it has type 'any'
error(
node,
capturedByArrowFunction ?
Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation :
Diagnostics.The_containing_arrow_function_captures_the_global_value_of_this_which_implicitly_has_type_any);
capturedByArrowFunction && container.kind === SyntaxKind.SourceFile ?
Diagnostics.The_containing_arrow_function_captures_the_global_value_of_this_which_implicitly_has_type_any :
Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation);
}
return type || anyType;
}

View File

@ -1,8 +1,11 @@
tests/cases/compiler/noImplicitThisFunctions.ts(13,12): error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
tests/cases/compiler/noImplicitThisFunctions.ts(17,38): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
tests/cases/compiler/noImplicitThisFunctions.ts(13,12): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
tests/cases/compiler/noImplicitThisFunctions.ts(17,38): error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
tests/cases/compiler/noImplicitThisFunctions.ts(18,22): error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
tests/cases/compiler/noImplicitThisFunctions.ts(20,36): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
tests/cases/compiler/noImplicitThisFunctions.ts(21,50): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
==== tests/cases/compiler/noImplicitThisFunctions.ts (2 errors) ====
==== tests/cases/compiler/noImplicitThisFunctions.ts (5 errors) ====
function f1(x) {
// implicit any is still allowed
return x + 1;
@ -17,11 +20,21 @@ tests/cases/compiler/noImplicitThisFunctions.ts(17,38): error TS2683: 'this' imp
// error: this is implicitly any
return this.a + z;
~~~~
!!! error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
}
// error: `this` is `window`, but is still of type `any`
let f4: (b: number) => number = b => this.c + b;
~~~~
!!! error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
let f5 = () => () => this;
~~~~
!!! error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
let f6 = function() { return () => this; };
~~~~
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
let f7 = function() { return function() { return this } };
~~~~
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

View File

@ -16,6 +16,10 @@ function f3(z: number): number {
// error: `this` is `window`, but is still of type `any`
let f4: (b: number) => number = b => this.c + b;
let f5 = () => () => this;
let f6 = function() { return () => this; };
let f7 = function() { return function() { return this } };
//// [noImplicitThisFunctions.js]
@ -34,3 +38,9 @@ function f3(z) {
}
// error: `this` is `window`, but is still of type `any`
var f4 = function (b) { return _this.c + b; };
var f5 = function () { return function () { return _this; }; };
var f6 = function () {
var _this = this;
return function () { return _this; };
};
var f7 = function () { return function () { return this; }; };

View File

@ -33,3 +33,12 @@ let f4: (b: number) => number = b => this.c + b;
>b : Symbol(b, Decl(noImplicitThisFunctions.ts, 16, 31))
>b : Symbol(b, Decl(noImplicitThisFunctions.ts, 16, 31))
let f5 = () => () => this;
>f5 : Symbol(f5, Decl(noImplicitThisFunctions.ts, 17, 3))
let f6 = function() { return () => this; };
>f6 : Symbol(f6, Decl(noImplicitThisFunctions.ts, 19, 3))
let f7 = function() { return function() { return this } };
>f7 : Symbol(f7, Decl(noImplicitThisFunctions.ts, 20, 3))

View File

@ -46,3 +46,21 @@ let f4: (b: number) => number = b => this.c + b;
>c : any
>b : number
let f5 = () => () => this;
>f5 : () => () => any
>() => () => this : () => () => any
>() => this : () => any
>this : any
let f6 = function() { return () => this; };
>f6 : () => () => any
>function() { return () => this; } : () => () => any
>() => this : () => any
>this : any
let f7 = function() { return function() { return this } };
>f7 : () => () => any
>function() { return function() { return this } } : () => () => any
>function() { return this } : () => any
>this : any

View File

@ -1,4 +1,4 @@
tests/cases/compiler/thisBinding2.ts(10,11): error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
tests/cases/compiler/thisBinding2.ts(10,11): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
==== tests/cases/compiler/thisBinding2.ts (1 errors) ====
@ -13,7 +13,7 @@ tests/cases/compiler/thisBinding2.ts(10,11): error TS7041: The containing arrow
var x = 1;
return this.x;
~~~~
!!! error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
}();
}
}

View File

@ -17,3 +17,7 @@ function f3(z: number): number {
// error: `this` is `window`, but is still of type `any`
let f4: (b: number) => number = b => this.c + b;
let f5 = () => () => this;
let f6 = function() { return () => this; };
let f7 = function() { return function() { return this } };