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

Commit 0401ffc

Browse files
authored
Fix: Calculate typeArguments loc data correctly if empty (fixes #395) (#396)
1 parent a214f71 commit 0401ffc

File tree

3 files changed

+282
-7
lines changed

3 files changed

+282
-7
lines changed

lib/convert.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,32 @@ module.exports = function convert(config) {
102102
* @returns {TypeParameterInstantiation} TypeParameterInstantiation node
103103
*/
104104
function convertTypeArgumentsToTypeParameters(typeArguments) {
105-
const firstTypeArgument = typeArguments[0];
106-
const lastTypeArgument = typeArguments[typeArguments.length - 1];
107-
const greaterThanToken = nodeUtils.findNextToken(lastTypeArgument, ast);
108-
105+
/**
106+
* Even if typeArguments is an empty array, TypeScript sets a `pos` and `end`
107+
* property on the array object so we can safely read the values here
108+
*/
109+
const start = typeArguments.pos - 1;
110+
let end = typeArguments.end + 1;
111+
if (typeArguments && typeArguments.length) {
112+
const firstTypeArgument = typeArguments[0];
113+
const typeArgumentsParent = firstTypeArgument.parent;
114+
/**
115+
* In the case of the parent being a CallExpression we have to use slightly different
116+
* logic to calculate the correct end position
117+
*/
118+
if (typeArgumentsParent && typeArgumentsParent.kind === SyntaxKind.CallExpression) {
119+
const lastTypeArgument = typeArguments[typeArguments.length - 1];
120+
const greaterThanToken = nodeUtils.findNextToken(lastTypeArgument, ast);
121+
end = greaterThanToken.end;
122+
}
123+
}
109124
return {
110125
type: AST_NODE_TYPES.TypeParameterInstantiation,
111126
range: [
112-
firstTypeArgument.pos - 1,
113-
greaterThanToken.end
127+
start,
128+
end
114129
],
115-
loc: nodeUtils.getLocFor(firstTypeArgument.pos - 1, greaterThanToken.end, ast),
130+
loc: nodeUtils.getLocFor(start, end, ast),
116131
params: typeArguments.map(typeArgument => {
117132
if (nodeUtils.isTypeKeyword(typeArgument.kind)) {
118133
return {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const foo: Foo<>

tests/lib/__snapshots__/typescript.js.snap

+259
Original file line numberDiff line numberDiff line change
@@ -55360,6 +55360,265 @@ Object {
5536055360
}
5536155361
`;
5536255362

55363+
exports[`typescript fixtures/errorRecovery/empty-type-arguments.src 1`] = `
55364+
Object {
55365+
"body": Array [
55366+
Object {
55367+
"declarations": Array [
55368+
Object {
55369+
"id": Object {
55370+
"loc": Object {
55371+
"end": Object {
55372+
"column": 16,
55373+
"line": 1,
55374+
},
55375+
"start": Object {
55376+
"column": 6,
55377+
"line": 1,
55378+
},
55379+
},
55380+
"name": "foo",
55381+
"range": Array [
55382+
6,
55383+
16,
55384+
],
55385+
"type": "Identifier",
55386+
"typeAnnotation": Object {
55387+
"loc": Object {
55388+
"end": Object {
55389+
"column": 16,
55390+
"line": 1,
55391+
},
55392+
"start": Object {
55393+
"column": 9,
55394+
"line": 1,
55395+
},
55396+
},
55397+
"range": Array [
55398+
9,
55399+
16,
55400+
],
55401+
"type": "TypeAnnotation",
55402+
"typeAnnotation": Object {
55403+
"loc": Object {
55404+
"end": Object {
55405+
"column": 16,
55406+
"line": 1,
55407+
},
55408+
"start": Object {
55409+
"column": 11,
55410+
"line": 1,
55411+
},
55412+
},
55413+
"range": Array [
55414+
11,
55415+
16,
55416+
],
55417+
"type": "TSTypeReference",
55418+
"typeName": Object {
55419+
"loc": Object {
55420+
"end": Object {
55421+
"column": 14,
55422+
"line": 1,
55423+
},
55424+
"start": Object {
55425+
"column": 11,
55426+
"line": 1,
55427+
},
55428+
},
55429+
"name": "Foo",
55430+
"range": Array [
55431+
11,
55432+
14,
55433+
],
55434+
"type": "Identifier",
55435+
},
55436+
"typeParameters": Object {
55437+
"loc": Object {
55438+
"end": Object {
55439+
"column": 16,
55440+
"line": 1,
55441+
},
55442+
"start": Object {
55443+
"column": 14,
55444+
"line": 1,
55445+
},
55446+
},
55447+
"params": Array [],
55448+
"range": Array [
55449+
14,
55450+
16,
55451+
],
55452+
"type": "TypeParameterInstantiation",
55453+
},
55454+
},
55455+
},
55456+
},
55457+
"init": null,
55458+
"loc": Object {
55459+
"end": Object {
55460+
"column": 16,
55461+
"line": 1,
55462+
},
55463+
"start": Object {
55464+
"column": 6,
55465+
"line": 1,
55466+
},
55467+
},
55468+
"range": Array [
55469+
6,
55470+
16,
55471+
],
55472+
"type": "VariableDeclarator",
55473+
},
55474+
],
55475+
"kind": "const",
55476+
"loc": Object {
55477+
"end": Object {
55478+
"column": 16,
55479+
"line": 1,
55480+
},
55481+
"start": Object {
55482+
"column": 0,
55483+
"line": 1,
55484+
},
55485+
},
55486+
"range": Array [
55487+
0,
55488+
16,
55489+
],
55490+
"type": "VariableDeclaration",
55491+
},
55492+
],
55493+
"loc": Object {
55494+
"end": Object {
55495+
"column": 16,
55496+
"line": 1,
55497+
},
55498+
"start": Object {
55499+
"column": 0,
55500+
"line": 1,
55501+
},
55502+
},
55503+
"range": Array [
55504+
0,
55505+
16,
55506+
],
55507+
"sourceType": "script",
55508+
"tokens": Array [
55509+
Object {
55510+
"loc": Object {
55511+
"end": Object {
55512+
"column": 5,
55513+
"line": 1,
55514+
},
55515+
"start": Object {
55516+
"column": 0,
55517+
"line": 1,
55518+
},
55519+
},
55520+
"range": Array [
55521+
0,
55522+
5,
55523+
],
55524+
"type": "Keyword",
55525+
"value": "const",
55526+
},
55527+
Object {
55528+
"loc": Object {
55529+
"end": Object {
55530+
"column": 9,
55531+
"line": 1,
55532+
},
55533+
"start": Object {
55534+
"column": 6,
55535+
"line": 1,
55536+
},
55537+
},
55538+
"range": Array [
55539+
6,
55540+
9,
55541+
],
55542+
"type": "Identifier",
55543+
"value": "foo",
55544+
},
55545+
Object {
55546+
"loc": Object {
55547+
"end": Object {
55548+
"column": 10,
55549+
"line": 1,
55550+
},
55551+
"start": Object {
55552+
"column": 9,
55553+
"line": 1,
55554+
},
55555+
},
55556+
"range": Array [
55557+
9,
55558+
10,
55559+
],
55560+
"type": "Punctuator",
55561+
"value": ":",
55562+
},
55563+
Object {
55564+
"loc": Object {
55565+
"end": Object {
55566+
"column": 14,
55567+
"line": 1,
55568+
},
55569+
"start": Object {
55570+
"column": 11,
55571+
"line": 1,
55572+
},
55573+
},
55574+
"range": Array [
55575+
11,
55576+
14,
55577+
],
55578+
"type": "Identifier",
55579+
"value": "Foo",
55580+
},
55581+
Object {
55582+
"loc": Object {
55583+
"end": Object {
55584+
"column": 15,
55585+
"line": 1,
55586+
},
55587+
"start": Object {
55588+
"column": 14,
55589+
"line": 1,
55590+
},
55591+
},
55592+
"range": Array [
55593+
14,
55594+
15,
55595+
],
55596+
"type": "Punctuator",
55597+
"value": "<",
55598+
},
55599+
Object {
55600+
"loc": Object {
55601+
"end": Object {
55602+
"column": 16,
55603+
"line": 1,
55604+
},
55605+
"start": Object {
55606+
"column": 15,
55607+
"line": 1,
55608+
},
55609+
},
55610+
"range": Array [
55611+
15,
55612+
16,
55613+
],
55614+
"type": "Punctuator",
55615+
"value": ">",
55616+
},
55617+
],
55618+
"type": "Program",
55619+
}
55620+
`;
55621+
5536355622
exports[`typescript fixtures/errorRecovery/enum-with-keywords.src 1`] = `
5536455623
Object {
5536555624
"body": Array [

0 commit comments

Comments
 (0)