Merge pull request #6493 from Microsoft/recommendStaticsFromNonStatics

Recommend static members from instance methods
This commit is contained in:
Daniel Rosenwasser
2016-01-14 17:35:54 -08:00
11 changed files with 104 additions and 25 deletions
+18 -15
View File
@@ -789,22 +789,25 @@ namespace ts {
let location = container;
while (location) {
if (isClassLike(location.parent)) {
const symbol = getSymbolOfNode(location.parent);
let classType: Type;
if (location.flags & NodeFlags.Static) {
classType = getTypeOfSymbol(symbol);
if (getPropertyOfType(classType, name)) {
error(errorLocation, Diagnostics.Cannot_find_name_0_Did_you_mean_the_static_member_1_0, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg), symbolToString(symbol));
return true;
}
const classSymbol = getSymbolOfNode(location.parent);
if (!classSymbol) {
break;
}
else {
if (location === container) {
classType = (<InterfaceType>getDeclaredTypeOfSymbol(symbol)).thisType;
if (getPropertyOfType(classType, name)) {
error(errorLocation, Diagnostics.Cannot_find_name_0_Did_you_mean_the_instance_member_this_0, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
return true;
}
// Check to see if a static member exists.
const constructorType = getTypeOfSymbol(classSymbol);
if (getPropertyOfType(constructorType, name)) {
error(errorLocation, Diagnostics.Cannot_find_name_0_Did_you_mean_the_static_member_1_0, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg), symbolToString(classSymbol));
return true;
}
// No static member is present.
// Check if we're in an instance method and look for a relevant instance member.
if (location === container && !(location.flags & NodeFlags.Static)) {
const instanceType = (<InterfaceType>getDeclaredTypeOfSymbol(classSymbol)).thisType;
if (getPropertyOfType(instanceType, name)) {
error(errorLocation, Diagnostics.Cannot_find_name_0_Did_you_mean_the_instance_member_this_0, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
return true;
}
}
}
@@ -0,0 +1,13 @@
tests/cases/compiler/accessInstanceMemberFromStaticMethod01.ts(5,17): error TS2662: Cannot find name 'foo'. Did you mean the static member 'C.foo'?
==== tests/cases/compiler/accessInstanceMemberFromStaticMethod01.ts (1 errors) ====
class C {
static foo: string;
bar() {
let k = foo;
~~~
!!! error TS2662: Cannot find name 'foo'. Did you mean the static member 'C.foo'?
}
}
@@ -0,0 +1,18 @@
//// [accessInstanceMemberFromStaticMethod01.ts]
class C {
static foo: string;
bar() {
let k = foo;
}
}
//// [accessInstanceMemberFromStaticMethod01.js]
var C = (function () {
function C() {
}
C.prototype.bar = function () {
var k = foo;
};
return C;
}());
@@ -0,0 +1,13 @@
tests/cases/compiler/accessStaticMemberFromInstanceMethod01.ts(5,17): error TS2304: Cannot find name 'foo'.
==== tests/cases/compiler/accessStaticMemberFromInstanceMethod01.ts (1 errors) ====
class C {
foo: string;
static bar() {
let k = foo;
~~~
!!! error TS2304: Cannot find name 'foo'.
}
}
@@ -0,0 +1,18 @@
//// [accessStaticMemberFromInstanceMethod01.ts]
class C {
foo: string;
static bar() {
let k = foo;
}
}
//// [accessStaticMemberFromInstanceMethod01.js]
var C = (function () {
function C() {
}
C.bar = function () {
var k = foo;
};
return C;
}());
@@ -1,5 +1,5 @@
tests/cases/compiler/scopeCheckExtendedClassInsidePublicMethod2.ts(4,7): error TS2663: Cannot find name 'v'. Did you mean the instance member 'this.v'?
tests/cases/compiler/scopeCheckExtendedClassInsidePublicMethod2.ts(6,7): error TS2304: Cannot find name 's'.
tests/cases/compiler/scopeCheckExtendedClassInsidePublicMethod2.ts(6,7): error TS2662: Cannot find name 's'. Did you mean the static member 'D.s'?
==== tests/cases/compiler/scopeCheckExtendedClassInsidePublicMethod2.ts (2 errors) ====
@@ -12,6 +12,6 @@ tests/cases/compiler/scopeCheckExtendedClassInsidePublicMethod2.ts(6,7): error T
this.p = 1;
s = 1;
~
!!! error TS2304: Cannot find name 's'.
!!! error TS2662: Cannot find name 's'. Did you mean the static member 'D.s'?
}
}
@@ -1,4 +1,4 @@
tests/cases/compiler/scopeCheckInsidePublicMethod1.ts(4,7): error TS2304: Cannot find name 's'.
tests/cases/compiler/scopeCheckInsidePublicMethod1.ts(4,7): error TS2662: Cannot find name 's'. Did you mean the static member 'C.s'?
==== tests/cases/compiler/scopeCheckInsidePublicMethod1.ts (1 errors) ====
@@ -7,6 +7,6 @@ tests/cases/compiler/scopeCheckInsidePublicMethod1.ts(4,7): error TS2304: Cannot
public a() {
s = 1; // ERR
~
!!! error TS2304: Cannot find name 's'.
!!! error TS2662: Cannot find name 's'. Did you mean the static member 'C.s'?
}
}
@@ -1,4 +1,4 @@
tests/cases/compiler/staticClassMemberError.ts(4,3): error TS2304: Cannot find name 's'.
tests/cases/compiler/staticClassMemberError.ts(4,3): error TS2662: Cannot find name 's'. Did you mean the static member 'C.s'?
tests/cases/compiler/staticClassMemberError.ts(9,10): error TS2300: Duplicate identifier 'Foo'.
tests/cases/compiler/staticClassMemberError.ts(9,10): error TS2391: Function implementation is missing or not immediately following the declaration.
tests/cases/compiler/staticClassMemberError.ts(10,7): error TS2300: Duplicate identifier 'Foo'.
@@ -10,7 +10,7 @@ tests/cases/compiler/staticClassMemberError.ts(10,7): error TS2300: Duplicate id
public a() {
s = 1;
~
!!! error TS2304: Cannot find name 's'.
!!! error TS2662: Cannot find name 's'. Did you mean the static member 'C.s'?
}
}
@@ -1,5 +1,5 @@
tests/cases/compiler/staticVisibility.ts(10,9): error TS2304: Cannot find name 's'.
tests/cases/compiler/staticVisibility.ts(13,9): error TS2304: Cannot find name 'b'.
tests/cases/compiler/staticVisibility.ts(10,9): error TS2662: Cannot find name 's'. Did you mean the static member 'C1.s'?
tests/cases/compiler/staticVisibility.ts(13,9): error TS2662: Cannot find name 'b'. Did you mean the static member 'C1.b'?
tests/cases/compiler/staticVisibility.ts(18,9): error TS2304: Cannot find name 'v'.
tests/cases/compiler/staticVisibility.ts(19,14): error TS2339: Property 'p' does not exist on type 'typeof C1'.
tests/cases/compiler/staticVisibility.ts(31,12): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
@@ -19,12 +19,12 @@ tests/cases/compiler/staticVisibility.ts(33,29): error TS2304: Cannot find name
s = 1; // should be error
~
!!! error TS2304: Cannot find name 's'.
!!! error TS2662: Cannot find name 's'. Did you mean the static member 'C1.s'?
C1.s = 1; // should be ok
b(); // should be error
~
!!! error TS2304: Cannot find name 'b'.
!!! error TS2662: Cannot find name 'b'. Did you mean the static member 'C1.b'?
C1.b(); // should be ok
}
@@ -0,0 +1,7 @@
class C {
static foo: string;
bar() {
let k = foo;
}
}
@@ -0,0 +1,7 @@
class C {
foo: string;
static bar() {
let k = foo;
}
}