Skip to content

Commit 6e695f4

Browse files
authored
Merge pull request #252 from guillaumebrunerie/various-fixes
Various fixes for valid code not being parsed properly
2 parents 51c4c9e + 2ff3eee commit 6e695f4

File tree

10 files changed

+273772
-269756
lines changed

10 files changed

+273772
-269756
lines changed

common/corpus/declarations.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ Type casts
457457

458458
foo as any as Array<number>
459459
bar satisfies number[]
460+
"foobar" as const
460461

461462
---
462463

@@ -466,7 +467,11 @@ bar satisfies number[]
466467
(as_expression (identifier) (predefined_type))
467468
(generic_type (type_identifier) (type_arguments (predefined_type)))))
468469
(expression_statement
469-
(satisfies_expression (identifier) (array_type (predefined_type)))))
470+
(satisfies_expression (identifier) (array_type (predefined_type))))
471+
(expression_statement
472+
(as_expression
473+
(string
474+
(string_fragment)))))
470475

471476
=================================
472477
Ambient export function declarations

common/corpus/functions.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ function foo<T, U>(this: T[]): U[] {
1818
return []
1919
}
2020

21+
function foo<const T, const U extends string>(x: T, y: U) {
22+
23+
}
24+
2125
---
2226

2327
(program
@@ -57,7 +61,20 @@ function foo<T, U>(this: T[]): U[] {
5761
pattern: (this)
5862
type: (type_annotation (array_type (type_identifier)))))
5963
return_type: (type_annotation (array_type (type_identifier)))
60-
body: (statement_block (return_statement (array)))))
64+
body: (statement_block (return_statement (array))))
65+
(function_declaration
66+
name: (identifier)
67+
type_parameters: (type_parameters
68+
(type_parameter name: (type_identifier))
69+
(type_parameter name: (type_identifier) constraint: (constraint (predefined_type))))
70+
parameters: (formal_parameters
71+
(required_parameter
72+
pattern: (identifier)
73+
type: (type_annotation (type_identifier)))
74+
(required_parameter
75+
pattern: (identifier)
76+
type: (type_annotation (type_identifier))))
77+
body: (statement_block)))
6178

6279
==================================
6380
New object with type arguments

common/corpus/types.txt

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,40 @@ let x: new < T1, T2 > ( p1, p2 ) => R;
272272
(type_identifier))))))
273273

274274

275+
=======================================
276+
Symbol types
277+
=======================================
278+
279+
const symFoo: unique symbol = Symbol("foo");
280+
const symBar: symbol = Symbol.for("bar");
281+
282+
---
283+
284+
(program
285+
(lexical_declaration
286+
(variable_declarator
287+
(identifier)
288+
(type_annotation
289+
(predefined_type))
290+
(call_expression
291+
(identifier)
292+
(arguments
293+
(string
294+
(string_fragment))))))
295+
(lexical_declaration
296+
(variable_declarator
297+
(identifier)
298+
(type_annotation
299+
(predefined_type))
300+
(call_expression
301+
(member_expression
302+
(identifier)
303+
(property_identifier))
304+
(arguments
305+
(string
306+
(string_fragment)))))))
307+
308+
275309
=======================================
276310
Type annotations in parenthesized expressions
277311
=======================================
@@ -855,7 +889,8 @@ function f(x: any): asserts x {
855889
(identifier)
856890
(formal_parameters
857891
(required_parameter (identifier) (type_annotation (predefined_type))))
858-
(asserts (identifier))
892+
(asserts_annotation
893+
(asserts (identifier)))
859894
(statement_block)))
860895

861896
=======================================
@@ -881,8 +916,9 @@ function isT(t: T): t is T {
881916
(formal_parameters
882917
(required_parameter
883918
(identifier) (type_annotation (predefined_type))))
884-
(asserts
885-
(type_predicate (identifier) (predefined_type)))
919+
(asserts_annotation
920+
(asserts
921+
(type_predicate (identifier) (predefined_type))))
886922
(statement_block))
887923
(class_declaration
888924
(type_identifier)
@@ -908,6 +944,45 @@ function isT(t: T): t is T {
908944
(type_identifier)))
909945
(statement_block (return_statement (true)))))
910946

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+
911986
==================================
912987
Type predicate and predefined types
913988
==================================
@@ -1045,6 +1120,7 @@ type F<T, X, Y> = (t: T) => X extends Y ? X : Y extends (t: T) => X extends Y ?
10451120
type T<X, Y> = T extends X<infer Y> ? Y : X
10461121
type T<X> = X extends (infer X)[] ? X : never;
10471122
type T<X> = T extends { x: infer X } ? X : never;
1123+
type T<X> = T extends { x: infer X extends number } ? X : never;
10481124

