@@ -25,7 +25,8 @@ import {
25
25
isOptional ,
26
26
findFirstMatchingToken ,
27
27
unescapeStringLiteralText ,
28
- getDeclarationKind
28
+ getDeclarationKind ,
29
+ getLastModifier
29
30
} from './node-utils' ;
30
31
import { AST_NODE_TYPES } from './ast-node-types' ;
31
32
import { ESTreeNode } from './temp-types-based-on-js-source' ;
@@ -923,11 +924,11 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
923
924
( openingParen as any ) . getStart ( ast )
924
925
) ,
925
926
nodeIsMethod = node . kind === SyntaxKind . MethodDeclaration ,
926
- method = {
927
+ method : ESTreeNode = {
927
928
type : AST_NODE_TYPES . FunctionExpression ,
928
929
id : null ,
929
930
generator : ! ! node . asteriskToken ,
930
- expression : false ,
931
+ expression : false , // ESTreeNode as ESTreeNode here
931
932
async : hasModifier ( SyntaxKind . AsyncKeyword , node ) ,
932
933
body : convertChild ( node . body ) ,
933
934
range : [ node . parameters . pos - 1 , result . range [ 1 ] ] ,
@@ -938,7 +939,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
938
939
} ,
939
940
end : result . loc . end
940
941
}
941
- } ;
942
+ } as any ;
942
943
943
944
if ( node . type ) {
944
945
( method as any ) . returnType = convertTypeAnnotation ( node . type ) ;
@@ -1007,112 +1008,92 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
1007
1008
} else if (
1008
1009
! ( result as any ) . static &&
1009
1010
node . name . kind === SyntaxKind . StringLiteral &&
1010
- node . name . text === 'constructor'
1011
+ node . name . text === 'constructor' &&
1012
+ result . type !== AST_NODE_TYPES . Property
1011
1013
) {
1012
1014
( result as any ) . kind = 'constructor' ;
1013
1015
}
1014
1016
1015
1017
// Process typeParameters
1016
1018
if ( node . typeParameters && node . typeParameters . length ) {
1017
- ( method as any ) . typeParameters = convertTSTypeParametersToTypeParametersDeclaration (
1018
- node . typeParameters
1019
- ) ;
1019
+ if ( result . type !== AST_NODE_TYPES . Property ) {
1020
+ method . typeParameters = convertTSTypeParametersToTypeParametersDeclaration (
1021
+ node . typeParameters
1022
+ ) ;
1023
+ } else {
1024
+ result . typeParameters = convertTSTypeParametersToTypeParametersDeclaration (
1025
+ node . typeParameters
1026
+ ) ;
1027
+ }
1020
1028
}
1021
1029
1022
1030
break ;
1023
1031
}
1024
1032
1025
1033
// TypeScript uses this even for static methods named "constructor"
1026
1034
case SyntaxKind . Constructor : {
1027
- const constructorIsStatic = hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1028
- constructorIsAbstract = hasModifier ( SyntaxKind . AbstractKeyword , node ) ,
1029
- firstConstructorToken = constructorIsStatic
1030
- ? findNextToken ( node . getFirstToken ( ) ! , ast , ast )
1031
- : node . getFirstToken ( ) ,
1032
- constructorLoc = ast . getLineAndCharacterOfPosition (
1033
- node . parameters . pos - 1
1034
- ) ,
1035
- constructor = {
1036
- type : AST_NODE_TYPES . FunctionExpression ,
1037
- id : null ,
1038
- params : convertParameters ( node . parameters ) ,
1039
- generator : false ,
1040
- expression : false ,
1041
- async : false ,
1042
- body : convertChild ( node . body ) ,
1043
- range : [ node . parameters . pos - 1 , result . range [ 1 ] ] ,
1044
- loc : {
1045
- start : {
1046
- line : constructorLoc . line + 1 ,
1047
- column : constructorLoc . character
1048
- } ,
1049
- end : result . loc . end
1050
- }
1051
- } ;
1035
+ const lastModifier = getLastModifier ( node ) ;
1036
+ const constructorToken =
1037
+ ( lastModifier && findNextToken ( lastModifier , node , ast ) ) ||
1038
+ node . getFirstToken ( ) ! ;
1039
+
1040
+ const constructorTokenRange = [
1041
+ constructorToken . getStart ( ast ) ,
1042
+ constructorToken . end
1043
+ ] ;
1044
+
1045
+ const constructorLoc = ast . getLineAndCharacterOfPosition (
1046
+ node . parameters . pos - 1
1047
+ ) ;
1052
1048
1053
- const constructorIdentifierLocStart = ast . getLineAndCharacterOfPosition (
1054
- ( firstConstructorToken as any ) . getStart ( ast )
1055
- ) ,
1056
- constructorIdentifierLocEnd = ast . getLineAndCharacterOfPosition (
1057
- ( firstConstructorToken as any ) . getEnd ( ast )
1058
- ) ,
1059
- constructorIsComputed = ! ! node . name && isComputedProperty ( node . name ) ;
1049
+ const constructor : ESTreeNode = {
1050
+ type : AST_NODE_TYPES . FunctionExpression ,
1051
+ id : null ,
1052
+ params : convertParameters ( node . parameters ) ,
1053
+ generator : false ,
1054
+ expression : false , // is not present in ESTreeNode
1055
+ async : false ,
1056
+ body : convertChild ( node . body ) ,
1057
+ range : [ node . parameters . pos - 1 , result . range [ 1 ] ] ,
1058
+ loc : {
1059
+ start : {
1060
+ line : constructorLoc . line + 1 ,
1061
+ column : constructorLoc . character
1062
+ } ,
1063
+ end : result . loc . end
1064
+ }
1065
+ } as any ;
1060
1066
1061
- let constructorKey ;
1067
+ // Process typeParameters
1068
+ if ( node . typeParameters && node . typeParameters . length ) {
1069
+ constructor . typeParameters = convertTSTypeParametersToTypeParametersDeclaration (
1070
+ node . typeParameters
1071
+ ) ;
1072
+ }
1062
1073
1063
- if ( constructorIsComputed ) {
1064
- constructorKey = {
1065
- type : AST_NODE_TYPES . Literal ,
1066
- value : 'constructor' ,
1067
- raw : node . name ! . getText ( ) ,
1068
- range : [
1069
- ( firstConstructorToken as any ) . getStart ( ast ) ,
1070
- ( firstConstructorToken as any ) . end
1071
- ] ,
1072
- loc : {
1073
- start : {
1074
- line : constructorIdentifierLocStart . line + 1 ,
1075
- column : constructorIdentifierLocStart . character
1076
- } ,
1077
- end : {
1078
- line : constructorIdentifierLocEnd . line + 1 ,
1079
- column : constructorIdentifierLocEnd . character
1080
- }
1081
- }
1082
- } ;
1083
- } else {
1084
- constructorKey = {
1085
- type : AST_NODE_TYPES . Identifier ,
1086
- name : 'constructor' ,
1087
- range : [
1088
- ( firstConstructorToken as any ) . getStart ( ast ) ,
1089
- ( firstConstructorToken as any ) . end
1090
- ] ,
1091
- loc : {
1092
- start : {
1093
- line : constructorIdentifierLocStart . line + 1 ,
1094
- column : constructorIdentifierLocStart . character
1095
- } ,
1096
- end : {
1097
- line : constructorIdentifierLocEnd . line + 1 ,
1098
- column : constructorIdentifierLocEnd . character
1099
- }
1100
- }
1101
- } ;
1074
+ // Process returnType
1075
+ if ( node . type ) {
1076
+ constructor . returnType = convertTypeAnnotation ( node . type ) ;
1102
1077
}
1103
1078
1079
+ const constructorKey = {
1080
+ type : AST_NODE_TYPES . Identifier ,
1081
+ name : 'constructor' ,
1082
+ range : constructorTokenRange ,
1083
+ loc : getLocFor ( constructorTokenRange [ 0 ] , constructorTokenRange [ 1 ] , ast )
1084
+ } ;
1085
+
1086
+ const isStatic = hasModifier ( SyntaxKind . StaticKeyword , node ) ;
1087
+
1104
1088
Object . assign ( result , {
1105
- type : constructorIsAbstract
1089
+ type : hasModifier ( SyntaxKind . AbstractKeyword , node )
1106
1090
? AST_NODE_TYPES . TSAbstractMethodDefinition
1107
1091
: AST_NODE_TYPES . MethodDefinition ,
1108
1092
key : constructorKey ,
1109
1093
value : constructor ,
1110
- computed : constructorIsComputed ,
1111
- static : constructorIsStatic ,
1112
- kind :
1113
- constructorIsStatic || constructorIsComputed
1114
- ? 'method'
1115
- : 'constructor'
1094
+ computed : false ,
1095
+ static : isStatic ,
1096
+ kind : isStatic ? 'method' : 'constructor'
1116
1097
} ) ;
1117
1098
1118
1099
const accessibility = getTSNodeAccessibility ( node ) ;
0 commit comments