Skip to content

Commit

Permalink
attempt to retire TS4112 and treat classes as extending Object
Browse files Browse the repository at this point in the history
  • Loading branch information
kmxz committed Oct 18, 2021
1 parent 26aea0d commit 08ef62d
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 59 deletions.
13 changes: 2 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38431,7 +38431,7 @@ namespace ts {
const nodeInAmbientContext = !!(node.flags & NodeFlags.Ambient);
const baseTypeNode = getEffectiveBaseTypeNode(node);
const baseTypes = baseTypeNode && getBaseTypes(type);
const baseWithThis = baseTypes?.length ? getTypeWithThisArgument(first(baseTypes), type.thisType) : undefined;
const baseWithThis = baseTypes?.length ? getTypeWithThisArgument(first(baseTypes), type.thisType) : globalObjectType;
const baseStaticType = getBaseConstructorTypeOfClass(type);

for (const member of node.members) {
Expand All @@ -38453,7 +38453,7 @@ namespace ts {
const hasOverride = hasOverrideModifier(member);
const hasStatic = isStatic(member);
const isJs = isInJSFile(member);
if (baseWithThis && (hasOverride || compilerOptions.noImplicitOverride)) {
if (hasOverride || compilerOptions.noImplicitOverride) {
const declaredProp = member.name && getSymbolAtLocation(member.name) || getSymbolAtLocation(member);
if (!declaredProp) {
return;
Expand Down Expand Up @@ -38503,15 +38503,6 @@ namespace ts {
}
}
}
else if (hasOverride) {
const className = typeToString(type);
error(
member,
isJs ?
Diagnostics.This_member_cannot_have_a_JSDoc_comment_with_an_override_tag_because_its_containing_class_0_does_not_extend_another_class :
Diagnostics.This_member_cannot_have_an_override_modifier_because_its_containing_class_0_does_not_extend_another_class,
className);
}
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/services/codefixes/fixOverrideModifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace ts.codefix {

const errorCodes = [
Diagnostics.This_member_cannot_have_an_override_modifier_because_it_is_not_declared_in_the_base_class_0.code,
Diagnostics.This_member_cannot_have_an_override_modifier_because_its_containing_class_0_does_not_extend_another_class.code,
Diagnostics.This_member_must_have_an_override_modifier_because_it_overrides_an_abstract_method_that_is_declared_in_the_base_class_0.code,
Diagnostics.This_member_must_have_an_override_modifier_because_it_overrides_a_member_in_the_base_class_0.code,
Diagnostics.This_parameter_property_must_have_an_override_modifier_because_it_overrides_a_member_in_base_class_0.code
Expand All @@ -24,9 +23,6 @@ namespace ts.codefix {
[Diagnostics.This_member_must_have_an_override_modifier_because_it_overrides_a_member_in_the_base_class_0.code]: [
Diagnostics.Add_override_modifier, fixAddOverrideId, Diagnostics.Add_all_missing_override_modifiers,
],
[Diagnostics.This_member_cannot_have_an_override_modifier_because_its_containing_class_0_does_not_extend_another_class.code]: [
Diagnostics.Remove_override_modifier, fixRemoveOverrideId, Diagnostics.Remove_all_unnecessary_override_modifiers
],
[Diagnostics.This_parameter_property_must_have_an_override_modifier_because_it_overrides_a_member_in_base_class_0.code]: [
Diagnostics.Add_override_modifier, fixAddOverrideId, Diagnostics.Add_all_missing_override_modifiers,
],
Expand Down Expand Up @@ -78,7 +74,6 @@ namespace ts.codefix {
case Diagnostics.This_parameter_property_must_have_an_override_modifier_because_it_overrides_a_member_in_base_class_0.code:
return doAddOverrideModifierChange(changeTracker, context.sourceFile, pos);
case Diagnostics.This_member_cannot_have_an_override_modifier_because_it_is_not_declared_in_the_base_class_0.code:
case Diagnostics.This_member_cannot_have_an_override_modifier_because_its_containing_class_0_does_not_extend_another_class.code:
return doRemoveOverrideModifierChange(changeTracker, context.sourceFile, pos);
default:
Debug.fail("Unexpected error code: " + errorCode);
Expand Down
20 changes: 12 additions & 8 deletions tests/baselines/reference/override1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
tests/cases/conformance/override/override1.ts(9,5): error TS4114: This member must have an 'override' modifier because it overrides a member in the base class 'B'.
tests/cases/conformance/override/override1.ts(11,14): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'B'.
tests/cases/conformance/override/override1.ts(15,14): error TS4112: This member cannot have an 'override' modifier because its containing class 'C' does not extend another class.
tests/cases/conformance/override/override1.ts(22,9): error TS4114: This member must have an 'override' modifier because it overrides a member in the base class 'B'.
tests/cases/conformance/override/override1.ts(24,18): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'B'.
tests/cases/conformance/override/override1.ts(33,5): error TS4114: This member must have an 'override' modifier because it overrides a member in the base class '(Anonymous class)'.
tests/cases/conformance/override/override1.ts(37,14): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class '(Anonymous class)'.
tests/cases/conformance/override/override1.ts(42,18): error TS4112: This member cannot have an 'override' modifier because its containing class '(Anonymous class)' does not extend another class.
tests/cases/conformance/override/override1.ts(17,14): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.
tests/cases/conformance/override/override1.ts(26,9): error TS4114: This member must have an 'override' modifier because it overrides a member in the base class 'B'.
tests/cases/conformance/override/override1.ts(28,18): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'B'.
tests/cases/conformance/override/override1.ts(37,5): error TS4114: This member must have an 'override' modifier because it overrides a member in the base class '(Anonymous class)'.
tests/cases/conformance/override/override1.ts(41,14): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class '(Anonymous class)'.
tests/cases/conformance/override/override1.ts(46,18): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.


==== tests/cases/conformance/override/override1.ts (8 errors) ====
Expand All @@ -24,12 +24,16 @@ tests/cases/conformance/override/override1.ts(42,18): error TS4112: This member
override bar(v: string) {}
~~~
!!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'B'.

override toString() { return ''; }
}

class C {
override foo(v: string) {}
~~~
!!! error TS4112: This member cannot have an 'override' modifier because its containing class 'C' does not extend another class.
!!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.

override toString() { return ''; }
}

function f () {
Expand Down Expand Up @@ -66,6 +70,6 @@ tests/cases/conformance/override/override1.ts(42,18): error TS4112: This member
return class {
override foo () {}
~~~
!!! error TS4112: This member cannot have an 'override' modifier because its containing class '(Anonymous class)' does not extend another class.
!!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.
}
}
8 changes: 8 additions & 0 deletions tests/baselines/reference/override1.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ class D extends B {
fooo (v: string) {}

override bar(v: string) {}

override toString() { return ''; }
}

class C {
override foo(v: string) {}

override toString() { return ''; }
}

function f () {
Expand Down Expand Up @@ -75,12 +79,14 @@ var D = /** @class */ (function (_super) {
D.prototype.foo = function (v) { };
D.prototype.fooo = function (v) { };
D.prototype.bar = function (v) { };
D.prototype.toString = function () { return ''; };
return D;
}(B));
var C = /** @class */ (function () {
function C() {
}
C.prototype.foo = function (v) { };
C.prototype.toString = function () { return ''; };
return C;
}());
function f() {
Expand Down Expand Up @@ -131,9 +137,11 @@ declare class D extends B {
foo(v: string): void;
fooo(v: string): void;
bar(v: string): void;
toString(): string;
}
declare class C {
foo(v: string): void;
toString(): string;
}
declare function f(): {
new (): {
Expand Down
44 changes: 25 additions & 19 deletions tests/baselines/reference/override1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -26,64 +26,70 @@ class D extends B {
override bar(v: string) {}
>bar : Symbol(D.bar, Decl(override1.ts, 8, 23))
>v : Symbol(v, Decl(override1.ts, 10, 17))

override toString() { return ''; }
>toString : Symbol(D.toString, Decl(override1.ts, 10, 30))
}

class C {
>C : Symbol(C, Decl(override1.ts, 11, 1))
>C : Symbol(C, Decl(override1.ts, 13, 1))

override foo(v: string) {}
>foo : Symbol(C.foo, Decl(override1.ts, 13, 9))
>v : Symbol(v, Decl(override1.ts, 14, 17))
>foo : Symbol(C.foo, Decl(override1.ts, 15, 9))
>v : Symbol(v, Decl(override1.ts, 16, 17))

override toString() { return ''; }
>toString : Symbol(C.toString, Decl(override1.ts, 16, 30))
}

function f () {
>f : Symbol(f, Decl(override1.ts, 15, 1))
>f : Symbol(f, Decl(override1.ts, 19, 1))

return class extends B {
>B : Symbol(B, Decl(override1.ts, 0, 0))

override foo (v: string) {}
>foo : Symbol((Anonymous class).foo, Decl(override1.ts, 18, 28))
>v : Symbol(v, Decl(override1.ts, 19, 22))
>foo : Symbol((Anonymous class).foo, Decl(override1.ts, 22, 28))
>v : Symbol(v, Decl(override1.ts, 23, 22))

fooo (v: string) {}
>fooo : Symbol((Anonymous class).fooo, Decl(override1.ts, 19, 35))
>v : Symbol(v, Decl(override1.ts, 21, 14))
>fooo : Symbol((Anonymous class).fooo, Decl(override1.ts, 23, 35))
>v : Symbol(v, Decl(override1.ts, 25, 14))

override bar(v: string) {}
>bar : Symbol((Anonymous class).bar, Decl(override1.ts, 21, 27))
>v : Symbol(v, Decl(override1.ts, 23, 21))
>bar : Symbol((Anonymous class).bar, Decl(override1.ts, 25, 27))
>v : Symbol(v, Decl(override1.ts, 27, 21))
}
}

class E extends (class {
>E : Symbol(E, Decl(override1.ts, 25, 1))
>E : Symbol(E, Decl(override1.ts, 29, 1))

foo () { }
>foo : Symbol((Anonymous class).foo, Decl(override1.ts, 27, 24))
>foo : Symbol((Anonymous class).foo, Decl(override1.ts, 31, 24))

bar () { }
>bar : Symbol((Anonymous class).bar, Decl(override1.ts, 28, 14))
>bar : Symbol((Anonymous class).bar, Decl(override1.ts, 32, 14))

}) {
override foo () { }
>foo : Symbol(E.foo, Decl(override1.ts, 30, 4))
>foo : Symbol(E.foo, Decl(override1.ts, 34, 4))

bar () { }
>bar : Symbol(E.bar, Decl(override1.ts, 31, 23))
>bar : Symbol(E.bar, Decl(override1.ts, 35, 23))

baz() {}
>baz : Symbol(E.baz, Decl(override1.ts, 32, 14))
>baz : Symbol(E.baz, Decl(override1.ts, 36, 14))

override bazz () {}
>bazz : Symbol(E.bazz, Decl(override1.ts, 34, 12))
>bazz : Symbol(E.bazz, Decl(override1.ts, 38, 12))
}

function ff () {
>ff : Symbol(ff, Decl(override1.ts, 37, 1))
>ff : Symbol(ff, Decl(override1.ts, 41, 1))

return class {
override foo () {}
>foo : Symbol((Anonymous class).foo, Decl(override1.ts, 40, 18))
>foo : Symbol((Anonymous class).foo, Decl(override1.ts, 44, 18))
}
}
8 changes: 8 additions & 0 deletions tests/baselines/reference/override1.types
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class D extends B {
override bar(v: string) {}
>bar : (v: string) => void
>v : string

override toString() { return ''; }
>toString : () => string
>'' : ""
}

class C {
Expand All @@ -34,6 +38,10 @@ class C {
override foo(v: string) {}
>foo : (v: string) => void
>v : string

override toString() { return ''; }
>toString : () => string
>'' : ""
}

function f () {
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/override5.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ tests/cases/conformance/override/override5.ts(25,14): error TS1029: 'public' mod
tests/cases/conformance/override/override5.ts(27,5): error TS1089: 'override' modifier cannot appear on a constructor declaration.
tests/cases/conformance/override/override5.ts(39,14): error TS1029: 'abstract' modifier must precede 'override' modifier.
tests/cases/conformance/override/override5.ts(44,14): error TS1029: 'abstract' modifier must precede 'override' modifier.
tests/cases/conformance/override/override5.ts(44,23): error TS4112: This member cannot have an 'override' modifier because its containing class 'AND' does not extend another class.
tests/cases/conformance/override/override5.ts(45,23): error TS4112: This member cannot have an 'override' modifier because its containing class 'AND' does not extend another class.
tests/cases/conformance/override/override5.ts(44,23): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.
tests/cases/conformance/override/override5.ts(45,23): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.


==== tests/cases/conformance/override/override5.ts (10 errors) ====
Expand Down Expand Up @@ -72,10 +72,10 @@ tests/cases/conformance/override/override5.ts(45,23): error TS4112: This member
~~~~~~~~
!!! error TS1029: 'abstract' modifier must precede 'override' modifier.
~
!!! error TS4112: This member cannot have an 'override' modifier because its containing class 'AND' does not extend another class.
!!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.
abstract override b(): void;
~
!!! error TS4112: This member cannot have an 'override' modifier because its containing class 'AND' does not extend another class.
!!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.
}

class ADD extends AD {
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/override7.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ tests/cases/conformance/override/override7.ts(22,21): error TS4113: This member
tests/cases/conformance/override/override7.ts(24,5): error TS1089: 'override' modifier cannot appear on a constructor declaration.
tests/cases/conformance/override/override7.ts(36,14): error TS1029: 'abstract' modifier must precede 'override' modifier.
tests/cases/conformance/override/override7.ts(41,14): error TS1029: 'abstract' modifier must precede 'override' modifier.
tests/cases/conformance/override/override7.ts(41,23): error TS4112: This member cannot have an 'override' modifier because its containing class 'AND' does not extend another class.
tests/cases/conformance/override/override7.ts(42,23): error TS4112: This member cannot have an 'override' modifier because its containing class 'AND' does not extend another class.
tests/cases/conformance/override/override7.ts(41,23): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.
tests/cases/conformance/override/override7.ts(42,23): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.


==== tests/cases/conformance/override/override7.ts (13 errors) ====
Expand Down Expand Up @@ -78,10 +78,10 @@ tests/cases/conformance/override/override7.ts(42,23): error TS4112: This member
~~~~~~~~
!!! error TS1029: 'abstract' modifier must precede 'override' modifier.
~
!!! error TS4112: This member cannot have an 'override' modifier because its containing class 'AND' does not extend another class.
!!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.
abstract override b(): void;
~
!!! error TS4112: This member cannot have an 'override' modifier because its containing class 'AND' does not extend another class.
!!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.
}

class ADD extends AD {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/conformance/override/overrideWithoutNoImplicitOverride1.ts(2,14): error TS4112: This member cannot have an 'override' modifier because its containing class 'AmbientClass' does not extend another class.
tests/cases/conformance/override/overrideWithoutNoImplicitOverride1.ts(6,14): error TS4112: This member cannot have an 'override' modifier because its containing class 'NonAmbientClass' does not extend another class.
tests/cases/conformance/override/overrideWithoutNoImplicitOverride1.ts(2,14): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.
tests/cases/conformance/override/overrideWithoutNoImplicitOverride1.ts(6,14): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.
tests/cases/conformance/override/overrideWithoutNoImplicitOverride1.ts(18,14): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'AmbientBase'.
tests/cases/conformance/override/overrideWithoutNoImplicitOverride1.ts(31,18): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'AmbientBase'.
tests/cases/conformance/override/overrideWithoutNoImplicitOverride1.ts(44,14): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'NonAmbientBase'.
Expand All @@ -9,13 +9,13 @@ tests/cases/conformance/override/overrideWithoutNoImplicitOverride1.ts(44,14): e
export declare class AmbientClass {
override yadda(): void;
~~~~~
!!! error TS4112: This member cannot have an 'override' modifier because its containing class 'AmbientClass' does not extend another class.
!!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.
}

export class NonAmbientClass {
override yadda(): void {}
~~~~~
!!! error TS4112: This member cannot have an 'override' modifier because its containing class 'NonAmbientClass' does not extend another class.
!!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'Object'.
}

/////
Expand Down
Loading

0 comments on commit 08ef62d

Please sign in to comment.