Skip to content

Commit a7fa5b5

Browse files
committed
Add type parameters to multiline arrow detection
1 parent a7d9782 commit a7fa5b5

9 files changed

+384
-33
lines changed

TypeScript.YAML-tmLanguage

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ variables:
5858
(\s*:{{returnTypeOfArrow}})? # return type
5959
\s*=> # arrow operator
6060
)
61+
possiblyMultilineArrow: ({{typeParameters}}?[\(]\s*$) # during lookup treat <typeparameters>?( followed by line end as arrow
6162
functionOrArrowLookup: |-
6263
\s*(
6364
((async\s+)?(
@@ -66,7 +67,7 @@ variables:
6667
({{identifier}}\s*=>)
6768
)) |
6869
((async\s*)?(
69-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
70+
{{possiblyMultilineArrow}} |
7071
{{arrowLookup}}
7172
))
7273
)
@@ -161,13 +162,13 @@ repository:
161162
- include: '#function-expression'
162163
- include: '#class-expression'
163164
- include: '#arrow-function'
165+
- include: '#paren-expression-possibly-arrow'
164166
- include: '#cast'
165167
- include: '#ternary-expression'
166168
- include: '#new-expr'
167169
- include: '#instanceof-expr'
168170
- include: '#object-literal'
169171
- include: '#expression-operators'
170-
- include: '#paren-expression-possibly-arrow'
171172
- include: '#function-call'
172173
- include: '#literal'
173174
- include: '#support-objects'
@@ -1154,6 +1155,12 @@ repository:
11541155
# but do this only from expression and as last resort
11551156
paren-expression-possibly-arrow:
11561157
patterns:
1158+
- begin: (?:{{startOfIdentifier}}(\basync))?(?=\s*{{typeParameters}}\(\s*$)
1159+
beginCaptures:
1160+
'1': { name: storage.modifier.async.ts }
1161+
end: (?<=>)(?!<)
1162+
patterns:
1163+
- include: '#type-parameters'
11571164
- begin: (?:{{startOfIdentifier}}(\basync)\s*)?(\()(?=\s*$)
11581165
beginCaptures:
11591166
'1': { name: storage.modifier.async.ts }

TypeScript.tmLanguage

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@
243243
<key>include</key>
244244
<string>#arrow-function</string>
245245
</dict>
246+
<dict>
247+
<key>include</key>
248+
<string>#paren-expression-possibly-arrow</string>
249+
</dict>
246250
<dict>
247251
<key>include</key>
248252
<string>#cast</string>
@@ -267,10 +271,6 @@
267271
<key>include</key>
268272
<string>#expression-operators</string>
269273
</dict>
270-
<dict>
271-
<key>include</key>
272-
<string>#paren-expression-possibly-arrow</string>
273-
</dict>
274274
<dict>
275275
<key>include</key>
276276
<string>#function-call</string>
@@ -422,7 +422,7 @@
422422
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
423423
)) |
424424
((async\s*)?(
425-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
425+
((&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;]|\&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;])*\&gt;)*&gt;\s*)?[\(]\s*$) |
426426
# sure shot arrow functions even if =&gt; is on new line
427427
(
428428
[(]\s*
@@ -462,7 +462,7 @@
462462
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
463463
)) |
464464
((async\s*)?(
465-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
465+
((&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;]|\&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;])*\&gt;)*&gt;\s*)?[\(]\s*$) |
466466
# sure shot arrow functions even if =&gt; is on new line
467467
(
468468
[(]\s*
@@ -839,7 +839,7 @@
839839
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
840840
)) |
841841
((async\s*)?(
842-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
842+
((&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;]|\&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;])*\&gt;)*&gt;\s*)?[\(]\s*$) |
843843
# sure shot arrow functions even if =&gt; is on new line
844844
(
845845
[(]\s*
@@ -879,7 +879,7 @@
879879
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
880880
)) |
881881
((async\s*)?(
882-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
882+
((&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;]|\&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;])*\&gt;)*&gt;\s*)?[\(]\s*$) |
883883
# sure shot arrow functions even if =&gt; is on new line
884884
(
885885
[(]\s*
@@ -1254,7 +1254,7 @@
12541254
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
12551255
)) |
12561256
((async\s*)?(
1257-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
1257+
((&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;]|\&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;])*\&gt;)*&gt;\s*)?[\(]\s*$) |
12581258
# sure shot arrow functions even if =&gt; is on new line
12591259
(
12601260
[(]\s*
@@ -1294,7 +1294,7 @@
12941294
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
12951295
)) |
12961296
((async\s*)?(
1297-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
1297+
((&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;]|\&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;])*\&gt;)*&gt;\s*)?[\(]\s*$) |
12981298
# sure shot arrow functions even if =&gt; is on new line
12991299
(
13001300
[(]\s*
@@ -3284,7 +3284,7 @@
32843284
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
32853285
)) |
32863286
((async\s*)?(
3287-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
3287+
((&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;]|\&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;])*\&gt;)*&gt;\s*)?[\(]\s*$) |
32883288
# sure shot arrow functions even if =&gt; is on new line
32893289
(
32903290
[(]\s*
@@ -3600,6 +3600,27 @@
36003600
<dict>
36013601
<key>patterns</key>
36023602
<array>
3603+
<dict>
3604+
<key>begin</key>
3605+
<string>(?:(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(\basync))?(?=\s*(&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;]|\&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;])*\&gt;)*&gt;\s*)\(\s*$)</string>
3606+
<key>beginCaptures</key>
3607+
<dict>
3608+
<key>1</key>
3609+
<dict>
3610+
<key>name</key>
3611+
<string>storage.modifier.async.ts</string>
3612+
</dict>
3613+
</dict>
3614+
<key>end</key>
3615+
<string>(?&lt;=&gt;)(?!&lt;)</string>
3616+
<key>patterns</key>
3617+
<array>
3618+
<dict>
3619+
<key>include</key>
3620+
<string>#type-parameters</string>
3621+
</dict>
3622+
</array>
3623+
</dict>
36033624
<dict>
36043625
<key>begin</key>
36053626
<string>(?:(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(\basync)\s*)?(\()(?=\s*$)</string>
@@ -4671,7 +4692,7 @@
46714692
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
46724693
)) |
46734694
((async\s*)?(
4674-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
4695+
((&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;]|\&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;])*\&gt;)*&gt;\s*)?[\(]\s*$) |
46754696
# sure shot arrow functions even if =&gt; is on new line
46764697
(
46774698
[(]\s*

TypeScriptReact.YAML-tmLanguage

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ scopeName: source.tsx
88
fileTypes: [tsx]
99
uuid: 805375ec-d614-41f5-8993-5843fe63ea82
1010

11+
variables:
12+
possiblyMultilineArrow: ([\(]\s*$) # during lookup treat ( followed by line end as arrow
13+
1114
repository:
1215
# Additions:
1316
# expression repository need to include jsx first followed by whaterver ts grammar says

TypeScriptReact.tmLanguage

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@
247247
<key>include</key>
248248
<string>#arrow-function</string>
249249
</dict>
250+
<dict>
251+
<key>include</key>
252+
<string>#paren-expression-possibly-arrow</string>
253+
</dict>
250254
<dict>
251255
<key>include</key>
252256
<string>#cast</string>
@@ -271,10 +275,6 @@
271275
<key>include</key>
272276
<string>#expression-operators</string>
273277
</dict>
274-
<dict>
275-
<key>include</key>
276-
<string>#paren-expression-possibly-arrow</string>
277-
</dict>
278278
<dict>
279279
<key>include</key>
280280
<string>#function-call</string>
@@ -426,7 +426,7 @@
426426
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
427427
)) |
428428
((async\s*)?(
429-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
429+
([\(]\s*$) |
430430
# sure shot arrow functions even if =&gt; is on new line
431431
(
432432
[(]\s*
@@ -466,7 +466,7 @@
466466
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
467467
)) |
468468
((async\s*)?(
469-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
469+
([\(]\s*$) |
470470
# sure shot arrow functions even if =&gt; is on new line
471471
(
472472
[(]\s*
@@ -843,7 +843,7 @@
843843
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
844844
)) |
845845
((async\s*)?(
846-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
846+
([\(]\s*$) |
847847
# sure shot arrow functions even if =&gt; is on new line
848848
(
849849
[(]\s*
@@ -883,7 +883,7 @@
883883
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
884884
)) |
885885
((async\s*)?(
886-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
886+
([\(]\s*$) |
887887
# sure shot arrow functions even if =&gt; is on new line
888888
(
889889
[(]\s*
@@ -1258,7 +1258,7 @@
12581258
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
12591259
)) |
12601260
((async\s*)?(
1261-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
1261+
([\(]\s*$) |
12621262
# sure shot arrow functions even if =&gt; is on new line
12631263
(
12641264
[(]\s*
@@ -1298,7 +1298,7 @@
12981298
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
12991299
)) |
13001300
((async\s*)?(
1301-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
1301+
([\(]\s*$) |
13021302
# sure shot arrow functions even if =&gt; is on new line
13031303
(
13041304
[(]\s*
@@ -3288,7 +3288,7 @@
32883288
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
32893289
)) |
32903290
((async\s*)?(
3291-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
3291+
([\(]\s*$) |
32923292
# sure shot arrow functions even if =&gt; is on new line
32933293
(
32943294
[(]\s*
@@ -3604,6 +3604,27 @@
36043604
<dict>
36053605
<key>patterns</key>
36063606
<array>
3607+
<dict>
3608+
<key>begin</key>
3609+
<string>(?:(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(\basync))?(?=\s*(&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;]|\&lt;\s*([_$[:alpha:]]|(\{([^\{\}]|(\{[^\{\}]*\}))*\})|(\(([^\(\)]|(\([^\(\)]*\)))*\))|(\[([^\[\]]|(\[[^\[\]]*\]))*\]))([^=&lt;&gt;]|=[^&lt;])*\&gt;)*&gt;\s*)\(\s*$)</string>
3610+
<key>beginCaptures</key>
3611+
<dict>
3612+
<key>1</key>
3613+
<dict>
3614+
<key>name</key>
3615+
<string>storage.modifier.async.tsx</string>
3616+
</dict>
3617+
</dict>
3618+
<key>end</key>
3619+
<string>(?&lt;=&gt;)(?!&lt;)</string>
3620+
<key>patterns</key>
3621+
<array>
3622+
<dict>
3623+
<key>include</key>
3624+
<string>#type-parameters</string>
3625+
</dict>
3626+
</array>
3627+
</dict>
36073628
<dict>
36083629
<key>begin</key>
36093630
<string>(?:(?&lt;![_$[:alnum:]])(?:(?&lt;=\.\.\.)|(?&lt;!\.))(\basync)\s*)?(\()(?=\s*$)</string>
@@ -4617,7 +4638,7 @@
46174638
([_$[:alpha:]][_$[:alnum:]]*\s*=&gt;)
46184639
)) |
46194640
((async\s*)?(
4620-
([\(]\s*$) | # during lookup treat ( followed by line end as arrow
4641+
([\(]\s*$) |
46214642
# sure shot arrow functions even if =&gt; is on new line
46224643
(
46234644
[(]\s*

build/build.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,17 @@ function transformGrammarRepository(grammar: any, propertyNames: string[], trans
5555
}
5656
}
5757

58-
function changeTsToTsxGrammar(grammar: any, variables: any) {
59-
const tsxUpdates = updateGrammarVariables(readYaml(file(Language.TypeScriptReact, Extension.YamlTmLangauge)), variables);
58+
function getTsxGrammar() {
59+
let variables: any;
60+
const tsxUpdatesBeforeTransformation = readYaml(file(Language.TypeScriptReact, Extension.YamlTmLangauge));
61+
const grammar = getTsGrammar(tsGrammarVariables => {
62+
variables = tsGrammarVariables;
63+
for (const variableName in tsxUpdatesBeforeTransformation.variables) {
64+
variables[variableName] = tsxUpdatesBeforeTransformation.variables[variableName];
65+
}
66+
return variables;
67+
});
68+
const tsxUpdates = updateGrammarVariables(tsxUpdatesBeforeTransformation, variables);
6069

6170
// Update name, file types, scope name and uuid
6271
for (let key in tsxUpdates) {
@@ -86,6 +95,11 @@ function changeTsToTsxGrammar(grammar: any, variables: any) {
8695
return grammar;
8796
}
8897

98+
function getTsGrammar(getVariables: (tsGrammarVariables: any) => any) {
99+
const tsGrammarBeforeTransformation = readYaml(file(Language.TypeScript, Extension.YamlTmLangauge));
100+
return updateGrammarVariables(tsGrammarBeforeTransformation, getVariables(tsGrammarBeforeTransformation.variables));
101+
}
102+
89103
function replacePatternVariables(pattern: string, variableReplacers: VariableReplacer[]) {
90104
let result = pattern;
91105
for (const [variableName, value] of variableReplacers) {
@@ -112,16 +126,13 @@ function updateGrammarVariables(grammar: any, variables: any) {
112126
}
113127

114128
function buildGrammar() {
115-
const tsGrammarBeforeTransformation = readYaml(file(Language.TypeScript, Extension.YamlTmLangauge));
116-
const variables = tsGrammarBeforeTransformation.variables;
117-
118-
const tsGrammar = updateGrammarVariables(tsGrammarBeforeTransformation, variables);
129+
const tsGrammar = getTsGrammar(grammarVariables => grammarVariables);
119130

120131
// Write TypeScript.tmLanguage
121132
writePlistFile(tsGrammar, file(Language.TypeScript, Extension.TmLanguage));
122133

123134
// Write TypeScriptReact.tmLangauge
124-
const tsxGrammar = changeTsToTsxGrammar(tsGrammar, variables);
135+
const tsxGrammar = getTsxGrammar();
125136
writePlistFile(tsxGrammar, file(Language.TypeScriptReact, Extension.TmLanguage));
126137
}
127138

0 commit comments

Comments
 (0)