Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce keyword order of abstract and override #43829

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40782,6 +40782,9 @@ namespace ts {
if (flags & ModifierFlags.Async && lastAsync) {
return grammarErrorOnNode(lastAsync, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "async", "abstract");
}
if (flags & ModifierFlags.Override) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "abstract", "override");
}
}
if (isNamedDeclaration(node) && node.name.kind === SyntaxKind.PrivateIdentifier) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_a_private_identifier, "abstract");
Expand Down
4 changes: 1 addition & 3 deletions tests/baselines/reference/override10.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ abstract class Base {
abstract bar(): void;
}

// No errors:
abstract class Sub extends Base {
override abstract foo(): number;
abstract override foo(): number;
bar() { }
}

Expand All @@ -31,7 +30,6 @@ var Base = /** @class */ (function () {
}
return Base;
}());
// No errors:
var Sub = /** @class */ (function (_super) {
__extends(Sub, _super);
function Sub() {
Expand Down
7 changes: 3 additions & 4 deletions tests/baselines/reference/override10.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ abstract class Base {
>bar : Symbol(Base.bar, Decl(override10.ts, 1, 28))
}

// No errors:
abstract class Sub extends Base {
>Sub : Symbol(Sub, Decl(override10.ts, 3, 1))
>Base : Symbol(Base, Decl(override10.ts, 0, 0))

override abstract foo(): number;
>foo : Symbol(Sub.foo, Decl(override10.ts, 6, 33))
abstract override foo(): number;
>foo : Symbol(Sub.foo, Decl(override10.ts, 5, 33))

bar() { }
>bar : Symbol(Sub.bar, Decl(override10.ts, 7, 36))
>bar : Symbol(Sub.bar, Decl(override10.ts, 6, 36))
}
3 changes: 1 addition & 2 deletions tests/baselines/reference/override10.types
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ abstract class Base {
>bar : () => void
}

// No errors:
abstract class Sub extends Base {
>Sub : Sub
>Base : Base

override abstract foo(): number;
abstract override foo(): number;
>foo : () => number

bar() { }
Expand Down
8 changes: 7 additions & 1 deletion tests/baselines/reference/override5.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ tests/cases/conformance/override/override5.ts(20,21): error TS4113: This member
tests/cases/conformance/override/override5.ts(22,14): error TS1030: 'override' modifier already seen.
tests/cases/conformance/override/override5.ts(25,14): error TS1029: 'public' modifier must precede 'override' modifier.
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 (8 errors) ====
==== tests/cases/conformance/override/override5.ts (10 errors) ====
class B {
p1: number = 1;
p2: number = 2;
Expand Down Expand Up @@ -60,11 +62,15 @@ tests/cases/conformance/override/override5.ts(45,23): error TS4112: This member

abstract class AD extends AB {
override abstract f(): void;
~~~~~~~~
!!! error TS1029: 'abstract' modifier must precede 'override' modifier.
abstract override b(): void;
}

abstract class AND {
override abstract f(): void;
~~~~~~~~
!!! 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.
abstract override b(): void;
Expand Down
8 changes: 7 additions & 1 deletion tests/baselines/reference/override7.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ tests/cases/conformance/override/override7.ts(21,21): error TS4113: This member
tests/cases/conformance/override/override7.ts(22,14): error TS1029: 'public' modifier must precede 'override' modifier.
tests/cases/conformance/override/override7.ts(22,21): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'B'.
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 (11 errors) ====
==== tests/cases/conformance/override/override7.ts (13 errors) ====
class B {
p1: number = 1;
p2: number = 2;
Expand Down Expand Up @@ -66,11 +68,15 @@ tests/cases/conformance/override/override7.ts(42,23): error TS4112: This member

abstract class AD extends AB {
override abstract f(): void;
~~~~~~~~
!!! error TS1029: 'abstract' modifier must precede 'override' modifier.
abstract override b(): void;
}

abstract class AND {
override abstract f(): void;
~~~~~~~~
!!! 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.
abstract override b(): void;
Expand Down
24 changes: 18 additions & 6 deletions tests/baselines/reference/overrideKeywordOrder.errors.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
tests/cases/conformance/override/overrideKeywordOrder.ts(12,9): error TS1029: 'override' modifier must precede 'async' modifier.
tests/cases/conformance/override/overrideKeywordOrder.ts(15,12): error TS1029: 'static' modifier must precede 'override' modifier.
tests/cases/conformance/override/overrideKeywordOrder.ts(19,12): error TS1029: 'public' modifier must precede 'override' modifier.
tests/cases/conformance/override/overrideKeywordOrder.ts(24,12): error TS1029: 'override' modifier must precede 'readonly' modifier.
tests/cases/conformance/override/overrideKeywordOrder.ts(21,12): error TS1029: 'public' modifier must precede 'override' modifier.
tests/cases/conformance/override/overrideKeywordOrder.ts(26,12): error TS1029: 'override' modifier must precede 'readonly' modifier.
tests/cases/conformance/override/overrideKeywordOrder.ts(32,12): error TS1029: 'abstract' modifier must precede 'override' modifier.


==== tests/cases/conformance/override/overrideKeywordOrder.ts (4 errors) ====
class Base {
==== tests/cases/conformance/override/overrideKeywordOrder.ts (5 errors) ====
abstract class Base {
static s1() {}
static s2() {}
m1() {}
m2() {}
abstract m1(): void;
abstract m2(): void;
p1: any;
p2: any;
}
Expand All @@ -25,6 +26,8 @@ tests/cases/conformance/override/overrideKeywordOrder.ts(24,12): error TS1029: '
~~~~~~
!!! error TS1029: 'static' modifier must precede 'override' modifier.
static override s2() {}
m1() {}
m2() {}
}
class Test3 extends Base {
override public m1() {} // error
Expand All @@ -37,5 +40,14 @@ tests/cases/conformance/override/overrideKeywordOrder.ts(24,12): error TS1029: '
readonly override p2: any; // error
~~~~~~~~
!!! error TS1029: 'override' modifier must precede 'readonly' modifier.
m1() {}
m2() {}
}

abstract class Test5 extends Base {
override abstract m1(): void; // error
~~~~~~~~
!!! error TS1029: 'abstract' modifier must precede 'override' modifier.
abstract override m2(): void;
}

3 changes: 1 addition & 2 deletions tests/cases/conformance/override/override10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ abstract class Base {
abstract bar(): void;
}

// No errors:
abstract class Sub extends Base {
override abstract foo(): number;
abstract override foo(): number;
bar() { }
}
15 changes: 12 additions & 3 deletions tests/cases/conformance/override/overrideKeywordOrder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// @noTypesAndSymbols: true
// @noEmit: true

class Base {
abstract class Base {
static s1() {}
static s2() {}
m1() {}
m2() {}
abstract m1(): void;
abstract m2(): void;
p1: any;
p2: any;
}
Expand All @@ -17,6 +17,8 @@ class Test1 extends Base {
class Test2 extends Base {
override static s1() {} // error
static override s2() {}
m1() {}
m2() {}
}
class Test3 extends Base {
override public m1() {} // error
Expand All @@ -25,4 +27,11 @@ class Test3 extends Base {
class Test4 extends Base {
override readonly p1: any;
readonly override p2: any; // error
m1() {}
m2() {}
}

abstract class Test5 extends Base {
override abstract m1(): void; // error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you miss update to the test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

abstract override m2(): void;
}