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

Breaking: Include type annotation range for Identifiers (fixes #314) #319

Merged
merged 2 commits into from
Jul 10, 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
17 changes: 13 additions & 4 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ module.exports = function convert(config) {

}

case SyntaxKind.VariableDeclaration:
case SyntaxKind.VariableDeclaration: {
Object.assign(result, {
type: AST_NODE_TYPES.VariableDeclarator,
id: convertChild(node.name),
Expand All @@ -535,8 +535,18 @@ module.exports = function convert(config) {

if (node.type) {
result.id.typeAnnotation = convertTypeAnnotation(node.type);
result.id.range[1] = node.type.getEnd();

const identifierEnd = node.name.getEnd();
const numCharsBetweenTypeAndIdentifier = node.type.getStart() - (node.type.getFullStart() - identifierEnd - ":".length) - identifierEnd;

result.id.typeAnnotation.range = [
result.id.typeAnnotation.range[0] - numCharsBetweenTypeAndIdentifier,
result.id.typeAnnotation.range[1]
];
}
break;
}

case SyntaxKind.VariableStatement:
Object.assign(result, {
Expand Down Expand Up @@ -1145,9 +1155,8 @@ module.exports = function convert(config) {
}

if (node.type) {
Object.assign(parameter, {
typeAnnotation: convertTypeAnnotation(node.type)
});
parameter.typeAnnotation = convertTypeAnnotation(node.type);
parameter.range[1] = node.type.getEnd();
}

if (node.questionToken) {
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/typescript/basics/var-with-type.src.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
var name:string = "Nicholas";
var foo: string = "Bar";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x : string;
Loading