Skip to content

Commit ad1c7da

Browse files
committed
Add special handling of arrow function end to be start of another declaration
This helps in error recovery scenario when arrow is detected instead of function parameters on new line Fixes #428
1 parent 36ad8ae commit ad1c7da

File tree

5 files changed

+165
-6
lines changed

5 files changed

+165
-6
lines changed

TypeScript.YAML-tmLanguage

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,7 @@ repository:
16451645
)
16461646
beginCaptures:
16471647
'1': { name: storage.modifier.async.ts }
1648-
end: (?==>|\{)
1648+
end: (?==>|\{|(^\s*(export|function|class|interface|let|var|const|import|enum|namespace|module|type|abstract|declare)\s+))
16491649
patterns:
16501650
- include: '#comment'
16511651
- include: '#type-parameters'
@@ -1667,7 +1667,7 @@ repository:
16671667
begin: '(?<=\))\s*(:)'
16681668
beginCaptures:
16691669
'1': { name: keyword.operator.type.annotation.ts }
1670-
end: (?==>|\{)
1670+
end: (?==>|\{|(^\s*(export|function|class|interface|let|var|const|import|enum|namespace|module|type|abstract|declare)\s+))
16711671
patterns:
16721672
# TODO: handle the fn and constructor type specifically.
16731673
# Handle returning of object type specifically here so as to not confuse it with the start of function block

TypeScript.tmLanguage

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4509,7 +4509,7 @@
45094509
</dict>
45104510
</dict>
45114511
<key>end</key>
4512-
<string>(?==&gt;|\{)</string>
4512+
<string>(?==&gt;|\{|(^\s*(export|function|class|interface|let|var|const|import|enum|namespace|module|type|abstract|declare)\s+))</string>
45134513
<key>patterns</key>
45144514
<array>
45154515
<dict>
@@ -4574,7 +4574,7 @@
45744574
</dict>
45754575
</dict>
45764576
<key>end</key>
4577-
<string>(?==&gt;|\{)</string>
4577+
<string>(?==&gt;|\{|(^\s*(export|function|class|interface|let|var|const|import|enum|namespace|module|type|abstract|declare)\s+))</string>
45784578
<key>patterns</key>
45794579
<array>
45804580
<dict>

TypeScriptReact.tmLanguage

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4455,7 +4455,7 @@
44554455
</dict>
44564456
</dict>
44574457
<key>end</key>
4458-
<string>(?==&gt;|\{)</string>
4458+
<string>(?==&gt;|\{|(^\s*(export|function|class|interface|let|var|const|import|enum|namespace|module|type|abstract|declare)\s+))</string>
44594459
<key>patterns</key>
44604460
<array>
44614461
<dict>
@@ -4520,7 +4520,7 @@
45204520
</dict>
45214521
</dict>
45224522
<key>end</key>
4523-
<string>(?==&gt;|\{)</string>
4523+
<string>(?==&gt;|\{|(^\s*(export|function|class|interface|let|var|const|import|enum|namespace|module|type|abstract|declare)\s+))</string>
45244524
<key>patterns</key>
45254525
<array>
45264526
<dict>

tests/baselines/Issue428.baseline.txt

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
original file
2+
-----------------------------------
3+
export function funct
4+
(a: number): string
5+
6+
export function funct
7+
(a: number): number
8+
9+
// this is implementation
10+
export function funct
11+
(a: number): any {
12+
13+
}
14+
-----------------------------------
15+
16+
Grammar: TypeScript.tmLanguage
17+
-----------------------------------
18+
>export function funct
19+
^^^^^^
20+
source.ts meta.function.ts keyword.control.export.ts
21+
^
22+
source.ts meta.function.ts
23+
^^^^^^^^
24+
source.ts meta.function.ts storage.type.function.ts
25+
^
26+
source.ts meta.function.ts
27+
^^^^^
28+
source.ts meta.function.ts meta.definition.function.ts entity.name.function.ts
29+
^^
30+
source.ts meta.function.ts
31+
> (a: number): string
32+
^^
33+
source.ts meta.arrow.ts
34+
^
35+
source.ts meta.arrow.ts meta.parameters.ts punctuation.definition.parameters.begin.ts
36+
^
37+
source.ts meta.arrow.ts meta.parameters.ts variable.parameter.ts
38+
^
39+
source.ts meta.arrow.ts meta.parameters.ts meta.type.annotation.ts keyword.operator.type.annotation.ts
40+
^
41+
source.ts meta.arrow.ts meta.parameters.ts meta.type.annotation.ts
42+
^^^^^^
43+
source.ts meta.arrow.ts meta.parameters.ts meta.type.annotation.ts support.type.primitive.ts
44+
^
45+
source.ts meta.arrow.ts meta.parameters.ts punctuation.definition.parameters.end.ts
46+
^
47+
source.ts meta.arrow.ts meta.return.type.arrow.ts keyword.operator.type.annotation.ts
48+
^
49+
source.ts meta.arrow.ts meta.return.type.arrow.ts
50+
^^^^^^
51+
source.ts meta.arrow.ts meta.return.type.arrow.ts support.type.primitive.ts
52+
^^^
53+
source.ts meta.arrow.ts meta.return.type.arrow.ts
54+
>
55+
^^
56+
source.ts meta.arrow.ts meta.return.type.arrow.ts
57+
>export function funct
58+
^^^^^^
59+
source.ts meta.function.ts keyword.control.export.ts
60+
^
61+
source.ts meta.function.ts
62+
^^^^^^^^
63+
source.ts meta.function.ts storage.type.function.ts
64+
^
65+
source.ts meta.function.ts
66+
^^^^^
67+
source.ts meta.function.ts meta.definition.function.ts entity.name.function.ts
68+
^^
69+
source.ts meta.function.ts
70+
> (a: number): number
71+
^^
72+
source.ts meta.arrow.ts
73+
^
74+
source.ts meta.arrow.ts meta.parameters.ts punctuation.definition.parameters.begin.ts
75+
^
76+
source.ts meta.arrow.ts meta.parameters.ts variable.parameter.ts
77+
^
78+
source.ts meta.arrow.ts meta.parameters.ts meta.type.annotation.ts keyword.operator.type.annotation.ts
79+
^
80+
source.ts meta.arrow.ts meta.parameters.ts meta.type.annotation.ts
81+
^^^^^^
82+
source.ts meta.arrow.ts meta.parameters.ts meta.type.annotation.ts support.type.primitive.ts
83+
^
84+
source.ts meta.arrow.ts meta.parameters.ts punctuation.definition.parameters.end.ts
85+
^
86+
source.ts meta.arrow.ts meta.return.type.arrow.ts keyword.operator.type.annotation.ts
87+
^
88+
source.ts meta.arrow.ts meta.return.type.arrow.ts
89+
^^^^^^
90+
source.ts meta.arrow.ts meta.return.type.arrow.ts support.type.primitive.ts
91+
^^
92+
source.ts meta.arrow.ts meta.return.type.arrow.ts
93+
>
94+
^^
95+
source.ts meta.arrow.ts meta.return.type.arrow.ts
96+
>// this is implementation
97+
^^
98+
source.ts meta.arrow.ts meta.return.type.arrow.ts comment.line.double-slash.ts punctuation.definition.comment.ts
99+
^^^^^^^^^^^^^^^^^^^^^^^^
100+
source.ts meta.arrow.ts meta.return.type.arrow.ts comment.line.double-slash.ts
101+
>export function funct
102+
^^^^^^
103+
source.ts meta.function.ts keyword.control.export.ts
104+
^
105+
source.ts meta.function.ts
106+
^^^^^^^^
107+
source.ts meta.function.ts storage.type.function.ts
108+
^
109+
source.ts meta.function.ts
110+
^^^^^
111+
source.ts meta.function.ts meta.definition.function.ts entity.name.function.ts
112+
^^
113+
source.ts meta.function.ts
114+
> (a: number): any {
115+
^^
116+
source.ts meta.arrow.ts
117+
^
118+
source.ts meta.arrow.ts meta.parameters.ts punctuation.definition.parameters.begin.ts
119+
^
120+
source.ts meta.arrow.ts meta.parameters.ts variable.parameter.ts
121+
^
122+
source.ts meta.arrow.ts meta.parameters.ts meta.type.annotation.ts keyword.operator.type.annotation.ts
123+
^
124+
source.ts meta.arrow.ts meta.parameters.ts meta.type.annotation.ts
125+
^^^^^^
126+
source.ts meta.arrow.ts meta.parameters.ts meta.type.annotation.ts support.type.primitive.ts
127+
^
128+
source.ts meta.arrow.ts meta.parameters.ts punctuation.definition.parameters.end.ts
129+
^
130+
source.ts meta.arrow.ts meta.return.type.arrow.ts keyword.operator.type.annotation.ts
131+
^
132+
source.ts meta.arrow.ts meta.return.type.arrow.ts
133+
^^^
134+
source.ts meta.arrow.ts meta.return.type.arrow.ts support.type.primitive.ts
135+
^
136+
source.ts meta.arrow.ts meta.return.type.arrow.ts
137+
^
138+
source.ts meta.block.ts punctuation.definition.block.ts
139+
^^
140+
source.ts meta.block.ts
141+
>
142+
^^^^
143+
source.ts meta.block.ts
144+
> }
145+
^^
146+
source.ts meta.block.ts
147+
^
148+
source.ts meta.block.ts punctuation.definition.block.ts

tests/cases/Issue428.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function funct
2+
(a: number): string
3+
4+
export function funct
5+
(a: number): number
6+
7+
// this is implementation
8+
export function funct
9+
(a: number): any {
10+
11+
}

0 commit comments

Comments
 (0)