Skip to content

Commit 84422d9

Browse files
committed
Fix type detection in instance of expression
Fixes #814
1 parent dd81cfb commit 84422d9

File tree

5 files changed

+192
-12
lines changed

5 files changed

+192
-12
lines changed

TypeScript.YAML-tmLanguage

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ variables:
5454
lookBehindTypeof: '^typeof|{{nonPropertyLookBehind}}typeof'
5555
lookBehindCase: '^case|{{nonPropertyLookBehind}}case'
5656
lookBehindImport: '^import|{{nonPropertyLookBehind}}import'
57+
lookAheadEndOfType: '[;),}\]:?\-\+\>]|\|\||\&\&|\!\=\=|$'
5758
matchingParenthesis: (\(([^\(\)]|(\([^\(\)]*\)))*\))
5859
matchingBraces: (\{([^\{\}]|(\{[^\{\}]*\}))*\})
5960
matchingBrackets: (\[([^\[\]]|(\[[^\[\]]*\]))*\])
@@ -1395,7 +1396,7 @@ repository:
13951396
begin: '{{startOfIdentifier}}(as)\s+'
13961397
beginCaptures:
13971398
'1': { name: keyword.control.as.ts }
1398-
end: (?=$|^|[,}]|\|\||\&\&|({{startOfIdentifier}}(as)\s+))
1399+
end: (?={{lookAheadEndOfType}}|^|({{startOfIdentifier}}(as)\s+))
13991400
patterns:
14001401
- include: '#type'
14011402
- name: meta.object.member.ts
@@ -1516,7 +1517,7 @@ repository:
15161517
begin: '{{startOfIdentifier}}(new){{endOfIdentifier}}'
15171518
beginCaptures:
15181519
'1': { name: keyword.operator.new.ts }
1519-
end: (?<=\))|(?=[;),}\]:\-\+]|\|\||\&\&|$|({{startOfIdentifier}}new{{endOfIdentifier}})|({{startOfIdentifier}}function((\s+{{identifier}})|(\s*[\(]))))
1520+
end: (?<=\))|(?={{lookAheadEndOfType}}|({{startOfIdentifier}}new{{endOfIdentifier}})|({{startOfIdentifier}}function((\s+{{identifier}})|(\s*[\(]))))
15201521
patterns:
15211522
- include: '#paren-expression'
15221523
- include: '#class-declaration'
@@ -1526,7 +1527,7 @@ repository:
15261527
begin: '{{startOfIdentifier}}(instanceof){{endOfIdentifier}}'
15271528
beginCaptures:
15281529
'1': { name: keyword.operator.expression.instanceof.ts }
1529-
end: (?<=\))|(?=[;),}\]:?]|\|\||\&\&|$|({{startOfIdentifier}}function((\s+{{identifier}})|(\s*[\(]))))
1530+
end: (?<=\))|(?={{lookAheadEndOfType}}|(([\&\~\^\|]\s*)?{{identifier}}\s+instanceof{{endOfIdentifier}})|({{startOfIdentifier}}function((\s+{{identifier}})|(\s*[\(]))))
15301531
patterns:
15311532
- include: '#type'
15321533

@@ -1677,7 +1678,7 @@ repository:
16771678
- begin: '{{startOfIdentifier}}(as)\s+'
16781679
beginCaptures:
16791680
'1': { name: keyword.control.as.ts }
1680-
end: (?=$|^|[;,:})\]]|\|\||\&\&|({{startOfIdentifier}}(as)\s+)|(\s+\<))
1681+
end: (?=^|{{lookAheadEndOfType}}|({{startOfIdentifier}}(as)\s+)|(\s+\<))
16811682
patterns:
16821683
- include: '#type'
16831684
- name: keyword.operator.spread.ts

TypeScript.tmLanguage

