Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit f141d73

Browse files
committed
Tokenize arrow functions in object literals
1 parent e7ca46a commit f141d73

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

grammars/javascript.cson

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,110 @@
537537
}
538538
]
539539
}
540+
{
541+
# foo: ... => ...
542+
'begin': '''(?x)
543+
(?=
544+
\\b[a-zA-Z_$][\\w$]*
545+
\\s*:\\s*
546+
((\\(([^\\(\\)]*)?\\))|[\\w$]+)
547+
\\s*=>
548+
)
549+
'''
550+
'end': '''(?x)
551+
(?<=})|
552+
((?!
553+
\\s*{|
554+
\\G[\\w$]+:|
555+
\\s*/\\*|\\s*//
556+
)(?=\\s*\\S))
557+
'''
558+
'patterns': [
559+
{
560+
'include': '#comments'
561+
}
562+
{
563+
'include': '#function_body'
564+
}
565+
{
566+
'begin': '\\G'
567+
'end': '(?<=(=>))'
568+
'name': 'meta.function.arrow.json.js'
569+
'patterns': [
570+
{
571+
'match': '\\b([a-zA-Z_$][\\w$]*)\\s*(:)\\s*'
572+
'captures':
573+
'1':
574+
'name': 'entity.name.function.js'
575+
'2':
576+
'name': 'keyword.operator.assignment.js'
577+
}
578+
{
579+
'include': '#arrow_function_innards'
580+
}
581+
]
582+
}
583+
]
584+
}
585+
{
586+
# "foo": ... => ...
587+
'begin': '''(?x)
588+
(?=
589+
((\'[^\']*?\')|("[^"]*?"))
590+
\\s*:\\s*
591+
((\\(([^\\(\\)]*)?\\))|[\\w$]+)
592+
\\s*=>
593+
)
594+
'''
595+
'end': '''(?x)
596+
(?<=})|
597+
((?!
598+
\\G((\'[^\']*?\')|("[^"]*?"))|
599+
\\s*{|
600+
\\s*/\\*|\\s*//
601+
)(?=\\s*\\S))
602+
'''
603+
'patterns': [
604+
{
605+
'include': '#comments'
606+
}
607+
{
608+
'include': '#function_body'
609+
}
610+
{
611+
'begin': '\\G'
612+
'end': '(?<=(=>))'
613+
'name': 'meta.function.arrow.json.js'
614+
'patterns': [
615+
{
616+
'match': '(?:((\')([^\']*?)(\'))|((")([^"]*?)(")))\\s*(:)'
617+
'captures':
618+
'1':
619+
'name': 'string.quoted.single.js'
620+
'2':
621+
'name': 'punctuation.definition.string.begin.js'
622+
'3':
623+
'name': 'entity.name.function.js'
624+
'4':
625+
'name': 'punctuation.definition.string.end.js'
626+
'5':
627+
'name': 'string.quoted.double.js'
628+
'6':
629+
'name': 'punctuation.definition.string.begin.js'
630+
'7':
631+
'name': 'entity.name.function.js'
632+
'8':
633+
'name': 'punctuation.definition.string.end.js'
634+
'9':
635+
'name': 'keyword.operator.assignment.js'
636+
}
637+
{
638+
'include': '#arrow_function_innards'
639+
}
640+
]
641+
}
642+
]
643+
}
540644
{
541645
'match': '(=>)'
542646
'captures':

spec/javascript-spec.coffee

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,19 @@ describe "Javascript grammar", ->
11121112
expect(tokens[11]).toEqual value: ')', scopes: ['source.js', 'meta.function.arrow.js', 'meta.parameters.js', 'punctuation.definition.parameters.end.bracket.round.js']
11131113
expect(tokens[13]).toEqual value: '=>', scopes: ['source.js', 'meta.function.arrow.js', 'storage.type.function.arrow.js']
11141114

1115+
it "tokenizes arrow functions in object literals", ->
1116+
{tokens} = grammar.tokenizeLine('foo: param => {}')
1117+
expect(tokens[0]).toEqual value: 'foo', scopes: ['source.js', 'meta.function.arrow.json.js', 'entity.name.function.js']
1118+
expect(tokens[1]).toEqual value: ':', scopes: ['source.js', 'meta.function.arrow.json.js', 'keyword.operator.assignment.js']
1119+
expect(tokens[3]).toEqual value: 'param', scopes: ['source.js', 'meta.function.arrow.json.js', 'meta.parameters.js', 'variable.parameter.function.js']
1120+
expect(tokens[5]).toEqual value: '=>', scopes: ['source.js', 'meta.function.arrow.json.js', 'storage.type.function.arrow.js']
1121+
1122+
{tokens} = grammar.tokenizeLine('"foo": param => {}')
1123+
expect(tokens[1]).toEqual value: 'foo', scopes: ['source.js', 'meta.function.arrow.json.js', 'string.quoted.double.js', 'entity.name.function.js']
1124+
expect(tokens[3]).toEqual value: ':', scopes: ['source.js', 'meta.function.arrow.json.js', 'keyword.operator.assignment.js']
1125+
expect(tokens[5]).toEqual value: 'param', scopes: ['source.js', 'meta.function.arrow.json.js', 'meta.parameters.js', 'variable.parameter.function.js']
1126+
expect(tokens[7]).toEqual value: '=>', scopes: ['source.js', 'meta.function.arrow.json.js', 'storage.type.function.arrow.js']
1127+
11151128
it "tokenizes default parameters", ->
11161129
{tokens} = grammar.tokenizeLine('function multiply(a, b = 1){}')
11171130
expect(tokens[7]).toEqual value: 'b', scopes: ['source.js', 'meta.function.js', 'meta.parameters.js', 'variable.parameter.function.js']

0 commit comments

Comments
 (0)