@@ -723,20 +723,105 @@ namespace ts {
723
723
return visitEachChild ( node , visitor , context ) ;
724
724
}
725
725
726
+ function wrapPrivateNameForDestructuringTarget ( node : PrivateNamedPropertyAccess ) {
727
+ return createPropertyAccess (
728
+ createObjectLiteral ( [
729
+ createSetAccessor (
730
+ /*decorators*/ undefined ,
731
+ /*modifiers*/ undefined ,
732
+ "value" ,
733
+ [ createParameter (
734
+ /*decorators*/ undefined ,
735
+ /*modifiers*/ undefined ,
736
+ /*dotDotDotToken*/ undefined , "x" ,
737
+ /*questionToken*/ undefined ,
738
+ /*type*/ undefined ,
739
+ /*initializer*/ undefined
740
+ ) ] ,
741
+ createBlock (
742
+ [ createExpressionStatement (
743
+ createAssignment (
744
+ visitNode ( node , visitor , isPrivateNamedPropertyAccess ) ,
745
+ createIdentifier ( "x" )
746
+ )
747
+ ) ]
748
+ )
749
+ )
750
+ ] ) ,
751
+ "value"
752
+ ) ;
753
+ }
754
+
755
+ function transformDestructuringAssignmentTarget ( node : ArrayLiteralExpression | ObjectLiteralExpression ) {
756
+ const hasPrivateNames = isArrayLiteralExpression ( node ) ?
757
+ forEach ( node . elements , isPrivateNamedPropertyAccess ) :
758
+ forEach ( node . properties , property => isPropertyAssignment ( property ) && isPrivateNamedPropertyAccess ( property . initializer ) ) ;
759
+ if ( ! hasPrivateNames ) {
760
+ return node ;
761
+ }
762
+ if ( isArrayLiteralExpression ( node ) ) {
763
+ // Transforms private names in destructuring assignment array bindings.
764
+ //
765
+ // Source:
766
+ // ([ this.#myProp ] = [ "hello" ]);
767
+ //
768
+ // Transformation:
769
+ // [ { set value(x) { this.#myProp = x; } }.value ] = [ "hello" ];
770
+ return updateArrayLiteral (
771
+ node ,
772
+ node . elements . map (
773
+ expr => isPrivateNamedPropertyAccess ( expr ) ?
774
+ wrapPrivateNameForDestructuringTarget ( expr ) :
775
+ expr
776
+ )
777
+ ) ;
778
+ }
779
+ else {
780
+ // Transforms private names in destructuring assignment object bindings.
781
+ //
782
+ // Source:
783
+ // ({ stringProperty: this.#myProp } = { stringProperty: "hello" });
784
+ //
785
+ // Transformation:
786
+ // ({ stringProperty: { set value(x) { this.#myProp = x; } }.value }) = { stringProperty: "hello" };
787
+ return updateObjectLiteral (
788
+ node ,
789
+ node . properties . map (
790
+ prop => isPropertyAssignment ( prop ) && isPrivateNamedPropertyAccess ( prop . initializer ) ?
791
+ updatePropertyAssignment (
792
+ prop ,
793
+ prop . name ,
794
+ wrapPrivateNameForDestructuringTarget ( prop . initializer )
795
+ ) :
796
+ prop
797
+ )
798
+ ) ;
799
+ }
800
+ }
801
+
726
802
/**
727
803
* Visits a BinaryExpression that contains a destructuring assignment.
728
804
*
729
805
* @param node A BinaryExpression node.
730
806
*/
731
807
function visitBinaryExpression ( node : BinaryExpression , noDestructuringValue : boolean ) : Expression {
732
- if ( isDestructuringAssignment ( node ) && node . left . transformFlags & TransformFlags . ContainsObjectRest ) {
733
- return flattenDestructuringAssignment (
734
- node ,
735
- visitor ,
736
- context ,
737
- FlattenLevel . ObjectRest ,
738
- ! noDestructuringValue
739
- ) ;
808
+ if ( isDestructuringAssignment ( node ) ) {
809
+ const left = transformDestructuringAssignmentTarget ( node . left ) ;
810
+ if ( left !== node . left || node . left . transformFlags & TransformFlags . ContainsObjectRest ) {
811
+ return flattenDestructuringAssignment (
812
+ left === node . left ? node : updateBinary (
813
+ node ,
814
+ left ,
815
+ node . right ,
816
+ node . operatorToken . kind
817
+ ) as DestructuringAssignment ,
818
+ visitor ,
819
+ context ,
820
+ node . left . transformFlags & TransformFlags . ContainsObjectRest
821
+ ? FlattenLevel . ObjectRest : FlattenLevel . All ,
822
+ ! noDestructuringValue
823
+ ) ;
824
+ }
740
825
}
741
826
else if ( node . operatorToken . kind === SyntaxKind . CommaToken ) {
742
827
return updateBinary (
0 commit comments