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

Commit c7cebe6

Browse files
committed
Breaking: Convert Signature types to be more ESTree like (fixes #262)
1 parent 379dcaf commit c7cebe6

13 files changed

+3494
-205
lines changed

lib/ast-node-types.js

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ module.exports = {
106106
TSConstructorType: "TSConstructorType",
107107
TSConstructSignature: "TSConstructSignature",
108108
TSDeclareKeyword: "TSDeclareKeyword",
109+
TSIndexSignature: "TSIndexSignature",
109110
TSInterfaceBody: "TSInterfaceBody",
110111
TSInterfaceDeclaration: "TSInterfaceDeclaration",
111112
TSInterfaceHeritage: "TSInterfaceHeritage",

lib/convert.js

+53-9
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,59 @@ module.exports = function convert(config) {
17231723

17241724
}
17251725

1726+
case SyntaxKind.MethodSignature: {
1727+
Object.assign(result, {
1728+
type: AST_NODE_TYPES.TSMethodSignature,
1729+
optional: (node.questionToken) ? (node.questionToken.kind === SyntaxKind.QuestionToken) : false,
1730+
computed: node.name.kind === SyntaxKind.ComputedPropertyName,
1731+
key: convertChild(node.name),
1732+
params: node.parameters.map(parameter => convertChild(parameter)),
1733+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
1734+
});
1735+
1736+
if (node.typeParameters) {
1737+
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1738+
}
1739+
1740+
break;
1741+
}
1742+
1743+
case SyntaxKind.PropertySignature: {
1744+
Object.assign(result, {
1745+
type: AST_NODE_TYPES.TSPropertySignature,
1746+
optional: (node.questionToken) ? (node.questionToken.kind === SyntaxKind.QuestionToken) : false,
1747+
computed: node.name.kind === SyntaxKind.ComputedPropertyName,
1748+
key: convertChild(node.name),
1749+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
1750+
});
1751+
1752+
break;
1753+
}
1754+
1755+
case SyntaxKind.IndexSignature: {
1756+
Object.assign(result, {
1757+
type: AST_NODE_TYPES.TSIndexSignature,
1758+
index: convertChild(node.parameters[0]),
1759+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
1760+
});
1761+
1762+
break;
1763+
}
1764+
1765+
case SyntaxKind.ConstructSignature: {
1766+
Object.assign(result, {
1767+
type: AST_NODE_TYPES.TSConstructSignature,
1768+
params: node.parameters.map(parameter => convertChild(parameter)),
1769+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
1770+
});
1771+
1772+
if (node.typeParameters) {
1773+
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1774+
}
1775+
1776+
break;
1777+
}
1778+
17261779
case SyntaxKind.InterfaceDeclaration: {
17271780
const interfaceHeritageClauses = node.heritageClauses || [];
17281781

@@ -1771,15 +1824,6 @@ module.exports = function convert(config) {
17711824
});
17721825
break;
17731826

1774-
case SyntaxKind.PropertySignature:
1775-
case SyntaxKind.MethodSignature:
1776-
deeplyCopy();
1777-
1778-
if (node.questionToken) {
1779-
result.name.optional = true;
1780-
}
1781-
break;
1782-
17831827
default:
17841828
deeplyCopy();
17851829
}

tests/fixtures/typescript/basics/export-type-class-declaration.result.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -107,63 +107,65 @@ module.exports = {
107107
48
108108
],
109109
"loc": {
110-
"end": {
111-
"line": 2,
112-
"column": 17
113-
},
114110
"start": {
115111
"line": 2,
116112
"column": 4
113+
},
114+
"end": {
115+
"line": 2,
116+
"column": 17
117117
}
118118
},
119-
"name": {
119+
"optional": false,
120+
"computed": false,
121+
"key": {
120122
"type": "Identifier",
121123
"range": [
122124
35,
123125
40
124126
],
125127
"loc": {
126-
"end": {
127-
"line": 2,
128-
"column": 9
129-
},
130128
"start": {
131129
"line": 2,
132130
"column": 4
131+
},
132+
"end": {
133+
"line": 2,
134+
"column": 9
133135
}
134136
},
135137
"name": "count"
136138
},
137139
"typeAnnotation": {
138140
"type": "TypeAnnotation",
139-
"range": [
140-
42,
141-
48
142-
],
143141
"loc": {
144-
"end": {
145-
"line": 2,
146-
"column": 17
147-
},
148142
"start": {
149143
"line": 2,
150144
"column": 11
145+
},
146+
"end": {
147+
"line": 2,
148+
"column": 17
151149
}
152150
},
151+
"range": [
152+
42,
153+
48
154+
],
153155
"typeAnnotation": {
154156
"type": "TSNumberKeyword",
155157
"range": [
156158
42,
157159
48
158160
],
159161
"loc": {
160-
"end": {
161-
"line": 2,
162-
"column": 17
163-
},
164162
"start": {
165163
"line": 2,
166164
"column": 11
165+
},
166+
"end": {
167+
"line": 2,
168+
"column": 17
167169
}
168170
}
169171
}

tests/fixtures/typescript/basics/function-with-object-type-with-optional-properties.result.js

+62-94
Original file line numberDiff line numberDiff line change
@@ -212,144 +212,112 @@ module.exports = {
212212
},
213213
"members": [
214214
{
215+
"type": "TSPropertySignature",
216+
"range": [
217+
26,
218+
39
219+
],
215220
"loc": {
216-
"end": {
217-
"column": 39,
218-
"line": 1
219-
},
220221
"start": {
221-
"column": 26,
222-
"line": 1
222+
"line": 1,
223+
"column": 26
224+
},
225+
"end": {
226+
"line": 1,
227+
"column": 39
223228
}
224229
},
225-
"name": {
226-
"loc": {
227-
"end": {
228-
"column": 29,
229-
"line": 1
230-
},
231-
"start": {
232-
"column": 26,
233-
"line": 1
234-
}
235-
},
236-
"name": "bar",
237-
"optional": true,
230+
"optional": true,
231+
"computed": false,
232+
"key": {
233+
"type": "Identifier",
238234
"range": [
239235
26,
240236
29
241237
],
242-
"type": "Identifier"
243-
},
244-
"questionToken": {
245238
"loc": {
246-
"end": {
247-
"column": 30,
248-
"line": 1
249-
},
250239
"start": {
251-
"column": 29,
252-
"line": 1
240+
"line": 1,
241+
"column": 26
242+
},
243+
"end": {
244+
"line": 1,
245+
"column": 29
253246
}
254247
},
255-
"range": [
256-
29,
257-
30
258-
],
259-
"type": "TSQuestionToken"
248+
"name": "bar"
260249
},
261-
"range": [
262-
26,
263-
39
264-
],
265-
"type": "TSPropertySignature",
266250
"typeAnnotation": {
251+
"type": "TypeAnnotation",
267252
"loc": {
268-
"end": {
269-
"column": 38,
270-
"line": 1
271-
},
272253
"start": {
273-
"column": 32,
274-
"line": 1
254+
"line": 1,
255+
"column": 32
256+
},
257+
"end": {
258+
"line": 1,
259+
"column": 38
275260
}
276261
},
277262
"range": [
278263
32,
279264
38
280265
],
281-
"type": "TypeAnnotation",
282266
"typeAnnotation": {
283-
"loc": {
284-
"end": {
285-
"column": 38,
286-
"line": 1
287-
},
288-
"start": {
289-
"column": 32,
290-
"line": 1
291-
}
292-
},
267+
"type": "TSStringKeyword",
293268
"range": [
294269
32,
295270
38
296271
],
297-
"type": "TSStringKeyword"
272+
"loc": {
273+
"start": {
274+
"line": 1,
275+
"column": 32
276+
},
277+
"end": {
278+
"line": 1,
279+
"column": 38
280+
}
281+
}
298282
}
299283
}
300284
},
301285
{
286+
"type": "TSPropertySignature",
287+
"range": [
288+
40,
289+
44
290+
],
302291
"loc": {
303-
"end": {
304-
"column": 44,
305-
"line": 1
306-
},
307292
"start": {
308-
"column": 40,
309-
"line": 1
293+
"line": 1,
294+
"column": 40
295+
},
296+
"end": {
297+
"line": 1,
298+
"column": 44
310299
}
311300
},
312-
"name": {
313-
"loc": {
314-
"end": {
315-
"column": 43,
316-
"line": 1
317-
},
318-
"start": {
319-
"column": 40,
320-
"line": 1
321-
}
322-
},
323-
"name": "baz",
324-
"optional": true,
301+
"optional": true,
302+
"computed": false,
303+
"key": {
304+
"type": "Identifier",
325305
"range": [
326306
40,
327307
43
328308
],
329-
"type": "Identifier"
330-
},
331-
"questionToken": {
332309
"loc": {
333-
"end": {
334-
"column": 44,
335-
"line": 1
336-
},
337310
"start": {
338-
"column": 43,
339-
"line": 1
311+
"line": 1,
312+
"column": 40
313+
},
314+
"end": {
315+
"line": 1,
316+
"column": 43
340317
}
341318
},
342-
"range": [
343-
43,
344-
44
345-
],
346-
"type": "TSQuestionToken"
319+
"name": "baz"
347320
},
348-
"range": [
349-
40,
350-
44
351-
],
352-
"type": "TSPropertySignature",
353321
"typeAnnotation": null
354322
}
355323
],

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ module.exports = {
234234
"column": 38
235235
}
236236
},
237-
"name": {
237+
"optional": false,
238+
"computed": false,
239+
"key": {
238240
"type": "Identifier",
239241
"range": [
240242
26,
@@ -303,7 +305,9 @@ module.exports = {
303305
"column": 42
304306
}
305307
},
306-
"name": {
308+
"optional": false,
309+
"computed": false,
310+
"key": {
307311
"type": "Identifier",
308312
"range": [
309313
39,

0 commit comments

Comments
 (0)