Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 799fd63

Browse files
JamesHenrynzakas
authored andcommitted
Fix: Distinguish between DeclareFunction and FunctionDeclaration (#79)
Refs #78
1 parent d58041f commit 799fd63

File tree

3 files changed

+329
-1
lines changed

3 files changed

+329
-1
lines changed

lib/ast-converter.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -765,22 +765,36 @@ module.exports = function(ast, extra) {
765765
// Declarations
766766

767767
case SyntaxKind.FunctionDeclaration:
768+
769+
var functionDeclarationType = "FunctionDeclaration";
770+
if (node.modifiers && node.modifiers.length) {
771+
var isDeclareFunction = node.modifiers.some(function(modifier) {
772+
return modifier.kind === ts.SyntaxKind.DeclareKeyword;
773+
});
774+
if (isDeclareFunction) {
775+
functionDeclarationType = "DeclareFunction";
776+
}
777+
}
778+
768779
assign(result, {
769-
type: "FunctionDeclaration",
780+
type: functionDeclarationType,
770781
id: convertChild(node.name),
771782
generator: !!node.asteriskToken,
772783
expression: false,
773784
params: node.parameters.map(convertChild),
774785
body: convertChild(node.body)
775786
});
787+
776788
// Process returnType
777789
if (node.type) {
778790
result.returnType = convertTypeAnnotation(node.type);
779791
}
792+
780793
// Process typeParameters
781794
if (node.typeParameters && node.typeParameters.length) {
782795
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
783796
}
797+
784798
// check for exports
785799
result = fixExports(node, result, ast);
786800

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
module.exports = {
2+
"type": "Program",
3+
"range": [
4+
0,
5+
42
6+
],
7+
"loc": {
8+
"start": {
9+
"line": 1,
10+
"column": 0
11+
},
12+
"end": {
13+
"line": 1,
14+
"column": 42
15+
}
16+
},
17+
"body": [
18+
{
19+
"type": "DeclareFunction",
20+
"range": [
21+
0,
22+
42
23+
],
24+
"loc": {
25+
"start": {
26+
"line": 1,
27+
"column": 0
28+
},
29+
"end": {
30+
"line": 1,
31+
"column": 42
32+
}
33+
},
34+
"id": {
35+
"type": "Identifier",
36+
"range": [
37+
17,
38+
20
39+
],
40+
"loc": {
41+
"start": {
42+
"line": 1,
43+
"column": 17
44+
},
45+
"end": {
46+
"line": 1,
47+
"column": 20
48+
}
49+
},
50+
"name": "foo"
51+
},
52+
"generator": false,
53+
"expression": false,
54+
"params": [
55+
{
56+
"type": "Identifier",
57+
"range": [
58+
21,
59+
24
60+
],
61+
"loc": {
62+
"start": {
63+
"line": 1,
64+
"column": 21
65+
},
66+
"end": {
67+
"line": 1,
68+
"column": 24
69+
}
70+
},
71+
"name": "bar"
72+
}
73+
],
74+
"body": null,
75+
"returnType": {
76+
"type": "TypeAnnotation",
77+
"loc": {
78+
"start": {
79+
"line": 1,
80+
"column": 35
81+
},
82+
"end": {
83+
"line": 1,
84+
"column": 41
85+
}
86+
},
87+
"range": [
88+
35,
89+
41
90+
],
91+
"typeAnnotation": {
92+
"type": "TSStringKeyword",
93+
"range": [
94+
35,
95+
41
96+
],
97+
"loc": {
98+
"start": {
99+
"line": 1,
100+
"column": 35
101+
},
102+
"end": {
103+
"line": 1,
104+
"column": 41
105+
}
106+
},
107+
"flags": 0
108+
}
109+
}
110+
}
111+
],
112+
"sourceType": "script",
113+
"tokens": [
114+
{
115+
"type": "Identifier",
116+
"value": "declare",
117+
"range": [
118+
0,
119+
7
120+
],
121+
"loc": {
122+
"start": {
123+
"line": 1,
124+
"column": 0
125+
},
126+
"end": {
127+
"line": 1,
128+
"column": 7
129+
}
130+
}
131+
},
132+
{
133+
"type": "Keyword",
134+
"value": "function",
135+
"range": [
136+
8,
137+
16
138+
],
139+
"loc": {
140+
"start": {
141+
"line": 1,
142+
"column": 8
143+
},
144+
"end": {
145+
"line": 1,
146+
"column": 16
147+
}
148+
}
149+
},
150+
{
151+
"type": "Identifier",
152+
"value": "foo",
153+
"range": [
154+
17,
155+
20
156+
],
157+
"loc": {
158+
"start": {
159+
"line": 1,
160+
"column": 17
161+
},
162+
"end": {
163+
"line": 1,
164+
"column": 20
165+
}
166+
}
167+
},
168+
{
169+
"type": "Punctuator",
170+
"value": "(",
171+
"range": [
172+
20,
173+
21
174+
],
175+
"loc": {
176+
"start": {
177+
"line": 1,
178+
"column": 20
179+
},
180+
"end": {
181+
"line": 1,
182+
"column": 21
183+
}
184+
}
185+
},
186+
{
187+
"type": "Identifier",
188+
"value": "bar",
189+
"range": [
190+
21,
191+
24
192+
],
193+
"loc": {
194+
"start": {
195+
"line": 1,
196+
"column": 21
197+
},
198+
"end": {
199+
"line": 1,
200+
"column": 24
201+
}
202+
}
203+
},
204+
{
205+
"type": "Punctuator",
206+
"value": ":",
207+
"range": [
208+
24,
209+
25
210+
],
211+
"loc": {
212+
"start": {
213+
"line": 1,
214+
"column": 24
215+
},
216+
"end": {
217+
"line": 1,
218+
"column": 25
219+
}
220+
}
221+
},
222+
{
223+
"type": "Identifier",
224+
"value": "string",
225+
"range": [
226+
26,
227+
32
228+
],
229+
"loc": {
230+
"start": {
231+
"line": 1,
232+
"column": 26
233+
},
234+
"end": {
235+
"line": 1,
236+
"column": 32
237+
}
238+
}
239+
},
240+
{
241+
"type": "Punctuator",
242+
"value": ")",
243+
"range": [
244+
32,
245+
33
246+
],
247+
"loc": {
248+
"start": {
249+
"line": 1,
250+
"column": 32
251+
},
252+
"end": {
253+
"line": 1,
254+
"column": 33
255+
}
256+
}
257+
},
258+
{
259+
"type": "Punctuator",
260+
"value": ":",
261+
"range": [
262+
33,
263+
34
264+
],
265+
"loc": {
266+
"start": {
267+
"line": 1,
268+
"column": 33
269+
},
270+
"end": {
271+
"line": 1,
272+
"column": 34
273+
}
274+
}
275+
},
276+
{
277+
"type": "Identifier",
278+
"value": "string",
279+
"range": [
280+
35,
281+
41
282+
],
283+
"loc": {
284+
"start": {
285+
"line": 1,
286+
"column": 35
287+
},
288+
"end": {
289+
"line": 1,
290+
"column": 41
291+
}
292+
}
293+
},
294+
{
295+
"type": "Punctuator",
296+
"value": ";",
297+
"range": [
298+
41,
299+
42
300+
],
301+
"loc": {
302+
"start": {
303+
"line": 1,
304+
"column": 41
305+
},
306+
"end": {
307+
"line": 1,
308+
"column": 42
309+
}
310+
}
311+
}
312+
]
313+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare function foo(bar: string): string;

0 commit comments

Comments
 (0)