@@ -229,6 +229,26 @@ module.exports = function convert(config) {
229
229
} ) ;
230
230
}
231
231
232
+ /**
233
+ * Converts an array of TSNode parameters into an array of ESTreeNode params
234
+ * @param {TSNode[] } parameters An array of TSNode params to be converted
235
+ * @returns {ESTreeNode[] } an array of converted ESTreeNode params
236
+ */
237
+ function convertParameters ( parameters ) {
238
+ if ( ! parameters || ! parameters . length ) {
239
+ return [ ] ;
240
+ }
241
+ return parameters . map ( param => {
242
+ const convertedParam = convertChild ( param ) ;
243
+ if ( ! param . decorators || ! param . decorators . length ) {
244
+ return convertedParam ;
245
+ }
246
+ return Object . assign ( convertedParam , {
247
+ decorators : convertDecorators ( param . decorators )
248
+ } ) ;
249
+ } ) ;
250
+ }
251
+
232
252
/**
233
253
* For nodes that are copied directly from the TypeScript AST into
234
254
* ESTree mostly as-is. The only difference is the addition of a type
@@ -470,7 +490,7 @@ module.exports = function convert(config) {
470
490
471
491
case SyntaxKind . ForInStatement :
472
492
case SyntaxKind . ForOfStatement : {
473
- const isAwait = node . awaitModifier && node . awaitModifier . kind === SyntaxKind . AwaitKeyword ;
493
+ const isAwait = ! ! ( node . awaitModifier && node . awaitModifier . kind === SyntaxKind . AwaitKeyword ) ;
474
494
Object . assign ( result , {
475
495
type : SyntaxKind [ node . kind ] ,
476
496
left : convertChild ( node . initializer ) ,
@@ -507,7 +527,7 @@ module.exports = function convert(config) {
507
527
generator : ! ! node . asteriskToken ,
508
528
expression : false ,
509
529
async : nodeUtils . hasModifier ( SyntaxKind . AsyncKeyword , node ) ,
510
- params : node . parameters . map ( convertChild ) ,
530
+ params : convertParameters ( node . parameters ) ,
511
531
body : convertChild ( node . body )
512
532
} ) ;
513
533
@@ -724,12 +744,19 @@ module.exports = function convert(config) {
724
744
value : convertChild ( node . initializer ) ,
725
745
computed : nodeUtils . isComputedProperty ( node . name ) ,
726
746
static : nodeUtils . hasStaticModifierFlag ( node ) ,
727
- accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
728
747
readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
729
- decorators : convertDecorators ( node . decorators ) ,
730
748
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null
731
749
} ) ;
732
750
751
+ if ( node . decorators ) {
752
+ result . decorators = convertDecorators ( node . decorators ) ;
753
+ }
754
+
755
+ const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
756
+ if ( accessibility ) {
757
+ result . accessibility = accessibility ;
758
+ }
759
+
733
760
if ( node . name . kind === SyntaxKind . Identifier && node . questionToken ) {
734
761
result . key . optional = true ;
735
762
}
@@ -788,11 +815,7 @@ module.exports = function convert(config) {
788
815
/**
789
816
* Unlike in object literal methods, class method params can have decorators
790
817
*/
791
- method . params = node . parameters . map ( param => {
792
- const convertedParam = convertChild ( param ) ;
793
- convertedParam . decorators = convertDecorators ( param . decorators ) ;
794
- return convertedParam ;
795
- } ) ;
818
+ method . params = convertParameters ( node . parameters ) ;
796
819
797
820
/**
798
821
* TypeScript class methods can be defined as "abstract"
@@ -807,11 +830,18 @@ module.exports = function convert(config) {
807
830
value : method ,
808
831
computed : nodeUtils . isComputedProperty ( node . name ) ,
809
832
static : nodeUtils . hasStaticModifierFlag ( node ) ,
810
- kind : "method" ,
811
- accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
812
- decorators : convertDecorators ( node . decorators )
833
+ kind : "method"
813
834
} ) ;
814
835
836
+ if ( node . decorators ) {
837
+ result . decorators = convertDecorators ( node . decorators ) ;
838
+ }
839
+
840
+ const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
841
+ if ( accessibility ) {
842
+ result . accessibility = accessibility ;
843
+ }
844
+
815
845
}
816
846
817
847
if ( result . key . type === AST_NODE_TYPES . Identifier && node . questionToken ) {
@@ -845,13 +875,7 @@ module.exports = function convert(config) {
845
875
constructor = {
846
876
type : AST_NODE_TYPES . FunctionExpression ,
847
877
id : null ,
848
- params : node . parameters . map ( param => {
849
- const convertedParam = convertChild ( param ) ;
850
- const decorators = convertDecorators ( param . decorators ) ;
851
- return Object . assign ( convertedParam , {
852
- decorators
853
- } ) ;
854
- } ) ,
878
+ params : convertParameters ( node . parameters ) ,
855
879
generator : false ,
856
880
expression : false ,
857
881
async : false ,
@@ -912,10 +936,15 @@ module.exports = function convert(config) {
912
936
key : constructorKey ,
913
937
value : constructor ,
914
938
computed : constructorIsComputed ,
915
- accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
916
939
static : constructorIsStatic ,
917
940
kind : ( constructorIsStatic || constructorIsComputed ) ? "method" : "constructor"
918
941
} ) ;
942
+
943
+ const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
944
+ if ( accessibility ) {
945
+ result . accessibility = accessibility ;
946
+ }
947
+
919
948
break ;
920
949
921
950
}
@@ -925,7 +954,7 @@ module.exports = function convert(config) {
925
954
type : AST_NODE_TYPES . FunctionExpression ,
926
955
id : convertChild ( node . name ) ,
927
956
generator : ! ! node . asteriskToken ,
928
- params : node . parameters . map ( convertChild ) ,
957
+ params : convertParameters ( node . parameters ) ,
929
958
body : convertChild ( node . body ) ,
930
959
async : nodeUtils . hasModifier ( SyntaxKind . AsyncKeyword , node ) ,
931
960
expression : false
@@ -1024,7 +1053,7 @@ module.exports = function convert(config) {
1024
1053
type : AST_NODE_TYPES . ArrowFunctionExpression ,
1025
1054
generator : false ,
1026
1055
id : null ,
1027
- params : node . parameters . map ( convertChild ) ,
1056
+ params : convertParameters ( node . parameters ) ,
1028
1057
body : convertChild ( node . body ) ,
1029
1058
async : nodeUtils . hasModifier ( SyntaxKind . AsyncKeyword , node ) ,
1030
1059
expression : node . body . kind !== SyntaxKind . Block
@@ -1273,11 +1302,17 @@ module.exports = function convert(config) {
1273
1302
range : [ openBrace . getStart ( ) , result . range [ 1 ] ] ,
1274
1303
loc : nodeUtils . getLocFor ( openBrace . getStart ( ) , node . end , ast )
1275
1304
} ,
1276
- superClass : ( superClass ? convertChild ( superClass . types [ 0 ] . expression ) : null ) ,
1277
- implements : hasImplements ? heritageClauses [ 0 ] . types . map ( convertClassImplements ) : [ ] ,
1278
- decorators : convertDecorators ( node . decorators )
1305
+ superClass : ( superClass ? convertChild ( superClass . types [ 0 ] . expression ) : null )
1279
1306
} ) ;
1280
1307
1308
+ if ( hasImplements ) {
1309
+ result . implements = heritageClauses [ 0 ] . types . map ( convertClassImplements ) ;
1310
+ }
1311
+
1312
+ if ( node . decorators ) {
1313
+ result . decorators = convertDecorators ( node . decorators ) ;
1314
+ }
1315
+
1281
1316
const filteredMembers = node . members . filter ( nodeUtils . isESTreeClassMember ) ;
1282
1317
1283
1318
if ( filteredMembers . length ) {
@@ -1845,14 +1880,18 @@ module.exports = function convert(config) {
1845
1880
optional : nodeUtils . isOptional ( node ) ,
1846
1881
computed : nodeUtils . isComputedProperty ( node . name ) ,
1847
1882
key : convertChild ( node . name ) ,
1848
- params : node . parameters . map ( parameter => convertChild ( parameter ) ) ,
1883
+ params : convertParameters ( node . parameters ) ,
1849
1884
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null ,
1850
- accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
1851
1885
readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1852
1886
static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1853
1887
export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node )
1854
1888
} ) ;
1855
1889
1890
+ const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
1891
+ if ( accessibility ) {
1892
+ result . accessibility = accessibility ;
1893
+ }
1894
+
1856
1895
if ( node . typeParameters ) {
1857
1896
result . typeParameters = convertTSTypeParametersToTypeParametersDeclaration ( node . typeParameters ) ;
1858
1897
}
@@ -1868,12 +1907,16 @@ module.exports = function convert(config) {
1868
1907
key : convertChild ( node . name ) ,
1869
1908
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null ,
1870
1909
initializer : convertChild ( node . initializer ) ,
1871
- accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
1872
1910
readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1873
1911
static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1874
1912
export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node )
1875
1913
} ) ;
1876
1914
1915
+ const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
1916
+ if ( accessibility ) {
1917
+ result . accessibility = accessibility ;
1918
+ }
1919
+
1877
1920
break ;
1878
1921
}
1879
1922
@@ -1882,19 +1925,23 @@ module.exports = function convert(config) {
1882
1925
type : AST_NODE_TYPES . TSIndexSignature ,
1883
1926
index : convertChild ( node . parameters [ 0 ] ) ,
1884
1927
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null ,
1885
- accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
1886
1928
readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1887
1929
static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1888
1930
export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node )
1889
1931
} ) ;
1890
1932
1933
+ const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
1934
+ if ( accessibility ) {
1935
+ result . accessibility = accessibility ;
1936
+ }
1937
+
1891
1938
break ;
1892
1939
}
1893
1940
1894
1941
case SyntaxKind . ConstructSignature : {
1895
1942
Object . assign ( result , {
1896
1943
type : AST_NODE_TYPES . TSConstructSignature ,
1897
- params : node . parameters . map ( parameter => convertChild ( parameter ) ) ,
1944
+ params : convertParameters ( node . parameters ) ,
1898
1945
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null
1899
1946
} ) ;
1900
1947
0 commit comments