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

Commit

Permalink
Fix: Calculate typeArguments loc data correctly if empty (fixes #395) (
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry committed Oct 22, 2017
1 parent a214f71 commit 0401ffc
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 7 deletions.
29 changes: 22 additions & 7 deletions lib/convert.js
Expand Up @@ -102,17 +102,32 @@ module.exports = function convert(config) {
* @returns {TypeParameterInstantiation} TypeParameterInstantiation node
*/
function convertTypeArgumentsToTypeParameters(typeArguments) {
const firstTypeArgument = typeArguments[0];
const lastTypeArgument = typeArguments[typeArguments.length - 1];
const greaterThanToken = nodeUtils.findNextToken(lastTypeArgument, ast);

/**
* Even if typeArguments is an empty array, TypeScript sets a `pos` and `end`
* property on the array object so we can safely read the values here
*/
const start = typeArguments.pos - 1;
let end = typeArguments.end + 1;
if (typeArguments && typeArguments.length) {
const firstTypeArgument = typeArguments[0];
const typeArgumentsParent = firstTypeArgument.parent;
/**
* In the case of the parent being a CallExpression we have to use slightly different
* logic to calculate the correct end position
*/
if (typeArgumentsParent && typeArgumentsParent.kind === SyntaxKind.CallExpression) {
const lastTypeArgument = typeArguments[typeArguments.length - 1];
const greaterThanToken = nodeUtils.findNextToken(lastTypeArgument, ast);
end = greaterThanToken.end;
}
}
return {
type: AST_NODE_TYPES.TypeParameterInstantiation,
range: [
firstTypeArgument.pos - 1,
greaterThanToken.end
start,
end
],
loc: nodeUtils.getLocFor(firstTypeArgument.pos - 1, greaterThanToken.end, ast),
loc: nodeUtils.getLocFor(start, end, ast),
params: typeArguments.map(typeArgument => {
if (nodeUtils.isTypeKeyword(typeArgument.kind)) {
return {
Expand Down
@@ -0,0 +1 @@
const foo: Foo<>
259 changes: 259 additions & 0 deletions tests/lib/__snapshots__/typescript.js.snap
Expand Up @@ -55360,6 +55360,265 @@ Object {
}
`;

exports[`typescript fixtures/errorRecovery/empty-type-arguments.src 1`] = `
Object {
"body": Array [
Object {
"declarations": Array [
Object {
"id": Object {
"loc": Object {
"end": Object {
"column": 16,
"line": 1,
},
"start": Object {
"column": 6,
"line": 1,
},
},
"name": "foo",
"range": Array [
6,
16,
],
"type": "Identifier",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 16,
"line": 1,
},
"start": Object {
"column": 9,
"line": 1,
},
},
"range": Array [
9,
16,
],
"type": "TypeAnnotation",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 16,
"line": 1,
},
"start": Object {
"column": 11,
"line": 1,
},
},
"range": Array [
11,
16,
],
"type": "TSTypeReference",
"typeName": Object {
"loc": Object {
"end": Object {
"column": 14,
"line": 1,
},
"start": Object {
"column": 11,
"line": 1,
},
},
"name": "Foo",
"range": Array [
11,
14,
],
"type": "Identifier",
},
"typeParameters": Object {
"loc": Object {
"end": Object {
"column": 16,
"line": 1,
},
"start": Object {
"column": 14,
"line": 1,
},
},
"params": Array [],
"range": Array [
14,
16,
],
"type": "TypeParameterInstantiation",
},
},
},
},
"init": null,
"loc": Object {
"end": Object {
"column": 16,
"line": 1,
},
"start": Object {
"column": 6,
"line": 1,
},
},
"range": Array [
6,
16,
],
"type": "VariableDeclarator",
},
],
"kind": "const",
"loc": Object {
"end": Object {
"column": 16,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
16,
],
"type": "VariableDeclaration",
},
],
"loc": Object {
"end": Object {
"column": 16,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
16,
],
"sourceType": "script",
"tokens": Array [
Object {
"loc": Object {
"end": Object {
"column": 5,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
5,
],
"type": "Keyword",
"value": "const",
},
Object {
"loc": Object {
"end": Object {
"column": 9,
"line": 1,
},
"start": Object {
"column": 6,
"line": 1,
},
},
"range": Array [
6,
9,
],
"type": "Identifier",
"value": "foo",
},
Object {
"loc": Object {
"end": Object {
"column": 10,
"line": 1,
},
"start": Object {
"column": 9,
"line": 1,
},
},
"range": Array [
9,
10,
],
"type": "Punctuator",
"value": ":",
},
Object {
"loc": Object {
"end": Object {
"column": 14,
"line": 1,
},
"start": Object {
"column": 11,
"line": 1,
},
},
"range": Array [
11,
14,
],
"type": "Identifier",
"value": "Foo",
},
Object {
"loc": Object {
"end": Object {
"column": 15,
"line": 1,
},
"start": Object {
"column": 14,
"line": 1,
},
},
"range": Array [
14,
15,
],
"type": "Punctuator",
"value": "<",
},
Object {
"loc": Object {
"end": Object {
"column": 16,
"line": 1,
},
"start": Object {
"column": 15,
"line": 1,
},
},
"range": Array [
15,
16,
],
"type": "Punctuator",
"value": ">",
},
],
"type": "Program",
}
`;

exports[`typescript fixtures/errorRecovery/enum-with-keywords.src 1`] = `
Object {
"body": Array [
Expand Down

0 comments on commit 0401ffc

Please sign in to comment.