Skip to content

Commit c89b525

Browse files
committed
Merge pull request #155 from zhengbli/issue150
Fix issue 150
2 parents 9f5f72e + 26bc1de commit c89b525

File tree

2 files changed

+237
-137
lines changed

2 files changed

+237
-137
lines changed

TypeScript.YAML-tmLanguage

Lines changed: 97 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,13 @@ repository:
117117

118118
field-declaration:
119119
name: meta.field.declaration.ts
120-
match: '(?<!\()\s*\b([a-zA-Z_$][\w$]*)\s*(\?\s*)?(?=(=|:))'
121-
captures:
120+
begin: '(?<!\()\s*\b([a-zA-Z_$][\w$]*)\s*(\?\s*)?(?=(=|:))'
121+
beginCaptures:
122122
'1': { name: variable.ts }
123123
'2': { name: keyword.operator.ts }
124+
end: '(?=\}|;|,|\n)|(?<=\})'
125+
patterns:
126+
- include: '#expression'
124127

125128
method-declaration:
126129
name: meta.method.declaration.ts
@@ -130,11 +133,11 @@ repository:
130133
'2': { name: keyword.operator.ts }
131134
'3': { name: entity.name.function.ts }
132135
'4': { name: keyword.operator.ts }
133-
end: '(?=\}|;|,)|(?<=\})'
136+
end: '(?=\}|;|,|\n)|(?<=\})'
134137
patterns:
135138
- include: '#comment'
136139
- include: '#type-parameters'
137-
- include: '#function-parameters'
140+
- include: '#function-type-parameters'
138141
- include: '#type-annotation'
139142
- include: '#decl-block'
140143

@@ -166,12 +169,20 @@ repository:
166169
'2': { name: entity.name.function.ts }
167170
end: (?=;|\})|(?<=\})
168171
patterns:
172+
- include: '#function-overload-declaration'
169173
- include: '#comment'
170174
- include: '#type-parameters'
171-
- include: '#function-parameters'
175+
- include: '#function-type-parameters'
172176
- include: '#return-type'
173177
- include: '#decl-block'
174178

179+
function-overload-declaration:
180+
name: meta.function.overload.ts
181+
match: \b(function)\b(?:\s+([a-zA-Z_$][\w$]*))?\s*
182+
captures:
183+
'1': { name: storage.type.function.ts }
184+
'2': { name: entity.name.function.ts }
185+
175186
block:
176187
name: meta.block.ts
177188
begin: \{
@@ -195,34 +206,6 @@ repository:
195206
patterns:
196207
- include: '#expression'
197208

198-
function-parameters:
199-
name: meta.function-parameters.ts
200-
begin: \(
201-
beginCaptures:
202-
'0': { name: meta.brace.round.ts }
203-
end: \)
204-
endCaptures:
205-
'0': { name: meta.brace.round.ts }
206-
patterns:
207-
- include: '#comment'
208-
- include: '#parameter-name'
209-
- include: '#type-annotation'
210-
- include: '#variable-initializer'
211-
212-
function-type-parameters:
213-
name: meta.function.type.ts
214-
begin: (\()
215-
beginCaptures:
216-
'1': { name: meta.brace.round.ts }
217-
end: (\))
218-
endCaptures:
219-
'1': { name: meta.brace.round.ts }
220-
patterns:
221-
- include: '#comment'
222-
- include: '#parameter-name'
223-
- include: '#type-annotation'
224-
- include: '#variable-initializer'
225-
226209
parameter-name:
227210
name: parameter.name.ts
228211
match: '(?:\s*\b(public|private)\b\s+)?(\.\.\.)?\s*([a-zA-Z_$][\w$]*)\s*(\??)'
@@ -234,51 +217,91 @@ repository:
234217

235218
return-type:
236219
name: meta.return.type.ts
237-
begin: '(?<=\)):'
220+
begin: '(?<=\))\s*:'
238221
end: (?=$)|(?=\{|;|//)
239222
patterns:
240223
- include: '#type'
241224

242225
type-annotation:
243226
name: meta.type.annotation.ts
244227
begin: ":"
245-
end: (?=$|[,);\}\[\]]|//)|(?==[^>])|(?<=\}|[a-zA-Z_$]|>)\s*(?=\{)
228+
end: (?=$|[,);\}\]]|//)|(?==[^>])|(?<=\}|[a-zA-Z_$]|>)\s*(?=\{)
246229
patterns:
247-
- include: '#type'
248-
- include: '#comment'
230+
- include: '#type'
231+
- include: '#comment'
249232

