From faeb8e6379370578138ed989af4396b2f3a0fc9f Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 13 Apr 2021 13:16:55 -0700 Subject: [PATCH 1/2] Enforce keyword order between override and static/async --- src/compiler/checker.ts | 6 +++ .../reference/overrideKeywordOrder.errors.txt | 41 +++++++++++++++++++ .../override/overrideKeywordOrder.ts | 28 +++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 tests/baselines/reference/overrideKeywordOrder.errors.txt create mode 100644 tests/cases/conformance/override/overrideKeywordOrder.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 514f18d01e9de..6782b05ac4599 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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"); } @@ -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; diff --git a/tests/baselines/reference/overrideKeywordOrder.errors.txt b/tests/baselines/reference/overrideKeywordOrder.errors.txt new file mode 100644 index 0000000000000..b4842347dbda4 --- /dev/null +++ b/tests/baselines/reference/overrideKeywordOrder.errors.txt @@ -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. + } + \ No newline at end of file diff --git a/tests/cases/conformance/override/overrideKeywordOrder.ts b/tests/cases/conformance/override/overrideKeywordOrder.ts new file mode 100644 index 0000000000000..c024ef6ec04aa --- /dev/null +++ b/tests/cases/conformance/override/overrideKeywordOrder.ts @@ -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 +} From 11dad27114016574035ec491fc41bfed1c8e622b Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 15 Apr 2021 11:31:52 -0700 Subject: [PATCH 2/2] Update old tests for new keyword order --- tests/baselines/reference/override5.errors.txt | 2 +- tests/baselines/reference/override5.js | 2 +- tests/baselines/reference/override5.symbols | 2 +- tests/baselines/reference/override5.types | 2 +- tests/baselines/reference/override7.errors.txt | 2 +- tests/baselines/reference/override7.js | 2 +- tests/baselines/reference/override7.symbols | 2 +- tests/baselines/reference/override7.types | 2 +- tests/cases/conformance/override/override5.ts | 2 +- tests/cases/conformance/override/override7.ts | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/baselines/reference/override5.errors.txt b/tests/baselines/reference/override5.errors.txt index e0dd689d47e39..03711150037b1 100644 --- a/tests/baselines/reference/override5.errors.txt +++ b/tests/baselines/reference/override5.errors.txt @@ -32,7 +32,7 @@ tests/cases/conformance/override/override5.ts(45,23): error TS4112: This member override readonly p4: number; - override static sp: number; + static override sp: number; ~~ !!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'B'. diff --git a/tests/baselines/reference/override5.js b/tests/baselines/reference/override5.js index 3ab9d96d52f6b..88ad03f1c827c 100644 --- a/tests/baselines/reference/override5.js +++ b/tests/baselines/reference/override5.js @@ -18,7 +18,7 @@ class D extends B{ override readonly p4: number; - override static sp: number; + static override sp: number; override override oop: number; diff --git a/tests/baselines/reference/override5.symbols b/tests/baselines/reference/override5.symbols index 9f504d682652e..4dd442b0bb462 100644 --- a/tests/baselines/reference/override5.symbols +++ b/tests/baselines/reference/override5.symbols @@ -40,7 +40,7 @@ class D extends B{ override readonly p4: number; >p4 : Symbol(D.p4, Decl(override5.ts, 15, 33)) - override static sp: number; + static override sp: number; >sp : Symbol(D.sp, Decl(override5.ts, 17, 33)) override override oop: number; diff --git a/tests/baselines/reference/override5.types b/tests/baselines/reference/override5.types index f65d7b3cabb43..76e3041949683 100644 --- a/tests/baselines/reference/override5.types +++ b/tests/baselines/reference/override5.types @@ -44,7 +44,7 @@ class D extends B{ override readonly p4: number; >p4 : number - override static sp: number; + static override sp: number; >sp : number override override oop: number; diff --git a/tests/baselines/reference/override7.errors.txt b/tests/baselines/reference/override7.errors.txt index e5d908c99920c..89240a4782ffa 100644 --- a/tests/baselines/reference/override7.errors.txt +++ b/tests/baselines/reference/override7.errors.txt @@ -32,7 +32,7 @@ tests/cases/conformance/override/override7.ts(42,23): error TS4112: This member override readonly p4: number; - override static sp: number; + static override sp: number; ~~ !!! error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'B'. diff --git a/tests/baselines/reference/override7.js b/tests/baselines/reference/override7.js index 36108f2359ed8..69d35235cd78f 100644 --- a/tests/baselines/reference/override7.js +++ b/tests/baselines/reference/override7.js @@ -15,7 +15,7 @@ class D extends B{ override readonly p4: number; - override static sp: number; + static override sp: number; override override oop: number; diff --git a/tests/baselines/reference/override7.symbols b/tests/baselines/reference/override7.symbols index 4a0a173424462..207873a161497 100644 --- a/tests/baselines/reference/override7.symbols +++ b/tests/baselines/reference/override7.symbols @@ -31,7 +31,7 @@ class D extends B{ override readonly p4: number; >p4 : Symbol(D.p4, Decl(override7.ts, 12, 33)) - override static sp: number; + static override sp: number; >sp : Symbol(D.sp, Decl(override7.ts, 14, 33)) override override oop: number; diff --git a/tests/baselines/reference/override7.types b/tests/baselines/reference/override7.types index 537b63572d7b0..cf3558aa930a3 100644 --- a/tests/baselines/reference/override7.types +++ b/tests/baselines/reference/override7.types @@ -35,7 +35,7 @@ class D extends B{ override readonly p4: number; >p4 : number - override static sp: number; + static override sp: number; >sp : number override override oop: number; diff --git a/tests/cases/conformance/override/override5.ts b/tests/cases/conformance/override/override5.ts index cd6bd1001c4a6..4e622f7f5cb0c 100644 --- a/tests/cases/conformance/override/override5.ts +++ b/tests/cases/conformance/override/override5.ts @@ -19,7 +19,7 @@ class D extends B{ override readonly p4: number; - override static sp: number; + static override sp: number; override override oop: number; diff --git a/tests/cases/conformance/override/override7.ts b/tests/cases/conformance/override/override7.ts index 14881e1051a3b..13c432e520879 100644 --- a/tests/cases/conformance/override/override7.ts +++ b/tests/cases/conformance/override/override7.ts @@ -16,7 +16,7 @@ class D extends B{ override readonly p4: number; - override static sp: number; + static override sp: number; override override oop: number;