@@ -35,6 +35,7 @@ interface ConvertConfig {
35
35
node : ts . Node ;
36
36
parent ?: ts . Node | null ;
37
37
inTypeMode ?: boolean ;
38
+ allowPattern ?: boolean ;
38
39
ast : ts . SourceFile ;
39
40
additionalOptions : ConvertAdditionalOptions ;
40
41
}
@@ -83,26 +84,40 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
83
84
loc : nodeUtils . getLoc ( node , ast )
84
85
} ;
85
86
86
- function converter ( child ?: ts . Node , inTypeMode ?: boolean ) : ESTreeNode | null {
87
+ function converter (
88
+ child ?: ts . Node ,
89
+ inTypeMode ?: boolean ,
90
+ allowPattern ?: boolean
91
+ ) : ESTreeNode | null {
87
92
if ( ! child ) {
88
93
return null ;
89
94
}
90
95
return convert ( {
91
96
node : child ,
92
97
parent : node ,
93
98
inTypeMode,
99
+ allowPattern,
94
100
ast,
95
101
additionalOptions
96
102
} ) ;
97
103
}
98
104
105
+ /**
106
+ * Converts a TypeScript node into an ESTree node.
107
+ * @param {ts.Node } child the child ts.Node
108
+ * @returns {ESTreeNode|null } the converted ESTree node
109
+ */
110
+ function convertPattern ( child ?: ts . Node ) : ESTreeNode | null {
111
+ return converter ( child , config . inTypeMode , true ) ;
112
+ }
113
+
99
114
/**
100
115
* Converts a TypeScript node into an ESTree node.
101
116
* @param {ts.Node } child the child ts.Node
102
117
* @returns {ESTreeNode|null } the converted ESTree node
103
118
*/
104
119
function convertChild ( child ?: ts . Node ) : ESTreeNode | null {
105
- return converter ( child , config . inTypeMode ) ;
120
+ return converter ( child , config . inTypeMode , false ) ;
106
121
}
107
122
108
123
/**
@@ -111,7 +126,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
111
126
* @returns {ESTreeNode|null } the converted ESTree node
112
127
*/
113
128
function convertChildType ( child ?: ts . Node ) : ESTreeNode | null {
114
- return converter ( child , true ) ;
129
+ return converter ( child , true , false ) ;
115
130
}
116
131
117
132
/**
@@ -632,7 +647,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
632
647
case SyntaxKind . ForOfStatement : {
633
648
Object . assign ( result , {
634
649
type : SyntaxKind [ node . kind ] ,
635
- left : convertChild ( node . initializer ) ,
650
+ left : convertPattern ( node . initializer ) ,
636
651
right : convertChild ( node . expression ) ,
637
652
body : convertChild ( node . statement )
638
653
} ) ;
@@ -691,7 +706,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
691
706
case SyntaxKind . VariableDeclaration : {
692
707
Object . assign ( result , {
693
708
type : AST_NODE_TYPES . VariableDeclarator ,
694
- id : convertChild ( node . name ) ,
709
+ id : convertPattern ( node . name ) ,
695
710
init : convertChild ( node . initializer )
696
711
} ) ;
697
712
@@ -746,42 +761,11 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
746
761
break ;
747
762
748
763
case SyntaxKind . ArrayLiteralExpression : {
749
- const arrayAssignNode = nodeUtils . findAncestorOfKind (
750
- node ,
751
- SyntaxKind . BinaryExpression
752
- ) ;
753
- const arrayIsInForOf =
754
- node . parent && node . parent . kind === SyntaxKind . ForOfStatement ;
755
- const arrayIsInForIn =
756
- node . parent && node . parent . kind === SyntaxKind . ForInStatement ;
757
- let arrayIsInAssignment ;
758
-
759
- if ( arrayAssignNode ) {
760
- if ( node . parent . kind === SyntaxKind . ShorthandPropertyAssignment ) {
761
- arrayIsInAssignment = false ;
762
- } else if ( node . parent . kind === SyntaxKind . CallExpression ) {
763
- arrayIsInAssignment = false ;
764
- } else if (
765
- nodeUtils . getBinaryExpressionType (
766
- ( arrayAssignNode as any ) . operatorToken
767
- ) === AST_NODE_TYPES . AssignmentExpression
768
- ) {
769
- arrayIsInAssignment =
770
- nodeUtils . findChildOfKind (
771
- ( arrayAssignNode as any ) . left ,
772
- SyntaxKind . ArrayLiteralExpression ,
773
- ast
774
- ) === node || ( arrayAssignNode as any ) . left === node ;
775
- } else {
776
- arrayIsInAssignment = false ;
777
- }
778
- }
779
-
780
764
// TypeScript uses ArrayLiteralExpression in destructuring assignment, too
781
- if ( arrayIsInAssignment || arrayIsInForOf || arrayIsInForIn ) {
765
+ if ( config . allowPattern ) {
782
766
Object . assign ( result , {
783
767
type : AST_NODE_TYPES . ArrayPattern ,
784
- elements : node . elements . map ( convertChild )
768
+ elements : node . elements . map ( convertPattern )
785
769
} ) ;
786
770
} else {
787
771
Object . assign ( result , {
@@ -793,59 +777,30 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
793
777
}
794
778
795
779
case SyntaxKind . ObjectLiteralExpression : {
796
- const ancestorNode = nodeUtils . findFirstMatchingAncestor (
797
- node ,
798
- parentNode =>
799
- parentNode . kind === SyntaxKind . BinaryExpression ||
800
- parentNode . kind === SyntaxKind . ArrowFunction
801
- ) ;
802
- const objectAssignNode =
803
- ancestorNode &&
804
- ancestorNode . kind === SyntaxKind . BinaryExpression &&
805
- ( ancestorNode as any ) . operatorToken . kind === SyntaxKind . FirstAssignment
806
- ? ancestorNode
807
- : null ;
808
-
809
- let objectIsInAssignment = false ;
810
-
811
- if ( objectAssignNode ) {
812
- if ( node . parent . kind === SyntaxKind . ShorthandPropertyAssignment ) {
813
- objectIsInAssignment = false ;
814
- } else if ( ( objectAssignNode as any ) . left === node ) {
815
- objectIsInAssignment = true ;
816
- } else if ( node . parent . kind === SyntaxKind . CallExpression ) {
817
- objectIsInAssignment = false ;
818
- } else {
819
- objectIsInAssignment =
820
- nodeUtils . findChildOfKind (
821
- ( objectAssignNode as any ) . left ,
822
- SyntaxKind . ObjectLiteralExpression ,
823
- ast
824
- ) === node ;
825
- }
826
- }
827
-
828
780
// TypeScript uses ObjectLiteralExpression in destructuring assignment, too
829
- if ( objectIsInAssignment ) {
781
+ if ( config . allowPattern ) {
830
782
Object . assign ( result , {
831
783
type : AST_NODE_TYPES . ObjectPattern ,
832
- properties : node . properties . map ( convertChild )
784
+ properties : node . properties . map ( convertPattern )
833
785
} ) ;
834
786
} else {
835
787
Object . assign ( result , {
836
788
type : AST_NODE_TYPES . ObjectExpression ,
837
789
properties : node . properties . map ( convertChild )
838
790
} ) ;
839
791
}
840
-
841
792
break ;
842
793
}
843
794
844
795
case SyntaxKind . PropertyAssignment :
845
796
Object . assign ( result , {
846
797
type : AST_NODE_TYPES . Property ,
847
798
key : convertChild ( node . name ) ,
848
- value : convertChild ( node . initializer ) ,
799
+ value : converter (
800
+ node . initializer ,
801
+ config . inTypeMode ,
802
+ config . allowPattern
803
+ ) ,
849
804
computed : nodeUtils . isComputedProperty ( node . name ) ,
850
805
method : false ,
851
806
shorthand : false ,
@@ -860,7 +815,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
860
815
key : convertChild ( node . name ) ,
861
816
value : {
862
817
type : AST_NODE_TYPES . AssignmentPattern ,
863
- left : convertChild ( node . name ) ,
818
+ left : convertPattern ( node . name ) ,
864
819
right : convertChild ( node . objectAssignmentInitializer ) ,
865
820
loc : result . loc ,
866
821
range : result . range
@@ -1208,7 +1163,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
1208
1163
case SyntaxKind . ArrayBindingPattern :
1209
1164
Object . assign ( result , {
1210
1165
type : AST_NODE_TYPES . ArrayPattern ,
1211
- elements : node . elements . map ( convertChild )
1166
+ elements : node . elements . map ( convertPattern )
1212
1167
} ) ;
1213
1168
break ;
1214
1169
@@ -1219,7 +1174,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
1219
1174
case SyntaxKind . ObjectBindingPattern :
1220
1175
Object . assign ( result , {
1221
1176
type : AST_NODE_TYPES . ObjectPattern ,
1222
- properties : node . elements . map ( convertChild )
1177
+ properties : node . elements . map ( convertPattern )
1223
1178
} ) ;
1224
1179
break ;
1225
1180
@@ -1387,50 +1342,19 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
1387
1342
1388
1343
// Patterns
1389
1344
1345
+ case SyntaxKind . SpreadAssignment :
1390
1346
case SyntaxKind . SpreadElement : {
1391
- let type = AST_NODE_TYPES . SpreadElement ;
1392
-
1393
- if (
1394
- node . parent &&
1395
- node . parent . parent &&
1396
- node . parent . parent . kind === SyntaxKind . BinaryExpression
1397
- ) {
1398
- if ( ( node . parent . parent as ts . BinaryExpression ) . left === node . parent ) {
1399
- type = AST_NODE_TYPES . RestElement ;
1400
- } else if (
1401
- ( node . parent . parent as ts . BinaryExpression ) . right === node . parent
1402
- ) {
1403
- type = AST_NODE_TYPES . SpreadElement ;
1404
- }
1405
- }
1406
-
1407
- Object . assign ( result , {
1408
- type,
1409
- argument : convertChild ( node . expression )
1410
- } ) ;
1411
- break ;
1412
- }
1413
- case SyntaxKind . SpreadAssignment : {
1414
- let type = AST_NODE_TYPES . SpreadElement ;
1415
-
1416
- if (
1417
- node . parent &&
1418
- node . parent . parent &&
1419
- node . parent . parent . kind === SyntaxKind . BinaryExpression
1420
- ) {
1421
- if ( ( node . parent . parent as ts . BinaryExpression ) . right === node . parent ) {
1422
- type = AST_NODE_TYPES . SpreadElement ;
1423
- } else if (
1424
- ( node . parent . parent as ts . BinaryExpression ) . left === node . parent
1425
- ) {
1426
- type = AST_NODE_TYPES . RestElement ;
1427
- }
1347
+ if ( config . allowPattern ) {
1348
+ Object . assign ( result , {
1349
+ type : AST_NODE_TYPES . RestElement ,
1350
+ argument : convertPattern ( node . expression )
1351
+ } ) ;
1352
+ } else {
1353
+ Object . assign ( result , {
1354
+ type : AST_NODE_TYPES . SpreadElement ,
1355
+ argument : convertChild ( node . expression )
1356
+ } ) ;
1428
1357
}
1429
-
1430
- Object . assign ( result , {
1431
- type,
1432
- argument : convertChild ( node . expression )
1433
- } ) ;
1434
1358
break ;
1435
1359
}
1436
1360
@@ -1788,44 +1712,21 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
1788
1712
( result as any ) . expressions . push ( right ) ;
1789
1713
}
1790
1714
} else {
1715
+ const type = nodeUtils . getBinaryExpressionType ( node . operatorToken ) ;
1791
1716
Object . assign ( result , {
1792
- type : nodeUtils . getBinaryExpressionType ( node . operatorToken ) ,
1717
+ type,
1793
1718
operator : nodeUtils . getTextForTokenKind ( node . operatorToken . kind ) ,
1794
- left : convertChild ( node . left ) ,
1719
+ left : converter (
1720
+ node . left ,
1721
+ config . inTypeMode ,
1722
+ type === AST_NODE_TYPES . AssignmentExpression
1723
+ ) ,
1795
1724
right : convertChild ( node . right )
1796
1725
} ) ;
1797
1726
1798
1727
// if the binary expression is in a destructured array, switch it
1799
1728
if ( result . type === AST_NODE_TYPES . AssignmentExpression ) {
1800
- const upperArrayNode = nodeUtils . findFirstMatchingAncestor (
1801
- node ,
1802
- parent =>
1803
- parent . kind === SyntaxKind . ArrayLiteralExpression ||
1804
- parent . kind === SyntaxKind . ObjectLiteralExpression
1805
- ) ;
1806
- const upperArrayAssignNode =
1807
- upperArrayNode &&
1808
- nodeUtils . findAncestorOfKind (
1809
- upperArrayNode ,
1810
- SyntaxKind . BinaryExpression
1811
- ) ;
1812
-
1813
- let upperArrayIsInAssignment ;
1814
-
1815
- if ( upperArrayAssignNode ) {
1816
- if ( ( upperArrayAssignNode as any ) . left === upperArrayNode ) {
1817
- upperArrayIsInAssignment = true ;
1818
- } else {
1819
- upperArrayIsInAssignment =
1820
- nodeUtils . findChildOfKind (
1821
- ( upperArrayAssignNode as any ) . left ,
1822
- SyntaxKind . ArrayLiteralExpression ,
1823
- ast
1824
- ) === upperArrayNode ;
1825
- }
1826
- }
1827
-
1828
- if ( upperArrayIsInAssignment ) {
1729
+ if ( config . allowPattern ) {
1829
1730
delete ( result as any ) . operator ;
1830
1731
result . type = AST_NODE_TYPES . AssignmentPattern ;
1831
1732
}
0 commit comments