+4-4
Original file line numberDiff line numberDiff line change
@@ -4488,7 +4488,7 @@
44884488
</dict>
44894489
</dict>
44904490
<key>end</key>
4491-
<string>(?=$|^|[,}]|\|\||\&amp;\&amp;|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(as)\s+))</string>
4491+
<string>(?=[;),}\]:?\-\+\&gt;]|\|\||\&amp;\&amp;|\!\=\=|$|^|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(as)\s+))</string>
44924492
<key>patterns</key>
44934493
<array>
44944494
<dict>
@@ -4868,7 +4868,7 @@
48684868
</dict>
48694869
</dict>
48704870
<key>end</key>
4871-
<string>(?&lt;=\))|(?=[;),}\]:\-\+]|\|\||\&amp;\&amp;|$|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))new(?![_$[:alnum:]])(?:(?=\.\.\.)|(?!\.)))|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))function((\s+[_$[:alpha:]][_$[:alnum:]]*)|(\s*[\(]))))</string>
4871+
<string>(?&lt;=\))|(?=[;),}\]:?\-\+\&gt;]|\|\||\&amp;\&amp;|\!\=\=|$|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))new(?![_$[:alnum:]])(?:(?=\.\.\.)|(?!\.)))|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))function((\s+[_$[:alpha:]][_$[:alnum:]]*)|(\s*[\(]))))</string>
48724872
<key>patterns</key>
48734873
<array>
48744874
<dict>
@@ -4898,7 +4898,7 @@
48984898
</dict>
48994899
</dict>
49004900
<key>end</key>
4901-
<string>(?&lt;=\))|(?=[;),}\]:?]|\|\||\&amp;\&amp;|$|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))function((\s+[_$[:alpha:]][_$[:alnum:]]*)|(\s*[\(]))))</string>
4901+
<string>(?&lt;=\))|(?=[;),}\]:?\-\+\&gt;]|\|\||\&amp;\&amp;|\!\=\=|$|(([\&amp;\~\^\|]\s*)?[_$[:alpha:]][_$[:alnum:]]*\s+instanceof(?![_$[:alnum:]])(?:(?=\.\.\.)|(?!\.)))|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))function((\s+[_$[:alpha:]][_$[:alnum:]]*)|(\s*[\(]))))</string>
49024902
<key>patterns</key>
49034903
<array>
49044904
<dict>
@@ -5438,7 +5438,7 @@
54385438
</dict>
54395439
</dict>
54405440
<key>end</key>
5441-
<string>(?=$|^|[;,:})\]]|\|\||\&amp;\&amp;|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(as)\s+)|(\s+\&lt;))</string>
5441+
<string>(?=^|[;),}\]:?\-\+\&gt;]|\|\||\&amp;\&amp;|\!\=\=|$|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(as)\s+)|(\s+\&lt;))</string>
54425442
<key>patterns</key>
54435443
<array>
54445444
<dict>

TypeScriptReact.tmLanguage

