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

Commit 2ec1b95

Browse files
committed
Fix inner vs outer typeAnnotation ranges
1 parent 4e1547d commit 2ec1b95

File tree

3 files changed

+130
-7
lines changed

3 files changed

+130
-7
lines changed

lib/convert.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ module.exports = function convert(config) {
518518

519519
}
520520

521-
case SyntaxKind.VariableDeclaration:
521+
case SyntaxKind.VariableDeclaration: {
522522
Object.assign(result, {
523523
type: AST_NODE_TYPES.VariableDeclarator,
524524
id: convertChild(node.name),
@@ -528,8 +528,17 @@ module.exports = function convert(config) {
528528
if (node.type) {
529529
result.id.typeAnnotation = convertTypeAnnotation(node.type);
530530
result.id.range[1] = node.type.getEnd();
531+
532+
const identifierEnd = node.name.getEnd();
533+
const numCharsBetweenTypeAndIdentifier = node.type.getStart() - (node.type.getFullStart() - identifierEnd - ":".length) - identifierEnd;
534+
535+
result.id.typeAnnotation.range = [
536+
result.id.typeAnnotation.range[0] - numCharsBetweenTypeAndIdentifier,
537+
result.id.typeAnnotation.range[1]
538+
];
531539
}
532540
break;
541+
}
533542

534543
case SyntaxKind.VariableStatement:
535544
Object.assign(result, {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let x : string;

tests/lib/__snapshots__/typescript.js.snap

Lines changed: 119 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15050,7 +15050,7 @@ Object {
1505015050
},
1505115051
},
1505215052
"range": Array [
15053-
17,
15053+
15,
1505415054
44,
1505515055
],
1505615056
"type": "TypeAnnotation",
@@ -15694,7 +15694,7 @@ Object {
1569415694
},
1569515695
},
1569615696
"range": Array [
15697-
7,
15697+
5,
1569815698
11,
1569915699
],
1570015700
"type": "TypeAnnotation",
@@ -15784,7 +15784,7 @@ Object {
1578415784
},
1578515785
},
1578615786
"range": Array [
15787-
20,
15787+
18,
1578815788
29,
1578915789
],
1579015790
"type": "TypeAnnotation",
@@ -17345,7 +17345,7 @@ Object {
1734517345
},
1734617346
},
1734717347
"range": Array [
17348-
9,
17348+
7,
1734917349
14,
1735017350
],
1735117351
"type": "TypeAnnotation",
@@ -17546,7 +17546,7 @@ Object {
1754617546
},
1754717547
},
1754817548
"range": Array [
17549-
9,
17549+
8,
1755017550
15,
1755117551
],
1755217552
"type": "TypeAnnotation",
@@ -17654,7 +17654,7 @@ Object {
1765417654
},
1765517655
},
1765617656
"range": Array [
17657-
39,
17657+
37,
1765817658
45,
1765917659
],
1766017660
"type": "TypeAnnotation",
@@ -17750,6 +17750,119 @@ Object {
1775017750
}
1775117751
`;
1775217752

17753+
exports[`typescript fixtures/basics/variable-declaration-type-annotation-spacing.src 1`] = `
17754+
Object {
17755+
"body": Array [
17756+
Object {
17757+
"declarations": Array [
17758+
Object {
17759+
"id": Object {
17760+
"loc": Object {
17761+
"end": Object {
17762+
"column": 5,
17763+
"line": 1,
17764+
},
17765+
"start": Object {
17766+
"column": 4,
17767+
"line": 1,
17768+
},
17769+
},
17770+
"name": "x",
17771+
"range": Array [
17772+
4,
17773+
21,
17774+
],
17775+
"type": "Identifier",
17776+
"typeAnnotation": Object {
17777+
"loc": Object {
17778+
"end": Object {
17779+
"column": 21,
17780+
"line": 1,
17781+
},
17782+
"start": Object {
17783+
"column": 15,
17784+
"line": 1,
17785+
},
17786+
},
17787+
"range": Array [
17788+
8,
17789+
21,
17790+
],
17791+
"type": "TypeAnnotation",
17792+
"typeAnnotation": Object {
17793+
"loc": Object {
17794+
"end": Object {
17795+
"column": 21,
17796+
"line": 1,
17797+
},
17798+
"start": Object {
17799+
"column": 15,
17800+
"line": 1,
17801+
},
17802+
},
17803+
"range": Array [
17804+
15,
17805+
21,
17806+
],
17807+
"type": "TSStringKeyword",
17808+
},
17809+
},
17810+
},
17811+
"init": null,
17812+
"loc": Object {
17813+
"end": Object {
17814+
"column": 21,
17815+
"line": 1,
17816+
},
17817+
"start": Object {
17818+
"column": 4,
17819+
"line": 1,
17820+
},
17821+
},
17822+
"range": Array [
17823+
4,
17824+
21,
17825+
],
17826+
"type": "VariableDeclarator",
17827+
},
17828+
],
17829+
"kind": "let",
17830+
"loc": Object {
17831+
"end": Object {
17832+
"column": 22,
17833+
"line": 1,
17834+
},
17835+
"start": Object {
17836+
"column": 0,
17837+
"line": 1,
17838+
},
17839+
},
17840+
"range": Array [
17841+
0,
17842+
22,
17843+
],
17844+
"type": "VariableDeclaration",
17845+
},
17846+
],
17847+
"loc": Object {
17848+
"end": Object {
17849+
"column": 22,
17850+
"line": 1,
17851+
},
17852+
"start": Object {
17853+
"column": 0,
17854+
"line": 1,
17855+
},
17856+
},
17857+
"range": Array [
17858+
0,
17859+
22,
17860+
],
17861+
"sourceType": "script",
17862+
"type": "Program",
17863+
}
17864+
`;
17865+
1775317866
exports[`typescript fixtures/decorators/accessor-decorators/accessor-decorator-factory-instance-member.src 1`] = `
1775417867
Object {
1775517868
"body": Array [

0 commit comments

Comments
 (0)