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