Skip to content

Disallows async abstract method declarations #28517

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

Closed
Closed
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 @@ -28342,6 +28342,9 @@ namespace ts {
if (flags & ModifierFlags.Private) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "private", "abstract");
}
if (flags & ModifierFlags.Async) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "async", "abstract");
Copy link
Member

Choose a reason for hiding this comment

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

I think the error spans that we give are on the wrong keyword in all of these places. For example, the problem isn't that you've declared the thing as abstract, it's that it makes no sense to mark an abstract member as async.

But I guess that's not your problem. It might be suitable for a follow-up PR if it's not too disruptive.

Copy link
Author

Choose a reason for hiding this comment

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

Do you mean the error should appear on async instead? I agree that's not really in the scope of this PR but that is one I would be happy to tackle as an experiment to evaluate whether it is too disruptive.

}
}

flags |= ModifierFlags.Abstract;
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/abstractAsync.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tests/cases/compiler/abstractAsync.ts(2,9): error TS1243: 'async' modifier cannot be used with 'abstract' modifier.


==== tests/cases/compiler/abstractAsync.ts (1 errors) ====
abstract class ShouldFailClass {
async abstract badMethod(): Promise<number>;
~~~~~~~~
!!! error TS1243: 'async' modifier cannot be used with 'abstract' modifier.
}
11 changes: 11 additions & 0 deletions tests/baselines/reference/abstractAsync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [abstractAsync.ts]
abstract class ShouldFailClass {
async abstract badMethod(): Promise<number>;
}

//// [abstractAsync.js]
var ShouldFailClass = /** @class */ (function () {
function ShouldFailClass() {
}
return ShouldFailClass;
}());
8 changes: 8 additions & 0 deletions tests/baselines/reference/abstractAsync.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/abstractAsync.ts ===
abstract class ShouldFailClass {
>ShouldFailClass : Symbol(ShouldFailClass, Decl(abstractAsync.ts, 0, 0))

async abstract badMethod(): Promise<number>;
>badMethod : Symbol(ShouldFailClass.badMethod, Decl(abstractAsync.ts, 0, 32))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
}
8 changes: 8 additions & 0 deletions tests/baselines/reference/abstractAsync.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/abstractAsync.ts ===
abstract class ShouldFailClass {
>ShouldFailClass : ShouldFailClass

async abstract badMethod(): Promise<number>;
>badMethod : () => Promise<number>
>Promise : Promise<T>
}
4 changes: 4 additions & 0 deletions tests/cases/compiler/abstractAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @lib: es2015
Copy link
Member

Choose a reason for hiding this comment

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

@rbuckton is // @lib: es2015 the right thing or is there a leaner way to run this?

abstract class ShouldFailClass {
async abstract badMethod(): Promise<number>;
}