mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-09 02:30:15 -06:00
Merge pull request #25978 from Microsoft/capturedGlobalThis
Better error message for captured global 'this' in noImplicitThis
This commit is contained in:
commit
6b60babeaf
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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`
|
||||
|
||||
@ -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'.
|
||||
}();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user