+4-4
Original file line numberDiff line numberDiff line change
@@ -4512,7 +4512,7 @@
45124512
</dict>
45134513
</dict>
45144514
<key>end</key>
4515-
<string>(?=$|^|[,}]|\|\||\&amp;\&amp;|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(as)\s+))</string>
4515+
<string>(?=[;),}\]:?\-\+\&gt;]|\|\||\&amp;\&amp;|\!\=\=|$|^|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(as)\s+))</string>
45164516
<key>patterns</key>
45174517
<array>
45184518
<dict>
@@ -4892,7 +4892,7 @@
48924892
</dict>
48934893
</dict>
48944894
<key>end</key>
4895-
<string>(?&lt;=\))|(?=[;),}\]:\-\+]|\|\||\&amp;\&amp;|$|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))new(?![_$[:alnum:]])(?:(?=\.\.\.)|(?!\.)))|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))function((\s+[_$[:alpha:]][_$[:alnum:]]*)|(\s*[\(]))))</string>
4895+
<string>(?&lt;=\))|(?=[;),}\]:?\-\+\&gt;]|\|\||\&amp;\&amp;|\!\=\=|$|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))new(?![_$[:alnum:]])(?:(?=\.\.\.)|(?!\.)))|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))function((\s+[_$[:alpha:]][_$[:alnum:]]*)|(\s*[\(]))))</string>
48964896
<key>patterns</key>
48974897
<array>
48984898
<dict>
@@ -4922,7 +4922,7 @@
49224922
</dict>
49234923
</dict>
49244924
<key>end</key>
4925-
<string>(?&lt;=\))|(?=[;),}\]:?]|\|\||\&amp;\&amp;|$|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))function((\s+[_$[:alpha:]][_$[:alnum:]]*)|(\s*[\(]))))</string>
4925+
<string>(?&lt;=\))|(?=[;),}\]:?\-\+\&gt;]|\|\||\&amp;\&amp;|\!\=\=|$|(([\&amp;\~\^\|]\s*)?[_$[:alpha:]][_$[:alnum:]]*\s+instanceof(?![_$[:alnum:]])(?:(?=\.\.\.)|(?!\.)))|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))function((\s+[_$[:alpha:]][_$[:alnum:]]*)|(\s*[\(]))))</string>
49264926
<key>patterns</key>
49274927
<array>
49284928
<dict>
@@ -5384,7 +5384,7 @@
53845384
</dict>
53855385
</dict>
53865386
<key>end</key>
5387-
<string>(?=$|^|[;,:})\]]|\|\||\&amp;\&amp;|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(as)\s+)|(\s+\&lt;))</string>
5387+
<string>(?=^|[;),}\]:?\-\+\&gt;]|\|\||\&amp;\&amp;|\!\=\=|$|((?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(as)\s+)|(\s+\&lt;))</string>
53885388
<key>patterns</key>
53895389
<array>
53905390
<dict>

