@@ -15279,7 +15279,7 @@ namespace ts {
15279
15279
// Stop at the first arrow function so that we can
15280
15280
// tell whether 'this' needs to be captured.
15281
15281
let container = getThisContainer(node, /* includeArrowFunctions */ true);
15282
- let needToCaptureLexicalThis = false;
15282
+ let capturedByArrowFunction = false;
15283
15283
15284
15284
if (container.kind === SyntaxKind.Constructor) {
15285
15285
checkThisBeforeSuper(node, container, Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class);
@@ -15288,9 +15288,7 @@ namespace ts {
15288
15288
// Now skip arrow functions to get the "real" owner of 'this'.
15289
15289
if (container.kind === SyntaxKind.ArrowFunction) {
15290
15290
container = getThisContainer(container, /* includeArrowFunctions */ false);
15291
-
15292
- // When targeting es6, arrow function lexically bind "this" so we do not need to do the work of binding "this" in emitted code
15293
- needToCaptureLexicalThis = (languageVersion < ScriptTarget.ES2015);
15291
+ capturedByArrowFunction = true;
15294
15292
}
15295
15293
15296
15294
switch (container.kind) {
@@ -15320,14 +15318,19 @@ namespace ts {
15320
15318
break;
15321
15319
}
15322
15320
15323
- if (needToCaptureLexicalThis) {
15321
+ // When targeting es6, mark that we'll need to capture `this` in its lexically bound scope.
15322
+ if (capturedByArrowFunction && languageVersion < ScriptTarget.ES2015) {
15324
15323
captureLexicalThis(node, container);
15325
15324
}
15326
15325
15327
15326
const type = tryGetThisTypeAt(node, container);
15328
15327
if (!type && noImplicitThis) {
15329
15328
// With noImplicitThis, functions may not reference 'this' if it has type 'any'
15330
- error(node, Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation);
15329
+ error(
15330
+ node,
15331
+ capturedByArrowFunction ?
15332
+ Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation :
15333
+ Diagnostics.The_containing_arrow_function_captures_the_global_value_of_this_which_implicitly_has_type_any);
15331
15334
}
15332
15335
return type || anyType;
15333
15336
}
0 commit comments