From 32454d0a84e84d820aa4946c5624d293c4002597 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 14 Jan 2016 16:28:07 -0800 Subject: [PATCH 1/4] Added tests. --- .../compiler/accessInstanceMemberFromStaticMethod01.ts | 7 +++++++ .../compiler/accessStaticMemberFromInstanceMethod01.ts | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 tests/cases/compiler/accessInstanceMemberFromStaticMethod01.ts create mode 100644 tests/cases/compiler/accessStaticMemberFromInstanceMethod01.ts diff --git a/tests/cases/compiler/accessInstanceMemberFromStaticMethod01.ts b/tests/cases/compiler/accessInstanceMemberFromStaticMethod01.ts new file mode 100644 index 00000000000..cdca697f13f --- /dev/null +++ b/tests/cases/compiler/accessInstanceMemberFromStaticMethod01.ts @@ -0,0 +1,7 @@ +class C { + static foo: string; + + bar() { + let k = foo; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/accessStaticMemberFromInstanceMethod01.ts b/tests/cases/compiler/accessStaticMemberFromInstanceMethod01.ts new file mode 100644 index 00000000000..654ae39aacd --- /dev/null +++ b/tests/cases/compiler/accessStaticMemberFromInstanceMethod01.ts @@ -0,0 +1,7 @@ +class C { + foo: string; + + static bar() { + let k = foo; + } +} \ No newline at end of file From e7cee960077dbefb783515867ead98490d4da4b4 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 14 Jan 2016 16:28:22 -0800 Subject: [PATCH 2/4] Accepted baselines. --- ...InstanceMemberFromStaticMethod01.errors.txt | 13 +++++++++++++ .../accessInstanceMemberFromStaticMethod01.js | 18 ++++++++++++++++++ ...StaticMemberFromInstanceMethod01.errors.txt | 13 +++++++++++++ .../accessStaticMemberFromInstanceMethod01.js | 18 ++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 tests/baselines/reference/accessInstanceMemberFromStaticMethod01.errors.txt create mode 100644 tests/baselines/reference/accessInstanceMemberFromStaticMethod01.js create mode 100644 tests/baselines/reference/accessStaticMemberFromInstanceMethod01.errors.txt create mode 100644 tests/baselines/reference/accessStaticMemberFromInstanceMethod01.js diff --git a/tests/baselines/reference/accessInstanceMemberFromStaticMethod01.errors.txt b/tests/baselines/reference/accessInstanceMemberFromStaticMethod01.errors.txt new file mode 100644 index 00000000000..f1cd943f9ac --- /dev/null +++ b/tests/baselines/reference/accessInstanceMemberFromStaticMethod01.errors.txt @@ -0,0 +1,13 @@ +tests/cases/compiler/accessInstanceMemberFromStaticMethod01.ts(5,17): error TS2304: Cannot find name 'foo'. + + +==== tests/cases/compiler/accessInstanceMemberFromStaticMethod01.ts (1 errors) ==== + class C { + static foo: string; + + bar() { + let k = foo; + ~~~ +!!! error TS2304: Cannot find name 'foo'. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/accessInstanceMemberFromStaticMethod01.js b/tests/baselines/reference/accessInstanceMemberFromStaticMethod01.js new file mode 100644 index 00000000000..763be0568dc --- /dev/null +++ b/tests/baselines/reference/accessInstanceMemberFromStaticMethod01.js @@ -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; +}()); diff --git a/tests/baselines/reference/accessStaticMemberFromInstanceMethod01.errors.txt b/tests/baselines/reference/accessStaticMemberFromInstanceMethod01.errors.txt new file mode 100644 index 00000000000..612a89a1043 --- /dev/null +++ b/tests/baselines/reference/accessStaticMemberFromInstanceMethod01.errors.txt @@ -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'. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/accessStaticMemberFromInstanceMethod01.js b/tests/baselines/reference/accessStaticMemberFromInstanceMethod01.js new file mode 100644 index 00000000000..fd8ee14b2e1 --- /dev/null +++ b/tests/baselines/reference/accessStaticMemberFromInstanceMethod01.js @@ -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; +}()); From e980f46cbe443d5b74b7f34b52232bff693ca685 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 14 Jan 2016 17:05:09 -0800 Subject: [PATCH 3/4] Look up static members from instance methods. --- src/compiler/checker.ts | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 83953daa95a..d84e550b9b9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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 = (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 = (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; } } } From 806565457fe73524fb946453c45c9414e7218498 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 14 Jan 2016 17:05:28 -0800 Subject: [PATCH 4/4] Accepted baselines. --- .../accessInstanceMemberFromStaticMethod01.errors.txt | 4 ++-- .../scopeCheckExtendedClassInsidePublicMethod2.errors.txt | 4 ++-- .../reference/scopeCheckInsidePublicMethod1.errors.txt | 4 ++-- .../baselines/reference/staticClassMemberError.errors.txt | 4 ++-- tests/baselines/reference/staticVisibility.errors.txt | 8 ++++---- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/baselines/reference/accessInstanceMemberFromStaticMethod01.errors.txt b/tests/baselines/reference/accessInstanceMemberFromStaticMethod01.errors.txt index f1cd943f9ac..0ce9c614618 100644 --- a/tests/baselines/reference/accessInstanceMemberFromStaticMethod01.errors.txt +++ b/tests/baselines/reference/accessInstanceMemberFromStaticMethod01.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/accessInstanceMemberFromStaticMethod01.ts(5,17): error TS2304: Cannot find name 'foo'. +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) ==== @@ -8,6 +8,6 @@ tests/cases/compiler/accessInstanceMemberFromStaticMethod01.ts(5,17): error TS23 bar() { let k = foo; ~~~ -!!! error TS2304: Cannot find name 'foo'. +!!! error TS2662: Cannot find name 'foo'. Did you mean the static member 'C.foo'? } } \ No newline at end of file diff --git a/tests/baselines/reference/scopeCheckExtendedClassInsidePublicMethod2.errors.txt b/tests/baselines/reference/scopeCheckExtendedClassInsidePublicMethod2.errors.txt index 91cb87dbeb8..378e20f19b8 100644 --- a/tests/baselines/reference/scopeCheckExtendedClassInsidePublicMethod2.errors.txt +++ b/tests/baselines/reference/scopeCheckExtendedClassInsidePublicMethod2.errors.txt @@ -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'? } } \ No newline at end of file diff --git a/tests/baselines/reference/scopeCheckInsidePublicMethod1.errors.txt b/tests/baselines/reference/scopeCheckInsidePublicMethod1.errors.txt index 5c9473a7875..ec93fea7369 100644 --- a/tests/baselines/reference/scopeCheckInsidePublicMethod1.errors.txt +++ b/tests/baselines/reference/scopeCheckInsidePublicMethod1.errors.txt @@ -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'? } } \ No newline at end of file diff --git a/tests/baselines/reference/staticClassMemberError.errors.txt b/tests/baselines/reference/staticClassMemberError.errors.txt index 9483e3387b7..43a2cd6934d 100644 --- a/tests/baselines/reference/staticClassMemberError.errors.txt +++ b/tests/baselines/reference/staticClassMemberError.errors.txt @@ -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'? } } diff --git a/tests/baselines/reference/staticVisibility.errors.txt b/tests/baselines/reference/staticVisibility.errors.txt index dccad4a2e2b..a92cb7f88cd 100644 --- a/tests/baselines/reference/staticVisibility.errors.txt +++ b/tests/baselines/reference/staticVisibility.errors.txt @@ -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 }