Merge pull request #25978 from Microsoft/capturedGlobalThis

Better error message for captured global 'this' in noImplicitThis
This commit is contained in:
Daniel Rosenwasser 2018-07-26 13:20:05 -07:00 committed by GitHub
commit 6b60babeaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 10 deletions

View File

@ -15279,7 +15279,7 @@ namespace ts {
// Stop at the first arrow function so that we can
// tell whether 'this' needs to be captured.
let container = getThisContainer(node, /* includeArrowFunctions */ true);
let needToCaptureLexicalThis = false;
let capturedByArrowFunction = false;
if (container.kind === SyntaxKind.Constructor) {
checkThisBeforeSuper(node, container, Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class);
@ -15288,9 +15288,7 @@ namespace ts {
// Now skip arrow functions to get the "real" owner of 'this'.
if (container.kind === SyntaxKind.ArrowFunction) {
container = getThisContainer(container, /* includeArrowFunctions */ false);
// When targeting es6, arrow function lexically bind "this" so we do not need to do the work of binding "this" in emitted code
needToCaptureLexicalThis = (languageVersion < ScriptTarget.ES2015);
capturedByArrowFunction = true;
}
switch (container.kind) {
@ -15320,14 +15318,19 @@ namespace ts {
break;
}
if (needToCaptureLexicalThis) {
// When targeting es6, mark that we'll need to capture `this` in its lexically bound scope.
if (capturedByArrowFunction && languageVersion < ScriptTarget.ES2015) {
captureLexicalThis(node, container);
}
const type = tryGetThisTypeAt(node, container);
if (!type && noImplicitThis) {
// With noImplicitThis, functions may not reference 'this' if it has type 'any'
error(node, Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation);
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);
}
return type || anyType;
}

View File

@ -3921,6 +3921,10 @@
"category": "Error",
"code": 7040
},
"The containing arrow function captures the global value of 'this' which implicitly has type 'any'.": {
"category": "Error",
"code": 7041
},
"You cannot rename this element.": {
"category": "Error",
"code": 8000

View File

@ -1,4 +1,4 @@
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(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.
@ -17,7 +17,7 @@ tests/cases/compiler/noImplicitThisFunctions.ts(17,38): error TS2683: 'this' imp
// error: this is implicitly any
return this.a + z;
~~~~
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
!!! error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
}
// error: `this` is `window`, but is still of type `any`

View File

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