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

Commit bfb1506

Browse files
PajnJamesHenry
authored andcommitted
New: Add type parameters to more AST nodes (fixes #184) (#183)
1 parent 0fadfc3 commit bfb1506

9 files changed

+1053
-1
lines changed

lib/ast-converter.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ module.exports = function(ast, extra) {
546546
typeArgument.end
547547
],
548548
loc: getLocFor(typeArgumentStart, typeArgument.end, ast),
549-
id: convertChild(typeArgument.typeName)
549+
id: convertChild(typeArgument.typeName || typeArgument)
550550
};
551551
})
552552
};
@@ -1518,6 +1518,7 @@ module.exports = function(ast, extra) {
15181518
if (!lastClassToken || lastTypeParameter.pos > lastClassToken.pos) {
15191519
lastClassToken = ts.findNextToken(lastTypeParameter, ast);
15201520
}
1521+
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
15211522
}
15221523

15231524
if (node.modifiers && node.modifiers.length) {
@@ -1816,6 +1817,9 @@ module.exports = function(ast, extra) {
18161817
callee: convertChild(node.expression),
18171818
arguments: node.arguments.map(convertChild)
18181819
});
1820+
if (node.typeArguments && node.typeArguments.length) {
1821+
result.typeParameters = convertTypeArgumentsToTypeParameters(node.typeArguments);
1822+
}
18191823
break;
18201824

18211825
case SyntaxKind.NewExpression:
@@ -1824,6 +1828,9 @@ module.exports = function(ast, extra) {
18241828
callee: convertChild(node.expression),
18251829
arguments: (node.arguments) ? node.arguments.map(convertChild) : []
18261830
});
1831+
if (node.typeArguments && node.typeArguments.length) {
1832+
result.typeParameters = convertTypeArgumentsToTypeParameters(node.typeArguments);
1833+
}
18271834
break;
18281835

18291836
case SyntaxKind.MetaProperty:

tests/fixtures/typescript/basics/export-default-class-with-generic.result.js

+38
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,44 @@ module.exports = {
3434
}
3535
},
3636
"id": null,
37+
"typeParameters": {
38+
"loc": {
39+
"end": {
40+
"column": 23,
41+
"line": 1
42+
},
43+
"start": {
44+
"column": 20,
45+
"line": 1
46+
}
47+
},
48+
"params": [
49+
{
50+
"loc": {
51+
"end": {
52+
"column": 22,
53+
"line": 1
54+
},
55+
"start": {
56+
"column": 21,
57+
"line": 1
58+
}
59+
},
60+
"name": "T",
61+
"range": [
62+
21,
63+
22
64+
],
65+
"type": "TypeParameter",
66+
"constraint": null
67+
}
68+
],
69+
"range": [
70+
20,
71+
23
72+
],
73+
"type": "TypeParameterDeclaration"
74+
},
3775
"body": {
3876
"type": "ClassBody",
3977
"body": [],

tests/fixtures/typescript/basics/export-default-class-with-multiple-generics.result.js

+57
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,63 @@ module.exports = {
3434
}
3535
},
3636
"id": null,
37+
"typeParameters": {
38+
"loc": {
39+
"end": {
40+
"column": 26,
41+
"line": 1
42+
},
43+
"start": {
44+
"column": 20,
45+
"line": 1
46+
}
47+
},
48+
"params": [
49+
{
50+
"loc": {
51+
"end": {
52+
"column": 22,
53+
"line": 1
54+
},
55+
"start": {
56+
"column": 21,
57+
"line": 1
58+
}
59+
},
60+
"name": "T",
61+
"range": [
62+
21,
63+
22
64+
],
65+
"type": "TypeParameter",
66+
"constraint": null
67+
},
68+
{
69+
"loc": {
70+
"end": {
71+
"column": 25,
72+
"line": 1
73+
},
74+
"start": {
75+
"column": 23,
76+
"line": 1
77+
}
78+
},
79+
"name": "U",
80+
"range": [
81+
23,
82+
25
83+
],
84+
"type": "TypeParameter",
85+
"constraint": null
86+
}
87+
],
88+
"range": [
89+
20,
90+
26
91+
],
92+
"type": "TypeParameterDeclaration"
93+
},
3794
"body": {
3895
"type": "ClassBody",
3996
"body": [],

tests/fixtures/typescript/basics/export-named-class-with-generic.result.js

+38
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,44 @@ module.exports = {
5151
},
5252
"name": "Foo"
5353
},
54+
"typeParameters": {
55+
"loc": {
56+
"end": {
57+
"column": 19,
58+
"line": 1
59+
},
60+
"start": {
61+
"column": 16,
62+
"line": 1
63+
}
64+
},
65+
"params": [
66+
{
67+
"loc": {
68+
"end": {
69+
"column": 18,
70+
"line": 1
71+
},
72+
"start": {
73+
"column": 17,
74+
"line": 1
75+
}
76+
},
77+
"name": "T",
78+
"range": [
79+
17,
80+
18
81+
],
82+
"type": "TypeParameter",
83+
"constraint": null
84+
}
85+
],
86+
"range": [
87+
16,
88+
19
89+
],
90+
"type": "TypeParameterDeclaration"
91+
},
5492
"body": {
5593
"type": "ClassBody",
5694
"body": [],

tests/fixtures/typescript/basics/export-named-class-with-multiple-generics.result.js

+57
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,63 @@ module.exports = {
5151
},
5252
"name": "Foo"
5353
},
54+
"typeParameters": {
55+
"loc": {
56+
"end": {
57+
"column": 22,
58+
"line": 1
59+
},
60+
"start": {
61+
"column": 16,
62+
"line": 1
63+
}
64+
},
65+
"params": [
66+
{
67+
"loc": {
68+
"end": {
69+
"column": 18,
70+
"line": 1
71+
},
72+
"start": {
73+
"column": 17,
74+
"line": 1
75+
}
76+
},
77+
"name": "T",
78+
"range": [
79+
17,
80+
18
81+
],
82+
"type": "TypeParameter",
83+
"constraint": null
84+
},
85+
{
86+
"loc": {
87+
"end": {
88+
"column": 21,
89+
"line": 1
90+
},
91+
"start": {
92+
"column": 19,
93+
"line": 1
94+
}
95+
},
96+
"name": "U",
97+
"range": [
98+
19,
99+
21
100+
],
101+
"type": "TypeParameter",
102+
"constraint": null
103+
}
104+
],
105+
"range": [
106+
16,
107+
22
108+
],
109+
"type": "TypeParameterDeclaration"
110+
},
54111
"body": {
55112
"type": "ClassBody",
56113
"body": [],

0 commit comments

Comments
 (0)