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

Commit b1efe69

Browse files
soda0289JamesHenry
authored andcommitted
Breaking: Change how interface node gets converted (fixes #201) (#241)
1 parent e311620 commit b1efe69

15 files changed

+2422
-1079
lines changed

lib/ast-converter.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,25 @@ module.exports = function(ast, extra) {
676676
return classImplementsNode;
677677
}
678678

679+
/**
680+
* Converts a child into a interface heritage node.
681+
* @param {TSNode} child The TypeScript AST node to convert.
682+
* @returns {ESTreeNode} The type annotation node.
683+
*/
684+
function convertInterfaceHeritageClause(child) {
685+
var id = convertChild(child.expression);
686+
var classImplementsNode = {
687+
type: "TSInterfaceHeritage",
688+
loc: id.loc,
689+
range: id.range,
690+
id: id
691+
};
692+
if (child.typeArguments && child.typeArguments.length) {
693+
classImplementsNode.typeParameters = convertTypeArgumentsToTypeParameters(child.typeArguments);
694+
}
695+
return classImplementsNode;
696+
}
697+
679698
/**
680699
* For nodes that are copied directly from the TypeScript AST into
681700
* ESTree mostly as-is. The only difference is the addition of a type
@@ -2083,6 +2102,38 @@ module.exports = function(ast, extra) {
20832102

20842103
break;
20852104

2105+
case SyntaxKind.InterfaceDeclaration:
2106+
var interfaceHeritageClauses = node.heritageClauses || [];
2107+
var interfaceLastClassToken = interfaceHeritageClauses.length ? interfaceHeritageClauses[interfaceHeritageClauses.length - 1] : node.name;
2108+
2109+
if (node.typeParameters && node.typeParameters.length) {
2110+
var interfaceLastTypeParameter = node.typeParameters[node.typeParameters.length - 1];
2111+
if (!interfaceLastClassToken || interfaceLastTypeParameter.pos > interfaceLastClassToken.pos) {
2112+
interfaceLastClassToken = ts.findNextToken(interfaceLastTypeParameter, ast);
2113+
}
2114+
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
2115+
}
2116+
2117+
var hasImplementsClause = interfaceHeritageClauses.length > 0;
2118+
var interfaceOpenBrace = ts.findNextToken(interfaceLastClassToken, ast);
2119+
2120+
var interfaceBody = {
2121+
type: "TSInterfaceBody",
2122+
body: node.members.map(function(member) {
2123+
return convertChild(member);
2124+
}),
2125+
range: [ interfaceOpenBrace.getStart(), result.range[1] ],
2126+
loc: getLocFor(interfaceOpenBrace.getStart(), node.end, ast)
2127+
};
2128+
2129+
assign(result, {
2130+
type: "TSInterfaceDeclaration",
2131+
body: interfaceBody,
2132+
id: convertChild(node.name),
2133+
heritage: hasImplementsClause ? interfaceHeritageClauses[0].types.map(convertInterfaceHeritageClause) : []
2134+
});
2135+
break;
2136+
20862137
default:
20872138
deeplyCopy();
20882139
}

tests/fixtures/typescript/basics/class-with-mixin.result.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,25 @@ module.exports = {
598598
"column": 15
599599
}
600600
},
601-
"name": {
601+
"body": {
602+
"type": "TSInterfaceBody",
603+
"body": [],
604+
"range": [
605+
154,
606+
157
607+
],
608+
"loc": {
609+
"start": {
610+
"line": 8,
611+
"column": 12
612+
},
613+
"end": {
614+
"line": 8,
615+
"column": 15
616+
}
617+
}
618+
},
619+
"id": {
602620
"type": "Identifier",
603621
"range": [
604622
152,
@@ -616,7 +634,7 @@ module.exports = {
616634
},
617635
"name": "I"
618636
},
619-
"members": []
637+
"heritage": []
620638
},
621639
{
622640
"type": "VariableDeclaration",
@@ -2024,4 +2042,4 @@ module.exports = {
20242042
}
20252043
}
20262044
]
2027-
};
2045+
};
Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
module.exports = {
2+
"type": "Program",
3+
"range": [
4+
0,
5+
34
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+
34
23+
],
24+
"loc": {
25+
"start": {
26+
"line": 1,
27+
"column": 0
28+
},
29+
"end": {
30+
"line": 3,
31+
"column": 1
32+
}
33+
},
34+
"body": {
35+
"type": "TSInterfaceBody",
36+
"body": [],
37+
"range": [
38+
30,
39+
34
40+
],
41+
"loc": {
42+
"start": {
43+
"line": 1,
44+
"column": 30
45+
},
46+
"end": {
47+
"line": 3,
48+
"column": 1
49+
}
50+
}
51+
},
52+
"id": {
53+
"type": "Identifier",
54+
"range": [
55+
10,
56+
13
57+
],
58+
"loc": {
59+
"start": {
60+
"line": 1,
61+
"column": 10
62+
},
63+
"end": {
64+
"line": 1,
65+
"column": 13
66+
}
67+
},
68+
"name": "Foo"
69+
},
70+
"heritage": [
71+
{
72+
"type": "TSInterfaceHeritage",
73+
"loc": {
74+
"start": {
75+
"line": 1,
76+
"column": 22
77+
},
78+
"end": {
79+
"line": 1,
80+
"column": 25
81+
}
82+
},
83+
"range": [
84+
22,
85+
25
86+
],
87+
"id": {
88+
"type": "Identifier",
89+
"range": [
90+
22,
91+
25
92+
],
93+
"loc": {
94+
"start": {
95+
"line": 1,
96+
"column": 22
97+
},
98+
"end": {
99+
"line": 1,
100+
"column": 25
101+
}
102+
},
103+
"name": "Bar"
104+
}
105+
},
106+
{
107+
"type": "TSInterfaceHeritage",
108+
"loc": {
109+
"start": {
110+
"line": 1,
111+
"column": 26
112+
},
113+
"end": {
114+
"line": 1,
115+
"column": 29
116+
}
117+
},
118+
"range": [
119+
26,
120+
29
121+
],
122+
"id": {
123+
"type": "Identifier",
124+
"range": [
125+
26,
126+
29
127+
],
128+
"loc": {
129+
"start": {
130+
"line": 1,
131+
"column": 26
132+
},
133+
"end": {
134+
"line": 1,
135+
"column": 29
136+
}
137+
},
138+
"name": "Baz"
139+
}
140+
}
141+
]
142+
}
143+
],
144+
"sourceType": "script",
145+
"tokens": [
146+
{
147+
"type": "Keyword",
148+
"value": "interface",
149+
"range": [
150+
0,
151+
9
152+
],
153+
"loc": {
154+
"start": {
155+
"line": 1,
156+
"column": 0
157+
},
158+
"end": {
159+
"line": 1,
160+
"column": 9
161+
}
162+
}
163+
},
164+
{
165+
"type": "Identifier",
166+
"value": "Foo",
167+
"range": [
168+
10,
169+
13
170+
],
171+
"loc": {
172+
"start": {
173+
"line": 1,
174+
"column": 10
175+
},
176+
"end": {
177+
"line": 1,
178+
"column": 13
179+
}
180+
}
181+
},
182+
{
183+
"type": "Keyword",
184+
"value": "extends",
185+
"range": [
186+
14,
187+
21
188+
],
189+
"loc": {
190+
"start": {
191+
"line": 1,
192+
"column": 14
193+
},
194+
"end": {
195+
"line": 1,
196+
"column": 21
197+
}
198+
}
199+
},
200+
{
201+
"type": "Identifier",
202+
"value": "Bar",
203+
"range": [
204+
22,
205+
25
206+
],
207+
"loc": {
208+
"start": {
209+
"line": 1,
210+
"column": 22
211+
},
212+
"end": {
213+
"line": 1,
214+
"column": 25
215+
}
216+
}
217+
},
218+
{
219+
"type": "Punctuator",
220+
"value": ",",
221+
"range": [
222+
25,
223+
26
224+
],
225+
"loc": {
226+
"start": {
227+
"line": 1,
228+
"column": 25
229+
},
230+
"end": {
231+
"line": 1,
232+
"column": 26
233+
}
234+
}
235+
},
236+
{
237+
"type": "Identifier",
238+
"value": "Baz",
239+
"range": [
240+
26,
241+
29
242+
],
243+
"loc": {
244+
"start": {
245+
"line": 1,
246+
"column": 26
247+
},
248+
"end": {
249+
"line": 1,
250+
"column": 29
251+
}
252+
}
253+
},
254+
{
255+
"type": "Punctuator",
256+
"value": "{",
257+
"range": [
258+
30,
259+
31
260+
],
261+
"loc": {
262+
"start": {
263+
"line": 1,
264+
"column": 30
265+
},
266+
"end": {
267+
"line": 1,
268+
"column": 31
269+
}
270+
}
271+
},
272+
{
273+
"type": "Punctuator",
274+
"value": "}",
275+
"range": [
276+
33,
277+
34
278+
],
279+
"loc": {
280+
"start": {
281+
"line": 3,
282+
"column": 0
283+
},
284+
"end": {
285+
"line": 3,
286+
"column": 1
287+
}
288+
}
289+
}
290+
]
291+
};

0 commit comments

Comments
 (0)