Skip to content

Commit b68820e

Browse files
committed
Treat only expression like <Identifier> on new line as cast expression
Fixes #434
1 parent 85b2cc2 commit b68820e

File tree

4 files changed

+443
-34
lines changed

4 files changed

+443
-34
lines changed

TypeScript.YAML-tmLanguage

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,19 +1444,33 @@ repository:
14441444
'2': { name: variable.other.object.ts }
14451445
14461446
cast:
1447-
name: cast.expr.ts
1448-
# We need to differentiate between the relational '<' operator and the beginning of a type assertion/cast using the surrounding context.
1449-
# If '<' is preceeded by 'return', 'throw', 'yield', or 'await', it's most likely a type assertion
1450-
# If '=', '(', ',', ':', or '>' are followed by a '<', it is also likely a type assertion as otherwise it would be a syntax error
1451-
# '<=' and '<<=' are cannot be type assertions, as they are assignment operators.
1452-
begin: (?:(?<=return|throw|yield|await|default|^|[=(,:>*]))\s*(<)(?!<?\=)
1453-
beginCaptures:
1454-
'1': { name: meta.brace.angle.ts }
1455-
end: \>
1456-
endCaptures:
1457-
'0': { name: meta.brace.angle.ts }
14581447
patterns:
1459-
- include: '#type'
1448+
- name: cast.expr.ts
1449+
# We need to differentiate between the relational '<' operator and the beginning of a type assertion/cast using the surrounding context.
1450+
# If '<' is preceeded by 'return', 'throw', 'yield', or 'await', it's most likely a type assertion
1451+
# If '=', '(', ',', ':', or '>' are followed by a '<', it is also likely a type assertion as otherwise it would be a syntax error
1452+
# '<=' and '<<=' are cannot be type assertions, as they are assignment operators.
1453+
begin: (?:(?<=return|throw|yield|await|default|[=(,:>*]))\s*(<)(?!<?\=)
1454+
beginCaptures:
1455+
'1': { name: meta.brace.angle.ts }
1456+
end: \>
1457+
endCaptures:
1458+
'0': { name: meta.brace.angle.ts }
1459+
patterns:
1460+
- include: '#type'
1461+
- name: cast.expr.ts
1462+
# Specialize the pattern that is allowed at the beginning of the new line to treat as cast
1463+
# < appears on new line allow only <Identifier> as type assertion
1464+
# This allows treating '<' on new line as relation operator in other cases.
1465+
# People writing one operator on new line and second one on same line should be rare to allow this.
1466+
begin: (?:(?<=^))\s*(<)(?=[_$[:alpha:]][_$[:alnum:]]*\s*>)
1467+
beginCaptures:
1468+
'1': { name: meta.brace.angle.ts }
1469+
end: \>
1470+
endCaptures:
1471+
'0': { name: meta.brace.angle.ts }
1472+
patterns:
1473+
- include: '#type'
14601474

14611475
new-expr:
14621476
name: new.expr.ts

TypeScript.tmLanguage

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3974,33 +3974,69 @@
39743974
</dict>
39753975
<key>cast</key>
39763976
<dict>
3977-
<key>name</key>
3978-
<string>cast.expr.ts</string>
3979-
<key>begin</key>
3980-
<string>(?:(?&lt;=return|throw|yield|await|default|^|[=(,:&gt;*]))\s*(&lt;)(?!&lt;?\=)</string>
3981-
<key>beginCaptures</key>
3982-
<dict>
3983-
<key>1</key>
3977+
<key>patterns</key>
3978+
<array>
39843979
<dict>
39853980
<key>name</key>
3986-
<string>meta.brace.angle.ts</string>
3981+
<string>cast.expr.ts</string>
3982+
<key>begin</key>
3983+
<string>(?:(?&lt;=return|throw|yield|await|default|[=(,:&gt;*]))\s*(&lt;)(?!&lt;?\=)</string>
3984+
<key>beginCaptures</key>
3985+
<dict>
3986+
<key>1</key>
3987+
<dict>
3988+
<key>name</key>
3989+
<string>meta.brace.angle.ts</string>
3990+
</dict>
3991+
</dict>
3992+
<key>end</key>
3993+
<string>\&gt;</string>
3994+
<key>endCaptures</key>
3995+
<dict>
3996+
<key>0</key>
3997+
<dict>
3998+
<key>name</key>
3999+
<string>meta.brace.angle.ts</string>
4000+
</dict>
4001+
</dict>
4002+
<key>patterns</key>
4003+
<array>
4004+
<dict>
4005+
<key>include</key>
4006+
<string>#type</string>
4007+
</dict>
4008+
</array>
39874009
</dict>
3988-
</dict>
3989-
<key>end</key>
3990-
<string>\&gt;</string>
3991-
<key>endCaptures</key>
3992-
<dict>
3993-
<key>0</key>
39944010
<dict>
39954011
<key>name</key>
3996-
<string>meta.brace.angle.ts</string>
3997-
</dict>
3998-
</dict>
3999-
<key>patterns</key>
4000-
<array>
4001-
<dict>
4002-
<key>include</key>
4003-
<string>#type</string>
4012+
<string>cast.expr.ts</string>
4013+
<key>begin</key>
4014+
<string>(?:(?&lt;=^))\s*(&lt;)(?=[_$[:alpha:]][_$[:alnum:]]*\s*&gt;)</string>
4015+
<key>beginCaptures</key>
4016+
<dict>
4017+
<key>1</key>
4018+
<dict>
4019+
<key>name</key>
4020+
<string>meta.brace.angle.ts</string>
4021+
</dict>
4022+
</dict>
4023+
<key>end</key>
4024+
<string>\&gt;</string>
4025+
<key>endCaptures</key>
4026+
<dict>
4027+
<key>0</key>
4028+
<dict>
4029+
<key>name</key>
4030+
<string>meta.brace.angle.ts</string>
4031+
</dict>
4032+
</dict>
4033+
<key>patterns</key>
4034+
<array>
4035+
<dict>
4036+
<key>include</key>
4037+
<string>#type</string>
4038+
</dict>
4039+
</array>
40044040
</dict>
40054041
</array>
40064042
</dict>

0 commit comments

Comments
 (0)