@@ -85,10 +85,12 @@ module.exports = function convert(config) {
85
85
*/
86
86
function convertTypeAnnotation ( child ) {
87
87
const annotation = convertChild ( child ) ;
88
+ const annotationStartCol = child . getFullStart ( ) - 1 ;
89
+ const loc = nodeUtils . getLocFor ( annotationStartCol , child . end , ast ) ;
88
90
return {
89
91
type : AST_NODE_TYPES . TypeAnnotation ,
90
- loc : annotation . loc ,
91
- range : annotation . range ,
92
+ loc,
93
+ range : [ annotationStartCol , child . end ] ,
92
94
typeAnnotation : annotation
93
95
} ;
94
96
}
@@ -160,7 +162,7 @@ module.exports = function convert(config) {
160
162
161
163
const constraint = typeParameter . constraint
162
164
? convert ( { node : typeParameter . constraint , parent : typeParameter , ast, additionalOptions } )
163
- : null ;
165
+ : undefined ;
164
166
165
167
const defaultParameter = typeParameter . default
166
168
? convert ( { node : typeParameter . default , parent : typeParameter , ast, additionalOptions } )
@@ -392,6 +394,19 @@ module.exports = function convert(config) {
392
394
result . modifiers = remainingModifiers . map ( convertChild ) ;
393
395
}
394
396
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
+
395
410
/**
396
411
* The core of the conversion logic:
397
412
* Identify and convert each relevant TypeScript SyntaxKind
@@ -619,15 +634,7 @@ module.exports = function convert(config) {
619
634
620
635
if ( node . type ) {
621
636
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 ) ;
631
638
}
632
639
break ;
633
640
}
@@ -806,7 +813,7 @@ module.exports = function convert(config) {
806
813
value : convertChild ( node . initializer ) ,
807
814
computed : nodeUtils . isComputedProperty ( node . name ) ,
808
815
static : nodeUtils . hasStaticModifierFlag ( node ) ,
809
- readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node )
816
+ readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) || undefined
810
817
} ) ;
811
818
812
819
if ( node . type ) {
@@ -1273,7 +1280,7 @@ module.exports = function convert(config) {
1273
1280
1274
1281
if ( node . type ) {
1275
1282
parameter . typeAnnotation = convertTypeAnnotation ( node . type ) ;
1276
- parameter . range [ 1 ] = node . type . getEnd ( ) ;
1283
+ fixTypeAnnotationParentLocation ( parameter ) ;
1277
1284
}
1278
1285
1279
1286
if ( node . questionToken ) {
@@ -1286,9 +1293,9 @@ module.exports = function convert(config) {
1286
1293
range : [ node . getStart ( ) , node . end ] ,
1287
1294
loc : nodeUtils . getLoc ( node , ast ) ,
1288
1295
accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
1289
- readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1296
+ readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) || undefined ,
1290
1297
static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1291
- export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node ) ,
1298
+ export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node ) || undefined ,
1292
1299
parameter : result
1293
1300
} ;
1294
1301
}
@@ -1941,9 +1948,9 @@ module.exports = function convert(config) {
1941
1948
key : convertChild ( node . name ) ,
1942
1949
params : convertParameters ( node . parameters ) ,
1943
1950
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null ,
1944
- readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1951
+ readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) || undefined ,
1945
1952
static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1946
- export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node )
1953
+ export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node ) || undefined
1947
1954
} ) ;
1948
1955
1949
1956
const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
@@ -1961,14 +1968,14 @@ module.exports = function convert(config) {
1961
1968
case SyntaxKind . PropertySignature : {
1962
1969
Object . assign ( result , {
1963
1970
type : AST_NODE_TYPES . TSPropertySignature ,
1964
- optional : nodeUtils . isOptional ( node ) ,
1971
+ optional : nodeUtils . isOptional ( node ) || undefined ,
1965
1972
computed : nodeUtils . isComputedProperty ( node . name ) ,
1966
1973
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
1972
1979
} ) ;
1973
1980
1974
1981
const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
@@ -1984,9 +1991,9 @@ module.exports = function convert(config) {
1984
1991
type : AST_NODE_TYPES . TSIndexSignature ,
1985
1992
index : convertChild ( node . parameters [ 0 ] ) ,
1986
1993
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null ,
1987
- readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1994
+ readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) || undefined ,
1988
1995
static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1989
- export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node )
1996
+ export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node ) || undefined
1990
1997
} ) ;
1991
1998
1992
1999
const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
0 commit comments