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

Commit 5421cea

Browse files
committed
Breaking: Convert Signature types to be more ESTree like (fixes #262)
1 parent f836bb9 commit 5421cea

13 files changed

+3492
-191
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
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,59 @@ module.exports = function convert(config) {
16881688

16891689
}
16901690

1691+
case SyntaxKind.MethodSignature: {
1692+
Object.assign(result, {
1693+
type: AST_NODE_TYPES.TSMethodSignature,
1694+
optional: (node.questionToken) ? (node.questionToken.kind === SyntaxKind.QuestionToken) : false,
1695+
computed: node.name.kind === SyntaxKind.ComputedPropertyName,
1696+
key: convertChild(node.name),
1697+
params: node.parameters.map(parameter => convertChild(parameter)),
1698+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
1699+
});
1700+
1701+
if (node.typeParameters) {
1702+
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1703+
}
1704+
1705+
break;
1706+
}
1707+
1708+
case SyntaxKind.PropertySignature: {
1709+
Object.assign(result, {
1710+
type: AST_NODE_TYPES.TSPropertySignature,
1711+
optional: (node.questionToken) ? (node.questionToken.kind === SyntaxKind.QuestionToken) : false,
1712+
computed: node.name.kind === SyntaxKind.ComputedPropertyName,
1713+
key: convertChild(node.name),
1714+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
1715+
});
1716+
1717+
break;
1718+
}
1719+
1720+
case SyntaxKind.IndexSignature: {
1721+
Object.assign(result, {
1722+
type: AST_NODE_TYPES.TSIndexSignature,
1723+
index: convertChild(node.parameters[0]),
1724+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
1725+
});
1726+
1727+
break;
1728+
}
1729+
1730+
case SyntaxKind.ConstructSignature: {
1731+
Object.assign(result, {
1732+
type: AST_NODE_TYPES.TSConstrcutSignature,
1733+
params: node.parameters.map(parameter => convertChild(parameter)),
1734+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
1735+
});
1736+
1737+
if (node.typeParameters) {
1738+
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1739+
}
1740+
1741+
break;
1742+
}
1743+
16911744
case SyntaxKind.InterfaceDeclaration: {
16921745
const interfaceHeritageClauses = node.heritageClauses || [];
16931746

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

+64-94
Original file line numberDiff line numberDiff line change
@@ -212,144 +212,114 @@ 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",
249+
"optional": true
260250
},
261-
"range": [
262-
26,
263-
39
264-
],
265-
"type": "TSPropertySignature",
266251
"typeAnnotation": {
252+
"type": "TypeAnnotation",
267253
"loc": {
268-
"end": {
269-
"column": 38,
270-
"line": 1
271-
},
272254
"start": {
273-
"column": 32,
274-
"line": 1
255+
"line": 1,
256+
"column": 32
257+
},
258+
"end": {
259+
"line": 1,
260+
"column": 38
275261
}
276262
},
277263
"range": [
278264
32,
279265
38
280266
],
281-
"type": "TypeAnnotation",
282267
"typeAnnotation": {
283-
"loc": {
284-
"end": {
285-
"column": 38,
286-
"line": 1
287-
},
288-
"start": {
289-
"column": 32,
290-
"line": 1
291-
}
292-
},
268+
"type": "TSStringKeyword",
293269
"range": [
294270
32,
295271
38
296272
],
297-
"type": "TSStringKeyword"
273+
"loc": {
274+
"start": {
275+
"line": 1,
276+
"column": 32
277+
},
278+
"end": {
279+
"line": 1,
280+
"column": 38
281+
}
282+
}
298283
}
299284
}
300285
},
301286
{
287+
"type": "TSPropertySignature",
288+
"range": [
289+
40,
290+
44
291+
],
302292
"loc": {
303-
"end": {
304-
"column": 44,
305-
"line": 1
306-
},
307293
"start": {
308-
"column": 40,
309-
"line": 1
294+
"line": 1,
295+
"column": 40
296+
},
297+
"end": {
298+
"line": 1,
299+
"column": 44
310300
}
311301
},
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,
302+
"optional": true,
303+
"computed": false,
304+
"key": {
305+
"type": "Identifier",
325306
"range": [
326307
40,
327308
43
328309
],
329-
"type": "Identifier"
330-
},
331-
"questionToken": {
332310
"loc": {
333-
"end": {
334-
"column": 44,
335-
"line": 1
336-
},
337311
"start": {
338-
"column": 43,
339-
"line": 1
312+
"line": 1,
313+
"column": 40
314+
},
315+
"end": {
316+
"line": 1,
317+
"column": 43
340318
}
341319
},
342-
"range": [
343-
43,
344-
44
345-
],
346-
"type": "TSQuestionToken"
320+
"name": "baz",
321+
"optional": true
347322
},
348-
"range": [
349-
40,
350-
44
351-
],
352-
"type": "TSPropertySignature",
353323
"typeAnnotation": null
354324
}
355325
],

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)