@@ -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 } )
@@ -279,7 +281,7 @@ module.exports = function convert(config) {
279
281
result . type = customType ;
280
282
Object
281
283
. keys ( node )
282
- . filter ( key => ! ( / ^ (?: k i n d | p a r e n t | p o s | e n d | f l a g s | m o d i f i e r F l a g s C a c h e | j s D o c ) $ / . test ( key ) ) )
284
+ . filter ( key => ! ( / ^ (?: _ c h i l d r e n | k i n d | p a r e n t | p o s | e n d | f l a g s | m o d i f i e r F l a g s C a c h e | j s D o c ) $ / . test ( key ) ) )
283
285
. forEach ( key => {
284
286
if ( key === "type" ) {
285
287
result . typeAnnotation = ( node . type ) ? convertTypeAnnotation ( node . type ) : null ;
@@ -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
}
@@ -704,7 +711,7 @@ module.exports = function convert(config) {
704
711
node ,
705
712
parentNode =>
706
713
( parentNode . kind === SyntaxKind . BinaryExpression || parentNode . kind === SyntaxKind . ArrowFunction )
707
- ) ;
714
+ ) ;
708
715
const objectAssignNode = (
709
716
ancestorNode &&
710
717
ancestorNode . kind === SyntaxKind . BinaryExpression &&
@@ -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 ) {
@@ -836,7 +843,12 @@ module.exports = function convert(config) {
836
843
case SyntaxKind . SetAccessor :
837
844
case SyntaxKind . MethodDeclaration : {
838
845
839
- const openingParen = nodeUtils . findNextToken ( node . name , ast ) ;
846
+ const openingParen = nodeUtils . findFirstMatchingToken ( node . name , ast , token => {
847
+ if ( ! token || ! token . kind ) {
848
+ return false ;
849
+ }
850
+ return nodeUtils . getTextForTokenKind ( token . kind ) === "(" ;
851
+ } ) ;
840
852
841
853
const methodLoc = ast . getLineAndCharacterOfPosition ( openingParen . getStart ( ) ) ,
842
854
nodeIsMethod = ( node . kind === SyntaxKind . MethodDeclaration ) ,
@@ -1273,7 +1285,7 @@ module.exports = function convert(config) {
1273
1285
1274
1286
if ( node . type ) {
1275
1287
parameter . typeAnnotation = convertTypeAnnotation ( node . type ) ;
1276
- parameter . range [ 1 ] = node . type . getEnd ( ) ;
1288
+ fixTypeAnnotationParentLocation ( parameter ) ;
1277
1289
}
1278
1290
1279
1291
if ( node . questionToken ) {
@@ -1285,10 +1297,10 @@ module.exports = function convert(config) {
1285
1297
type : AST_NODE_TYPES . TSParameterProperty ,
1286
1298
range : [ node . getStart ( ) , node . end ] ,
1287
1299
loc : nodeUtils . getLoc ( node , ast ) ,
1288
- accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
1289
- readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1290
- static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1291
- export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node ) ,
1300
+ accessibility : nodeUtils . getTSNodeAccessibility ( node ) || undefined ,
1301
+ readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) || undefined ,
1302
+ static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) || undefined ,
1303
+ export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node ) || undefined ,
1292
1304
parameter : result
1293
1305
} ;
1294
1306
}
@@ -1941,9 +1953,9 @@ module.exports = function convert(config) {
1941
1953
key : convertChild ( node . name ) ,
1942
1954
params : convertParameters ( node . parameters ) ,
1943
1955
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null ,
1944
- readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1956
+ readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) || undefined ,
1945
1957
static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1946
- export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node )
1958
+ export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node ) || undefined
1947
1959
} ) ;
1948
1960
1949
1961
const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
@@ -1961,14 +1973,14 @@ module.exports = function convert(config) {
1961
1973
case SyntaxKind . PropertySignature : {
1962
1974
Object . assign ( result , {
1963
1975
type : AST_NODE_TYPES . TSPropertySignature ,
1964
- optional : nodeUtils . isOptional ( node ) ,
1976
+ optional : nodeUtils . isOptional ( node ) || undefined ,
1965
1977
computed : nodeUtils . isComputedProperty ( node . name ) ,
1966
1978
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 )
1979
+ typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : undefined ,
1980
+ initializer : convertChild ( node . initializer ) || undefined ,
1981
+ readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) || undefined ,
1982
+ static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) || undefined ,
1983
+ export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node ) || undefined
1972
1984
} ) ;
1973
1985
1974
1986
const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
@@ -1984,9 +1996,9 @@ module.exports = function convert(config) {
1984
1996
type : AST_NODE_TYPES . TSIndexSignature ,
1985
1997
index : convertChild ( node . parameters [ 0 ] ) ,
1986
1998
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null ,
1987
- readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1999
+ readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) || undefined ,
1988
2000
static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1989
- export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node )
2001
+ export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node ) || undefined
1990
2002
} ) ;
1991
2003
1992
2004
const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
@@ -2057,6 +2069,11 @@ module.exports = function convert(config) {
2057
2069
parameterName : convertChild ( node . parameterName ) ,
2058
2070
typeAnnotation : convertTypeAnnotation ( node . type )
2059
2071
} ) ;
2072
+ /**
2073
+ * Specific fix for type-guard location data
2074
+ */
2075
+ result . typeAnnotation . loc = result . typeAnnotation . typeAnnotation . loc ;
2076
+ result . typeAnnotation . range = result . typeAnnotation . typeAnnotation . range ;
2060
2077
break ;
2061
2078
2062
2079
case SyntaxKind . EnumDeclaration : {
0 commit comments