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 between override and static/async #43660

Merged
merged 2 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40471,6 +40471,9 @@ namespace ts {
else if (flags & ModifierFlags.Readonly) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "override", "readonly");
}
else if (flags & ModifierFlags.Async) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "override", "async");
}
if (node.kind === SyntaxKind.Parameter) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_parameter, "override");
}
Expand Down Expand Up @@ -40534,6 +40537,9 @@ namespace ts {
else if (flags & ModifierFlags.Abstract) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "static", "abstract");
}
else if (flags & ModifierFlags.Override) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "static", "override");
}
flags |= ModifierFlags.Static;
lastStatic = modifier;
break;
Expand Down
41 changes: 41 additions & 0 deletions tests/baselines/reference/overrideKeywordOrder.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 (4 errors) ====
class Base {
static s1() {}
static s2() {}
m1() {}
m2() {}
p1: any;
p2: any;
}

class Test1 extends Base {
override async m1() {}
async override m2() {} // error
~~~~~~~~
!!! error TS1029: 'override' modifier must precede 'async' modifier.
}
class Test2 extends Base {
override static s1() {} // error
~~~~~~
!!! error TS1029: 'static' modifier must precede 'override' modifier.
static override s2() {}
}
class Test3 extends Base {
override public m1() {} // error
~~~~~~
!!! error TS1029: 'public' modifier must precede 'override' modifier.
public override m2() {}
}
class Test4 extends Base {
override readonly p1: any;
readonly override p2: any; // error
~~~~~~~~
!!! error TS1029: 'override' modifier must precede 'readonly' modifier.
}

28 changes: 28 additions & 0 deletions tests/cases/conformance/override/overrideKeywordOrder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @noTypesAndSymbols: true
// @noEmit: true

class Base {
static s1() {}
static s2() {}
m1() {}
m2() {}
p1: any;
p2: any;
}

class Test1 extends Base {
override async m1() {}
async override m2() {} // error
}
class Test2 extends Base {
override static s1() {} // error
static override s2() {}
}
class Test3 extends Base {
override public m1() {} // error
public override m2() {}
}
class Test4 extends Base {
override readonly p1: any;
readonly override p2: any; // error
}