Skip to content

Commit d1aa01c

Browse files
Merge pull request #6493 from Microsoft/recommendStaticsFromNonStatics
Recommend static members from instance methods
2 parents 403a93d + 8065654 commit d1aa01c

11 files changed

+104
-25
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -789,22 +789,25 @@ namespace ts {
789789
let location = container;
790790
while (location) {
791791
if (isClassLike(location.parent)) {
792-
const symbol = getSymbolOfNode(location.parent);
793-
let classType: Type;
794-
if (location.flags & NodeFlags.Static) {
795-
classType = getTypeOfSymbol(symbol);
796-
if (getPropertyOfType(classType, name)) {
797-
error(errorLocation, Diagnostics.Cannot_find_name_0_Did_you_mean_the_static_member_1_0, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg), symbolToString(symbol));
798-
return true;
799-
}
792+
const classSymbol = getSymbolOfNode(location.parent);
793+
if (!classSymbol) {
794+
break;
800795
}
801-
else {
802-
if (location === container) {
803-
classType = (<InterfaceType>getDeclaredTypeOfSymbol(symbol)).thisType;
804-
if (getPropertyOfType(classType, name)) {
805-
error(errorLocation, Diagnostics.Cannot_find_name_0_Did_you_mean_the_instance_member_this_0, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
806-
return true;
807-
}
796+
797+
// Check to see if a static member exists.
798+
const constructorType = getTypeOfSymbol(classSymbol);
799+
if (getPropertyOfType(constructorType, name)) {
800+
error(errorLocation, Diagnostics.Cannot_find_name_0_Did_you_mean_the_static_member_1_0, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg), symbolToString(classSymbol));
801+
return true;
802+
}
803+
804+
// No static member is present.
805+
// Check if we're in an instance method and look for a relevant instance member.
806+
if (location === container && !(location.flags & NodeFlags.Static)) {
807+
const instanceType = (<InterfaceType>getDeclaredTypeOfSymbol(classSymbol)).thisType;
808+
if (getPropertyOfType(instanceType, name)) {
809+
error(errorLocation, Diagnostics.Cannot_find_name_0_Did_you_mean_the_instance_member_this_0, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
810+
return true;
808811
}
809812
}
810813
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/compiler/accessInstanceMemberFromStaticMethod01.ts(5,17): error TS2662: Cannot find name 'foo'. Did you mean the static member 'C.foo'?
2+
3+
4+
==== tests/cases/compiler/accessInstanceMemberFromStaticMethod01.ts (1 errors) ====
5+
class C {
6+
static foo: string;
7+
8+
bar() {
9+
let k = foo;
10+
~~~
11+
!!! error TS2662: Cannot find name 'foo'. Did you mean the static member 'C.foo'?
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [accessInstanceMemberFromStaticMethod01.ts]
2+
class C {
3+
static foo: string;
4+
5+
bar() {
6+
let k = foo;
7+
}
8+
}
9+
10+
//// [accessInstanceMemberFromStaticMethod01.js]
11+
var C = (function () {
12+
function C() {
13+
}
14+
C.prototype.bar = function () {
15+
var k = foo;
16+
};
17+
return C;
18+
}());
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/compiler/accessStaticMemberFromInstanceMethod01.ts(5,17): error TS2304: Cannot find name 'foo'.
2+
3+
4+
==== tests/cases/compiler/accessStaticMemberFromInstanceMethod01.ts (1 errors) ====
5+
class C {
6+
foo: string;
7+
8+
static bar() {
9+
let k = foo;
10+
~~~
11+
!!! error TS2304: Cannot find name 'foo'.
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [accessStaticMemberFromInstanceMethod01.ts]
2+
class C {
3+
foo: string;
4+
5+
static bar() {
6+
let k = foo;
7+
}
8+
}
9+
10+
//// [accessStaticMemberFromInstanceMethod01.js]
11+
var C = (function () {
12+
function C() {
13+
}
14+
C.bar = function () {
15+
var k = foo;
16+
};
17+
return C;
18+
}());

tests/baselines/reference/scopeCheckExtendedClassInsidePublicMethod2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/scopeCheckExtendedClassInsidePublicMethod2.ts(4,7): error TS2663: Cannot find name 'v'. Did you mean the instance member 'this.v'?
2-
tests/cases/compiler/scopeCheckExtendedClassInsidePublicMethod2.ts(6,7): error TS2304: Cannot find name 's'.
2+
tests/cases/compiler/scopeCheckExtendedClassInsidePublicMethod2.ts(6,7): error TS2662: Cannot find name 's'. Did you mean the static member 'D.s'?
33

44

55
==== tests/cases/compiler/scopeCheckExtendedClassInsidePublicMethod2.ts (2 errors) ====
@@ -12,6 +12,6 @@ tests/cases/compiler/scopeCheckExtendedClassInsidePublicMethod2.ts(6,7): error T
1212
this.p = 1;
1313
s = 1;
1414
~
15-
!!! error TS2304: Cannot find name 's'.
15+
!!! error TS2662: Cannot find name 's'. Did you mean the static member 'D.s'?
1616
}
1717
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/scopeCheckInsidePublicMethod1.ts(4,7): error TS2304: Cannot find name 's'.
1+
tests/cases/compiler/scopeCheckInsidePublicMethod1.ts(4,7): error TS2662: Cannot find name 's'. Did you mean the static member 'C.s'?
22

33

44
==== tests/cases/compiler/scopeCheckInsidePublicMethod1.ts (1 errors) ====
@@ -7,6 +7,6 @@ tests/cases/compiler/scopeCheckInsidePublicMethod1.ts(4,7): error TS2304: Cannot
77
public a() {
88
s = 1; // ERR
99
~
10-
!!! error TS2304: Cannot find name 's'.
10+
!!! error TS2662: Cannot find name 's'. Did you mean the static member 'C.s'?
1111
}
1212
}

tests/baselines/reference/staticClassMemberError.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/staticClassMemberError.ts(4,3): error TS2304: Cannot find name 's'.
1+
tests/cases/compiler/staticClassMemberError.ts(4,3): error TS2662: Cannot find name 's'. Did you mean the static member 'C.s'?
22
tests/cases/compiler/staticClassMemberError.ts(9,10): error TS2300: Duplicate identifier 'Foo'.
33
tests/cases/compiler/staticClassMemberError.ts(9,10): error TS2391: Function implementation is missing or not immediately following the declaration.
44
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
1010
public a() {
1111
s = 1;
1212
~
13-
!!! error TS2304: Cannot find name 's'.
13+
!!! error TS2662: Cannot find name 's'. Did you mean the static member 'C.s'?
1414
}
1515
}
1616

tests/baselines/reference/staticVisibility.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/compiler/staticVisibility.ts(10,9): error TS2304: Cannot find name 's'.
2-
tests/cases/compiler/staticVisibility.ts(13,9): error TS2304: Cannot find name 'b'.
1+
tests/cases/compiler/staticVisibility.ts(10,9): error TS2662: Cannot find name 's'. Did you mean the static member 'C1.s'?
2+
tests/cases/compiler/staticVisibility.ts(13,9): error TS2662: Cannot find name 'b'. Did you mean the static member 'C1.b'?
33
tests/cases/compiler/staticVisibility.ts(18,9): error TS2304: Cannot find name 'v'.
44
tests/cases/compiler/staticVisibility.ts(19,14): error TS2339: Property 'p' does not exist on type 'typeof C1'.
55
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
1919

2020
s = 1; // should be error
2121
~
22-
!!! error TS2304: Cannot find name 's'.
22+
!!! error TS2662: Cannot find name 's'. Did you mean the static member 'C1.s'?
2323
C1.s = 1; // should be ok
2424

2525
b(); // should be error
2626
~
27-
!!! error TS2304: Cannot find name 'b'.
27+
!!! error TS2662: Cannot find name 'b'. Did you mean the static member 'C1.b'?
2828
C1.b(); // should be ok
2929
}
3030

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class C {
2+
static foo: string;
3+
4+
bar() {
5+
let k = foo;
6+
}
7+
}

0 commit comments

Comments
 (0)