250233
type:
251234
name: meta.type.ts
252235
patterns:
253-
- include: '#type-name'
236+
- include: '#type-primitive'
254237
- include: '#type-parameters'
255-
- include: '#type-union'
238+
- include: '#type-tuple'
256239
- include: '#object-type'
257-
- include: '#function-type-parameters'
240+
- include: '#type-operator'
241+
- include: '#paren-type-or-function-type-parameters'
258242
- include: '#function-type-return-type'
243+
- include: '#type-name'
244+
245+
function-type-parameters:
246+
name: meta.function.type.parameter.ts
247+
begin: \(
248+
beginCaptures:
249+
'0': { name: meta.brace.round.ts }
250+
end: \)
251+
endCaptures:
252+
'0': { name: meta.brace.round.ts }
253+
patterns:
254+
- include: '#comment'
255+
- include: '#parameter-name'
256+
- include: '#type-annotation'
257+
- include: '#variable-initializer'
258+
259+
type-primitive:
260+
name: storage.type.ts
261+
match: '\b(string|number|boolean|symbol|any|void)\b'
262+
263+
# Parenthesis can contain either types and function parameters
264+
# (number | string) or (param: number, param2: string)
265+
# and it can be nested, for example
266+
# (number | (param: number, param2: string) => void)
267+
paren-type-or-function-type-parameters:
268+
name: meta.type.paren.cover.ts
269+
begin: '(?:\b(new)\b)?\s*\('
270+
beginCaptures:
271+
'1': { name: keyword.operator.ts }
272+
end: '\)'
273+
patterns:
274+
- include: '#comment'
275+
- include: '#type'
276+
- include: '#function-type-parameters'
277+
278+
type-operator:
279+
name: keyword.operator.type.ts
280+
match: '[.|]'
259281

260282
function-type-return-type:
261283
name: meta.function.type.return.ts
262284
begin: '=>'
263285
beginCaptures:
264286
'0': { name: keyword.operator.ts }
265-
end: (?=[,\)\{=;]|//)
287+
end: (?=[,\)\{=;]|//|$)
266288
patterns:
267289
- include: '#type'
268290

269-
type-union:
270-
name: meta.type.union.ts
271-
begin: '(\|)'
291+
type-tuple:
292+
name: meta.type.tuple.ts
293+
begin: \[
272294
beginCaptures:
273-
'1': { name: keyword.operator.ts }
274-
end: '([a-zA-Z_$][.\w$]*)'
295+
'0': { name: meta.brace.square.ts }
296+
end: \]
275297
endCaptures:
276-
'1': { name: storage.type.ts }
298+
'0': { name: meta.brace.square.ts }
277299
patterns:
300+
- include: '#type'
278301
- include: '#comment'
279302

280303
type-name:
281-
name: storage.type.ts
304+
name: entity.name.type.ts
282305
match: '[a-zA-Z_$][.\w$]*'
283306

284307
type-parameters:
@@ -332,18 +355,32 @@ repository:
332355
captures:
333356
'1': { name: storage.type.ts }
334357
'3': { name: keyword.operator.ts }
358+
335359
cast:
336360
name: cast.expr.ts
337-
match: '<\s*([a-zA-Z_$][.\w$]*)\s*(?:<([a-zA-Z_$][.\w$]*)>)?\s*(\[\])*\s*>'
338-
captures:
339-
'1': { name: storage.type.ts }
340-
'2': { name: storage.type.ts }
361+
# To differentiate a cast vs a relational <, this regexp tries to use the surrounding context
362+
# If < is preceeded by 'return', 'throw', 'yield' its most likely a type assertion
363+
# An '=', '(', ',', ':', or '>' character followed by '<' is likely a type assertion, as otherwise its a syntax error
364+
# '<=' and '<<=' are definately not type assertions, as they are assignment operators.
365+
begin: '(?:(?<=return|throw|yield|[=(,:>]))\s*(<)(?!<?\=)'
366+
beginCaptures:
367+
'1': { name: keyword.operator.type.ts }
368+
end: '>'
369+
endCaptures:
370+
'0': { name: keyword.operator.type.ts }
371+
patterns:
372+
- include: '#type'
373+
341374
new-expr:
342375
name: new.expr.ts
343-
match: '\b(new)\b\s*([a-zA-Z_$][.\w$]*)'
344-
captures:
345-
'1': { name: keyword.operator.ts }
346-
'2': { name: storage.type.ts }
376+
begin: '\b(new)\b'
377+
beginCaptures:
378+
'1': { name: keyword.operator.ts }
379+
# new-expr ends just before any type or call arguments. Type arguments will be picked up by #cast.
380+
end: '(?=[<(;]|$)'
381+
patterns:
382+
- include: '#type'
383+
347384
object-member:
348385
name: meta.object.member.ts
349386
begin: '[a-zA-Z_$][\w$]*\s*:'
@@ -356,19 +393,19 @@ repository:
356393
match: =>|\b(delete|export|import|in|instanceof|module|new|typeof|void)\b
357394

358395
arithmetic-operator:
359-
name: keyword.operator.ts
396+
name: keyword.operator.arithmetic.ts
360397
match: \*|/|\-\-|\-|\+\+|\+|%
361398

362399
relational-operator:
363-
name: keyword.operator.ts
400+
name: keyword.operator.comparison.ts
364401
match: ===|==|=|!=|!==|<=|>=|<>|<|>
365402

366403
assignment-operator:
367-
name: keyword.operator.ts
404+
name: keyword.operator.assignment.ts
368405
match: <<=|>>=|>>>=|\*=|(?<!\()/=|%=|\+=|\-=|&=|\^=
369406

370407
logic-operator:
371-
name: keyword.operator.ts
408+
name: keyword.operator.arithmetic.ts
372409
match: \!|&|~|\||&&|\|\|
373410

374411
storage-keyword:

0 commit comments

Comments
 (0)