Add --noImplicitThis flag

It's basically another --noImplicitAny error, but one that would break
large amount of JavaScript-style code.
This commit is contained in:
Nathan Shively-Sanders 2016-03-25 16:37:28 -07:00
parent c9f5f3d67e
commit a91cdccfc5
7 changed files with 95 additions and 1 deletions

View File

@ -7707,6 +7707,10 @@ namespace ts {
}
}
if (compilerOptions.noImplicitThis && isFunctionLike(container)) {
// 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);
}
return anyType;
}

View File

@ -125,6 +125,10 @@ namespace ts {
type: "boolean",
description: Diagnostics.Raise_error_on_expressions_and_declarations_with_an_implied_any_type,
},
{
name: "noImplicitThis",
type: "boolean",
},
{
name: "noLib",
type: "boolean",

View File

@ -1863,7 +1863,11 @@
"category": "Error",
"code": 2680
},
"Import declaration '{0}' is using private name '{1}'.": {
"'this' implicitly has type 'any' because it does not have a type annotation.": {
"category": "Error",
"code": 2681
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
"code": 4000
},

View File

@ -2395,6 +2395,7 @@ namespace ts {
noEmitOnError?: boolean;
noErrorTruncation?: boolean;
noImplicitAny?: boolean;
noImplicitThis?: boolean;
noLib?: boolean;
noResolve?: boolean;
out?: string;

View File

@ -0,0 +1,25 @@
tests/cases/compiler/noImplicitThisFunctions.ts(14,12): error TS2681: 'this' implicitly has type 'any' because it does not have a type annotation.
==== tests/cases/compiler/noImplicitThisFunctions.ts (1 errors) ====
function f1(x) {
// implicit any is still allowed
return x + 1;
}
function f2(y: number) {
// ok: no reference to this
return y + 1;
}
function f3(z: number): number {
// error: this is implicitly any
return this.a + z;
~~~~
!!! error TS2681: 'this' implicitly has type 'any' because it does not have a type annotation.
}
// ok, arrow functions don't even bind `this`, so `this` is just `window`
let f4: (b: number) => number = b => this.c + b;

View File

@ -0,0 +1,37 @@
//// [noImplicitThisFunctions.ts]
function f1(x) {
// implicit any is still allowed
return x + 1;
}
function f2(y: number) {
// ok: no reference to this
return y + 1;
}
function f3(z: number): number {
// error: this is implicitly any
return this.a + z;
}
// ok, arrow functions don't even bind `this`, so `this` is just `window`
let f4: (b: number) => number = b => this.c + b;
//// [noImplicitThisFunctions.js]
var _this = this;
function f1(x) {
// implicit any is still allowed
return x + 1;
}
function f2(y) {
// ok: no reference to this
return y + 1;
}
function f3(z) {
// error: this is implicitly any
return this.a + z;
}
// ok, arrow functions don't even bind `this`, so `this` is just `window`
var f4 = function (b) { return _this.c + b; };

View File

@ -0,0 +1,19 @@
// @noImplicitThis: true
function f1(x) {
// implicit any is still allowed
return x + 1;
}
function f2(y: number) {
// ok: no reference to this
return y + 1;
}
function f3(z: number): number {
// error: this is implicitly any
return this.a + z;
}
// ok, arrow functions don't even bind `this`, so `this` is just `window`
let f4: (b: number) => number = b => this.c + b;