From 13734e7d68bdcbacf85623318331ef188a2ea1fc Mon Sep 17 00:00:00 2001 From: Elizabeth Dinella Date: Thu, 24 May 2018 14:12:13 -0700 Subject: [PATCH] Fix for issue #6154 - overriding methods with properties in the derived class (#24343) * Fix to issue 6154 - Overriding a method with a property in the derived class should not cause a compiler error * new baselines * fixed deleted baselines --- src/compiler/checker.ts | 2 +- .../checkJsFiles_noErrorLocation.errors.txt | 25 ------------- ...eMemberAccessorOverridingMethod.errors.txt | 36 ------------------ ...heritanceMemberAccessorOverridingMethod.js | 8 ++-- ...anceMemberAccessorOverridingMethod.symbols | 6 +-- ...itanceMemberAccessorOverridingMethod.types | 13 ++++--- ...eMemberPropertyOverridingMethod.errors.txt | 15 -------- .../reference/propertyOverridingPrototype.js | 37 +++++++++++++++++++ .../propertyOverridingPrototype.symbols | 18 +++++++++ .../propertyOverridingPrototype.types | 18 +++++++++ ...heritanceMemberAccessorOverridingMethod.ts | 7 ++-- .../compiler/propertyOverridingPrototype.ts | 9 +++++ 12 files changed, 101 insertions(+), 93 deletions(-) delete mode 100644 tests/baselines/reference/checkJsFiles_noErrorLocation.errors.txt delete mode 100644 tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.errors.txt delete mode 100644 tests/baselines/reference/inheritanceMemberPropertyOverridingMethod.errors.txt create mode 100644 tests/baselines/reference/propertyOverridingPrototype.js create mode 100644 tests/baselines/reference/propertyOverridingPrototype.symbols create mode 100644 tests/baselines/reference/propertyOverridingPrototype.types create mode 100644 tests/cases/compiler/propertyOverridingPrototype.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index fb1f62ddaf57c..ceb1cdd581d36 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -24489,7 +24489,7 @@ namespace ts { continue; } - if (isPrototypeProperty(base) && isPrototypeProperty(derived) || base.flags & SymbolFlags.PropertyOrAccessor && derived.flags & SymbolFlags.PropertyOrAccessor) { + if (isPrototypeProperty(base) || base.flags & SymbolFlags.PropertyOrAccessor && derived.flags & SymbolFlags.PropertyOrAccessor) { // method is overridden with method or property/accessor is overridden with property/accessor - correct case continue; } diff --git a/tests/baselines/reference/checkJsFiles_noErrorLocation.errors.txt b/tests/baselines/reference/checkJsFiles_noErrorLocation.errors.txt deleted file mode 100644 index d9104608284a9..0000000000000 --- a/tests/baselines/reference/checkJsFiles_noErrorLocation.errors.txt +++ /dev/null @@ -1,25 +0,0 @@ -tests/cases/compiler/a.js(14,10): error TS2424: Class 'A' defines instance member function 'foo', but extended class 'B' defines it as instance member property. - - -==== tests/cases/compiler/a.js (1 errors) ==== - // @ts-check - class A { - constructor() { - - } - foo() { - return 4; - } - } - - class B extends A { - constructor() { - super(); - this.foo = () => 3; - ~~~ -!!! error TS2424: Class 'A' defines instance member function 'foo', but extended class 'B' defines it as instance member property. - } - } - - const i = new B(); - i.foo(); \ No newline at end of file diff --git a/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.errors.txt b/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.errors.txt deleted file mode 100644 index 66d955324c8bd..0000000000000 --- a/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.errors.txt +++ /dev/null @@ -1,36 +0,0 @@ -tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts(8,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. -tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts(8,9): error TS2416: Property 'x' in type 'b' is not assignable to the same property in base type 'a'. - Type 'string' is not assignable to type '() => string'. -tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts(8,9): error TS2423: Class 'a' defines instance member function 'x', but extended class 'b' defines it as instance member accessor. -tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts(11,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. -tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts(11,9): error TS2416: Property 'x' in type 'b' is not assignable to the same property in base type 'a'. - Type 'string' is not assignable to type '() => string'. - - -==== tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts (5 errors) ==== - class a { - x() { - return "20"; - } - } - - class b extends a { - get x() { - ~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~ -!!! error TS2416: Property 'x' in type 'b' is not assignable to the same property in base type 'a'. -!!! error TS2416: Type 'string' is not assignable to type '() => string'. - ~ -!!! error TS2423: Class 'a' defines instance member function 'x', but extended class 'b' defines it as instance member accessor. - return "20"; - } - set x(aValue: string) { - ~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~ -!!! error TS2416: Property 'x' in type 'b' is not assignable to the same property in base type 'a'. -!!! error TS2416: Type 'string' is not assignable to type '() => string'. - - } - } \ No newline at end of file diff --git a/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.js b/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.js index bf309a975733d..9f4133c897b42 100644 --- a/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.js +++ b/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.js @@ -7,10 +7,10 @@ class a { class b extends a { get x() { - return "20"; + return () => "20"; } - set x(aValue: string) { - + set x(aValue) { + } } @@ -40,7 +40,7 @@ var b = /** @class */ (function (_super) { } Object.defineProperty(b.prototype, "x", { get: function () { - return "20"; + return function () { return "20"; }; }, set: function (aValue) { }, diff --git a/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.symbols b/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.symbols index aecf2b9a02035..8b308f2db7b3d 100644 --- a/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.symbols +++ b/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.symbols @@ -16,11 +16,11 @@ class b extends a { get x() { >x : Symbol(b.x, Decl(inheritanceMemberAccessorOverridingMethod.ts, 6, 19), Decl(inheritanceMemberAccessorOverridingMethod.ts, 9, 5)) - return "20"; + return () => "20"; } - set x(aValue: string) { + set x(aValue) { >x : Symbol(b.x, Decl(inheritanceMemberAccessorOverridingMethod.ts, 6, 19), Decl(inheritanceMemberAccessorOverridingMethod.ts, 9, 5)) >aValue : Symbol(aValue, Decl(inheritanceMemberAccessorOverridingMethod.ts, 10, 10)) - + } } diff --git a/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.types b/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.types index 5078acf884b36..e1c29c86f405a 100644 --- a/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.types +++ b/tests/baselines/reference/inheritanceMemberAccessorOverridingMethod.types @@ -15,14 +15,15 @@ class b extends a { >a : a get x() { ->x : string +>x : () => string - return "20"; + return () => "20"; +>() => "20" : () => string >"20" : "20" } - set x(aValue: string) { ->x : string ->aValue : string - + set x(aValue) { +>x : () => string +>aValue : () => string + } } diff --git a/tests/baselines/reference/inheritanceMemberPropertyOverridingMethod.errors.txt b/tests/baselines/reference/inheritanceMemberPropertyOverridingMethod.errors.txt deleted file mode 100644 index 2b56866b22b33..0000000000000 --- a/tests/baselines/reference/inheritanceMemberPropertyOverridingMethod.errors.txt +++ /dev/null @@ -1,15 +0,0 @@ -tests/cases/compiler/inheritanceMemberPropertyOverridingMethod.ts(8,5): error TS2424: Class 'a' defines instance member function 'x', but extended class 'b' defines it as instance member property. - - -==== tests/cases/compiler/inheritanceMemberPropertyOverridingMethod.ts (1 errors) ==== - class a { - x() { - return "20"; - } - } - - class b extends a { - x: () => string; - ~ -!!! error TS2424: Class 'a' defines instance member function 'x', but extended class 'b' defines it as instance member property. - } \ No newline at end of file diff --git a/tests/baselines/reference/propertyOverridingPrototype.js b/tests/baselines/reference/propertyOverridingPrototype.js new file mode 100644 index 0000000000000..47dc9ed5b701f --- /dev/null +++ b/tests/baselines/reference/propertyOverridingPrototype.js @@ -0,0 +1,37 @@ +//// [propertyOverridingPrototype.ts] +class Base { + foo() { + } +} + +class Derived extends Base { + foo: () => { }; +} + + + +//// [propertyOverridingPrototype.js] +var __extends = (this && this.__extends) || (function () { + var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var Base = /** @class */ (function () { + function Base() { + } + Base.prototype.foo = function () { + }; + return Base; +}()); +var Derived = /** @class */ (function (_super) { + __extends(Derived, _super); + function Derived() { + return _super !== null && _super.apply(this, arguments) || this; + } + return Derived; +}(Base)); diff --git a/tests/baselines/reference/propertyOverridingPrototype.symbols b/tests/baselines/reference/propertyOverridingPrototype.symbols new file mode 100644 index 0000000000000..a8a39dc227ce4 --- /dev/null +++ b/tests/baselines/reference/propertyOverridingPrototype.symbols @@ -0,0 +1,18 @@ +=== tests/cases/compiler/propertyOverridingPrototype.ts === +class Base { +>Base : Symbol(Base, Decl(propertyOverridingPrototype.ts, 0, 0)) + + foo() { +>foo : Symbol(Base.foo, Decl(propertyOverridingPrototype.ts, 0, 12)) + } +} + +class Derived extends Base { +>Derived : Symbol(Derived, Decl(propertyOverridingPrototype.ts, 3, 1)) +>Base : Symbol(Base, Decl(propertyOverridingPrototype.ts, 0, 0)) + + foo: () => { }; +>foo : Symbol(Derived.foo, Decl(propertyOverridingPrototype.ts, 5, 28)) +} + + diff --git a/tests/baselines/reference/propertyOverridingPrototype.types b/tests/baselines/reference/propertyOverridingPrototype.types new file mode 100644 index 0000000000000..2f68d9bdf7904 --- /dev/null +++ b/tests/baselines/reference/propertyOverridingPrototype.types @@ -0,0 +1,18 @@ +=== tests/cases/compiler/propertyOverridingPrototype.ts === +class Base { +>Base : Base + + foo() { +>foo : () => void + } +} + +class Derived extends Base { +>Derived : Derived +>Base : Base + + foo: () => { }; +>foo : () => {} +} + + diff --git a/tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts b/tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts index 3165e30ee2936..5684276beb2ca 100644 --- a/tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts +++ b/tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts @@ -1,3 +1,4 @@ +// @target: es5 class a { x() { return "20"; @@ -6,9 +7,9 @@ class a { class b extends a { get x() { - return "20"; + return () => "20"; } - set x(aValue: string) { - + set x(aValue) { + } } \ No newline at end of file diff --git a/tests/cases/compiler/propertyOverridingPrototype.ts b/tests/cases/compiler/propertyOverridingPrototype.ts new file mode 100644 index 0000000000000..06ca94c49c32a --- /dev/null +++ b/tests/cases/compiler/propertyOverridingPrototype.ts @@ -0,0 +1,9 @@ +class Base { + foo() { + } +} + +class Derived extends Base { + foo: () => { }; +} +