From d8c82a84e504a33f7889d6064fa6811772c568ab Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 14 Aug 2018 22:55:06 -0700 Subject: [PATCH 1/6] Make it better. --- src/compiler/checker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 509f8975eb5..46c74816921 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15474,8 +15474,8 @@ namespace ts { 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); + 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; } From c0531c74f419fd15f6a75d651fa70a78556c5695 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 14 Aug 2018 22:55:44 -0700 Subject: [PATCH 2/6] Accepted baselines. --- .../reference/noImplicitThisFunctions.errors.txt | 8 ++++---- tests/baselines/reference/thisBinding2.errors.txt | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/baselines/reference/noImplicitThisFunctions.errors.txt b/tests/baselines/reference/noImplicitThisFunctions.errors.txt index 7e56648af57..29801b5f50b 100644 --- a/tests/baselines/reference/noImplicitThisFunctions.errors.txt +++ b/tests/baselines/reference/noImplicitThisFunctions.errors.txt @@ -1,5 +1,5 @@ -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 (2 errors) ==== @@ -17,11 +17,11 @@ 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 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'. \ No newline at end of file diff --git a/tests/baselines/reference/thisBinding2.errors.txt b/tests/baselines/reference/thisBinding2.errors.txt index b4c279e4f74..c91a16c812a 100644 --- a/tests/baselines/reference/thisBinding2.errors.txt +++ b/tests/baselines/reference/thisBinding2.errors.txt @@ -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. }(); } } From dc1fbc24cd4a30ee090f00c6b6561ebbf9bb375e Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 14 Aug 2018 23:02:08 -0700 Subject: [PATCH 3/6] Updated test cases. --- tests/cases/compiler/noImplicitThisFunctions.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/cases/compiler/noImplicitThisFunctions.ts b/tests/cases/compiler/noImplicitThisFunctions.ts index 45f0e5a1eb9..43890762e75 100644 --- a/tests/cases/compiler/noImplicitThisFunctions.ts +++ b/tests/cases/compiler/noImplicitThisFunctions.ts @@ -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 } }; From 31d8cbe8956419a79b8238c7bfe84804b4800e81 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 14 Aug 2018 23:04:54 -0700 Subject: [PATCH 4/6] Accepted baselines. --- .../noImplicitThisFunctions.errors.txt | 15 ++++++++++++++- .../reference/noImplicitThisFunctions.js | 10 ++++++++++ .../reference/noImplicitThisFunctions.symbols | 9 +++++++++ .../reference/noImplicitThisFunctions.types | 18 ++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/baselines/reference/noImplicitThisFunctions.errors.txt b/tests/baselines/reference/noImplicitThisFunctions.errors.txt index 29801b5f50b..1837acf4300 100644 --- a/tests/baselines/reference/noImplicitThisFunctions.errors.txt +++ b/tests/baselines/reference/noImplicitThisFunctions.errors.txt @@ -1,8 +1,11 @@ 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 TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'. +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; @@ -24,4 +27,14 @@ tests/cases/compiler/noImplicitThisFunctions.ts(17,38): error TS7041: The contai 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 TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'. + let f7 = function() { return function() { return this } }; + ~~~~ +!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation. \ No newline at end of file diff --git a/tests/baselines/reference/noImplicitThisFunctions.js b/tests/baselines/reference/noImplicitThisFunctions.js index c54a9cfaa8a..7d9db241f88 100644 --- a/tests/baselines/reference/noImplicitThisFunctions.js +++ b/tests/baselines/reference/noImplicitThisFunctions.js @@ -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; }; }; diff --git a/tests/baselines/reference/noImplicitThisFunctions.symbols b/tests/baselines/reference/noImplicitThisFunctions.symbols index 0e36ea13d81..d8d937893f3 100644 --- a/tests/baselines/reference/noImplicitThisFunctions.symbols +++ b/tests/baselines/reference/noImplicitThisFunctions.symbols @@ -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)) + diff --git a/tests/baselines/reference/noImplicitThisFunctions.types b/tests/baselines/reference/noImplicitThisFunctions.types index 83c6a2983a2..f5b93fa9efb 100644 --- a/tests/baselines/reference/noImplicitThisFunctions.types +++ b/tests/baselines/reference/noImplicitThisFunctions.types @@ -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 + From dac00d97b742c81b1e31ac1880b3d71e4a2a0b37 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 15 Aug 2018 09:00:23 -0700 Subject: [PATCH 5/6] Do it correctly. --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 46c74816921..7b059833be0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15473,7 +15473,7 @@ namespace ts { // With noImplicitThis, functions may not reference 'this' if it has type 'any' error( node, - capturedByArrowFunction ? + 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); } From aba82901ac59986ef58fe916438173dafda9b046 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 15 Aug 2018 09:00:37 -0700 Subject: [PATCH 6/6] Accepted baselines. --- tests/baselines/reference/noImplicitThisFunctions.errors.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/noImplicitThisFunctions.errors.txt b/tests/baselines/reference/noImplicitThisFunctions.errors.txt index 1837acf4300..aac8faa566c 100644 --- a/tests/baselines/reference/noImplicitThisFunctions.errors.txt +++ b/tests/baselines/reference/noImplicitThisFunctions.errors.txt @@ -1,7 +1,7 @@ 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 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. @@ -33,7 +33,7 @@ tests/cases/compiler/noImplicitThisFunctions.ts(21,50): error TS2683: 'this' imp let f6 = function() { return () => this; }; ~~~~ -!!! 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. let f7 = function() { return function() { return this } }; ~~~~ !!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.