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

Commit 992f1fa

Browse files
soda0289JamesHenry
authored andcommitted
Fix: Unescape type parameter names (fixes #296) (#298)
1 parent 5ed8573 commit 992f1fa

File tree

4 files changed

+264
-37
lines changed

4 files changed

+264
-37
lines changed

lib/convert.js

+17-33
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,18 @@ module.exports = function convert(config) {
108108
lastTypeArgument.end + 1
109109
],
110110
loc: nodeUtils.getLocFor(firstTypeArgument.pos - 1, lastTypeArgument.end + 1, ast),
111-
params: typeArguments.map(typeArgument => {
112-
/**
113-
* Have to manually calculate the start of the range,
114-
* because TypeScript includes leading whitespace but Flow does not
115-
*/
116-
const typeArgumentStart = (typeArgument.typeName && typeArgument.typeName.text)
117-
? typeArgument.end - typeArgument.typeName.text.length
118-
: typeArgument.pos;
119-
return {
120-
type: AST_NODE_TYPES.GenericTypeAnnotation,
121-
range: [
122-
typeArgumentStart,
123-
typeArgument.end
124-
],
125-
loc: nodeUtils.getLocFor(typeArgumentStart, typeArgument.end, ast),
126-
id: convertChild(typeArgument.typeName || typeArgument),
127-
typeParameters: (typeArgument.typeArguments)
128-
? convertTypeArgumentsToTypeParameters(typeArgument.typeArguments)
129-
: null
130-
};
131-
})
111+
params: typeArguments.map(typeArgument => ({
112+
type: AST_NODE_TYPES.GenericTypeAnnotation,
113+
range: [
114+
typeArgument.getStart(),
115+
typeArgument.getEnd()
116+
],
117+
loc: nodeUtils.getLoc(typeArgument, ast),
118+
id: convertChild(typeArgument.typeName || typeArgument),
119+
typeParameters: (typeArgument.typeArguments)
120+
? convertTypeArgumentsToTypeParameters(typeArgument.typeArguments)
121+
: null
122+
}))
132123
};
133124
}
134125

