Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 11d5a7d

Browse files
soda0289nzakas
authored andcommittedFeb 5, 2017
Fix: Handle object types without annotations (fixes #148) (#154)
Typescript allows object/interface type definitions without type annotations. This commit will replace missing type annotations with null instead of trying to convert them.
1 parent fc1e6bb commit 11d5a7d

7 files changed

+1306
-1
lines changed
 

‎lib/ast-converter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ module.exports = function(ast, extra) {
659659
return !(/^(?:kind|parent|pos|end|flags)$/.test(key));
660660
}).forEach(function(key) {
661661
if (key === "type") {
662-
result.typeAnnotation = convertTypeAnnotation(node.type);
662+
result.typeAnnotation = (node.type) ? convertTypeAnnotation(node.type) : null;
663663
} else {
664664
if (Array.isArray(node[key])) {
665665
result[key] = node[key].map(convertChild);

‎tests/fixtures/typescript/basics/function-with-object-type-without-annotation.result.js

Lines changed: 696 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function foo({bar, baz}: {bar: string, baz}) {
2+
3+
}
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
module.exports = {
2+
"type": "Program",
3+
"range": [
4+
0,
5+
27
6+
],
7+
"loc": {
8+
"start": {
9+
"line": 1,
10+
"column": 0
11+
},
12+
"end": {
13+
"line": 3,
14+
"column": 1
15+
}
16+
},
17+
"body": [
18+
{
19+
"type": "TSInterfaceDeclaration",
20+
"range": [
21+
0,
22+
27
23+
],
24+
"loc": {
25+
"start": {
26+
"line": 1,
27+
"column": 0
28+
},
29+
"end": {
30+
"line": 3,
31+
"column": 1
32+
}
33+
},
34+
"name": {
35+
"type": "Identifier",
36+
"range": [
37+
10,
38+
14
39+
],
40+
"loc": {
41+
"start": {
42+
"line": 1,
43+
"column": 10
44+
},
45+
"end": {
46+
"line": 1,
47+
"column": 14
48+
}
49+
},
50+
"name": "test"
51+
},
52+
"members": [
53+
{
54+
"type": "TSPropertySignature",
55+
"range": [
56+
21,
57+
25
58+
],
59+
"loc": {
60+
"start": {
61+
"line": 2,
62+
"column": 4
63+
},
64+
"end": {
65+
"line": 2,
66+
"column": 8
67+
}
68+
},
69+
"name": {
70+
"type": "Identifier",
71+
"range": [
72+
21,
73+
24
74+
],
75+
"loc": {
76+
"start": {
77+
"line": 2,
78+
"column": 4
79+
},
80+
"end": {
81+
"line": 2,
82+
"column": 7
83+
}
84+
},
85+
"name": "foo"
86+
},
87+
"typeAnnotation": null
88+
}
89+
]
90+
}
91+
],
92+
"sourceType": "script",
93+
"tokens": [
94+
{
95+
"type": "Keyword",
96+
"value": "interface",
97+
"range": [
98+
0,
99+
9
100+
],
101+
"loc": {
102+
"start": {
103+
"line": 1,
104+
"column": 0
105+
},
106+
"end": {
107+
"line": 1,
108+
"column": 9
109+
}
110+
}
111+
},
112+
{
113+
"type": "Identifier",
114+
"value": "test",
115+
"range": [
116+
10,
117+
14
118+
],
119+
"loc": {
120+
"start": {
121+
"line": 1,
122+
"column": 10
123+
},
124+
"end": {
125+
"line": 1,
126+
"column": 14
127+
}
128+
}
129+
},
130+
{
131+
"type": "Punctuator",
132+
"value": "{",
133+
"range": [
134+
15,
135+
16
136+
],
137+
"loc": {
138+
"start": {
139+
"line": 1,
140+
"column": 15
141+
},
142+
"end": {
143+
"line": 1,
144+
"column": 16
145+
}
146+
}
147+
},
148+
{
149+
"type": "Identifier",
150+
"value": "foo",
151+
"range": [
152+
21,
153+
24
154+
],
155+
"loc": {
156+
"start": {
157+
"line": 2,
158+
"column": 4
159+
},
160+
"end": {
161+
"line": 2,
162+
"column": 7
163+
}
164+
}
165+
},
166+
{
167+
"type": "Punctuator",
168+
"value": ";",
169+
"range": [
170+
24,
171+
25
172+
],
173+
"loc": {
174+
"start": {
175+
"line": 2,
176+
"column": 7
177+
},
178+
"end": {
179+
"line": 2,
180+
"column": 8
181+
}
182+
}
183+
},
184+
{
185+
"type": "Punctuator",
186+
"value": "}",
187+
"range": [
188+
26,
189+
27
190+
],
191+
"loc": {
192+
"start": {
193+
"line": 3,
194+
"column": 0
195+
},
196+
"end": {
197+
"line": 3,
198+
"column": 1
199+
}
200+
}
201+
}
202+
]
203+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface test {
2+
foo;
3+
}
Lines changed: 399 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,399 @@
1+
module.exports = {
2+
"type": "Program",
3+
"range": [
4+
0,
5+
30
6+
],
7+
"loc": {
8+
"start": {
9+
"line": 1,
10+
"column": 0
11+
},
12+
"end": {
13+
"line": 1,
14+
"column": 30
15+
}
16+
},
17+
"body": [
18+
{
19+
"type": "VariableDeclaration",
20+
"range": [
21+
0,
22+
30
23+
],
24+
"loc": {
25+
"start": {
26+
"line": 1,
27+
"column": 0
28+
},
29+
"end": {
30+
"line": 1,
31+
"column": 30
32+
}
33+
},
34+
"kind": "type",
35+
"declarations": [
36+
{
37+
"type": "VariableDeclarator",
38+
"id": {
39+
"type": "Identifier",
40+
"range": [
41+
5,
42+
8
43+
],
44+
"loc": {
45+
"start": {
46+
"line": 1,
47+
"column": 5
48+
},
49+
"end": {
50+
"line": 1,
51+
"column": 8
52+
}
53+
},
54+
"name": "foo"
55+
},
56+
"init": {
57+
"type": "TSTypeLiteral",
58+
"range": [
59+
11,
60+
29
61+
],
62+
"loc": {
63+
"start": {
64+
"line": 1,
65+
"column": 11
66+
},
67+
"end": {
68+
"line": 1,
69+
"column": 29
70+
}
71+
},
72+
"members": [
73+
{
74+
"type": "TSPropertySignature",
75+
"range": [
76+
12,
77+
24
78+
],
79+
"loc": {
80+
"start": {
81+
"line": 1,
82+
"column": 12
83+
},
84+
"end": {
85+
"line": 1,
86+
"column": 24
87+
}
88+
},
89+
"name": {
90+
"type": "Identifier",
91+
"range": [
92+
12,
93+
15
94+
],
95+
"loc": {
96+
"start": {
97+
"line": 1,
98+
"column": 12
99+
},
100+
"end": {
101+
"line": 1,
102+
"column": 15
103+
}
104+
},
105+
"name": "bar"
106+
},
107+
"typeAnnotation": {
108+
"type": "TypeAnnotation",
109+
"loc": {
110+
"start": {
111+
"line": 1,
112+
"column": 17
113+
},
114+
"end": {
115+
"line": 1,
116+
"column": 23
117+
}
118+
},
119+
"range": [
120+
17,
121+
23
122+
],
123+
"typeAnnotation": {
124+
"type": "TSStringKeyword",
125+
"range": [
126+
17,
127+
23
128+
],
129+
"loc": {
130+
"start": {
131+
"line": 1,
132+
"column": 17
133+
},
134+
"end": {
135+
"line": 1,
136+
"column": 23
137+
}
138+
}
139+
}
140+
}
141+
},
142+
{
143+
"type": "TSPropertySignature",
144+
"range": [
145+
25,
146+
28
147+
],
148+
"loc": {
149+
"start": {
150+
"line": 1,
151+
"column": 25
152+
},
153+
"end": {
154+
"line": 1,
155+
"column": 28
156+
}
157+
},
158+
"name": {
159+
"type": "Identifier",
160+
"range": [
161+
25,
162+
28
163+
],
164+
"loc": {
165+
"start": {
166+
"line": 1,
167+
"column": 25
168+
},
169+
"end": {
170+
"line": 1,
171+
"column": 28
172+
}
173+
},
174+
"name": "baz"
175+
},
176+
"typeAnnotation": null
177+
}
178+
]
179+
},
180+
"range": [
181+
5,
182+
30
183+
],
184+
"loc": {
185+
"start": {
186+
"line": 1,
187+
"column": 5
188+
},
189+
"end": {
190+
"line": 1,
191+
"column": 30
192+
}
193+
}
194+
}
195+
]
196+
}
197+
],
198+
"sourceType": "script",
199+
"tokens": [
200+
{
201+
"type": "Identifier",
202+
"value": "type",
203+
"range": [
204+
0,
205+
4
206+
],
207+
"loc": {
208+
"start": {
209+
"line": 1,
210+
"column": 0
211+
},
212+
"end": {
213+
"line": 1,
214+
"column": 4
215+
}
216+
}
217+
},
218+
{
219+
"type": "Identifier",
220+
"value": "foo",
221+
"range": [
222+
5,
223+
8
224+
],
225+
"loc": {
226+
"start": {
227+
"line": 1,
228+
"column": 5
229+
},
230+
"end": {
231+
"line": 1,
232+
"column": 8
233+
}
234+
}
235+
},
236+
{
237+
"type": "Punctuator",
238+
"value": "=",
239+
"range": [
240+
9,
241+
10
242+
],
243+
"loc": {
244+
"start": {
245+
"line": 1,
246+
"column": 9
247+
},
248+
"end": {
249+
"line": 1,
250+
"column": 10
251+
}
252+
}
253+
},
254+
{
255+
"type": "Punctuator",
256+
"value": "{",
257+
"range": [
258+
11,
259+
12
260+
],
261+
"loc": {
262+
"start": {
263+
"line": 1,
264+
"column": 11
265+
},
266+
"end": {
267+
"line": 1,
268+
"column": 12
269+
}
270+
}
271+
},
272+
{
273+
"type": "Identifier",
274+
"value": "bar",
275+
"range": [
276+
12,
277+
15
278+
],
279+
"loc": {
280+
"start": {
281+
"line": 1,
282+
"column": 12
283+
},
284+
"end": {
285+
"line": 1,
286+
"column": 15
287+
}
288+
}
289+
},
290+
{
291+
"type": "Punctuator",
292+
"value": ":",
293+
"range": [
294+
15,
295+
16
296+
],
297+
"loc": {
298+
"start": {
299+
"line": 1,
300+
"column": 15
301+
},
302+
"end": {
303+
"line": 1,
304+
"column": 16
305+
}
306+
}
307+
},
308+
{
309+
"type": "Identifier",
310+
"value": "string",
311+
"range": [
312+
17,
313+
23
314+
],
315+
"loc": {
316+
"start": {
317+
"line": 1,
318+
"column": 17
319+
},
320+
"end": {
321+
"line": 1,
322+
"column": 23
323+
}
324+
}
325+
},
326+
{
327+
"type": "Punctuator",
328+
"value": ",",
329+
"range": [
330+
23,
331+
24
332+
],
333+
"loc": {
334+
"start": {
335+
"line": 1,
336+
"column": 23
337+
},
338+
"end": {
339+
"line": 1,
340+
"column": 24
341+
}
342+
}
343+
},
344+
{
345+
"type": "Identifier",
346+
"value": "baz",
347+
"range": [
348+
25,
349+
28
350+
],
351+
"loc": {
352+
"start": {
353+
"line": 1,
354+
"column": 25
355+
},
356+
"end": {
357+
"line": 1,
358+
"column": 28
359+
}
360+
}
361+
},
362+
{
363+
"type": "Punctuator",
364+
"value": "}",
365+
"range": [
366+
28,
367+
29
368+
],
369+
"loc": {
370+
"start": {
371+
"line": 1,
372+
"column": 28
373+
},
374+
"end": {
375+
"line": 1,
376+
"column": 29
377+
}
378+
}
379+
},
380+
{
381+
"type": "Punctuator",
382+
"value": ";",
383+
"range": [
384+
29,
385+
30
386+
],
387+
"loc": {
388+
"start": {
389+
"line": 1,
390+
"column": 29
391+
},
392+
"end": {
393+
"line": 1,
394+
"column": 30
395+
}
396+
}
397+
}
398+
]
399+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type foo = {bar: string, baz};

0 commit comments

Comments
 (0)
This repository has been archived.