Skip to content

Commit bf216b8

Browse files
Add support for asserts in type declarations
1 parent 3ca110e commit bf216b8

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

common/corpus/types.txt

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,8 @@ function f(x: any): asserts x {
889889
(identifier)
890890
(formal_parameters
891891
(required_parameter (identifier) (type_annotation (predefined_type))))
892-
(asserts (identifier))
892+
(asserts_annotation
893+
(asserts (identifier)))
893894
(statement_block)))
894895

895896
=======================================
@@ -915,8 +916,9 @@ function isT(t: T): t is T {
915916
(formal_parameters
916917
(required_parameter
917918
(identifier) (type_annotation (predefined_type))))
918-
(asserts
919-
(type_predicate (identifier) (predefined_type)))
919+
(asserts_annotation
920+
(asserts
921+
(type_predicate (identifier) (predefined_type))))
920922
(statement_block))
921923
(class_declaration
922924
(type_identifier)
@@ -942,6 +944,45 @@ function isT(t: T): t is T {
942944
(type_identifier)))
943945
(statement_block (return_statement (true)))))
944946

947+
=======================================
948+
Type of an assertion function
949+
=======================================
950+
951+
declare const f: (x: any) => asserts x;
952+
declare const g: (x: any) => asserts x is number;
953+
954+
---
955+
956+
(program
957+
(ambient_declaration
958+
(lexical_declaration
959+
(variable_declarator
960+
(identifier)
961+
(type_annotation
962+
(function_type
963+
(formal_parameters
964+
(required_parameter
965+
(identifier)
966+
(type_annotation
967+
(predefined_type))))
968+
(asserts
969+
(identifier)))))))
970+
(ambient_declaration
971+
(lexical_declaration
972+
(variable_declarator
973+
(identifier)
974+
(type_annotation
975+
(function_type
976+
(formal_parameters
977+
(required_parameter
978+
(identifier)
979+
(type_annotation
980+
(predefined_type))))
981+
(asserts
982+
(type_predicate
983+
(identifier)
984+
(predefined_type)))))))))
985+
945986
==================================
946987
Type predicate and predefined types
947988
==================================

common/define-grammar.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,11 +627,14 @@ module.exports = function defineGrammar(dialect) {
627627
type_annotation: $ => seq(':', $._type),
628628

629629
asserts: $ => seq(
630-
':',
631630
'asserts',
632631
choice($.type_predicate, $.identifier, $.this)
633632
),
634633

634+
asserts_annotation: $ => seq(
635+
seq(':', $.asserts)
636+
),
637+
635638
_type: $ => choice(
636639
$._primary_type,
637640
$.function_type,
@@ -882,7 +885,7 @@ module.exports = function defineGrammar(dialect) {
882885
field('type_parameters', optional($.type_parameters)),
883886
field('parameters', $.formal_parameters),
884887
field('return_type', optional(
885-
choice($.type_annotation, $.asserts, $.type_predicate_annotation)
888+
choice($.type_annotation, $.asserts_annotation, $.type_predicate_annotation)
886889
))
887890
),
888891

@@ -954,7 +957,7 @@ module.exports = function defineGrammar(dialect) {
954957
field('type_parameters', optional($.type_parameters)),
955958
field('parameters', $.formal_parameters),
956959
'=>',
957-
field('return_type', choice($._type, $.type_predicate)),
960+
field('return_type', choice($._type, $.asserts, $.type_predicate)),
958961
)),
959962

960963
_type_identifier: $ => alias($.identifier, $.type_identifier),

0 commit comments

Comments
 (0)