Skip to content

Commit 3383ceb

Browse files
authored
Add more predefined and literal types (#184)
* fix typeof according to typescript rules * regenerate parser * add more predefined and literal types * regenerate parser Co-authored-by: resolritter <[email protected]>
1 parent b00d5a5 commit 3383ceb

File tree

9 files changed

+247842
-246840
lines changed

9 files changed

+247842
-246840
lines changed

common/corpus/declarations.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ finally {}
188188
body: (statement_block)
189189
handler: (catch_clause
190190
parameter: (identifier)
191-
type: (type_annotation (type_identifier))
191+
type: (type_annotation (predefined_type))
192192
body: (statement_block)
193193
)
194194
finalizer: (finally_clause body: (statement_block))
@@ -620,7 +620,14 @@ class Foo {
620620
(identifier)
621621
(type_annotation (function_type
622622
(formal_parameters)
623-
(union_type (type_identifier) (generic_type (type_identifier) (type_arguments (union_type (type_identifier) (type_identifier))))))))
623+
(union_type
624+
(type_identifier)
625+
(generic_type
626+
(type_identifier)
627+
(type_arguments
628+
(union_type
629+
(type_identifier)
630+
(literal_type (undefined)))))))))
624631
(optional_parameter
625632
(identifier)
626633
(type_annotation (function_type

common/corpus/types.txt

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ type T<X> = T extends { x: infer X } ? X : never;
10631063
(array_type
10641064
(parenthesized_type (infer_type (type_identifier))))
10651065
(type_identifier)
1066-
(type_identifier)))
1066+
(predefined_type)))
10671067
(type_alias_declaration
10681068
(type_identifier)
10691069
(type_parameters
@@ -1075,7 +1075,7 @@ type T<X> = T extends { x: infer X } ? X : never;
10751075
(property_identifier)
10761076
(type_annotation (infer_type (type_identifier)))))
10771077
(type_identifier)
1078-
(type_identifier))))
1078+
(predefined_type))))
10791079

10801080
==================================
10811081
Template literal types
@@ -1157,8 +1157,8 @@ type A<B, C> = B extends C
11571157
(string (string_fragment)))))
11581158
(template_type
11591159
(type_identifier)))
1160-
(type_identifier))
1161-
(type_identifier))))
1160+
(predefined_type))
1161+
(predefined_type))))
11621162

11631163
==================================
11641164
Mapped type 'as' clauses
@@ -1266,3 +1266,36 @@ type T = Foo<Bar<typeof bar["baz"]>>
12661266
(identifier)
12671267
(string
12681268
(string_fragment))))))))))
1269+
1270+
==================================
1271+
Literal types and pre-defined types
1272+
==================================
1273+
1274+
type T = Foo<any, unknown, number, string, void, true, false, null, undefined, 0, "foo">
1275+
1276+
---
1277+
1278+
(program
1279+
(type_alias_declaration
1280+
(type_identifier)
1281+
(generic_type
1282+
(type_identifier)
1283+
(type_arguments
1284+
(predefined_type)
1285+
(predefined_type)
1286+
(predefined_type)
1287+
(predefined_type)
1288+
(predefined_type)
1289+
(literal_type
1290+
(true))
1291+
(literal_type
1292+
(false))
1293+
(literal_type
1294+
(null))
1295+
(literal_type
1296+
(undefined))
1297+
(literal_type
1298+
(number))
1299+
(literal_type
1300+
(string
1301+
(string_fragment)))))))

common/define-grammar.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,9 @@ module.exports = function defineGrammar(dialect) {
728728
$.number,
729729
$.string,
730730
$.true,
731-
$.false
731+
$.false,
732+
$.null,
733+
$.undefined,
732734
),
733735

734736
_number: $ => prec.left(1, seq(
@@ -750,7 +752,11 @@ module.exports = function defineGrammar(dialect) {
750752
'boolean',
751753
'string',
752754
'symbol',
753-
'void'
755+
'void',
756+
'unknown',
757+
'string',
758+
'never',
759+
'object'
754760
),
755761

756762
type_arguments: $ => seq(

tsx/src/grammar.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9040,6 +9040,14 @@
90409040
{
90419041
"type": "SYMBOL",
90429042
"name": "false"
9043+
},
9044+
{
9045+
"type": "SYMBOL",
9046+
"name": "null"
9047+
},
9048+
{
9049+
"type": "SYMBOL",
9050+
"name": "undefined"
90439051
}
90449052
]
90459053
},
@@ -9141,6 +9149,22 @@
91419149
{
91429150
"type": "STRING",
91439151
"value": "void"
9152+
},
9153+
{
9154+
"type": "STRING",
9155+
"value": "unknown"
9156+
},
9157+
{
9158+
"type": "STRING",
9159+
"value": "string"
9160+
},
9161+
{
9162+
"type": "STRING",
9163+
"value": "never"
9164+
},
9165+
{
9166+
"type": "STRING",
9167+
"value": "object"
91449168
}
91459169
]
91469170
},

tsx/src/node-types.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3398,6 +3398,10 @@
33983398
"type": "false",
33993399
"named": true
34003400
},
3401+
{
3402+
"type": "null",
3403+
"named": true
3404+
},
34013405
{
34023406
"type": "number",
34033407
"named": true
@@ -3413,6 +3417,10 @@
34133417
{
34143418
"type": "unary_expression",
34153419
"named": true
3420+
},
3421+
{
3422+
"type": "undefined",
3423+
"named": true
34163424
}
34173425
]
34183426
}
@@ -5977,6 +5985,10 @@
59775985
"type": "namespace",
59785986
"named": false
59795987
},
5988+
{
5989+
"type": "never",
5990+
"named": false
5991+
},
59805992
{
59815993
"type": "new",
59825994
"named": false
@@ -5993,6 +6005,10 @@
59936005
"type": "number",
59946006
"named": true
59956007
},
6008+
{
6009+
"type": "object",
6010+
"named": false
6011+
},
59966012
{
59976013
"type": "of",
59986014
"named": false
@@ -6113,6 +6129,10 @@
61136129
"type": "undefined",
61146130
"named": true
61156131
},
6132+
{
6133+
"type": "unknown",
6134+
"named": false
6135+
},
61166136
{
61176137
"type": "var",
61186138
"named": false

0 commit comments

Comments
 (0)