@@ -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 } )
@@ -279,7 +281,7 @@ module.exports = function convert(config) {
279281 result . type = customType ;
280282 Object
281283 . 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 ) ) )
283285 . forEach ( key => {
284286 if ( key === "type" ) {
285287 result . typeAnnotation = ( node . type ) ? convertTypeAnnotation ( node . type ) : null ;
@@ -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 }
@@ -704,7 +711,7 @@ module.exports = function convert(config) {
704711 node ,
705712 parentNode =>
706713 ( parentNode . kind === SyntaxKind . BinaryExpression || parentNode . kind === SyntaxKind . ArrowFunction )
707- ) ;
714+ ) ;
708715 const objectAssignNode = (
709716 ancestorNode &&
710717 ancestorNode . kind === SyntaxKind . BinaryExpression &&
@@ -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 ) {
@@ -836,7 +843,12 @@ module.exports = function convert(config) {
836843 case SyntaxKind . SetAccessor :
837844 case SyntaxKind . MethodDeclaration : {
838845
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+ } ) ;
840852
841853 const methodLoc = ast . getLineAndCharacterOfPosition ( openingParen . getStart ( ) ) ,
842854 nodeIsMethod = ( node . kind === SyntaxKind . MethodDeclaration ) ,
@@ -1273,7 +1285,7 @@ module.exports = function convert(config) {
12731285
12741286 if ( node . type ) {
12751287 parameter . typeAnnotation = convertTypeAnnotation ( node . type ) ;
1276- parameter . range [ 1 ] = node . type . getEnd ( ) ;
1288+ fixTypeAnnotationParentLocation ( parameter ) ;
12771289 }
12781290
12791291 if ( node . questionToken ) {
@@ -1285,10 +1297,10 @@ module.exports = function convert(config) {
12851297 type : AST_NODE_TYPES . TSParameterProperty ,
12861298 range : [ node . getStart ( ) , node . end ] ,
12871299 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 ,
12921304 parameter : result
12931305 } ;
12941306 }
@@ -1941,9 +1953,9 @@ module.exports = function convert(config) {
19411953 key : convertChild ( node . name ) ,
19421954 params : convertParameters ( node . parameters ) ,
19431955 typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null ,
1944- readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1956+ readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) || undefined ,
19451957 static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1946- export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node )
1958+ export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node ) || undefined
19471959 } ) ;
19481960
19491961 const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
@@ -1961,14 +1973,14 @@ module.exports = function convert(config) {
19611973 case SyntaxKind . PropertySignature : {
19621974 Object . assign ( result , {
19631975 type : AST_NODE_TYPES . TSPropertySignature ,
1964- optional : nodeUtils . isOptional ( node ) ,
1976+ optional : nodeUtils . isOptional ( node ) || undefined ,
19651977 computed : nodeUtils . isComputedProperty ( node . name ) ,
19661978 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
19721984 } ) ;
19731985
19741986 const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
@@ -1984,9 +1996,9 @@ module.exports = function convert(config) {
19841996 type : AST_NODE_TYPES . TSIndexSignature ,
19851997 index : convertChild ( node . parameters [ 0 ] ) ,
19861998 typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null ,
1987- readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1999+ readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) || undefined ,
19882000 static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1989- export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node )
2001+ export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node ) || undefined
19902002 } ) ;
19912003
19922004 const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
@@ -2057,6 +2069,11 @@ module.exports = function convert(config) {
20572069 parameterName : convertChild ( node . parameterName ) ,
20582070 typeAnnotation : convertTypeAnnotation ( node . type )
20592071 } ) ;
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 ;
20602077 break ;
20612078
20622079 case SyntaxKind . EnumDeclaration : {
0 commit comments