tests/baselines/issue814.baseline.txt

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
original file
2+
-----------------------------------
3+
class A{}
4+
class B{}
5+
6+
const a = new A()
7+
const b = new B()
8+
9+
a instanceof A !== b instanceof B
10+
a instanceof A > b instanceof B
11+
a instanceof A & b instanceof B
12+
-----------------------------------
13+
14+
Grammar: TypeScript.tmLanguage
15+
-----------------------------------
16+
>class A{}
17+
^^^^^
18+
source.ts meta.class.ts storage.type.class.ts
19+
^
20+
source.ts meta.class.ts
21+
^
22+
source.ts meta.class.ts entity.name.type.class.ts
23+
^
24+
source.ts meta.class.ts punctuation.definition.block.ts
25+
^
26+
source.ts meta.class.ts punctuation.definition.block.ts
27+
>class B{}
28+
^^^^^
29+
source.ts meta.class.ts storage.type.class.ts
30+
^
31+
source.ts meta.class.ts
32+
^
33+
source.ts meta.class.ts entity.name.type.class.ts
34+
^
35+
source.ts meta.class.ts punctuation.definition.block.ts
36+
^
37+
source.ts meta.class.ts punctuation.definition.block.ts
38+
>
39+
^
40+
source.ts
41+
>const a = new A()
42+
^^^^^
43+
source.ts meta.var.expr.ts storage.type.ts
44+
^
45+
source.ts meta.var.expr.ts
46+
^
47+
source.ts meta.var.expr.ts meta.var-single-variable.expr.ts meta.definition.variable.ts variable.other.constant.ts
48+
^
49+
source.ts meta.var.expr.ts meta.var-single-variable.expr.ts
50+
^
51+
source.ts meta.var.expr.ts keyword.operator.assignment.ts
52+
^
53+
source.ts meta.var.expr.ts
54+
^^^
55+
source.ts meta.var.expr.ts new.expr.ts keyword.operator.new.ts
56+
^
57+
source.ts meta.var.expr.ts new.expr.ts
58+
^
59+
source.ts meta.var.expr.ts new.expr.ts entity.name.type.ts
60+
^
61+
source.ts meta.var.expr.ts new.expr.ts meta.brace.round.ts
62+
^
63+
source.ts meta.var.expr.ts new.expr.ts meta.brace.round.ts
64+
>const b = new B()
65+
^^^^^
66+
source.ts meta.var.expr.ts storage.type.ts
67+
^
68+
source.ts meta.var.expr.ts
69+
^
70+
source.ts meta.var.expr.ts meta.var-single-variable.expr.ts meta.definition.variable.ts variable.other.constant.ts
71+
^
72+
source.ts meta.var.expr.ts meta.var-single-variable.expr.ts
73+
^
74+
source.ts meta.var.expr.ts keyword.operator.assignment.ts
75+
^
76+
source.ts meta.var.expr.ts
77+
^^^
78+
source.ts meta.var.expr.ts new.expr.ts keyword.operator.new.ts
79+
^
80+
source.ts meta.var.expr.ts new.expr.ts
81+
^
82+
source.ts meta.var.expr.ts new.expr.ts entity.name.type.ts
83+
^
84+
source.ts meta.var.expr.ts new.expr.ts meta.brace.round.ts
85+
^
86+
source.ts meta.var.expr.ts new.expr.ts meta.brace.round.ts
87+
>
88+
^
89+
source.ts
90+
>a instanceof A !== b instanceof B
91+
^
92+
source.ts variable.other.readwrite.ts
93+
^
94+
source.ts
95+
^^^^^^^^^^
96+
source.ts keyword.operator.expression.instanceof.ts
97+
^
98+
source.ts
99+
^
100+
source.ts entity.name.type.ts
101+
^
102+
source.ts
103+
^^^
104+
source.ts keyword.operator.comparison.ts
105+
^
106+
source.ts
107+
^
108+
source.ts variable.other.readwrite.ts
109+
^
110+
source.ts
111+
^^^^^^^^^^
112+
source.ts keyword.operator.expression.instanceof.ts
113+
^
114+
source.ts
115+
^
116+
source.ts entity.name.type.ts
117+
>a instanceof A > b instanceof B
118+
^
119+
source.ts variable.other.readwrite.ts
120+
^
121+
source.ts
122+
^^^^^^^^^^
123+
source.ts keyword.operator.expression.instanceof.ts
124+
^
125+
source.ts
126+
^
127+
source.ts entity.name.type.ts
128+
^
129+
source.ts
130+
^
131+
source.ts keyword.operator.relational.ts
132+
^
133+
source.ts
134+
^
135+
source.ts variable.other.readwrite.ts
136+
^
137+
source.ts
138+
^^^^^^^^^^
139+
source.ts keyword.operator.expression.instanceof.ts
140+
^
141+
source.ts
142+
^
143+
source.ts entity.name.type.ts
144+
>a instanceof A & b instanceof B
145+
^
146+
source.ts variable.other.readwrite.ts
147+
^
148+
source.ts
149+
^^^^^^^^^^
150+
source.ts keyword.operator.expression.instanceof.ts
151+
^
152+
source.ts
153+
^
154+
source.ts entity.name.type.ts
155+
^
156+
source.ts
157+
^
158+
source.ts keyword.operator.bitwise.ts
159+
^
160+
source.ts
161+
^
162+
source.ts variable.other.readwrite.ts
163+
^
164+
source.ts
165+
^^^^^^^^^^
166+
source.ts keyword.operator.expression.instanceof.ts
167+
^
168+
source.ts
169+
^
170+
source.ts entity.name.type.ts

tests/cases/issue814.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class A{}
2+
class B{}
3+
4+
const a = new A()
5+
const b = new B()
6+
7+
a instanceof A !== b instanceof B
8+
a instanceof A > b instanceof B
9+
a instanceof A & b instanceof B

0 commit comments

Comments
 (0)