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

Fix: Unescape type parameter names (fixes #296) #298

Merged
merged 1 commit into from
May 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 17 additions & 33 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,18 @@ module.exports = function convert(config) {
lastTypeArgument.end + 1
],
loc: nodeUtils.getLocFor(firstTypeArgument.pos - 1, lastTypeArgument.end + 1, ast),
params: typeArguments.map(typeArgument => {
/**
* Have to manually calculate the start of the range,
* because TypeScript includes leading whitespace but Flow does not
*/
const typeArgumentStart = (typeArgument.typeName && typeArgument.typeName.text)
? typeArgument.end - typeArgument.typeName.text.length
: typeArgument.pos;
return {
type: AST_NODE_TYPES.GenericTypeAnnotation,
range: [
typeArgumentStart,
typeArgument.end
],
loc: nodeUtils.getLocFor(typeArgumentStart, typeArgument.end, ast),
id: convertChild(typeArgument.typeName || typeArgument),
typeParameters: (typeArgument.typeArguments)
? convertTypeArgumentsToTypeParameters(typeArgument.typeArguments)
: null
};
})
params: typeArguments.map(typeArgument => ({
type: AST_NODE_TYPES.GenericTypeAnnotation,
range: [
typeArgument.getStart(),
typeArgument.getEnd()
],
loc: nodeUtils.getLoc(typeArgument, ast),
id: convertChild(typeArgument.typeName || typeArgument),
typeParameters: (typeArgument.typeArguments)
? convertTypeArgumentsToTypeParameters(typeArgument.typeArguments)
: null
}))
};
}

Expand All @@ -148,14 +139,7 @@ module.exports = function convert(config) {
],
loc: nodeUtils.getLocFor(firstTypeParameter.pos - 1, lastTypeParameter.end + 1, ast),
params: typeParameters.map(typeParameter => {

/**
* Have to manually calculate the start of the range,
* because TypeScript includes leading whitespace but Flow does not
*/
const typeParameterStart = (typeParameter.name && typeParameter.name.text)
? typeParameter.name.end - typeParameter.name.text.length
: typeParameter.pos;
const name = nodeUtils.unescapeIdentifier(typeParameter.name.text);

const defaultParameter = typeParameter.default
? convert({ node: typeParameter.default, parent: typeParameter, ast, additionalOptions })
Expand All @@ -164,11 +148,11 @@ module.exports = function convert(config) {
return {
type: AST_NODE_TYPES.TypeParameter,
range: [
typeParameterStart,
typeParameter.end
typeParameter.getStart(),
typeParameter.getEnd()
],
loc: nodeUtils.getLocFor(typeParameterStart, typeParameter.end, ast),
name: typeParameter.name.text,
loc: nodeUtils.getLoc(typeParameter, ast),
name,
constraint: (typeParameter.constraint)
? convertTypeAnnotation(typeParameter.constraint)
: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
module.exports = {
"type": "Program",
"range": [
0,
15
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 15
}
},
"body": [
{
"type": "ClassDeclaration",
"range": [
0,
15
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 15
}
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"range": [
7,
12
],
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 12
}
},
"params": [
{
"type": "TypeParameter",
"range": [
8,
11
],
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 11
}
},
"name": "__P",
"constraint": null
}
]
},
"id": {
"type": "Identifier",
"range": [
6,
7
],
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
}
},
"name": "A"
},
"body": {
"type": "ClassBody",
"body": [],
"range": [
13,
15
],
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 15
}
}
},
"superClass": null,
"implements": [],
"decorators": []
}
],
"sourceType": "script",
"tokens": [
{
"type": "Keyword",
"value": "class",
"range": [
0,
5
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 5
}
}
},
{
"type": "Identifier",
"value": "A",
"range": [
6,
7
],
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
}
}
},
{
"type": "Punctuator",
"value": "<",
"range": [
7,
8
],
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 8
}
}
},
{
"type": "Identifier",
"value": "__P",
"range": [
8,
11
],
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 11
}
}
},
{
"type": "Punctuator",
"value": ">",
"range": [
11,
12
],
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 12
}
}
},
{
"type": "Punctuator",
"value": "{",
"range": [
13,
14
],
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 14
}
}
},
{
"type": "Punctuator",
"value": "}",
"range": [
14,
15
],
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 15
}
}
}
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class A<__P> {}
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ module.exports = {
"line": 1
},
"start": {
"column": 38,
"column": 23,
"line": 1
}
},
"range": [
38,
23,
43
],
"type": "GenericTypeAnnotation",
Expand Down Expand Up @@ -135,12 +135,12 @@ module.exports = {
"line": 1
},
"start": {
"column": 37,
"column": 29,
"line": 1
}
},
"range": [
37,
29,
42
],
"type": "GenericTypeAnnotation",
Expand Down