Skip to content

Commit b8439c2

Browse files
committed
fix: allow this in type queries
1 parent 4ca6454 commit b8439c2

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

common/define-grammar.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ module.exports = function defineGrammar(dialect) {
819819
_type_query_member_expression: $ => seq(
820820
field('object', choice(
821821
$.identifier,
822+
$.this,
822823
alias($._type_query_subscript_expression, $.subscript_expression),
823824
alias($._type_query_member_expression, $.member_expression),
824825
alias($._type_query_call_expression, $.call_expression),
@@ -832,6 +833,7 @@ module.exports = function defineGrammar(dialect) {
832833
_type_query_subscript_expression: $ => seq(
833834
field('object', choice(
834835
$.identifier,
836+
$.this,
835837
alias($._type_query_subscript_expression, $.subscript_expression),
836838
alias($._type_query_member_expression, $.member_expression),
837839
alias($._type_query_call_expression, $.call_expression),
@@ -865,6 +867,7 @@ module.exports = function defineGrammar(dialect) {
865867
alias($._type_query_call_expression, $.call_expression),
866868
alias($._type_query_instantiation_expression, $.instantiation_expression),
867869
$.identifier,
870+
$.this,
868871
),
869872
)),
870873

test/corpus/types.txt

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2100,7 +2100,7 @@ type X2 = (typeof Y)[keyof typeof Z];
21002100
(identifier))))))
21012101

21022102
=========================
2103-
Type of instantiation expressions
2103+
Typeof instantiation expressions
21042104
=========================
21052105

21062106
// This is a special construct, different from both "typeof (f<T>)" and
@@ -2119,3 +2119,51 @@ type T = typeof f<T>;
21192119
(identifier)
21202120
(type_arguments
21212121
(type_identifier))))))
2122+
2123+
=========================
2124+
Typeof this
2125+
=========================
2126+
2127+
class A {
2128+
colors: [ { color: "green", code: "#E5F8F2" } ]
2129+
color: (typeof this.colors)[number] = this.colors[0]
2130+
}
2131+
2132+
---
2133+
2134+
(program
2135+
(class_declaration
2136+
(type_identifier)
2137+
(class_body
2138+
(public_field_definition
2139+
(property_identifier)
2140+
(type_annotation
2141+
(tuple_type
2142+
(object_type
2143+
(property_signature
2144+
(property_identifier)
2145+
(type_annotation
2146+
(literal_type
2147+
(string
2148+
(string_fragment)))))
2149+
(property_signature
2150+
(property_identifier)
2151+
(type_annotation
2152+
(literal_type
2153+
(string
2154+
(string_fragment)))))))))
2155+
(public_field_definition
2156+
(property_identifier)
2157+
(type_annotation
2158+
(lookup_type
2159+
(parenthesized_type
2160+
(type_query
2161+
(member_expression
2162+
(this)
2163+
(property_identifier))))
2164+
(predefined_type)))
2165+
(subscript_expression
2166+
(member_expression
2167+
(this)
2168+
(property_identifier))
2169+
(number))))))

0 commit comments

Comments
 (0)