10491125
---
10501126
(program
@@ -1154,6 +1230,18 @@ type T<X> = T extends { x: infer X } ? X : never;
11541230
(property_identifier)
11551231
(type_annotation (infer_type (type_identifier)))))
11561232
(type_identifier)
1233+
(predefined_type)))
1234+
(type_alias_declaration
1235+
(type_identifier)
1236+
(type_parameters
1237+
(type_parameter (type_identifier)))
1238+
(conditional_type
1239+
(type_identifier)
1240+
(object_type
1241+
(property_signature
1242+
(property_identifier)
1243+
(type_annotation (infer_type (type_identifier) (predefined_type)))))
1244+
(type_identifier)
11571245
(predefined_type))))
11581246

11591247
==================================
@@ -1170,6 +1258,7 @@ type A<B, C> = B extends C
11701258
: never
11711259
type Trim<S extends string> = S extends `${infer R}` ? Trim<R> : S;
11721260
type A = `${true & ('foo' | false)}`;
1261+
type StringToNumber<S extends string> = S extends `${infer N extends number}` ? N : never;
11731262
---
11741263
(program
11751264
(type_alias_declaration
@@ -1271,7 +1360,23 @@ type A = `${true & ('foo' | false)}`;
12711360
(string
12721361
(string_fragment)))
12731362
(literal_type
1274-
(false)))))))))
1363+
(false))))))))
1364+
(type_alias_declaration
1365+
(type_identifier)
1366+
(type_parameters
1367+
(type_parameter
1368+
(type_identifier)
1369+
(constraint
1370+
(predefined_type))))
1371+
(conditional_type
1372+
(type_identifier)
1373+
(template_literal_type
1374+
(template_type
1375+
(infer_type
1376+
(type_identifier)
1377+
(predefined_type))))
1378+
(type_identifier)
1379+
(predefined_type))))
12751380

12761381
==================================
12771382
Mapped type 'as' clauses

common/define-grammar.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ module.exports = function defineGrammar(dialect) {
438438
as_expression: $ => prec.left('binary', seq(
439439
$.expression,
440440
'as',
441-
$._type
441+
choice('const', $._type)
442442
)),
443443

444444
satisfies_expression: $ => prec.left('binary', seq(
@@ -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,
@@ -704,7 +707,14 @@ module.exports = function defineGrammar(dialect) {
704707
'`'
705708
),
706709

707-
infer_type: $ => seq("infer", $._type_identifier),
710+
infer_type: $ => prec.right(seq(
711+
'infer',
712+
$._type_identifier,
713+
optional(seq(
714+
'extends',
715+
$._type
716+
))
717+
)),
708718

709719
conditional_type: $ => prec.left(seq(
710720
field('left', $._type),
@@ -834,6 +844,7 @@ module.exports = function defineGrammar(dialect) {
834844
'boolean',
835845
'string',
836846
'symbol',
847+
alias(seq('unique', 'symbol'), 'unique symbol'),
837848
'void',
838849
'unknown',
839850
'string',
@@ -881,7 +892,7 @@ module.exports = function defineGrammar(dialect) {
881892
field('type_parameters', optional($.type_parameters)),
882893
field('parameters', $.formal_parameters),
883894
field('return_type', optional(
884-
choice($.type_annotation, $.asserts, $.type_predicate_annotation)
895+
choice($.type_annotation, $.asserts_annotation, $.type_predicate_annotation)
885896
))
886897
),
887898

@@ -890,6 +901,7 @@ module.exports = function defineGrammar(dialect) {
890901
),
891902

892903
type_parameter: $ => seq(
904+
optional('const'),
893905
field('name', $._type_identifier),
894906
field('constraint', optional($.constraint)),
895907
field('value', optional($.default_type))
@@ -953,7 +965,7 @@ module.exports = function defineGrammar(dialect) {
953965
field('type_parameters', optional($.type_parameters)),
954966
field('parameters', $.formal_parameters),
955967
'=>',
956-
field('return_type', choice($._type, $.type_predicate)),
968+
field('return_type', choice($._type, $.asserts, $.type_predicate)),
957969
)),
958970

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

0 commit comments

Comments
 (0)