@@ -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
}
@@ -787,11 +814,7 @@ module.exports = function convert(config) {
787
814
/**
788
815
* Unlike in object literal methods, class method params can have decorators
789
816
*/
790
- method . params = node . parameters . map ( param => {
791
- const convertedParam = convertChild ( param ) ;
792
- convertedParam . decorators = convertDecorators ( param . decorators ) ;
793
- return convertedParam ;
794
- } ) ;
817
+ method . params = convertParameters ( node . parameters ) ;
795
818
796
819
/**
797
820
* TypeScript class methods can be defined as "abstract"
@@ -806,11 +829,18 @@ module.exports = function convert(config) {
806
829
value : method ,
807
830
computed : nodeUtils . isComputedProperty ( node . name ) ,
808
831
static : nodeUtils . hasStaticModifierFlag ( node ) ,
809
- kind : "method" ,
810
- accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
811
- decorators : convertDecorators ( node . decorators )
832
+ kind : "method"
812
833
} ) ;
813
834
835
+ if ( node . decorators ) {
836
+ result . decorators = convertDecorators ( node . decorators ) ;
837
+ }
838
+
839
+ const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
840
+ if ( accessibility ) {
841
+ result . accessibility = accessibility ;
842
+ }
843
+
814
844
}
815
845
816
846
if ( result . key . type === AST_NODE_TYPES . Identifier && node . questionToken ) {
@@ -844,13 +874,7 @@ module.exports = function convert(config) {
844
874
constructor = {
845
875
type : AST_NODE_TYPES . FunctionExpression ,
846
876
id : null ,
847
- params : node . parameters . map ( param => {
848
- const convertedParam = convertChild ( param ) ;
849
- const decorators = convertDecorators ( param . decorators ) ;
850
- return Object . assign ( convertedParam , {
851
- decorators
852
- } ) ;
853
- } ) ,
877
+ params : convertParameters ( node . parameters ) ,
854
878
generator : false ,
855
879
expression : false ,
856
880
async : false ,
@@ -910,10 +934,15 @@ module.exports = function convert(config) {
910
934
key : constructorKey ,
911
935
value : constructor ,
912
936
computed : constructorIsComputed ,
913
- accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
914
937
static : constructorIsStatic ,
915
938
kind : ( constructorIsStatic || constructorIsComputed ) ? "method" : "constructor"
916
939
} ) ;
940
+
941
+ const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
942
+ if ( accessibility ) {
943
+ result . accessibility = accessibility ;
944
+ }
945
+
917
946
break ;
918
947
919
948
}
@@ -923,7 +952,7 @@ module.exports = function convert(config) {
923
952
type : AST_NODE_TYPES . FunctionExpression ,
924
953
id : convertChild ( node . name ) ,
925
954
generator : ! ! node . asteriskToken ,
926
- params : node . parameters . map ( convertChild ) ,
955
+ params : convertParameters ( node . parameters ) ,
927
956
body : convertChild ( node . body ) ,
928
957
async : nodeUtils . hasModifier ( SyntaxKind . AsyncKeyword , node ) ,
929
958
expression : false
@@ -1022,7 +1051,7 @@ module.exports = function convert(config) {
1022
1051
type : AST_NODE_TYPES . ArrowFunctionExpression ,
1023
1052
generator : false ,
1024
1053
id : null ,
1025
- params : node . parameters . map ( convertChild ) ,
1054
+ params : convertParameters ( node . parameters ) ,
1026
1055
body : convertChild ( node . body ) ,
1027
1056
async : nodeUtils . hasModifier ( SyntaxKind . AsyncKeyword , node ) ,
1028
1057
expression : node . body . kind !== SyntaxKind . Block
@@ -1271,11 +1300,17 @@ module.exports = function convert(config) {
1271
1300
range : [ openBrace . getStart ( ) , result . range [ 1 ] ] ,
1272
1301
loc : nodeUtils . getLocFor ( openBrace . getStart ( ) , node . end , ast )
1273
1302
} ,
1274
- superClass : ( superClass ? convertChild ( superClass . types [ 0 ] . expression ) : null ) ,
1275
- implements : hasImplements ? heritageClauses [ 0 ] . types . map ( convertClassImplements ) : [ ] ,
1276
- decorators : convertDecorators ( node . decorators )
1303
+ superClass : ( superClass ? convertChild ( superClass . types [ 0 ] . expression ) : null )
1277
1304
} ) ;
1278
1305
1306
+ if ( hasImplements ) {
1307
+ result . implements = heritageClauses [ 0 ] . types . map ( convertClassImplements ) ;
1308
+ }
1309
+
1310
+ if ( node . decorators ) {
1311
+ result . decorators = convertDecorators ( node . decorators ) ;
1312
+ }
1313
+
1279
1314
const filteredMembers = node . members . filter ( nodeUtils . isESTreeClassMember ) ;
1280
1315
1281
1316
if ( filteredMembers . length ) {
@@ -1836,14 +1871,18 @@ module.exports = function convert(config) {
1836
1871
optional : nodeUtils . isOptional ( node ) ,
1837
1872
computed : nodeUtils . isComputedProperty ( node . name ) ,
1838
1873
key : convertChild ( node . name ) ,
1839
- params : node . parameters . map ( parameter => convertChild ( parameter ) ) ,
1874
+ params : convertParameters ( node . parameters ) ,
1840
1875
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null ,
1841
- accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
1842
1876
readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1843
1877
static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1844
1878
export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node )
1845
1879
} ) ;
1846
1880
1881
+ const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
1882
+ if ( accessibility ) {
1883
+ result . accessibility = accessibility ;
1884
+ }
1885
+
1847
1886
if ( node . typeParameters ) {
1848
1887
result . typeParameters = convertTSTypeParametersToTypeParametersDeclaration ( node . typeParameters ) ;
1849
1888
}
@@ -1859,12 +1898,16 @@ module.exports = function convert(config) {
1859
1898
key : convertChild ( node . name ) ,
1860
1899
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null ,
1861
1900
initializer : convertChild ( node . initializer ) ,
1862
- accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
1863
1901
readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1864
1902
static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1865
1903
export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node )
1866
1904
} ) ;
1867
1905
1906
+ const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
1907
+ if ( accessibility ) {
1908
+ result . accessibility = accessibility ;
1909
+ }
1910
+
1868
1911
break ;
1869
1912
}
1870
1913
@@ -1873,19 +1916,23 @@ module.exports = function convert(config) {
1873
1916
type : AST_NODE_TYPES . TSIndexSignature ,
1874
1917
index : convertChild ( node . parameters [ 0 ] ) ,
1875
1918
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null ,
1876
- accessibility : nodeUtils . getTSNodeAccessibility ( node ) ,
1877
1919
readonly : nodeUtils . hasModifier ( SyntaxKind . ReadonlyKeyword , node ) ,
1878
1920
static : nodeUtils . hasModifier ( SyntaxKind . StaticKeyword , node ) ,
1879
1921
export : nodeUtils . hasModifier ( SyntaxKind . ExportKeyword , node )
1880
1922
} ) ;
1881
1923
1924
+ const accessibility = nodeUtils . getTSNodeAccessibility ( node ) ;
1925
+ if ( accessibility ) {
1926
+ result . accessibility = accessibility ;
1927
+ }
1928
+
1882
1929
break ;
1883
1930
}
1884
1931
1885
1932
case SyntaxKind . ConstructSignature : {
1886
1933
Object . assign ( result , {
1887
1934
type : AST_NODE_TYPES . TSConstructSignature ,
1888
- params : node . parameters . map ( parameter => convertChild ( parameter ) ) ,
1935
+ params : convertParameters ( node . parameters ) ,
1889
1936
typeAnnotation : ( node . type ) ? convertTypeAnnotation ( node . type ) : null
1890
1937
} ) ;
1891
1938
0 commit comments