Skip to content

Commit a6985c5

Browse files
committed
fix typeof according to typescript rules
1 parent 74a2748 commit a6985c5

File tree

2 files changed

+98
-10
lines changed

2 files changed

+98
-10
lines changed

common/corpus/types.txt

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -711,14 +711,15 @@ type T = keyof (Person);
711711

712712
type T = typeof Person;
713713
type T = typeof Person.P;
714-
type T = typeof Person<P>;
715714
type T = typeof import('person');
716715

717716
type T = keyof typeof Person;
718717

719718
type T = keyof U[number];
720719
type T = keyof U & V;
721720

721+
type T = typeof array[number];
722+
722723
---
723724

724725
(program
@@ -734,24 +735,23 @@ type T = keyof U & V;
734735
(type_query (identifier)))
735736
(type_alias_declaration (type_identifier)
736737
(type_query (member_expression (identifier) (property_identifier))))
737-
(type_alias_declaration (type_identifier)
738-
(type_query (generic_type (type_identifier) (type_arguments (type_identifier)))))
739738
(type_alias_declaration (type_identifier)
740739
(type_query (call_expression (import) (arguments (string (string_fragment))))))
741740
(type_alias_declaration (type_identifier)
742741
(index_type_query (type_query (identifier))))
743742
(type_alias_declaration (type_identifier)
744743
(index_type_query (lookup_type (type_identifier) (predefined_type))))
745744
(type_alias_declaration (type_identifier)
746-
(intersection_type (index_type_query (type_identifier)) (type_identifier))))
745+
(intersection_type (index_type_query (type_identifier)) (type_identifier)))
746+
(type_alias_declaration (type_identifier)
747+
(type_query (subscript_expression (identifier) (predefined_type)))))
747748

748749
=======================================
749750
Lookup types
750751
=======================================
751752

752753
type K1 = Foo[bar]
753754
type K1 = Foo['bar' | 'baz']
754-
type A = typeof some_array[number]
755755

756756
---
757757

@@ -761,10 +761,7 @@ type A = typeof some_array[number]
761761
(lookup_type (type_identifier) (type_identifier)))
762762
(type_alias_declaration
763763
(type_identifier)
764-
(lookup_type (type_identifier) (union_type (literal_type (string (string_fragment))) (literal_type (string (string_fragment))))))
765-
(type_alias_declaration
766-
(type_identifier)
767-
(lookup_type (type_query (identifier)) (predefined_type))))
764+
(lookup_type (type_identifier) (union_type (literal_type (string (string_fragment))) (literal_type (string (string_fragment)))))))
768765

769766

770767
=======================================
@@ -1222,3 +1219,50 @@ type A<B> = { [B in keyof C & string as `${P}1` | `${P}2`]: A[B] }
12221219
(lookup_type
12231220
(type_identifier)
12241221
(type_identifier)))))))
1222+
1223+
============================
1224+
typeof in generic arguments
1225+
============================
1226+
1227+
type T = Foo<Bar<typeof bar>>
1228+
type T = Foo<Bar<typeof bar.baz>>
1229+
type T = Foo<Bar<typeof bar["baz"]>>
1230+
1231+
---
1232+
1233+
(program
1234+
(type_alias_declaration
1235+
(type_identifier)
1236+
(generic_type
1237+
(type_identifier)
1238+
(type_arguments
1239+
(generic_type
1240+
(type_identifier)
1241+
(type_arguments
1242+
(type_query
1243+
(identifier)))))))
1244+
(type_alias_declaration
1245+
(type_identifier)
1246+
(generic_type
1247+
(type_identifier)
1248+
(type_arguments
1249+
(generic_type
1250+
(type_identifier)
1251+
(type_arguments
1252+
(type_query
1253+
(member_expression
1254+
(identifier)
1255+
(property_identifier))))))))
1256+
(type_alias_declaration
1257+
(type_identifier)
1258+
(generic_type
1259+
(type_identifier)
1260+
(type_arguments
1261+
(generic_type
1262+
(type_identifier)
1263+
(type_arguments
1264+
(type_query
1265+
(subscript_expression
1266+
(identifier)
1267+
(string
1268+
(string_fragment))))))))))

common/define-grammar.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ module.exports = function defineGrammar(dialect) {
4747
[$.type_query, $.subscript_expression, $.expression],
4848
[$.nested_type_identifier, $.generic_type, $._primary_type, $.lookup_type, $.index_type_query, $._type],
4949
[$.as_expression, $._primary_type],
50+
[$._type_query_member_expression, $.member_expression],
51+
[$._type_query_member_expression, $.primary_expression],
52+
[$._type_query_subscript_expression, $.subscript_expression],
53+
[$._type_query_subscript_expression, $.primary_expression],
54+
[$._type_query_call_expression, $.primary_expression],
55+
[$.type_query, $.primary_expression],
5056
]),
5157

5258
conflicts: ($, previous) => previous.concat([
@@ -655,9 +661,47 @@ module.exports = function defineGrammar(dialect) {
655661
seq(':', $.type_predicate)
656662
),
657663

664+
// Type query expressions are more restrictive than regular expressions
665+
_type_query_member_expression: $ => seq(
666+
field('object', choice(
667+
$.identifier,
668+
alias($._type_query_subscript_expression, $.subscript_expression),
669+
alias($._type_query_member_expression, $.member_expression),
670+
alias($._type_query_call_expression, $.call_expression)
671+
)),
672+
choice('.', '?.'),
673+
field('property', choice(
674+
$.private_property_identifier,
675+
alias($.identifier, $.property_identifier)
676+
))
677+
),
678+
_type_query_subscript_expression: $ => seq(
679+
field('object', choice(
680+
$.identifier,
681+
alias($._type_query_subscript_expression, $.subscript_expression),
682+
alias($._type_query_member_expression, $.member_expression),
683+
alias($._type_query_call_expression, $.call_expression)
684+
)),
685+
optional('?.'),
686+
'[', field('index', choice($.predefined_type, $.string, $.number)), ']'
687+
),
688+
_type_query_call_expression: $ => seq(
689+
field('function', choice(
690+
$.import,
691+
$.identifier,
692+
alias($._type_query_member_expression, $.member_expression),
693+
alias($._type_query_subscript_expression, $.subscript_expression)
694+
)),
695+
field('arguments', $.arguments)
696+
),
658697
type_query: $ => prec.right(seq(
659698
'typeof',
660-
choice($.primary_expression, $.generic_type),
699+
choice(
700+
alias($._type_query_subscript_expression, $.subscript_expression),
701+
alias($._type_query_member_expression, $.member_expression),
702+
alias($._type_query_call_expression, $.call_expression),
703+
$.identifier
704+
),
661705
)),
662706

663707
index_type_query: $ => seq(

0 commit comments

Comments
 (0)