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

Commit 367d2a0

Browse files
committed
Fix: Location data for typeAnnotations
1 parent 62088b1 commit 367d2a0

File tree

5 files changed

+738
-614
lines changed

5 files changed

+738
-614
lines changed

lib/convert.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ module.exports = function convert(config) {
8585
*/
8686
function convertTypeAnnotation(child) {
8787
const annotation = convertChild(child);
88+
const annotationStartCol = child.getFullStart() - 1;
89+
const loc = nodeUtils.getLocFor(annotationStartCol, child.end, ast);
8890
return {
8991
type: AST_NODE_TYPES.TypeAnnotation,
90-
loc: annotation.loc,
91-
range: annotation.range,
92+
loc,
93+
range: [annotationStartCol, child.end],
9294
typeAnnotation: annotation
9395
};
9496
}
@@ -160,7 +162,7 @@ module.exports = function convert(config) {
160162

161163
const constraint = typeParameter.constraint
162164
? convert({ node: typeParameter.constraint, parent: typeParameter, ast, additionalOptions })
163-
: null;
165+
: undefined;
164166

165167
const defaultParameter = typeParameter.default
166168
? convert({ node: typeParameter.default, parent: typeParameter, ast, additionalOptions })
@@ -392,6 +394,19 @@ module.exports = function convert(config) {
392394
result.modifiers = remainingModifiers.map(convertChild);
393395
}
394396

397+
/**
398+
* Uses the current TSNode's end location for its `type` to adjust the location data of the given
399+
* ESTreeNode, which should be the parent of the final typeAnnotation node
400+
* @param {ESTreeNode} typeAnnotationParent The node that will have its location data mutated
401+
* @returns {void}
402+
*/
403+
function fixTypeAnnotationParentLocation(typeAnnotationParent) {
404+
const end = node.type.getEnd();
405+
typeAnnotationParent.range[1] = end;
406+
const loc = nodeUtils.getLocFor(typeAnnotationParent.range[0], typeAnnotationParent.range[1], ast);
407+
typeAnnotationParent.loc = loc;
408+
}
409+
395410
/**
396411
* The core of the conversion logic:
397412
* Identify and convert each relevant TypeScript SyntaxKind
@@ -619,15 +634,7 @@ module.exports = function convert(config) {
619634

620635
if (node.type) {
621636
result.id.typeAnnotation = convertTypeAnnotation(node.type);
622-
result.id.range[1] = node.type.getEnd();
623-
624-
const identifierEnd = node.name.getEnd();
625-
const numCharsBetweenTypeAndIdentifier = node.type.getStart() - (node.type.getFullStart() - identifierEnd - ":".length) - identifierEnd;
626-
627-
result.id.typeAnnotation.range = [
628-
result.id.typeAnnotation.range[0] - numCharsBetweenTypeAndIdentifier,
629-
result.id.typeAnnotation.range[1]
630-
];
637+
fixTypeAnnotationParentLocation(result.id);
631638
}
632639
break;
633640
}
@@ -806,7 +813,7 @@ module.exports = function convert(config) {
806813
value: convertChild(node.initializer),
807814
computed: nodeUtils.isComputedProperty(node.name),
808815
static: nodeUtils.hasStaticModifierFlag(node),
809-
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node)
816+
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined
810817
});
811818

812819
if (node.type) {
@@ -1273,7 +1280,7 @@ module.exports = function convert(config) {
12731280

12741281
if (node.type) {
12751282
parameter.typeAnnotation = convertTypeAnnotation(node.type);
1276-
parameter.range[1] = node.type.getEnd();
1283+
fixTypeAnnotationParentLocation(parameter);
12771284
}
12781285

12791286
if (node.questionToken) {
@@ -1286,9 +1293,9 @@ module.exports = function convert(config) {
12861293
range: [node.getStart(), node.end],
12871294
loc: nodeUtils.getLoc(node, ast),
12881295
accessibility: nodeUtils.getTSNodeAccessibility(node),
1289-
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
1296+
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined,
12901297
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
1291-
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node),
1298+
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node) || undefined,
12921299
parameter: result
12931300
};
12941301
}
@@ -1941,9 +1948,9 @@ module.exports = function convert(config) {
19411948
key: convertChild(node.name),
19421949
params: convertParameters(node.parameters),
19431950
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null,
1944-
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
1951+
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined,
19451952
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
1946-
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node)
1953+
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node) || undefined
19471954
});
19481955

19491956
const accessibility = nodeUtils.getTSNodeAccessibility(node);
@@ -1961,14 +1968,14 @@ module.exports = function convert(config) {
19611968
case SyntaxKind.PropertySignature: {
19621969
Object.assign(result, {
19631970
type: AST_NODE_TYPES.TSPropertySignature,
1964-
optional: nodeUtils.isOptional(node),
1971+
optional: nodeUtils.isOptional(node) || undefined,
19651972
computed: nodeUtils.isComputedProperty(node.name),
19661973
key: convertChild(node.name),
1967-
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null,
1968-
initializer: convertChild(node.initializer),
1969-
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
1970-
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
1971-
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node)
1974+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : undefined,
1975+
initializer: convertChild(node.initializer) || undefined,
1976+
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined,
1977+
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node) || undefined,
1978+
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node) || undefined
19721979
});
19731980

19741981
const accessibility = nodeUtils.getTSNodeAccessibility(node);
@@ -1984,9 +1991,9 @@ module.exports = function convert(config) {
19841991
type: AST_NODE_TYPES.TSIndexSignature,
19851992
index: convertChild(node.parameters[0]),
19861993
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null,
1987-
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
1994+
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined,
19881995
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
1989-
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node)
1996+
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node) || undefined
19901997
});
19911998

19921999
const accessibility = nodeUtils.getTSNodeAccessibility(node);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"license": "BSD-2-Clause",
2020
"devDependencies": {
2121
"babel-code-frame": "^6.22.0",
22-
"babylon": "^7.0.0-beta.20",
22+
"babylon": "^7.0.0-beta.21",
2323
"eslint": "3.19.0",
2424
"eslint-config-eslint": "4.0.0",
2525
"eslint-plugin-node": "4.2.2",

0 commit comments

Comments
 (0)