tree-sitter / tree-sitter-typescript

TypeScript grammar for tree-sitter
MIT License
335 stars 104 forks source link

Add support for asserts in type declarations #250

Closed guillaumebrunerie closed 12 months ago

guillaumebrunerie commented 12 months ago

Currently, the following (valid) code doesn’t parse:

declare const f: (x: any) => asserts x;
declare const g: (x: any) => asserts x is number;

It does not parse because the grammar only supports the asserts keyword when it follows a colon (as in function f(x: any): asserts x {}). This pull request adds support for asserts in type declarations as above.

I removed the colon from the $.asserts rule and added a new rule $.asserts_annotation which adds the colon back (so that we can use $.asserts in places that do not have a colon). This mirrors the way the existing $.type_annotation and $.type_predicate_annotation work, but unfortunately it modifies the parse tree of existing code. But I think this change still makes sense.

Checklist:

guillaumebrunerie commented 12 months ago

Closing in favor of #252.