@@ -148,14 +139,7 @@ module.exports = function convert(config) {
148139
],
149140
loc: nodeUtils.getLocFor(firstTypeParameter.pos - 1, lastTypeParameter.end + 1, ast),
150141
params: typeParameters.map(typeParameter => {
151-
152-
/**
153-
* Have to manually calculate the start of the range,
154-
* because TypeScript includes leading whitespace but Flow does not
155-
*/
156-
const typeParameterStart = (typeParameter.name && typeParameter.name.text)
157-
? typeParameter.name.end - typeParameter.name.text.length
158-
: typeParameter.pos;
142+
const name = nodeUtils.unescapeIdentifier(typeParameter.name.text);
159143

160144
const defaultParameter = typeParameter.default
161145
? convert({ node: typeParameter.default, parent: typeParameter, ast, additionalOptions })
@@ -164,11 +148,11 @@ module.exports = function convert(config) {
164148
return {
165149
type: AST_NODE_TYPES.TypeParameter,
166150
range: [
167-
typeParameterStart,
168-
typeParameter.end
151+
typeParameter.getStart(),
152+
typeParameter.getEnd()
169153
],
170-
loc: nodeUtils.getLocFor(typeParameterStart, typeParameter.end, ast),
171-
name: typeParameter.name.text,
154+
loc: nodeUtils.getLoc(typeParameter, ast),
155+
name,
172156
constraint: (typeParameter.constraint)
173157
? convertTypeAnnotation(typeParameter.constraint)
174158
: null,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
module.exports = {
2+
"type": "Program",
3+
"range": [
4+
0,
5+
15
6+
],
7+
"loc": {
8+
"start": {
9+
"line": 1,
10+
"column": 0
11+
},
12+
"end": {
13+
"line": 1,
14+
"column": 15
15+
}
16+
},
17+
"body": [
18+
{
19+
"type": "ClassDeclaration",
20+
"range": [
21+
0,
22+
15
23+
],
24+
"loc": {
25+
"start": {
26+
"line": 1,
27+
"column": 0
28+
},
29+
"end": {
30+
"line": 1,
31+
"column": 15
32+
}
33+
},
34+
"typeParameters": {
35+
"type": "TypeParameterDeclaration",
36+
"range": [
37+
7,
38+
12
39+
],
40+
"loc": {
41+
"start": {
42+
"line": 1,
43+
"column": 7
44+
},
45+
"end": {
46+
"line": 1,
47+
"column": 12
48+
}
49+
},
50+
"params": [
51+
{
52+
"type": "TypeParameter",
53+
"range": [
54+
8,
55+
11
56+
],
57+
"loc": {
58+
"start": {
59+
"line": 1,
60+
"column": 8
61+
},
62+
"end": {
63+
"line": 1,
64+
"column": 11
65+
}
66+
},
67+
"name": "__P",
68+
"constraint": null
69+
}
70+
]
71+
},
72+
"id": {
73+
"type": "Identifier",
74+
"range": [
75+
6,
76+
7
77+
],
78+
"loc": {
79+
"start": {
80+
"line": 1,
81+
"column": 6
82+
},
83+
"end": {
84+
"line": 1,
85+
"column": 7
86+
}
87+
},
88+
"name": "A"
89+
},
90+
"body": {
91+
"type": "ClassBody",
92+
"body": [],
93+
"range": [
94+
13,
95+
15
96+
],
97+
"loc": {
98+
"start": {
99+
"line": 1,
100+
"column": 13
101+
},
102+
"end": {
103+
"line": 1,
104+
"column": 15
105+
}
106+
}
107+
},
108+
"superClass": null,
109+
"implements": [],
110+
"decorators": []
111+
}
112+
],
113+
"sourceType": "script",
114+
"tokens": [
115+
{
116+
"type": "Keyword",
117+
"value": "class",
118+
"range": [
119+
0,
120+
5
121+
],
122+
"loc": {
123+
"start": {
124+
"line": 1,
125+
"column": 0
126+
},
127+
"end": {
128+
"line": 1,
129+
"column": 5
130+
}
131+
}
132+
},
133+
{
134+
"type": "Identifier",
135+
"value": "A",
136+
"range": [
137+
6,
138+
7
139+
],
140+
"loc": {
141+
"start": {
142+
"line": 1,
143+
"column": 6
144+
},
145+
"end": {
146+
"line": 1,
147+
"column": 7
148+
}
149+
}
150+
},
151+
{
152+
"type": "Punctuator",
153+
"value": "<",
154+
"range": [
155+
7,
156+
8
157+
],
158+
"loc": {
159+
"start": {
160+
"line": 1,
161+
"column": 7
162+
},
163+
"end": {
164+
"line": 1,
165+
"column": 8
166+
}
167+
}
168+
},
169+
{
170+
"type": "Identifier",
171+
"value": "__P",
172+
"range": [
173+
8,
174+
11
175+
],
176+
"loc": {
177+
"start": {
178+
"line": 1,
179+
"column": 8
180+
},
181+
"end": {
182+
"line": 1,
183+
"column": 11
184+
}
185+
}
186+
},
187+
{
188+
"type": "Punctuator",
189+
"value": ">",
190+
"range": [
191+
11,
192+
12
193+
],
194+
"loc": {
195+
"start": {
196+
"line": 1,
197+
"column": 11
198+
},
199+
"end": {
200+
"line": 1,
201+
"column": 12
202+
}
203+
}
204+
},
205+
{
206+
"type": "Punctuator",
207+
"value": "{",
208+
"range": [
209+
13,
210+
14
211+
],
212+
"loc": {
213+
"start": {
214+
"line": 1,
215+
"column": 13
216+
},
217+
"end": {
218+
"line": 1,
219+
"column": 14
220+
}
221+
}
222+
},
223+
{
224+
"type": "Punctuator",
225+
"value": "}",
226+
"range": [
227+
14,
228+
15
229+
],
230+
"loc": {
231+
"start": {
232+
"line": 1,
233+
"column": 14
234+
},
235+
"end": {
236+
"line": 1,
237+
"column": 15
238+
}
239+
}
240+
}
241+
]
242+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A<__P> {}

tests/fixtures/typescript/basics/nested-type-arguments.result.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ module.exports = {
8989
"line": 1
9090
},
9191
"start": {
92-
"column": 38,
92+
"column": 23,
9393
"line": 1
9494
}
9595
},
9696
"range": [
97-
38,
97+
23,
9898
43
9999
],
100100
"type": "GenericTypeAnnotation",
@@ -135,12 +135,12 @@ module.exports = {
135135
"line": 1
136136
},
137137
"start": {
138-
"column": 37,
138+
"column": 29,
139139
"line": 1
140140
}
141141
},
142142
"range": [
143-
37,
143+
29,
144144
42
145145
],
146146
"type": "GenericTypeAnnotation",

0 commit comments

Comments
 (0)